Search in sources :

Example 71 with BytesRefBuilder

use of org.apache.lucene.util.BytesRefBuilder in project lucene-solr by apache.

the class TestDocumentsWriterDeleteQueue method testStressDeleteQueue.

public void testStressDeleteQueue() throws InterruptedException {
    DocumentsWriterDeleteQueue queue = new DocumentsWriterDeleteQueue();
    Set<Term> uniqueValues = new HashSet<>();
    final int size = 10000 + random().nextInt(500) * RANDOM_MULTIPLIER;
    Integer[] ids = new Integer[size];
    for (int i = 0; i < ids.length; i++) {
        ids[i] = random().nextInt();
        uniqueValues.add(new Term("id", ids[i].toString()));
    }
    CountDownLatch latch = new CountDownLatch(1);
    AtomicInteger index = new AtomicInteger(0);
    final int numThreads = 2 + random().nextInt(5);
    UpdateThread[] threads = new UpdateThread[numThreads];
    for (int i = 0; i < threads.length; i++) {
        threads[i] = new UpdateThread(queue, index, ids, latch);
        threads[i].start();
    }
    latch.countDown();
    for (int i = 0; i < threads.length; i++) {
        threads[i].join();
    }
    for (UpdateThread updateThread : threads) {
        DeleteSlice slice = updateThread.slice;
        queue.updateSlice(slice);
        BufferedUpdates deletes = updateThread.deletes;
        slice.apply(deletes, BufferedUpdates.MAX_INT);
        assertEquals(uniqueValues, deletes.terms.keySet());
    }
    queue.tryApplyGlobalSlice();
    Set<Term> frozenSet = new HashSet<>();
    BytesRefBuilder builder = new BytesRefBuilder();
    TermIterator iter = queue.freezeGlobalBuffer(null).termIterator();
    while (iter.next() != null) {
        builder.copyBytes(iter.bytes);
        frozenSet.add(new Term(iter.field(), builder.toBytesRef()));
    }
    assertEquals("num deletes must be 0 after freeze", 0, queue.numGlobalTermDeletes());
    assertEquals(uniqueValues.size(), frozenSet.size());
    assertEquals(uniqueValues, frozenSet);
}
Also used : BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) TermIterator(org.apache.lucene.index.PrefixCodedTerms.TermIterator) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DeleteSlice(org.apache.lucene.index.DocumentsWriterDeleteQueue.DeleteSlice) HashSet(java.util.HashSet)

Example 72 with BytesRefBuilder

use of org.apache.lucene.util.BytesRefBuilder in project gerrit by GerritCodeReview.

the class QueryBuilder method stringTerm.

static Term stringTerm(String name, String value) {
    BytesRefBuilder builder = new BytesRefBuilder();
    builder.append(value.getBytes(UTF_8), 0, value.length());
    return new Term(name, builder.get());
}
Also used : BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) Term(org.apache.lucene.index.Term)

Example 73 with BytesRefBuilder

use of org.apache.lucene.util.BytesRefBuilder in project gerrit by GerritCodeReview.

the class QueryBuilder method intTerm.

static Term intTerm(String name, int value) {
    BytesRefBuilder builder = new BytesRefBuilder();
    LegacyNumericUtils.intToPrefixCoded(value, 0, builder);
    return new Term(name, builder.get());
}
Also used : BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) Term(org.apache.lucene.index.Term)

Example 74 with BytesRefBuilder

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

the class ESBlobStoreContainerTestCase method testWriteRead.

public void testWriteRead() throws IOException {
    try (BlobStore store = newBlobStore()) {
        final BlobContainer container = store.blobContainer(new BlobPath());
        byte[] data = randomBytes(randomIntBetween(10, scaledRandomIntBetween(1024, 1 << 16)));
        writeBlob(container, "foobar", new BytesArray(data), randomBoolean());
        if (randomBoolean()) {
            // override file, to check if we get latest contents
            data = randomBytes(randomIntBetween(10, scaledRandomIntBetween(1024, 1 << 16)));
            writeBlob(container, "foobar", new BytesArray(data), false);
        }
        try (InputStream stream = container.readBlob("foobar")) {
            BytesRefBuilder target = new BytesRefBuilder();
            while (target.length() < data.length) {
                byte[] buffer = new byte[scaledRandomIntBetween(1, data.length - target.length())];
                int offset = scaledRandomIntBetween(0, buffer.length - 1);
                int read = stream.read(buffer, offset, buffer.length - offset);
                target.append(new BytesRef(buffer, offset, read));
            }
            assertEquals(data.length, target.length());
            assertArrayEquals(data, Arrays.copyOfRange(target.bytes(), 0, target.length()));
        }
    }
}
Also used : BlobPath(org.elasticsearch.common.blobstore.BlobPath) BytesArray(org.elasticsearch.common.bytes.BytesArray) BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) InputStream(java.io.InputStream) BlobContainer(org.elasticsearch.common.blobstore.BlobContainer) BlobStore(org.elasticsearch.common.blobstore.BlobStore) BytesRef(org.apache.lucene.util.BytesRef)

Example 75 with BytesRefBuilder

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

the class AbstractBytesReferenceTestCase method testIteratorRandom.

public void testIteratorRandom() throws IOException {
    int length = randomIntBetween(10, PAGE_SIZE * randomIntBetween(2, 8));
    BytesReference pbr = newBytesReference(length);
    if (randomBoolean()) {
        int sliceOffset = randomIntBetween(0, pbr.length());
        int sliceLength = randomIntBetween(0, pbr.length() - sliceOffset);
        pbr = pbr.slice(sliceOffset, sliceLength);
    }
    if (randomBoolean()) {
        pbr = new BytesArray(pbr.toBytesRef());
    }
    BytesRefIterator iterator = pbr.iterator();
    BytesRef ref = null;
    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)

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