Search in sources :

Example 1 with BytesRefIterator

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

the class AbstractBytesReferenceTestCase method testSliceIterator.

public void testSliceIterator() throws IOException {
    int length = randomIntBetween(10, PAGE_SIZE * randomIntBetween(2, 8));
    BytesReference pbr = newBytesReference(length);
    int sliceOffset = randomIntBetween(0, pbr.length());
    int sliceLength = randomIntBetween(0, pbr.length() - sliceOffset);
    BytesReference slice = pbr.slice(sliceOffset, sliceLength);
    BytesRefIterator iterator = slice.iterator();
    BytesRef ref = null;
    BytesRefBuilder builder = new BytesRefBuilder();
    while ((ref = iterator.next()) != null) {
        builder.append(ref);
    }
    assertArrayEquals(BytesReference.toBytes(slice), 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 2 with BytesRefIterator

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

the class RecoveryTarget method writeFileChunk.

@Override
public void writeFileChunk(StoreFileMetaData fileMetaData, long position, BytesReference content, boolean lastChunk, int totalTranslogOps) throws IOException {
    final Store store = store();
    final String name = fileMetaData.name();
    state().getTranslog().totalOperations(totalTranslogOps);
    final RecoveryState.Index indexState = state().getIndex();
    IndexOutput indexOutput;
    if (position == 0) {
        indexOutput = openAndPutIndexOutput(name, fileMetaData, store);
    } else {
        indexOutput = getOpenIndexOutput(name);
    }
    BytesRefIterator iterator = content.iterator();
    BytesRef scratch;
    while ((scratch = iterator.next()) != null) {
        // we iterate over all pages - this is a 0-copy for all core impls
        indexOutput.writeBytes(scratch.bytes, scratch.offset, scratch.length);
    }
    indexState.addRecoveredBytesToFile(name, content.length());
    if (indexOutput.getFilePointer() >= fileMetaData.length() || lastChunk) {
        try {
            Store.verify(indexOutput);
        } finally {
            // we are done
            indexOutput.close();
        }
        final String temporaryFileName = getTempNameForFile(name);
        assert Arrays.asList(store.directory().listAll()).contains(temporaryFileName) : "expected: [" + temporaryFileName + "] in " + Arrays.toString(store.directory().listAll());
        store.directory().sync(Collections.singleton(temporaryFileName));
        IndexOutput remove = removeOpenIndexOutputs(name);
        // remove maybe null if we got finished
        assert remove == null || remove == indexOutput;
    }
}
Also used : BytesRefIterator(org.apache.lucene.util.BytesRefIterator) Store(org.elasticsearch.index.store.Store) IndexOutput(org.apache.lucene.store.IndexOutput) BytesRef(org.apache.lucene.util.BytesRef)

Example 3 with BytesRefIterator

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

the class BytesReference method compareIterators.

/**
     * Compares the two references using the given int function.
     */
private static int compareIterators(final BytesReference a, final BytesReference b, final ToIntBiFunction<BytesRef, BytesRef> f) {
    try {
        // we use the iterators since it's a 0-copy comparison where possible!
        final long lengthToCompare = Math.min(a.length(), b.length());
        final BytesRefIterator aIter = a.iterator();
        final BytesRefIterator bIter = b.iterator();
        BytesRef aRef = aIter.next();
        BytesRef bRef = bIter.next();
        if (aRef != null && bRef != null) {
            // do we have any data?
            // we clone since we modify the offsets and length in the iteration below
            aRef = aRef.clone();
            bRef = bRef.clone();
            if (aRef.length == a.length() && bRef.length == b.length()) {
                // is it only one array slice we are comparing?
                return f.applyAsInt(aRef, bRef);
            } else {
                for (int i = 0; i < lengthToCompare; ) {
                    if (aRef.length == 0) {
                        // must be non null otherwise we have a bug
                        aRef = aIter.next().clone();
                    }
                    if (bRef.length == 0) {
                        // must be non null otherwise we have a bug
                        bRef = bIter.next().clone();
                    }
                    final int aLength = aRef.length;
                    final int bLength = bRef.length;
                    // shrink to the same length and use the fast compare in lucene
                    final int length = Math.min(aLength, bLength);
                    aRef.length = bRef.length = length;
                    // now we move to the fast comparison - this is the hot part of the loop
                    int diff = f.applyAsInt(aRef, bRef);
                    aRef.length = aLength;
                    bRef.length = bLength;
                    if (diff != 0) {
                        return diff;
                    }
                    advance(aRef, length);
                    advance(bRef, length);
                    i += length;
                }
            }
        }
        // One is a prefix of the other, or, they are equal:
        return a.length() - b.length();
    } catch (IOException ex) {
        throw new AssertionError("can not happen", ex);
    }
}
Also used : BytesRefIterator(org.apache.lucene.util.BytesRefIterator) IOException(java.io.IOException) BytesRef(org.apache.lucene.util.BytesRef)

Example 4 with BytesRefIterator

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

the class BytesReference method hashCode.

@Override
public int hashCode() {
    if (hash == null) {
        final BytesRefIterator iterator = iterator();
        BytesRef ref;
        int result = 1;
        try {
            while ((ref = iterator.next()) != null) {
                for (int i = 0; i < ref.length; i++) {
                    result = 31 * result + ref.bytes[ref.offset + i];
                }
            }
        } catch (IOException ex) {
            throw new AssertionError("wont happen", ex);
        }
        return hash = result;
    } else {
        return hash.intValue();
    }
}
Also used : BytesRefIterator(org.apache.lucene.util.BytesRefIterator) IOException(java.io.IOException) BytesRef(org.apache.lucene.util.BytesRef)

Example 5 with BytesRefIterator

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

the class BytesReference method writeTo.

/**
     * Writes the bytes directly to the output stream.
     */
public void writeTo(OutputStream os) throws IOException {
    final BytesRefIterator iterator = iterator();
    BytesRef ref;
    while ((ref = iterator.next()) != null) {
        os.write(ref.bytes, ref.offset, ref.length);
    }
}
Also used : BytesRefIterator(org.apache.lucene.util.BytesRefIterator) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

BytesRefIterator (org.apache.lucene.util.BytesRefIterator)37 BytesRef (org.apache.lucene.util.BytesRef)35 IOException (java.io.IOException)10 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)10 ArrayList (java.util.ArrayList)4 ByteBuf (io.netty.buffer.ByteBuf)2 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)2 ByteBuffer (java.nio.ByteBuffer)2 IndexReader (org.apache.lucene.index.IndexReader)2 IndexWriter (org.apache.lucene.index.IndexWriter)2 Directory (org.apache.lucene.store.Directory)2 IndexOutput (org.apache.lucene.store.IndexOutput)2 DataFormatException (java.util.zip.DataFormatException)1 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)1 Document (org.apache.lucene.document.Document)1 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)1 PostingsEnum (org.apache.lucene.index.PostingsEnum)1 Terms (org.apache.lucene.index.Terms)1 TermsEnum (org.apache.lucene.index.TermsEnum)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1