Search in sources :

Example 76 with BytesRefBuilder

use of org.apache.lucene.util.BytesRefBuilder in project crate by crate.

the class AbstractBytesReferenceTestCase method testIterator.

public void testIterator() throws IOException {
    int length = randomIntBetween(10, PAGE_SIZE * randomIntBetween(2, 8));
    BytesReference pbr = newBytesReference(length);
    BytesRefIterator iterator = pbr.iterator();
    BytesRef ref;
    BytesRefBuilder builder = new BytesRefBuilder();
    while ((ref = iterator.next()) != null) {
        builder.append(ref);
    }
    assertArrayEquals(BytesReference.toBytes(pbr), BytesRef.deepCopyOf(builder.toBytesRef()).bytes);
}
Also used : BytesRefIterator(org.apache.lucene.util.BytesRefIterator) BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) BytesRef(org.apache.lucene.util.BytesRef)

Example 77 with BytesRefBuilder

use of org.apache.lucene.util.BytesRefBuilder in project crate by crate.

the class AbstractBytesReferenceTestCase method testRandomReads.

public void testRandomReads() throws IOException {
    int length = randomIntBetween(10, scaledRandomIntBetween(PAGE_SIZE * 2, PAGE_SIZE * 20));
    BytesReference pbr = newBytesReference(length);
    StreamInput streamInput = pbr.streamInput();
    BytesRefBuilder target = new BytesRefBuilder();
    while (target.length() < pbr.length()) {
        switch(randomIntBetween(0, 10)) {
            case 6:
            case 5:
                target.append(new BytesRef(new byte[] { streamInput.readByte() }));
                break;
            case 4:
            case 3:
                BytesRef bytesRef = streamInput.readBytesRef(scaledRandomIntBetween(1, pbr.length() - target.length()));
                target.append(bytesRef);
                break;
            default:
                byte[] buffer = new byte[scaledRandomIntBetween(1, pbr.length() - target.length())];
                int offset = scaledRandomIntBetween(0, buffer.length - 1);
                int read = streamInput.read(buffer, offset, buffer.length - offset);
                target.append(new BytesRef(buffer, offset, read));
                break;
        }
    }
    assertEquals(pbr.length(), target.length());
    BytesRef targetBytes = target.get();
    assertArrayEquals(BytesReference.toBytes(pbr), Arrays.copyOfRange(targetBytes.bytes, targetBytes.offset, targetBytes.length));
}
Also used : BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) StreamInput(org.elasticsearch.common.io.stream.StreamInput) BytesRef(org.apache.lucene.util.BytesRef)

Example 78 with BytesRefBuilder

use of org.apache.lucene.util.BytesRefBuilder in project crate by crate.

the class CompositeBytesReference method toBytesRef.

@Override
public BytesRef toBytesRef() {
    BytesRefBuilder builder = new BytesRefBuilder();
    builder.grow(length());
    BytesRef spare;
    BytesRefIterator iterator = iterator();
    try {
        while ((spare = iterator.next()) != null) {
            builder.append(spare);
        }
    } catch (IOException ex) {
        // this is really an error since we don't do IO in our bytesreferences
        throw new AssertionError("won't happen", ex);
    }
    return builder.toBytesRef();
}
Also used : BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) BytesRefIterator(org.apache.lucene.util.BytesRefIterator) IOException(java.io.IOException) BytesRef(org.apache.lucene.util.BytesRef)

Example 79 with BytesRefBuilder

use of org.apache.lucene.util.BytesRefBuilder in project stargate-core by tuplejump.

the class LuceneUtils method tsTerm.

public static Term tsTerm(long ts) {
    BytesRefBuilder tsBytes = new BytesRefBuilder();
    NumericUtils.longToPrefixCoded(ts, NumericUtils.PRECISION_STEP_DEFAULT, tsBytes);
    return new Term(CF_TS_INDEXED, tsBytes);
}
Also used : BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder)

Example 80 with BytesRefBuilder

use of org.apache.lucene.util.BytesRefBuilder in project elasticsearch by elastic.

the class StringTermsAggregator method getLeafCollector.

@Override
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx, final LeafBucketCollector sub) throws IOException {
    final SortedBinaryDocValues values = valuesSource.bytesValues(ctx);
    return new LeafBucketCollectorBase(sub, values) {

        final BytesRefBuilder previous = new BytesRefBuilder();

        @Override
        public void collect(int doc, long bucket) throws IOException {
            assert bucket == 0;
            values.setDocument(doc);
            final int valuesCount = values.count();
            // SortedBinaryDocValues don't guarantee uniqueness so we need to take care of dups
            previous.clear();
            for (int i = 0; i < valuesCount; ++i) {
                final BytesRef bytes = values.valueAt(i);
                if (includeExclude != null && !includeExclude.accept(bytes)) {
                    continue;
                }
                if (previous.get().equals(bytes)) {
                    continue;
                }
                long bucketOrdinal = bucketOrds.add(bytes);
                if (bucketOrdinal < 0) {
                    // already seen
                    bucketOrdinal = -1 - bucketOrdinal;
                    collectExistingBucket(sub, doc, bucketOrdinal);
                } else {
                    collectBucket(sub, doc, bucketOrdinal);
                }
                previous.copyBytes(bytes);
            }
        }
    };
}
Also used : BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) LeafBucketCollectorBase(org.elasticsearch.search.aggregations.LeafBucketCollectorBase) BytesRef(org.apache.lucene.util.BytesRef) SortedBinaryDocValues(org.elasticsearch.index.fielddata.SortedBinaryDocValues)

Aggregations

BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)150 BytesRef (org.apache.lucene.util.BytesRef)79 ArrayList (java.util.ArrayList)21 IOException (java.io.IOException)17 Term (org.apache.lucene.index.Term)16 HashSet (java.util.HashSet)15 ChecksumIndexInput (org.apache.lucene.store.ChecksumIndexInput)14 FieldType (org.apache.solr.schema.FieldType)14 IndexInput (org.apache.lucene.store.IndexInput)12 BytesRefIterator (org.apache.lucene.util.BytesRefIterator)10 CharsRefBuilder (org.apache.lucene.util.CharsRefBuilder)10 IntsRef (org.apache.lucene.util.IntsRef)10 SchemaField (org.apache.solr.schema.SchemaField)10 BufferedChecksumIndexInput (org.apache.lucene.store.BufferedChecksumIndexInput)9 ParseException (java.text.ParseException)8 IntsRefBuilder (org.apache.lucene.util.IntsRefBuilder)8 DecimalFormat (java.text.DecimalFormat)7 HashMap (java.util.HashMap)7 Map (java.util.Map)7 Directory (org.apache.lucene.store.Directory)7