Cut over to IntsRef in favor of IntsArrayRef

This commit is contained in:
Simon Willnauer 2013-03-31 10:46:21 +02:00
parent b3356d9f8d
commit 7efa92636a
12 changed files with 161 additions and 318 deletions

View File

@ -19,8 +19,8 @@
package org.elasticsearch.index.fielddata.ordinals; package org.elasticsearch.index.fielddata.ordinals;
import org.apache.lucene.util.IntsRef;
import org.elasticsearch.common.RamUsage; import org.elasticsearch.common.RamUsage;
import org.elasticsearch.index.fielddata.util.IntArrayRef;
/** /**
* Ordinals that effectively are single valued and map "one to one" to the * Ordinals that effectively are single valued and map "one to one" to the
@ -81,7 +81,7 @@ public class DocIdOrdinals implements Ordinals {
public static class Docs implements Ordinals.Docs { public static class Docs implements Ordinals.Docs {
private final DocIdOrdinals parent; private final DocIdOrdinals parent;
private final IntArrayRef intsScratch = new IntArrayRef(new int[1]); private final IntsRef intsScratch = new IntsRef(new int[1], 0, 1);
private final SingleValueIter iter = new SingleValueIter(); private final SingleValueIter iter = new SingleValueIter();
public Docs(DocIdOrdinals parent) { public Docs(DocIdOrdinals parent) {
@ -119,8 +119,8 @@ public class DocIdOrdinals implements Ordinals {
} }
@Override @Override
public IntArrayRef getOrds(int docId) { public IntsRef getOrds(int docId) {
intsScratch.values[0] = docId + 1; intsScratch.ints[0] = docId + 1;
return intsScratch; return intsScratch;
} }

View File

@ -19,7 +19,7 @@
package org.elasticsearch.index.fielddata.ordinals; package org.elasticsearch.index.fielddata.ordinals;
import org.elasticsearch.index.fielddata.util.IntArrayRef; import org.apache.lucene.util.IntsRef;
/** /**
*/ */
@ -74,6 +74,7 @@ public class EmptyOrdinals implements Ordinals {
public static class Docs implements Ordinals.Docs { public static class Docs implements Ordinals.Docs {
private final EmptyOrdinals parent; private final EmptyOrdinals parent;
public static final IntsRef EMPTY_INTS_REF = new IntsRef();
public Docs(EmptyOrdinals parent) { public Docs(EmptyOrdinals parent) {
this.parent = parent; this.parent = parent;
@ -110,8 +111,8 @@ public class EmptyOrdinals implements Ordinals {
} }
@Override @Override
public IntArrayRef getOrds(int docId) { public IntsRef getOrds(int docId) {
return IntArrayRef.EMPTY; return EMPTY_INTS_REF;
} }
@Override @Override

View File

@ -19,8 +19,8 @@
package org.elasticsearch.index.fielddata.ordinals; package org.elasticsearch.index.fielddata.ordinals;
import org.apache.lucene.util.IntsRef;
import org.elasticsearch.common.RamUsage; import org.elasticsearch.common.RamUsage;
import org.elasticsearch.index.fielddata.util.IntArrayRef;
/** /**
* "Flat" multi valued ordinals, the first level array size is as the maximum * "Flat" multi valued ordinals, the first level array size is as the maximum
@ -99,13 +99,13 @@ public final class MultiFlatArrayOrdinals implements Ordinals {
private final int[][] ordinals; private final int[][] ordinals;
private final IterImpl iter; private final IterImpl iter;
private final IntArrayRef intsScratch; private final IntsRef intsScratch;
public Docs(MultiFlatArrayOrdinals parent, int[][] ordinals) { public Docs(MultiFlatArrayOrdinals parent, int[][] ordinals) {
this.parent = parent; this.parent = parent;
this.ordinals = ordinals; this.ordinals = ordinals;
this.iter = new IterImpl(ordinals); this.iter = new IterImpl(ordinals);
this.intsScratch = new IntArrayRef(new int[16]); this.intsScratch = new IntsRef(new int[16], 0 , 16);
} }
@Override @Override
@ -139,19 +139,22 @@ public final class MultiFlatArrayOrdinals implements Ordinals {
} }
@Override @Override
public IntArrayRef getOrds(int docId) { public IntsRef getOrds(int docId) {
intsScratch.end = 0; intsScratch.offset = 0;
int i; int i;
for (i = 0; i < ordinals.length; i++) { for (i = 0; i < ordinals.length; i++) {
int ordinal = ordinals[i][docId]; int ordinal = ordinals[i][docId];
if (ordinal == 0) { if (ordinal == 0) {
if (i == 0) return IntArrayRef.EMPTY; if (i == 0) {
intsScratch.length = 0;
return intsScratch;
}
break; break;
} }
intsScratch.growIfNeeded(i); intsScratch.grow(i+1);
intsScratch.values[i] = ordinal; intsScratch.ints[i] = ordinal;
} }
intsScratch.end = i; intsScratch.length = i;
return intsScratch; return intsScratch;
} }

View File

@ -19,7 +19,7 @@
package org.elasticsearch.index.fielddata.ordinals; package org.elasticsearch.index.fielddata.ordinals;
import org.elasticsearch.index.fielddata.util.IntArrayRef; import org.apache.lucene.util.IntsRef;
/** /**
* A thread safe ordinals abstraction. Ordinals can only be positive integers. * A thread safe ordinals abstraction. Ordinals can only be positive integers.
@ -111,7 +111,7 @@ public interface Ordinals {
* Returns an array of ordinals matching the docIds, with 0 length one * Returns an array of ordinals matching the docIds, with 0 length one
* for a doc with no ordinals. * for a doc with no ordinals.
*/ */
IntArrayRef getOrds(int docId); IntsRef getOrds(int docId);
/** /**
* Returns an iterator of the ordinals that match the docId, with an * Returns an iterator of the ordinals that match the docId, with an

View File

@ -32,12 +32,12 @@ import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.BytesRefIterator; import org.apache.lucene.util.BytesRefIterator;
import org.apache.lucene.util.FixedBitSet; import org.apache.lucene.util.FixedBitSet;
import org.apache.lucene.util.IntBlockPool; import org.apache.lucene.util.IntBlockPool;
import org.apache.lucene.util.NumericUtils;
import org.apache.lucene.util.IntBlockPool.Allocator; import org.apache.lucene.util.IntBlockPool.Allocator;
import org.apache.lucene.util.IntBlockPool.DirectAllocator; import org.apache.lucene.util.IntBlockPool.DirectAllocator;
import org.apache.lucene.util.IntsRef;
import org.apache.lucene.util.NumericUtils;
import org.elasticsearch.ElasticSearchIllegalArgumentException; import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.fielddata.util.IntArrayRef;
/** /**
* Simple class to build document ID <-> ordinal mapping. Note: Ordinals are * Simple class to build document ID <-> ordinal mapping. Note: Ordinals are
@ -50,7 +50,7 @@ public final class OrdinalsBuilder implements Closeable {
private int[] offsets; private int[] offsets;
private final IntBlockPool pool; private final IntBlockPool pool;
private final IntBlockPool.SliceWriter writer; private final IntBlockPool.SliceWriter writer;
private final IntArrayRef intsRef = new IntArrayRef(new int[1]); private final IntsRef intsRef = new IntsRef(1);
private final IntBlockPool.SliceReader reader; private final IntBlockPool.SliceReader reader;
private int currentOrd = 0; private int currentOrd = 0;
private int numDocsWithValue = 0; private int numDocsWithValue = 0;
@ -185,12 +185,13 @@ public final class OrdinalsBuilder implements Closeable {
if ("flat".equals(multiOrdinals)) { if ("flat".equals(multiOrdinals)) {
final ArrayList<int[]> ordinalBuffer = new ArrayList<int[]>(); final ArrayList<int[]> ordinalBuffer = new ArrayList<int[]>();
for (int i = 0; i < ords.length; i++) { for (int i = 0; i < ords.length; i++) {
IntArrayRef docOrds = docOrds(i); final IntsRef docOrds = docOrds(i);
while (ordinalBuffer.size() < docOrds.size()) { while (ordinalBuffer.size() < docOrds.length) {
ordinalBuffer.add(new int[ords.length]); ordinalBuffer.add(new int[ords.length]);
} }
for (int j = docOrds.start; j < docOrds.end; j++) {
ordinalBuffer.get(j)[i] = docOrds.values[j]; for (int j = docOrds.offset; j < docOrds.offset+docOrds.length; j++) {
ordinalBuffer.get(j)[i] = docOrds.ints[j];
} }
} }
int[][] nativeOrdinals = new int[ordinalBuffer.size()][]; int[][] nativeOrdinals = new int[ordinalBuffer.size()][];
@ -207,27 +208,27 @@ public final class OrdinalsBuilder implements Closeable {
} }
/** /**
* Returns a shared {@link IntArrayRef} instance for the given doc ID holding all ordinals associated with it. * Returns a shared {@link IntsRef} instance for the given doc ID holding all ordinals associated with it.
*/ */
public IntArrayRef docOrds(int doc) { public IntsRef docOrds(int doc) {
int docsOrd = ords[doc]; int docsOrd = ords[doc];
intsRef.start = 0; intsRef.offset = 0;
if (docsOrd == 0) { if (docsOrd == 0) {
intsRef.end = 0; intsRef.length = 0;
} else if (docsOrd > 0) { } else if (docsOrd > 0) {
intsRef.values[0] = ords[doc]; intsRef.ints[0] = ords[doc];
intsRef.end = 1; intsRef.length = 1;
} else { } else {
assert offsets != null; assert offsets != null;
reader.reset(-1 * (ords[doc] + 1), offsets[doc]); reader.reset(-1 * (ords[doc] + 1), offsets[doc]);
int pos = 0; int pos = 0;
while (!reader.endOfSlice()) { while (!reader.endOfSlice()) {
if (intsRef.values.length <= pos) { if (intsRef.ints.length <= pos) {
intsRef.values = ArrayUtil.grow(intsRef.values, pos + 1); intsRef.ints = ArrayUtil.grow(intsRef.ints, pos + 1);
} }
intsRef.values[pos++] = reader.readInt(); intsRef.ints[pos++] = reader.readInt();
} }
intsRef.end = pos; intsRef.length = pos;
} }
return intsRef; return intsRef;
} }

View File

@ -19,9 +19,9 @@ package org.elasticsearch.index.fielddata.ordinals;
* under the License. * under the License.
*/ */
import org.apache.lucene.util.ArrayUtil; import org.apache.lucene.util.ArrayUtil;
import org.apache.lucene.util.IntsRef;
import org.elasticsearch.ElasticSearchIllegalArgumentException; import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.common.RamUsage; import org.elasticsearch.common.RamUsage;
import org.elasticsearch.index.fielddata.util.IntArrayRef;
/** /**
* An efficient store for positive integer slices. This pool uses multiple * An efficient store for positive integer slices. This pool uses multiple
@ -72,17 +72,16 @@ final class PositiveIntPool {
* Adds all integers in the given slices and returns the positive offset * Adds all integers in the given slices and returns the positive offset
* into the datastructure to retrive this slice. * into the datastructure to retrive this slice.
*/ */
public int put(IntArrayRef slice) { public int put(IntsRef slice) {
int length = slice.end - slice.start; if ( slice.length > blockSize) {
if ( length > blockSize) {
throw new ElasticSearchIllegalArgumentException("Can not store slices greater or equal to: " + blockSize); throw new ElasticSearchIllegalArgumentException("Can not store slices greater or equal to: " + blockSize);
} }
if ((intUpto + length) > blockSize) { if ((intUpto + slice.length) > blockSize) {
nextBuffer(); nextBuffer();
} }
final int relativeOffset = intUpto; final int relativeOffset = intUpto;
System.arraycopy(slice.values, slice.start, buffer, relativeOffset, length); System.arraycopy(slice.ints, slice.offset, buffer, relativeOffset, slice.length);
intUpto += length; intUpto += slice.length;
buffer[intUpto - 1] *= -1; // mark as end buffer[intUpto - 1] *= -1; // mark as end
return relativeOffset + intOffset; return relativeOffset + intOffset;
} }
@ -105,26 +104,26 @@ final class PositiveIntPool {
/** /**
* Retrieves a previously stored slice from the pool. * Retrieves a previously stored slice from the pool.
* *
* @param slice the sclice to fill * @param slice the slice to fill
* @param offset the offset where the slice is stored * @param offset the offset where the slice is stored
*/ */
public void fill(IntArrayRef slice, int offset) { public void fill(IntsRef slice, int offset) {
final int blockOffset = offset >> blockShift; final int blockOffset = offset >> blockShift;
final int relativeOffset = offset & blockMask; final int relativeOffset = offset & blockMask;
final int[] currentBuffer = buffers[blockOffset]; final int[] currentBuffer = buffers[blockOffset];
slice.start = 0; slice.offset = 0;
slice.end = 0; slice.length = 0;
for (int i = relativeOffset; i < currentBuffer.length; i++) { for (int i = relativeOffset; i < currentBuffer.length; i++) {
slice.end++; slice.length++;
if (currentBuffer[i] < 0) { if (currentBuffer[i] < 0) {
break; break;
} }
} }
if (slice.end != 0) { if (slice.length != 0) {
slice.values = ArrayUtil.grow(slice.values, slice.end); slice.ints = ArrayUtil.grow(slice.ints, slice.length);
System.arraycopy(currentBuffer, relativeOffset, slice.values, 0, slice.end); System.arraycopy(currentBuffer, relativeOffset, slice.ints, 0, slice.length);
slice.values[slice.end-1] *= -1; slice.ints[slice.length-1] *= -1;
} }
} }

View File

@ -19,8 +19,8 @@
package org.elasticsearch.index.fielddata.ordinals; package org.elasticsearch.index.fielddata.ordinals;
import org.apache.lucene.util.IntsRef;
import org.elasticsearch.common.RamUsage; import org.elasticsearch.common.RamUsage;
import org.elasticsearch.index.fielddata.util.IntArrayRef;
/** /**
*/ */
@ -87,7 +87,7 @@ public class SingleArrayOrdinals implements Ordinals {
private final SingleArrayOrdinals parent; private final SingleArrayOrdinals parent;
private final int[] ordinals; private final int[] ordinals;
private final IntArrayRef intsScratch = new IntArrayRef(new int[1]); private final IntsRef intsScratch = new IntsRef(1);
private final SingleValueIter iter = new SingleValueIter(); private final SingleValueIter iter = new SingleValueIter();
public Docs(SingleArrayOrdinals parent, int[] ordinals) { public Docs(SingleArrayOrdinals parent, int[] ordinals) {
@ -126,10 +126,15 @@ public class SingleArrayOrdinals implements Ordinals {
} }
@Override @Override
public IntArrayRef getOrds(int docId) { public IntsRef getOrds(int docId) {
int ordinal = ordinals[docId]; final int ordinal = ordinals[docId];
if (ordinal == 0) return IntArrayRef.EMPTY; if (ordinal == 0) {
intsScratch.values[0] = ordinal; intsScratch.length = 0;
} else {
intsScratch.ints[0] = ordinal;
intsScratch.offset = 0;
intsScratch.length = 1;
}
return intsScratch; return intsScratch;
} }

View File

@ -19,9 +19,9 @@
package org.elasticsearch.index.fielddata.ordinals; package org.elasticsearch.index.fielddata.ordinals;
import org.apache.lucene.util.IntsRef;
import org.apache.lucene.util.packed.PackedInts; import org.apache.lucene.util.packed.PackedInts;
import org.elasticsearch.common.RamUsage; import org.elasticsearch.common.RamUsage;
import org.elasticsearch.index.fielddata.util.IntArrayRef;
/** /**
*/ */
@ -91,7 +91,7 @@ public class SinglePackedOrdinals implements Ordinals {
private final SinglePackedOrdinals parent; private final SinglePackedOrdinals parent;
private final PackedInts.Reader reader; private final PackedInts.Reader reader;
private final IntArrayRef intsScratch = new IntArrayRef(new int[1]); private final IntsRef intsScratch = new IntsRef(1);
private final SingleValueIter iter = new SingleValueIter(); private final SingleValueIter iter = new SingleValueIter();
public Docs(SinglePackedOrdinals parent, PackedInts.Reader reader) { public Docs(SinglePackedOrdinals parent, PackedInts.Reader reader) {
@ -130,10 +130,15 @@ public class SinglePackedOrdinals implements Ordinals {
} }
@Override @Override
public IntArrayRef getOrds(int docId) { public IntsRef getOrds(int docId) {
int ordinal = (int) reader.get(docId); final int ordinal = (int) reader.get(docId);
if (ordinal == 0) return IntArrayRef.EMPTY; if (ordinal == 0) {
intsScratch.values[0] = ordinal; intsScratch.length = 0;
} else {
intsScratch.offset = 0;
intsScratch.length = 1;
intsScratch.ints[0] = ordinal;
}
return intsScratch; return intsScratch;
} }

View File

@ -19,8 +19,8 @@
package org.elasticsearch.index.fielddata.ordinals; package org.elasticsearch.index.fielddata.ordinals;
import org.apache.lucene.util.IntsRef;
import org.elasticsearch.common.RamUsage; import org.elasticsearch.common.RamUsage;
import org.elasticsearch.index.fielddata.util.IntArrayRef;
/** /**
* Ordinals implementation that stores the ordinals into sparse fixed arrays. * Ordinals implementation that stores the ordinals into sparse fixed arrays.
@ -46,14 +46,14 @@ public final class SparseMultiArrayOrdinals implements Ordinals {
this.lookup = new int[numDocs]; this.lookup = new int[numDocs];
this.numOrds = builder.getNumOrds(); this.numOrds = builder.getNumOrds();
this.maxOrd = numOrds + 1; this.maxOrd = numOrds + 1;
IntArrayRef spare; IntsRef spare;
for (int doc = 0; doc < numDocs; doc++) { for (int doc = 0; doc < numDocs; doc++) {
spare = builder.docOrds(doc); spare = builder.docOrds(doc);
int size = spare.size(); final int size = spare.length;
if (size == 0) { if (size == 0) {
lookup[doc] = 0; lookup[doc] = 0;
} else if (size == 1) { } else if (size == 1) {
lookup[doc] = spare.values[spare.start]; lookup[doc] = spare.ints[spare.offset];
} else { } else {
int offset = pool.put(spare); int offset = pool.put(spare);
lookup[doc] = -(offset) - 1; lookup[doc] = -(offset) - 1;
@ -115,7 +115,7 @@ public final class SparseMultiArrayOrdinals implements Ordinals {
private final IterImpl iter; private final IterImpl iter;
private final PositiveIntPool pool; private final PositiveIntPool pool;
private final IntArrayRef spare = new IntArrayRef(new int[1]); private final IntsRef spare = new IntsRef(1);
public Docs(SparseMultiArrayOrdinals parent, int[] lookup, PositiveIntPool pool) { public Docs(SparseMultiArrayOrdinals parent, int[] lookup, PositiveIntPool pool) {
this.parent = parent; this.parent = parent;
@ -159,19 +159,20 @@ public final class SparseMultiArrayOrdinals implements Ordinals {
} }
@Override @Override
public IntArrayRef getOrds(int docId) { public IntsRef getOrds(int docId) {
spare.end = 0; spare.offset = 0;
int pointer = lookup[docId]; int pointer = lookup[docId];
if (pointer == 0) { if (pointer == 0) {
return IntArrayRef.EMPTY; spare.length = 0;
} else if (pointer > 0) { } else if (pointer > 0) {
spare.end = 1; spare.length = 1;
spare.values[0] = pointer; spare.ints[0] = pointer;
return spare; return spare;
} else { } else {
pool.fill(spare, -(pointer + 1)); pool.fill(spare, -(pointer + 1));
return spare; return spare;
} }
return spare;
} }
@Override @Override
@ -186,8 +187,8 @@ public final class SparseMultiArrayOrdinals implements Ordinals {
proc.onOrdinal(docId, pointer); proc.onOrdinal(docId, pointer);
} else { } else {
pool.fill(spare, -(pointer + 1)); pool.fill(spare, -(pointer + 1));
for (int i = spare.start; i < spare.end; i++) { for (int i = spare.offset; i < spare.length + spare.offset; i++) {
proc.onOrdinal(docId, spare.values[i]); proc.onOrdinal(docId, spare.ints[i]);
} }
} }
} }
@ -195,7 +196,7 @@ public final class SparseMultiArrayOrdinals implements Ordinals {
class IterImpl implements Docs.Iter { class IterImpl implements Docs.Iter {
private final int[] lookup; private final int[] lookup;
private final PositiveIntPool pool; private final PositiveIntPool pool;
private final IntArrayRef slice = new IntArrayRef(new int[1]); private final IntsRef slice = new IntsRef(1);
private int valuesOffset; private int valuesOffset;
public IterImpl(int[] lookup, PositiveIntPool pool) { public IterImpl(int[] lookup, PositiveIntPool pool) {
@ -208,9 +209,9 @@ public final class SparseMultiArrayOrdinals implements Ordinals {
if (pointer < 0) { if (pointer < 0) {
pool.fill(slice, -(pointer + 1)); pool.fill(slice, -(pointer + 1));
} else { } else {
slice.values[0] = pointer; slice.ints[0] = pointer;
slice.start = 0; slice.offset = 0;
slice.end = 1; slice.length = 1;
} }
valuesOffset = 0; valuesOffset = 0;
return this; return this;
@ -218,10 +219,10 @@ public final class SparseMultiArrayOrdinals implements Ordinals {
@Override @Override
public int next() { public int next() {
if (valuesOffset >= slice.end) { if (valuesOffset >= slice.length) {
return 0; return 0;
} }
return slice.values[slice.start + (valuesOffset++)]; return slice.ints[slice.offset + (valuesOffset++)];
} }
} }
} }

View File

@ -1,178 +0,0 @@
/*
* Licensed to ElasticSearch and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. ElasticSearch licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.index.fielddata.util;
import com.google.common.primitives.Ints;
import org.apache.lucene.util.ArrayUtil;
import java.util.AbstractList;
import java.util.RandomAccess;
/**
*/
public class IntArrayRef extends AbstractList<Integer> implements RandomAccess {
public static final IntArrayRef EMPTY = new IntArrayRef(new int[0]);
public int[] values;
public int start;
public int end;
public IntArrayRef(int[] values) {
this(values, 0, values.length);
}
public IntArrayRef(int[] values, int length) {
this(values, 0, length);
}
public IntArrayRef(int[] values, int start, int end) {
this.values = values;
this.start = start;
this.end = end;
}
public void reset(int newLength) {
assert start == 0; // NOTE: senseless if offset != 0
end = 0;
if (values.length < newLength) {
values = new int[ArrayUtil.oversize(newLength, 32)];
}
}
public void growIfNeeded(int index) {
if (index >= values.length) {
values = ArrayUtil.grow(values);
}
}
@Override
public int size() {
return end - start;
}
@Override
public boolean isEmpty() {
return size() == 0;
}
@Override
public Integer get(int index) {
assert index < size();
return values[start + index];
}
@Override
public boolean contains(Object target) {
// Overridden to prevent a ton of boxing
return (target instanceof Integer)
&& indexOf(values, (Integer) target, start, end) != -1;
}
@Override
public int indexOf(Object target) {
// Overridden to prevent a ton of boxing
if (target instanceof Integer) {
int i = indexOf(values, (Integer) target, start, end);
if (i >= 0) {
return i - start;
}
}
return -1;
}
@Override
public int lastIndexOf(Object target) {
// Overridden to prevent a ton of boxing
if (target instanceof Integer) {
int i = lastIndexOf(values, (Integer) target, start, end);
if (i >= 0) {
return i - start;
}
}
return -1;
}
@Override
public Integer set(int index, Integer element) {
assert index < size();
int oldValue = values[start + index];
values[start + index] = element;
return oldValue;
}
@Override
public boolean equals(Object object) {
if (object == this) {
return true;
}
if (object instanceof IntArrayRef) {
IntArrayRef that = (IntArrayRef) object;
int size = size();
if (that.size() != size) {
return false;
}
for (int i = 0; i < size; i++) {
if (values[start + i] != that.values[that.start + i]) {
return false;
}
}
return true;
}
return super.equals(object);
}
@Override
public int hashCode() {
int result = 1;
for (int i = start; i < end; i++) {
result = 31 * result + Ints.hashCode(values[i]);
}
return result;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder(size() * 10);
builder.append('[').append(values[start]);
for (int i = start + 1; i < end; i++) {
builder.append(", ").append(values[i]);
}
return builder.append(']').toString();
}
private static int indexOf(int[] array, int target, int start, int end) {
for (int i = start; i < end; i++) {
if (array[i] == target) {
return i;
}
}
return -1;
}
private static int lastIndexOf(int[] array, int target, int start, int end) {
for (int i = end - 1; i >= start; i--) {
if (array[i] == target) {
return i;
}
}
return -1;
}
}

View File

@ -19,17 +19,23 @@
package org.elasticsearch.test.unit.index.fielddata.ordinals; package org.elasticsearch.test.unit.index.fielddata.ordinals;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;
import org.apache.lucene.util.IntsRef;
import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.index.fielddata.ordinals.Ordinals; import org.elasticsearch.index.fielddata.ordinals.Ordinals;
import org.elasticsearch.index.fielddata.ordinals.OrdinalsBuilder; import org.elasticsearch.index.fielddata.ordinals.OrdinalsBuilder;
import org.elasticsearch.index.fielddata.util.IntArrayRef;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import java.util.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
/** /**
*/ */
public abstract class MultiOrdinalsTests { public abstract class MultiOrdinalsTests {
@ -110,11 +116,11 @@ public abstract class MultiOrdinalsTests {
} else { } else {
if (!docOrds.isEmpty()) { if (!docOrds.isEmpty()) {
assertThat(docs.getOrd(docId), equalTo(docOrds.get(0))); assertThat(docs.getOrd(docId), equalTo(docOrds.get(0)));
IntArrayRef ref = docs.getOrds(docId); IntsRef ref = docs.getOrds(docId);
assertThat(ref.start, equalTo(0)); assertThat(ref.offset, equalTo(0));
for (int i = ref.start; i < ref.end; i++) { for (int i = ref.offset; i < ref.length; i++) {
assertThat(ref.values[i], equalTo(docOrds.get(i))); assertThat(ref.ints[i], equalTo(docOrds.get(i)));
} }
final int[] array = new int[docOrds.size()]; final int[] array = new int[docOrds.size()];
for (int i = 0; i < array.length; i++) { for (int i = 0; i < array.length; i++) {
@ -202,78 +208,78 @@ public abstract class MultiOrdinalsTests {
// Document 1 // Document 1
assertThat(docs.getOrd(0), equalTo(2)); assertThat(docs.getOrd(0), equalTo(2));
IntArrayRef ref = docs.getOrds(0); IntsRef ref = docs.getOrds(0);
assertThat(ref.start, equalTo(0)); assertThat(ref.offset, equalTo(0));
assertThat(ref.values[0], equalTo(2)); assertThat(ref.ints[0], equalTo(2));
assertThat(ref.values[1], equalTo(4)); assertThat(ref.ints[1], equalTo(4));
assertThat(ref.end, equalTo(2)); assertThat(ref.length, equalTo(2));
assertIter(docs.getIter(0), 2, 4); assertIter(docs.getIter(0), 2, 4);
docs.forEachOrdinalInDoc(0, assertOrdinalInProcDoc(2, 4)); docs.forEachOrdinalInDoc(0, assertOrdinalInProcDoc(2, 4));
// Document 2 // Document 2
assertThat(docs.getOrd(1), equalTo(1)); assertThat(docs.getOrd(1), equalTo(1));
ref = docs.getOrds(1); ref = docs.getOrds(1);
assertThat(ref.start, equalTo(0)); assertThat(ref.offset, equalTo(0));
assertThat(ref.values[0], equalTo(1)); assertThat(ref.ints[0], equalTo(1));
assertThat(ref.end, equalTo(1)); assertThat(ref.length, equalTo(1));
assertIter(docs.getIter(1), 1); assertIter(docs.getIter(1), 1);
docs.forEachOrdinalInDoc(1, assertOrdinalInProcDoc(1)); docs.forEachOrdinalInDoc(1, assertOrdinalInProcDoc(1));
// Document 3 // Document 3
assertThat(docs.getOrd(2), equalTo(3)); assertThat(docs.getOrd(2), equalTo(3));
ref = docs.getOrds(2); ref = docs.getOrds(2);
assertThat(ref.start, equalTo(0)); assertThat(ref.offset, equalTo(0));
assertThat(ref.values[0], equalTo(3)); assertThat(ref.ints[0], equalTo(3));
assertThat(ref.end, equalTo(1)); assertThat(ref.length, equalTo(1));
assertIter(docs.getIter(2), 3); assertIter(docs.getIter(2), 3);
docs.forEachOrdinalInDoc(2, assertOrdinalInProcDoc(3)); docs.forEachOrdinalInDoc(2, assertOrdinalInProcDoc(3));
// Document 4 // Document 4
assertThat(docs.getOrd(3), equalTo(0)); assertThat(docs.getOrd(3), equalTo(0));
ref = docs.getOrds(3); ref = docs.getOrds(3);
assertThat(ref.start, equalTo(0)); assertThat(ref.offset, equalTo(0));
assertThat(ref.end, equalTo(0)); assertThat(ref.length, equalTo(0));
assertIter(docs.getIter(3)); assertIter(docs.getIter(3));
docs.forEachOrdinalInDoc(3, assertOrdinalInProcDoc(0)); docs.forEachOrdinalInDoc(3, assertOrdinalInProcDoc(0));
// Document 5 // Document 5
assertThat(docs.getOrd(4), equalTo(1)); assertThat(docs.getOrd(4), equalTo(1));
ref = docs.getOrds(4); ref = docs.getOrds(4);
assertThat(ref.start, equalTo(0)); assertThat(ref.offset, equalTo(0));
assertThat(ref.values[0], equalTo(1)); assertThat(ref.ints[0], equalTo(1));
assertThat(ref.values[1], equalTo(3)); assertThat(ref.ints[1], equalTo(3));
assertThat(ref.values[2], equalTo(4)); assertThat(ref.ints[2], equalTo(4));
assertThat(ref.values[3], equalTo(5)); assertThat(ref.ints[3], equalTo(5));
assertThat(ref.values[4], equalTo(6)); assertThat(ref.ints[4], equalTo(6));
assertThat(ref.end, equalTo(5)); assertThat(ref.length, equalTo(5));
assertIter(docs.getIter(4), 1, 3, 4, 5, 6); assertIter(docs.getIter(4), 1, 3, 4, 5, 6);
docs.forEachOrdinalInDoc(4, assertOrdinalInProcDoc(1, 3, 4, 5, 6)); docs.forEachOrdinalInDoc(4, assertOrdinalInProcDoc(1, 3, 4, 5, 6));
// Document 6 // Document 6
assertThat(docs.getOrd(5), equalTo(1)); assertThat(docs.getOrd(5), equalTo(1));
ref = docs.getOrds(5); ref = docs.getOrds(5);
assertThat(ref.start, equalTo(0)); assertThat(ref.offset, equalTo(0));
int[] expectedOrds = new int[maxOrds]; int[] expectedOrds = new int[maxOrds];
for (int i = 0; i < maxOrds; i++) { for (int i = 0; i < maxOrds; i++) {
expectedOrds[i] = i + 1; expectedOrds[i] = i + 1;
assertThat(ref.values[i], equalTo(i + 1)); assertThat(ref.ints[i], equalTo(i + 1));
} }
assertIter(docs.getIter(5), expectedOrds); assertIter(docs.getIter(5), expectedOrds);
docs.forEachOrdinalInDoc(5, assertOrdinalInProcDoc(expectedOrds)); docs.forEachOrdinalInDoc(5, assertOrdinalInProcDoc(expectedOrds));
assertThat(ref.end, equalTo(maxOrds)); assertThat(ref.length, equalTo(maxOrds));
// Document 7 // Document 7
assertThat(docs.getOrd(6), equalTo(1)); assertThat(docs.getOrd(6), equalTo(1));
ref = docs.getOrds(6); ref = docs.getOrds(6);
assertThat(ref.start, equalTo(0)); assertThat(ref.offset, equalTo(0));
expectedOrds = new int[maxOrds]; expectedOrds = new int[maxOrds];
for (int i = 0; i < maxOrds; i++) { for (int i = 0; i < maxOrds; i++) {
expectedOrds[i] = i + 1; expectedOrds[i] = i + 1;
assertThat(ref.values[i], equalTo(i + 1)); assertThat(ref.ints[i], equalTo(i + 1));
} }
assertIter(docs.getIter(6), expectedOrds); assertIter(docs.getIter(6), expectedOrds);
docs.forEachOrdinalInDoc(6, assertOrdinalInProcDoc(expectedOrds)); docs.forEachOrdinalInDoc(6, assertOrdinalInProcDoc(expectedOrds));
assertThat(ref.end, equalTo(maxOrds)); assertThat(ref.length, equalTo(maxOrds));
} }
protected static void assertIter(Ordinals.Docs.Iter iter, int... expectedOrdinals) { protected static void assertIter(Ordinals.Docs.Iter iter, int... expectedOrdinals) {

View File

@ -19,13 +19,13 @@
package org.elasticsearch.test.unit.index.fielddata.ordinals; package org.elasticsearch.test.unit.index.fielddata.ordinals;
import org.apache.lucene.util.IntsRef;
import org.elasticsearch.ElasticSearchException; import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.ImmutableSettings.Builder; import org.elasticsearch.common.settings.ImmutableSettings.Builder;
import org.elasticsearch.index.fielddata.ordinals.Ordinals; import org.elasticsearch.index.fielddata.ordinals.Ordinals;
import org.elasticsearch.index.fielddata.ordinals.OrdinalsBuilder; import org.elasticsearch.index.fielddata.ordinals.OrdinalsBuilder;
import org.elasticsearch.index.fielddata.ordinals.SparseMultiArrayOrdinals; import org.elasticsearch.index.fielddata.ordinals.SparseMultiArrayOrdinals;
import org.elasticsearch.index.fielddata.util.IntArrayRef;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
@ -103,62 +103,62 @@ public class SparseMultiOrdinalsTests extends MultiOrdinalsTests {
// Document 1 // Document 1
assertThat(docs.getOrd(0), equalTo(1)); assertThat(docs.getOrd(0), equalTo(1));
IntArrayRef ref = docs.getOrds(0); IntsRef ref = docs.getOrds(0);
assertThat(ref.start, equalTo(0)); assertThat(ref.offset, equalTo(0));
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
assertThat(ref.values[i], equalTo(i + 1)); assertThat(ref.ints[i], equalTo(i + 1));
} }
assertThat(ref.end, equalTo(10)); assertThat(ref.length, equalTo(10));
// Document 2 // Document 2
assertThat(docs.getOrd(1), equalTo(1)); assertThat(docs.getOrd(1), equalTo(1));
ref = docs.getOrds(1); ref = docs.getOrds(1);
assertThat(ref.start, equalTo(0)); assertThat(ref.offset, equalTo(0));
for (int i = 0; i < 15; i++) { for (int i = 0; i < 15; i++) {
assertThat(ref.values[i], equalTo(i + 1)); assertThat(ref.ints[i], equalTo(i + 1));
} }
assertThat(ref.end, equalTo(15)); assertThat(ref.length, equalTo(15));
// Document 3 // Document 3
assertThat(docs.getOrd(2), equalTo(1)); assertThat(docs.getOrd(2), equalTo(1));
ref = docs.getOrds(2); ref = docs.getOrds(2);
assertThat(ref.start, equalTo(0)); assertThat(ref.offset, equalTo(0));
assertThat(ref.values[0], equalTo(1)); assertThat(ref.ints[0], equalTo(1));
assertThat(ref.end, equalTo(1)); assertThat(ref.length, equalTo(1));
// Document 4 // Document 4
assertThat(docs.getOrd(3), equalTo(1)); assertThat(docs.getOrd(3), equalTo(1));
ref = docs.getOrds(3); ref = docs.getOrds(3);
assertThat(ref.start, equalTo(0)); assertThat(ref.offset, equalTo(0));
for (int i = 0; i < 5; i++) { for (int i = 0; i < 5; i++) {
assertThat(ref.values[i], equalTo(i + 1)); assertThat(ref.ints[i], equalTo(i + 1));
} }
assertThat(ref.end, equalTo(5)); assertThat(ref.length, equalTo(5));
// Document 5 // Document 5
assertThat(docs.getOrd(4), equalTo(1)); assertThat(docs.getOrd(4), equalTo(1));
ref = docs.getOrds(4); ref = docs.getOrds(4);
assertThat(ref.start, equalTo(0)); assertThat(ref.offset, equalTo(0));
for (int i = 0; i < 6; i++) { for (int i = 0; i < 6; i++) {
assertThat(ref.values[i], equalTo(i + 1)); assertThat(ref.ints[i], equalTo(i + 1));
} }
assertThat(ref.end, equalTo(6)); assertThat(ref.length, equalTo(6));
// Document 6 // Document 6
assertThat(docs.getOrd(5), equalTo(2)); assertThat(docs.getOrd(5), equalTo(2));
ref = docs.getOrds(5); ref = docs.getOrds(5);
assertThat(ref.start, equalTo(0)); assertThat(ref.offset, equalTo(0));
assertThat(ref.values[0], equalTo(2)); assertThat(ref.ints[0], equalTo(2));
assertThat(ref.end, equalTo(1)); assertThat(ref.length, equalTo(1));
// Document 7 // Document 7
assertThat(docs.getOrd(6), equalTo(1)); assertThat(docs.getOrd(6), equalTo(1));
ref = docs.getOrds(6); ref = docs.getOrds(6);
assertThat(ref.start, equalTo(0)); assertThat(ref.offset, equalTo(0));
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
assertThat(ref.values[i], equalTo(i + 1)); assertThat(ref.ints[i], equalTo(i + 1));
} }
assertThat(ref.end, equalTo(10)); assertThat(ref.length, equalTo(10));
} }
} }