From 89314f29a7130177ccf9e3b6f06dd6e9197e2c87 Mon Sep 17 00:00:00 2001 From: kimchy Date: Mon, 10 Jan 2011 20:58:31 +0200 Subject: [PATCH] improve multi value field cache handling both in terms of memory usage and GC behavior --- .../geo/MultiValueGeoPointFieldData.java | 132 +++++++++++------- 1 file changed, 85 insertions(+), 47 deletions(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/geo/MultiValueGeoPointFieldData.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/geo/MultiValueGeoPointFieldData.java index 9287f34241e..f2e8dc20124 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/geo/MultiValueGeoPointFieldData.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/geo/MultiValueGeoPointFieldData.java @@ -86,98 +86,136 @@ public class MultiValueGeoPointFieldData extends GeoPointFieldData { } @Override public boolean hasValue(int docId) { - return ordinals[docId] != null; + for (int[] ordinal : ordinals) { + if (ordinal[docId] != 0) { + return true; + } + } + return false; } @Override public void forEachValueInDoc(int docId, StringValueInDocProc proc) { - int[] docOrders = ordinals[docId]; - if (docOrders == null) { - return; - } - for (int docOrder : docOrders) { - proc.onValue(docId, GeoHashUtils.encode(lat[docOrder], lon[docOrder])); + for (int[] ordinal : ordinals) { + int loc = ordinal[docId]; + if (loc != 0) { + proc.onValue(docId, GeoHashUtils.encode(lat[loc], lon[loc])); + } } } @Override public GeoPoint value(int docId) { - int[] docOrders = ordinals[docId]; - if (docOrders == null) { - return null; + for (int[] ordinal : ordinals) { + int loc = ordinal[docId]; + if (loc != 0) { + GeoPoint point = valuesCache.get().get(); + point.latlon(lat[loc], lon[loc]); + return point; + } } - GeoPoint point = valuesCache.get().get(); - int loc = docOrders[0]; - point.latlon(lat[loc], lon[loc]); - return point; + return null; } @Override public GeoPoint[] values(int docId) { - int[] docOrders = ordinals[docId]; - if (docOrders == null) { + int length = 0; + for (int[] ordinal : ordinals) { + if (ordinal[docId] != 0) { + length++; + } + } + if (length == 0) { return EMPTY_ARRAY; } GeoPoint[] points; - if (docOrders.length < VALUE_CACHE_SIZE) { - points = valuesArrayCache.get().get()[docOrders.length]; - for (int i = 0; i < docOrders.length; i++) { - int loc = docOrders[i]; - points[i].latlon(lat[loc], lon[loc]); + if (length < VALUE_CACHE_SIZE) { + points = valuesArrayCache.get().get()[length]; + int i = 0; + for (int[] ordinal : ordinals) { + int loc = ordinal[docId]; + if (loc != 0) { + points[i++].latlon(lat[loc], lon[loc]); + } } } else { - points = new GeoPoint[docOrders.length]; - for (int i = 0; i < docOrders.length; i++) { - int loc = docOrders[i]; - points[i] = new GeoPoint(lat[loc], lon[loc]); + points = new GeoPoint[length]; + int i = 0; + for (int[] ordinal : ordinals) { + int loc = ordinal[docId]; + if (loc != 0) { + points[i++] = new GeoPoint(lat[loc], lon[loc]); + } } } return points; } @Override public double latValue(int docId) { - int[] docOrders = ordinals[docId]; - if (docOrders == null) { - return 0; + for (int[] ordinal : ordinals) { + int loc = ordinal[docId]; + if (loc != 0) { + return lat[loc]; + } } - return lat[docOrders[0]]; + return 0; } @Override public double lonValue(int docId) { - int[] docOrders = ordinals[docId]; - if (docOrders == null) { - return 0; + for (int[] ordinal : ordinals) { + int loc = ordinal[docId]; + if (loc != 0) { + return lon[loc]; + } } - return lon[docOrders[0]]; + return 0; } @Override public double[] latValues(int docId) { - int[] docOrders = ordinals[docId]; - if (docOrders == null) { + int length = 0; + for (int[] ordinal : ordinals) { + if (ordinal[docId] != 0) { + length++; + } + } + if (length == 0) { return DoubleFieldData.EMPTY_DOUBLE_ARRAY; } double[] doubles; - if (docOrders.length < VALUE_CACHE_SIZE) { - doubles = valuesLatCache.get().get()[docOrders.length]; + if (length < VALUE_CACHE_SIZE) { + doubles = valuesLatCache.get().get()[length]; } else { - doubles = new double[docOrders.length]; + doubles = new double[length]; } - for (int i = 0; i < docOrders.length; i++) { - doubles[i] = lat[docOrders[i]]; + int i = 0; + for (int[] ordinal : ordinals) { + int loc = ordinal[docId]; + if (loc != 0) { + doubles[i++] = lat[loc]; + } } return doubles; } @Override public double[] lonValues(int docId) { - int[] docOrders = ordinals[docId]; - if (docOrders == null) { + int length = 0; + for (int[] ordinal : ordinals) { + if (ordinal[docId] != 0) { + length++; + } + } + if (length == 0) { return DoubleFieldData.EMPTY_DOUBLE_ARRAY; } double[] doubles; - if (docOrders.length < VALUE_CACHE_SIZE) { - doubles = valuesLonCache.get().get()[docOrders.length]; + if (length < VALUE_CACHE_SIZE) { + doubles = valuesLonCache.get().get()[length]; } else { - doubles = new double[docOrders.length]; + doubles = new double[length]; } - for (int i = 0; i < docOrders.length; i++) { - doubles[i] = lon[docOrders[i]]; + int i = 0; + for (int[] ordinal : ordinals) { + int loc = ordinal[docId]; + if (loc != 0) { + doubles[i++] = lon[loc]; + } } return doubles; }