From 2d46b4102142c70c6f083da0221429a6221b537e Mon Sep 17 00:00:00 2001 From: Shay Banon Date: Thu, 7 Jun 2012 14:59:20 +0200 Subject: [PATCH] improve iteration on multi valued field data by breaking fast --- .../data/bytes/MultiValueByteFieldData.java | 121 ++++++++-------- .../doubles/MultiValueDoubleFieldData.java | 108 +++++++------- .../data/floats/MultiValueFloatFieldData.java | 121 ++++++++-------- .../data/ints/MultiValueIntFieldData.java | 121 ++++++++-------- .../data/longs/MultiValueLongFieldData.java | 135 +++++++++--------- .../strings/MultiValueStringFieldData.java | 47 +++--- .../geo/MultiValueGeoPointFieldData.java | 83 +++++------ 7 files changed, 339 insertions(+), 397 deletions(-) diff --git a/src/main/java/org/elasticsearch/index/field/data/bytes/MultiValueByteFieldData.java b/src/main/java/org/elasticsearch/index/field/data/bytes/MultiValueByteFieldData.java index 9e43ebe27e4..8728342d410 100644 --- a/src/main/java/org/elasticsearch/index/field/data/bytes/MultiValueByteFieldData.java +++ b/src/main/java/org/elasticsearch/index/field/data/bytes/MultiValueByteFieldData.java @@ -87,16 +87,15 @@ public class MultiValueByteFieldData extends ByteFieldData { @Override public void forEachValueInDoc(int docId, StringValueInDocProc proc) { - boolean found = false; - for (int[] ordinal : ordinals) { - int loc = ordinal[docId]; - if (loc != 0) { - found = true; - proc.onValue(docId, Byte.toString(values[loc])); + for (int i = 0; i < ordinals.length; i++) { + int loc = ordinals[i][docId]; + if (loc == 0) { + if (i == 0) { + proc.onMissing(docId); + } + break; } - } - if (!found) { - proc.onMissing(docId); + proc.onValue(docId, Byte.toString(values[loc])); } } @@ -104,9 +103,10 @@ public class MultiValueByteFieldData extends ByteFieldData { public void forEachValueInDoc(int docId, DoubleValueInDocProc proc) { for (int[] ordinal : ordinals) { int loc = ordinal[docId]; - if (loc != 0) { - proc.onValue(docId, values[loc]); + if (loc == 0) { + break; } + proc.onValue(docId, values[loc]); } } @@ -114,69 +114,66 @@ public class MultiValueByteFieldData extends ByteFieldData { public void forEachValueInDoc(int docId, LongValueInDocProc proc) { for (int[] ordinal : ordinals) { int loc = ordinal[docId]; - if (loc != 0) { - proc.onValue(docId, values[loc]); + if (loc == 0) { + break; } + proc.onValue(docId, values[loc]); } } @Override public void forEachValueInDoc(int docId, MissingDoubleValueInDocProc proc) { - boolean found = false; - for (int[] ordinal : ordinals) { - int loc = ordinal[docId]; - if (loc != 0) { - found = true; - proc.onValue(docId, values[loc]); + for (int i = 0; i < ordinals.length; i++) { + int loc = ordinals[i][docId]; + if (loc == 0) { + if (i == 0) { + proc.onMissing(docId); + } + break; } - } - if (!found) { - proc.onMissing(docId); + proc.onValue(docId, values[loc]); } } @Override public void forEachValueInDoc(int docId, MissingLongValueInDocProc proc) { - boolean found = false; - for (int[] ordinal : ordinals) { - int loc = ordinal[docId]; - if (loc != 0) { - found = true; - proc.onValue(docId, values[loc]); + for (int i = 0; i < ordinals.length; i++) { + int loc = ordinals[i][docId]; + if (loc == 0) { + if (i == 0) { + proc.onMissing(docId); + } + break; } - } - if (!found) { - proc.onMissing(docId); + proc.onValue(docId, values[loc]); } } @Override public void forEachValueInDoc(int docId, ValueInDocProc proc) { - boolean found = false; - for (int[] ordinal : ordinals) { - int loc = ordinal[docId]; - if (loc != 0) { - found = true; - proc.onValue(docId, values[loc]); + for (int i = 0; i < ordinals.length; i++) { + int loc = ordinals[i][docId]; + if (loc == 0) { + if (i == 0) { + proc.onMissing(docId); + } + break; } - } - if (!found) { - proc.onMissing(docId); + proc.onValue(docId, values[loc]); } } @Override public void forEachOrdinalInDoc(int docId, OrdinalInDocProc proc) { - boolean found = false; - for (int[] ordinal : ordinals) { - int loc = ordinal[docId]; - if (loc != 0) { - found = true; - proc.onOrdinal(docId, loc); + for (int i = 0; i < ordinals.length; i++) { + int loc = ordinals[i][docId]; + if (loc == 0) { + if (i == 0) { + proc.onOrdinal(docId, 0); + } + break; } - } - if (!found) { - proc.onOrdinal(docId, 0); + proc.onOrdinal(docId, loc); } } @@ -184,9 +181,10 @@ public class MultiValueByteFieldData extends ByteFieldData { public double[] doubleValues(int docId) { int length = 0; for (int[] ordinal : ordinals) { - if (ordinal[docId] != 0) { - length++; + if (ordinal[docId] == 0) { + break; } + length++; } if (length == 0) { return DoubleFieldData.EMPTY_DOUBLE_ARRAY; @@ -197,12 +195,8 @@ public class MultiValueByteFieldData extends ByteFieldData { } else { doubles = new double[length]; } - int i = 0; - for (int[] ordinal : ordinals) { - int loc = ordinal[docId]; - if (loc != 0) { - doubles[i++] = values[loc]; - } + for (int i = 0; i < length; i++) { + doubles[i] = values[ordinals[i][docId]]; } return doubles; } @@ -222,9 +216,10 @@ public class MultiValueByteFieldData extends ByteFieldData { public byte[] values(int docId) { int length = 0; for (int[] ordinal : ordinals) { - if (ordinal[docId] != 0) { - length++; + if (ordinal[docId] == 0) { + break; } + length++; } if (length == 0) { return EMPTY_BYTE_ARRAY; @@ -235,12 +230,8 @@ public class MultiValueByteFieldData extends ByteFieldData { } else { bytes = new byte[length]; } - int i = 0; - for (int[] ordinal : ordinals) { - int loc = ordinal[docId]; - if (loc != 0) { - bytes[i++] = values[loc]; - } + for (int i = 0; i < length; i++) { + bytes[i] = values[ordinals[i][docId]]; } return bytes; } diff --git a/src/main/java/org/elasticsearch/index/field/data/doubles/MultiValueDoubleFieldData.java b/src/main/java/org/elasticsearch/index/field/data/doubles/MultiValueDoubleFieldData.java index bf36be1f6a8..8e60c57fa34 100644 --- a/src/main/java/org/elasticsearch/index/field/data/doubles/MultiValueDoubleFieldData.java +++ b/src/main/java/org/elasticsearch/index/field/data/doubles/MultiValueDoubleFieldData.java @@ -75,16 +75,15 @@ public class MultiValueDoubleFieldData extends DoubleFieldData { @Override public void forEachValueInDoc(int docId, StringValueInDocProc proc) { - boolean found = false; - for (int[] ordinal : ordinals) { - int loc = ordinal[docId]; - if (loc != 0) { - found = true; - proc.onValue(docId, Double.toString(values[loc])); + for (int i = 0; i < ordinals.length; i++) { + int loc = ordinals[i][docId]; + if (loc == 0) { + if (i == 0) { + proc.onMissing(docId); + } + break; } - } - if (!found) { - proc.onMissing(docId); + proc.onValue(docId, Double.toString(values[loc])); } } @@ -92,9 +91,10 @@ public class MultiValueDoubleFieldData extends DoubleFieldData { public void forEachValueInDoc(int docId, DoubleValueInDocProc proc) { for (int[] ordinal : ordinals) { int loc = ordinal[docId]; - if (loc != 0) { - proc.onValue(docId, values[loc]); + if (loc == 0) { + break; } + proc.onValue(docId, values[loc]); } } @@ -102,69 +102,66 @@ public class MultiValueDoubleFieldData extends DoubleFieldData { public void forEachValueInDoc(int docId, LongValueInDocProc proc) { for (int[] ordinal : ordinals) { int loc = ordinal[docId]; - if (loc != 0) { - proc.onValue(docId, (long) values[loc]); + if (loc == 0) { + break; } + proc.onValue(docId, (long) values[loc]); } } @Override public void forEachValueInDoc(int docId, MissingDoubleValueInDocProc proc) { - boolean found = false; - for (int[] ordinal : ordinals) { - int loc = ordinal[docId]; - if (loc != 0) { - found = true; - proc.onValue(docId, values[loc]); + for (int i = 0; i < ordinals.length; i++) { + int loc = ordinals[i][docId]; + if (loc == 0) { + if (i == 0) { + proc.onMissing(docId); + } + break; } - } - if (!found) { - proc.onMissing(docId); + proc.onValue(docId, values[loc]); } } @Override public void forEachValueInDoc(int docId, MissingLongValueInDocProc proc) { - boolean found = false; - for (int[] ordinal : ordinals) { - int loc = ordinal[docId]; - if (loc != 0) { - found = true; - proc.onValue(docId, (long) values[loc]); + for (int i = 0; i < ordinals.length; i++) { + int loc = ordinals[i][docId]; + if (loc == 0) { + if (i == 0) { + proc.onMissing(docId); + } + break; } - } - if (!found) { - proc.onMissing(docId); + proc.onValue(docId, (long) values[loc]); } } @Override public void forEachValueInDoc(int docId, ValueInDocProc proc) { - boolean found = false; - for (int[] ordinal : ordinals) { - int loc = ordinal[docId]; - if (loc != 0) { - found = true; - proc.onValue(docId, values[loc]); + for (int i = 0; i < ordinals.length; i++) { + int loc = ordinals[i][docId]; + if (loc == 0) { + if (i == 0) { + proc.onMissing(docId); + } + break; } - } - if (!found) { - proc.onMissing(docId); + proc.onValue(docId, values[loc]); } } @Override public void forEachOrdinalInDoc(int docId, OrdinalInDocProc proc) { - boolean found = false; - for (int[] ordinal : ordinals) { - int loc = ordinal[docId]; - if (loc != 0) { - found = true; - proc.onOrdinal(docId, loc); + for (int i = 0; i < ordinals.length; i++) { + int loc = ordinals[i][docId]; + if (loc == 0) { + if (i == 0) { + proc.onOrdinal(docId, 0); + } + break; } - } - if (!found) { - proc.onOrdinal(docId, 0); + proc.onOrdinal(docId, loc); } } @@ -188,9 +185,10 @@ public class MultiValueDoubleFieldData extends DoubleFieldData { public double[] values(int docId) { int length = 0; for (int[] ordinal : ordinals) { - if (ordinal[docId] != 0) { - length++; + if (ordinal[docId] == 0) { + break; } + length++; } if (length == 0) { return EMPTY_DOUBLE_ARRAY; @@ -201,12 +199,8 @@ public class MultiValueDoubleFieldData extends DoubleFieldData { } else { doubles = new double[length]; } - int i = 0; - for (int[] ordinal : ordinals) { - int loc = ordinal[docId]; - if (loc != 0) { - doubles[i++] = values[loc]; - } + for (int i = 0; i < length; i++) { + doubles[i] = values[ordinals[i][docId]]; } return doubles; } diff --git a/src/main/java/org/elasticsearch/index/field/data/floats/MultiValueFloatFieldData.java b/src/main/java/org/elasticsearch/index/field/data/floats/MultiValueFloatFieldData.java index e08204cd11d..b08facd8fb8 100644 --- a/src/main/java/org/elasticsearch/index/field/data/floats/MultiValueFloatFieldData.java +++ b/src/main/java/org/elasticsearch/index/field/data/floats/MultiValueFloatFieldData.java @@ -87,16 +87,15 @@ public class MultiValueFloatFieldData extends FloatFieldData { @Override public void forEachValueInDoc(int docId, StringValueInDocProc proc) { - boolean found = false; - for (int[] ordinal : ordinals) { - int loc = ordinal[docId]; - if (loc != 0) { - found = true; - proc.onValue(docId, Float.toString(values[loc])); + for (int i = 0; i < ordinals.length; i++) { + int loc = ordinals[i][docId]; + if (loc == 0) { + if (i == 0) { + proc.onMissing(docId); + } + break; } - } - if (!found) { - proc.onMissing(docId); + proc.onValue(docId, Double.toString(values[loc])); } } @@ -104,9 +103,10 @@ public class MultiValueFloatFieldData extends FloatFieldData { public void forEachValueInDoc(int docId, DoubleValueInDocProc proc) { for (int[] ordinal : ordinals) { int loc = ordinal[docId]; - if (loc != 0) { - proc.onValue(docId, values[loc]); + if (loc == 0) { + break; } + proc.onValue(docId, values[loc]); } } @@ -114,69 +114,66 @@ public class MultiValueFloatFieldData extends FloatFieldData { public void forEachValueInDoc(int docId, LongValueInDocProc proc) { for (int[] ordinal : ordinals) { int loc = ordinal[docId]; - if (loc != 0) { - proc.onValue(docId, (long) values[loc]); + if (loc == 0) { + break; } + proc.onValue(docId, (long) values[loc]); } } @Override public void forEachValueInDoc(int docId, MissingDoubleValueInDocProc proc) { - boolean found = false; - for (int[] ordinal : ordinals) { - int loc = ordinal[docId]; - if (loc != 0) { - found = true; - proc.onValue(docId, values[loc]); + for (int i = 0; i < ordinals.length; i++) { + int loc = ordinals[i][docId]; + if (loc == 0) { + if (i == 0) { + proc.onMissing(docId); + } + break; } - } - if (!found) { - proc.onMissing(docId); + proc.onValue(docId, values[loc]); } } @Override public void forEachValueInDoc(int docId, MissingLongValueInDocProc proc) { - boolean found = false; - for (int[] ordinal : ordinals) { - int loc = ordinal[docId]; - if (loc != 0) { - found = true; - proc.onValue(docId, (long) values[loc]); + for (int i = 0; i < ordinals.length; i++) { + int loc = ordinals[i][docId]; + if (loc == 0) { + if (i == 0) { + proc.onMissing(docId); + } + break; } - } - if (!found) { - proc.onMissing(docId); + proc.onValue(docId, (long) values[loc]); } } @Override public void forEachValueInDoc(int docId, ValueInDocProc proc) { - boolean found = false; - for (int[] ordinal : ordinals) { - int loc = ordinal[docId]; - if (loc != 0) { - found = true; - proc.onValue(docId, values[loc]); + for (int i = 0; i < ordinals.length; i++) { + int loc = ordinals[i][docId]; + if (loc == 0) { + if (i == 0) { + proc.onMissing(docId); + } + break; } - } - if (!found) { - proc.onMissing(docId); + proc.onValue(docId, values[loc]); } } @Override public void forEachOrdinalInDoc(int docId, OrdinalInDocProc proc) { - boolean found = false; - for (int[] ordinal : ordinals) { - int loc = ordinal[docId]; - if (loc != 0) { - found = true; - proc.onOrdinal(docId, loc); + for (int i = 0; i < ordinals.length; i++) { + int loc = ordinals[i][docId]; + if (loc == 0) { + if (i == 0) { + proc.onOrdinal(docId, 0); + } + break; } - } - if (!found) { - proc.onOrdinal(docId, 0); + proc.onOrdinal(docId, loc); } } @@ -184,9 +181,10 @@ public class MultiValueFloatFieldData extends FloatFieldData { public double[] doubleValues(int docId) { int length = 0; for (int[] ordinal : ordinals) { - if (ordinal[docId] != 0) { - length++; + if (ordinal[docId] == 0) { + break; } + length++; } if (length == 0) { return DoubleFieldData.EMPTY_DOUBLE_ARRAY; @@ -197,12 +195,8 @@ public class MultiValueFloatFieldData extends FloatFieldData { } else { doubles = new double[length]; } - int i = 0; - for (int[] ordinal : ordinals) { - int loc = ordinal[docId]; - if (loc != 0) { - doubles[i++] = values[loc]; - } + for (int i = 0; i < length; i++) { + doubles[i] = values[ordinals[i][docId]]; } return doubles; } @@ -222,9 +216,10 @@ public class MultiValueFloatFieldData extends FloatFieldData { public float[] values(int docId) { int length = 0; for (int[] ordinal : ordinals) { - if (ordinal[docId] != 0) { - length++; + if (ordinal[docId] == 0) { + break; } + length++; } if (length == 0) { return EMPTY_FLOAT_ARRAY; @@ -235,12 +230,8 @@ public class MultiValueFloatFieldData extends FloatFieldData { } else { floats = new float[length]; } - int i = 0; - for (int[] ordinal : ordinals) { - int loc = ordinal[docId]; - if (loc != 0) { - floats[i++] = values[loc]; - } + for (int i = 0; i < length; i++) { + floats[i] = values[ordinals[i][docId]]; } return floats; } diff --git a/src/main/java/org/elasticsearch/index/field/data/ints/MultiValueIntFieldData.java b/src/main/java/org/elasticsearch/index/field/data/ints/MultiValueIntFieldData.java index 5fd13dfcfc8..6751d480280 100644 --- a/src/main/java/org/elasticsearch/index/field/data/ints/MultiValueIntFieldData.java +++ b/src/main/java/org/elasticsearch/index/field/data/ints/MultiValueIntFieldData.java @@ -87,16 +87,15 @@ public class MultiValueIntFieldData extends IntFieldData { @Override public void forEachValueInDoc(int docId, StringValueInDocProc proc) { - boolean found = false; - for (int[] ordinal : ordinals) { - int loc = ordinal[docId]; - if (loc != 0) { - found = true; - proc.onValue(docId, Integer.toString(values[loc])); + for (int i = 0; i < ordinals.length; i++) { + int loc = ordinals[i][docId]; + if (loc == 0) { + if (i == 0) { + proc.onMissing(docId); + } + break; } - } - if (!found) { - proc.onMissing(docId); + proc.onValue(docId, Integer.toString(values[loc])); } } @@ -104,9 +103,10 @@ public class MultiValueIntFieldData extends IntFieldData { public void forEachValueInDoc(int docId, DoubleValueInDocProc proc) { for (int[] ordinal : ordinals) { int loc = ordinal[docId]; - if (loc != 0) { - proc.onValue(docId, values[loc]); + if (loc == 0) { + break; } + proc.onValue(docId, values[loc]); } } @@ -114,69 +114,66 @@ public class MultiValueIntFieldData extends IntFieldData { public void forEachValueInDoc(int docId, LongValueInDocProc proc) { for (int[] ordinal : ordinals) { int loc = ordinal[docId]; - if (loc != 0) { - proc.onValue(docId, values[loc]); + if (loc == 0) { + break; } + proc.onValue(docId, values[loc]); } } @Override public void forEachValueInDoc(int docId, MissingDoubleValueInDocProc proc) { - boolean found = false; - for (int[] ordinal : ordinals) { - int loc = ordinal[docId]; - if (loc != 0) { - found = true; - proc.onValue(docId, values[loc]); + for (int i = 0; i < ordinals.length; i++) { + int loc = ordinals[i][docId]; + if (loc == 0) { + if (i == 0) { + proc.onMissing(docId); + } + break; } - } - if (!found) { - proc.onMissing(docId); + proc.onValue(docId, values[loc]); } } @Override public void forEachValueInDoc(int docId, MissingLongValueInDocProc proc) { - boolean found = false; - for (int[] ordinal : ordinals) { - int loc = ordinal[docId]; - if (loc != 0) { - found = true; - proc.onValue(docId, values[loc]); + for (int i = 0; i < ordinals.length; i++) { + int loc = ordinals[i][docId]; + if (loc == 0) { + if (i == 0) { + proc.onMissing(docId); + } + break; } - } - if (!found) { - proc.onMissing(docId); + proc.onValue(docId, values[loc]); } } @Override public void forEachValueInDoc(int docId, ValueInDocProc proc) { - boolean found = false; - for (int[] ordinal : ordinals) { - int loc = ordinal[docId]; - if (loc != 0) { - found = true; - proc.onValue(docId, values[loc]); + for (int i = 0; i < ordinals.length; i++) { + int loc = ordinals[i][docId]; + if (loc == 0) { + if (i == 0) { + proc.onMissing(docId); + } + break; } - } - if (!found) { - proc.onMissing(docId); + proc.onValue(docId, values[loc]); } } @Override public void forEachOrdinalInDoc(int docId, OrdinalInDocProc proc) { - boolean found = false; - for (int[] ordinal : ordinals) { - int loc = ordinal[docId]; - if (loc != 0) { - found = true; - proc.onOrdinal(docId, loc); + for (int i = 0; i < ordinals.length; i++) { + int loc = ordinals[i][docId]; + if (loc == 0) { + if (i == 0) { + proc.onOrdinal(docId, 0); + } + break; } - } - if (!found) { - proc.onOrdinal(docId, 0); + proc.onOrdinal(docId, loc); } } @@ -184,9 +181,10 @@ public class MultiValueIntFieldData extends IntFieldData { public double[] doubleValues(int docId) { int length = 0; for (int[] ordinal : ordinals) { - if (ordinal[docId] != 0) { - length++; + if (ordinal[docId] == 0) { + break; } + length++; } if (length == 0) { return DoubleFieldData.EMPTY_DOUBLE_ARRAY; @@ -197,12 +195,8 @@ public class MultiValueIntFieldData extends IntFieldData { } else { doubles = new double[length]; } - int i = 0; - for (int[] ordinal : ordinals) { - int loc = ordinal[docId]; - if (loc != 0) { - doubles[i++] = values[loc]; - } + for (int i = 0; i < length; i++) { + doubles[i] = values[ordinals[i][docId]]; } return doubles; } @@ -222,9 +216,10 @@ public class MultiValueIntFieldData extends IntFieldData { public int[] values(int docId) { int length = 0; for (int[] ordinal : ordinals) { - if (ordinal[docId] != 0) { - length++; + if (ordinal[docId] == 0) { + break; } + length++; } if (length == 0) { return EMPTY_INT_ARRAY; @@ -235,12 +230,8 @@ public class MultiValueIntFieldData extends IntFieldData { } else { ints = new int[length]; } - int i = 0; - for (int[] ordinal : ordinals) { - int loc = ordinal[docId]; - if (loc != 0) { - ints[i++] = values[loc]; - } + for (int i = 0; i < length; i++) { + ints[i] = values[ordinals[i][docId]]; } return ints; } diff --git a/src/main/java/org/elasticsearch/index/field/data/longs/MultiValueLongFieldData.java b/src/main/java/org/elasticsearch/index/field/data/longs/MultiValueLongFieldData.java index 083476f3871..d72caf81227 100644 --- a/src/main/java/org/elasticsearch/index/field/data/longs/MultiValueLongFieldData.java +++ b/src/main/java/org/elasticsearch/index/field/data/longs/MultiValueLongFieldData.java @@ -104,16 +104,15 @@ public class MultiValueLongFieldData extends LongFieldData { @Override public void forEachValueInDoc(int docId, StringValueInDocProc proc) { - boolean found = false; - for (int[] ordinal : ordinals) { - int loc = ordinal[docId]; - if (loc != 0) { - found = true; - proc.onValue(docId, Long.toString(values[loc])); + for (int i = 0; i < ordinals.length; i++) { + int loc = ordinals[i][docId]; + if (loc == 0) { + if (i == 0) { + proc.onMissing(docId); + } + break; } - } - if (!found) { - proc.onMissing(docId); + proc.onValue(docId, Long.toString(values[loc])); } } @@ -121,9 +120,10 @@ public class MultiValueLongFieldData extends LongFieldData { public void forEachValueInDoc(int docId, DoubleValueInDocProc proc) { for (int[] ordinal : ordinals) { int loc = ordinal[docId]; - if (loc != 0) { - proc.onValue(docId, values[loc]); + if (loc == 0) { + break; } + proc.onValue(docId, values[loc]); } } @@ -131,69 +131,66 @@ public class MultiValueLongFieldData extends LongFieldData { public void forEachValueInDoc(int docId, LongValueInDocProc proc) { for (int[] ordinal : ordinals) { int loc = ordinal[docId]; - if (loc != 0) { - proc.onValue(docId, values[loc]); + if (loc == 0) { + break; } + proc.onValue(docId, values[loc]); } } @Override public void forEachValueInDoc(int docId, MissingDoubleValueInDocProc proc) { - boolean found = false; - for (int[] ordinal : ordinals) { - int loc = ordinal[docId]; - if (loc != 0) { - found = true; - proc.onValue(docId, values[loc]); + for (int i = 0; i < ordinals.length; i++) { + int loc = ordinals[i][docId]; + if (loc == 0) { + if (i == 0) { + proc.onMissing(docId); + } + break; } - } - if (!found) { - proc.onMissing(docId); + proc.onValue(docId, values[loc]); } } @Override public void forEachValueInDoc(int docId, MissingLongValueInDocProc proc) { - boolean found = false; - for (int[] ordinal : ordinals) { - int loc = ordinal[docId]; - if (loc != 0) { - found = true; - proc.onValue(docId, values[loc]); + for (int i = 0; i < ordinals.length; i++) { + int loc = ordinals[i][docId]; + if (loc == 0) { + if (i == 0) { + proc.onMissing(docId); + } + break; } - } - if (!found) { - proc.onMissing(docId); + proc.onValue(docId, values[loc]); } } @Override public void forEachOrdinalInDoc(int docId, OrdinalInDocProc proc) { - boolean found = false; - for (int[] ordinal : ordinals) { - int loc = ordinal[docId]; - if (loc != 0) { - found = true; - proc.onOrdinal(docId, loc); + for (int i = 0; i < ordinals.length; i++) { + int loc = ordinals[i][docId]; + if (loc == 0) { + if (i == 0) { + proc.onOrdinal(docId, 0); + } + break; } - } - if (!found) { - proc.onOrdinal(docId, 0); + proc.onOrdinal(docId, loc); } } @Override public void forEachValueInDoc(int docId, ValueInDocProc proc) { - boolean found = false; - for (int[] ordinal : ordinals) { - int loc = ordinal[docId]; - if (loc != 0) { - found = true; - proc.onValue(docId, values[loc]); + for (int i = 0; i < ordinals.length; i++) { + int loc = ordinals[i][docId]; + if (loc == 0) { + if (i == 0) { + proc.onMissing(docId); + } + break; } - } - if (!found) { - proc.onMissing(docId); + proc.onValue(docId, values[loc]); } } @@ -202,10 +199,11 @@ public class MultiValueLongFieldData extends LongFieldData { MutableDateTime dateTime = dateTimeCache.get().get(); for (int[] ordinal : ordinals) { int loc = ordinal[docId]; - if (loc != 0) { - dateTime.setMillis(values[loc]); - proc.onValue(docId, dateTime); + if (loc == 0) { + break; } + dateTime.setMillis(values[loc]); + proc.onValue(docId, dateTime); } } @@ -213,10 +211,11 @@ public class MultiValueLongFieldData extends LongFieldData { public void forEachValueInDoc(int docId, MutableDateTime dateTime, DateValueInDocProc proc) { for (int[] ordinal : ordinals) { int loc = ordinal[docId]; - if (loc != 0) { - dateTime.setMillis(values[loc]); - proc.onValue(docId, dateTime); + if (loc == 0) { + break; } + dateTime.setMillis(values[loc]); + proc.onValue(docId, dateTime); } } @@ -254,9 +253,10 @@ public class MultiValueLongFieldData extends LongFieldData { public double[] doubleValues(int docId) { int length = 0; for (int[] ordinal : ordinals) { - if (ordinal[docId] != 0) { - length++; + if (ordinal[docId] == 0) { + break; } + length++; } if (length == 0) { return DoubleFieldData.EMPTY_DOUBLE_ARRAY; @@ -267,12 +267,8 @@ public class MultiValueLongFieldData extends LongFieldData { } else { doubles = new double[length]; } - int i = 0; - for (int[] ordinal : ordinals) { - int loc = ordinal[docId]; - if (loc != 0) { - doubles[i++] = values[loc]; - } + for (int i = 0; i < length; i++) { + doubles[i] = values[ordinals[i][docId]]; } return doubles; } @@ -292,9 +288,10 @@ public class MultiValueLongFieldData extends LongFieldData { public long[] values(int docId) { int length = 0; for (int[] ordinal : ordinals) { - if (ordinal[docId] != 0) { - length++; + if (ordinal[docId] == 0) { + break; } + length++; } if (length == 0) { return EMPTY_LONG_ARRAY; @@ -305,12 +302,8 @@ public class MultiValueLongFieldData extends LongFieldData { } else { longs = new long[length]; } - int i = 0; - for (int[] ordinal : ordinals) { - int loc = ordinal[docId]; - if (loc != 0) { - longs[i++] = values[loc]; - } + for (int i = 0; i < length; i++) { + longs[i] = values[ordinals[i][docId]]; } return longs; } diff --git a/src/main/java/org/elasticsearch/index/field/data/strings/MultiValueStringFieldData.java b/src/main/java/org/elasticsearch/index/field/data/strings/MultiValueStringFieldData.java index 31e5df1dabf..50c86bbd411 100644 --- a/src/main/java/org/elasticsearch/index/field/data/strings/MultiValueStringFieldData.java +++ b/src/main/java/org/elasticsearch/index/field/data/strings/MultiValueStringFieldData.java @@ -76,31 +76,29 @@ public class MultiValueStringFieldData extends StringFieldData { @Override public void forEachValueInDoc(int docId, StringValueInDocProc proc) { - boolean found = false; - for (int[] ordinal : ordinals) { - int loc = ordinal[docId]; - if (loc != 0) { - found = true; - proc.onValue(docId, values[loc]); + for (int i = 0; i < ordinals.length; i++) { + int loc = ordinals[i][docId]; + if (loc == 0) { + if (i == 0) { + proc.onMissing(docId); + } + break; } - } - if (!found) { - proc.onMissing(docId); + proc.onValue(docId, values[loc]); } } @Override public void forEachOrdinalInDoc(int docId, OrdinalInDocProc proc) { - boolean found = false; - for (int[] ordinal : ordinals) { - int loc = ordinal[docId]; - if (loc != 0) { - found = true; - proc.onOrdinal(docId, loc); + for (int i = 0; i < ordinals.length; i++) { + int loc = ordinals[i][docId]; + if (loc == 0) { + if (i == 0) { + proc.onOrdinal(docId, 0); + } + break; } - } - if (!found) { - proc.onOrdinal(docId, 0); + proc.onOrdinal(docId, loc); } } @@ -119,9 +117,10 @@ public class MultiValueStringFieldData extends StringFieldData { public String[] values(int docId) { int length = 0; for (int[] ordinal : ordinals) { - if (ordinal[docId] != 0) { - length++; + if (ordinal[docId] == 0) { + break; } + length++; } if (length == 0) { return Strings.EMPTY_ARRAY; @@ -132,12 +131,8 @@ public class MultiValueStringFieldData extends StringFieldData { } else { strings = new String[length]; } - int i = 0; - for (int[] ordinal : ordinals) { - int loc = ordinal[docId]; - if (loc != 0) { - strings[i++] = values[loc]; - } + for (int i = 0; i < length; i++) { + strings[i] = values[ordinals[i][docId]]; } return strings; } diff --git a/src/main/java/org/elasticsearch/index/mapper/geo/MultiValueGeoPointFieldData.java b/src/main/java/org/elasticsearch/index/mapper/geo/MultiValueGeoPointFieldData.java index 51428013142..a011dd3fbad 100644 --- a/src/main/java/org/elasticsearch/index/mapper/geo/MultiValueGeoPointFieldData.java +++ b/src/main/java/org/elasticsearch/index/mapper/geo/MultiValueGeoPointFieldData.java @@ -102,16 +102,15 @@ public class MultiValueGeoPointFieldData extends GeoPointFieldData { @Override public void forEachValueInDoc(int docId, StringValueInDocProc proc) { - boolean found = false; - for (int[] ordinal : ordinals) { - int loc = ordinal[docId]; - if (loc != 0) { - found = true; - proc.onValue(docId, GeoHashUtils.encode(lat[loc], lon[loc])); + for (int i = 0; i < ordinals.length; i++) { + int loc = ordinals[i][docId]; + if (loc == 0) { + if (i == 0) { + proc.onMissing(docId); + } + break; } - } - if (!found) { - proc.onMissing(docId); + proc.onValue(docId, GeoHashUtils.encode(lat[loc], lon[loc])); } } @@ -119,24 +118,24 @@ public class MultiValueGeoPointFieldData extends GeoPointFieldData { public void forEachValueInDoc(int docId, ValueInDocProc proc) { for (int[] ordinal : ordinals) { int loc = ordinal[docId]; - if (loc != 0) { - proc.onValue(docId, lat[loc], lon[loc]); + if (loc == 0) { + break; } + proc.onValue(docId, lat[loc], lon[loc]); } } @Override public void forEachOrdinalInDoc(int docId, OrdinalInDocProc proc) { - boolean found = false; - for (int[] ordinal : ordinals) { - int loc = ordinal[docId]; - if (loc != 0) { - found = true; - proc.onOrdinal(docId, loc); + for (int i = 0; i < ordinals.length; i++) { + int loc = ordinals[i][docId]; + if (loc == 0) { + if (i == 0) { + proc.onOrdinal(docId, 0); + } + break; } - } - if (!found) { - proc.onOrdinal(docId, 0); + proc.onOrdinal(docId, loc); } } @@ -157,9 +156,10 @@ public class MultiValueGeoPointFieldData extends GeoPointFieldData { public GeoPoint[] values(int docId) { int length = 0; for (int[] ordinal : ordinals) { - if (ordinal[docId] != 0) { - length++; + if (ordinal[docId] == 0) { + break; } + length++; } if (length == 0) { return EMPTY_ARRAY; @@ -167,21 +167,15 @@ public class MultiValueGeoPointFieldData extends GeoPointFieldData { GeoPoint[] points; 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]); - } + for (int i = 0; i < length; i++) { + int loc = ordinals[i][docId]; + points[i].latlon(lat[loc], lon[loc]); } } else { 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]); - } + for (int i = 0; i < length; i++) { + int loc = ordinals[i][docId]; + points[i] = new GeoPoint(lat[loc], lon[loc]); } } return points; @@ -213,9 +207,10 @@ public class MultiValueGeoPointFieldData extends GeoPointFieldData { public double[] latValues(int docId) { int length = 0; for (int[] ordinal : ordinals) { - if (ordinal[docId] != 0) { - length++; + if (ordinal[docId] == 0) { + break; } + length++; } if (length == 0) { return DoubleFieldData.EMPTY_DOUBLE_ARRAY; @@ -226,12 +221,8 @@ public class MultiValueGeoPointFieldData extends GeoPointFieldData { } else { doubles = new double[length]; } - int i = 0; - for (int[] ordinal : ordinals) { - int loc = ordinal[docId]; - if (loc != 0) { - doubles[i++] = lat[loc]; - } + for (int i = 0; i < length; i++) { + doubles[i] = lat[ordinals[i][docId]]; } return doubles; } @@ -253,12 +244,8 @@ public class MultiValueGeoPointFieldData extends GeoPointFieldData { } else { doubles = new double[length]; } - int i = 0; - for (int[] ordinal : ordinals) { - int loc = ordinal[docId]; - if (loc != 0) { - doubles[i++] = lon[loc]; - } + for (int i = 0; i < length; i++) { + doubles[i] = lon[ordinals[i][docId]]; } return doubles; }