List of usage examples for org.apache.lucene.util BytesRefBuilder length
public int length()
From source file:net.semanticmetadata.lire.solr.LireValueSource.java
License:Open Source License
@Override /**/*from w ww .ja v a2 s.c om*/ * Check also {@link org.apache.lucene.queries.function.valuesource.BytesRefFieldSource} */ public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException { final FieldInfo fieldInfo = readerContext.reader().getFieldInfos().fieldInfo(field); if (fieldInfo != null && fieldInfo.getDocValuesType() == DocValuesType.BINARY) { final BinaryDocValues binaryValues = DocValues.getBinary(readerContext.reader(), field); final Bits docsWithField = DocValues.getDocsWithField(readerContext.reader(), field); return new FunctionValues() { @Override public boolean exists(int doc) { return docsWithField.get(doc); } @Override public boolean bytesVal(int doc, BytesRefBuilder target) { target.copyBytes(binaryValues.get(doc)); return target.length() > 0; } @Override public float floatVal(int doc) { return (float) doubleVal(doc); } public String strVal(int doc) { final BytesRefBuilder bytes = new BytesRefBuilder(); return bytesVal(doc, bytes) ? bytes.get().utf8ToString() : null; } /** * This method basically decides which type is delivered on request. It can be a String, * in this case it is the double form the distance function. * @param doc * @return the distance as Double, mapping to {@link FunctionValues#doubleVal(int)} */ @Override public Object objectVal(int doc) { return doubleVal(doc); } @Override public String toString(int doc) { return description() + '=' + strVal(doc); } @Override /** * This method has to be implemented to support sorting! */ public double doubleVal(int doc) { if (binaryValues.get(doc).length > 0) { tmpFeature.setByteArrayRepresentation(binaryValues.get(doc).bytes, binaryValues.get(doc).offset, binaryValues.get(doc).length); return tmpFeature.getDistance(feature); } else return maxDistance; // make sure max distance is returned for those without value } }; } else { // there is no DocVal to sort by. Therefore we need to set the function value to -1 and everything without DocVal gets ranked first? return new DocTermsIndexDocValues(this, readerContext, field) { @Override protected String toTerm(String readableValue) { return Double.toString(maxDistance); } @Override public Object objectVal(int doc) { return maxDistance; } @Override public String toString(int doc) { return description() + '=' + strVal(doc); } public double doubleVal(int doc) { return maxDistance; } }; } }
From source file:org.apache.solr.codecs.onsql.ONSQLUtil.java
License:Apache License
public static void checkFooter(ChecksumIndexInput input) throws IOException { BytesRefBuilder scratch = new BytesRefBuilder(); String expectedChecksum = String.format(Locale.ROOT, "%020d", input.getChecksum()); ONSQLUtil.readLine(input, scratch);//from w w w .ja va2s . c om if (StringHelper.startsWith(scratch.get(), CHECKSUM) == false) { throw new CorruptIndexException("ONSQL failure: expected checksum line but got " + scratch.get().utf8ToString() + " (resource=" + input + ")"); } String actualChecksum = new BytesRef(scratch.bytes(), CHECKSUM.length, scratch.length() - CHECKSUM.length) .utf8ToString(); if (!expectedChecksum.equals(actualChecksum)) { throw new CorruptIndexException("ONSQL checksum failure: " + actualChecksum + " != " + expectedChecksum + " (resource=" + input + ")"); } if (input.length() != input.getFilePointer()) { throw new CorruptIndexException( "Unexpected stuff at the end of file, please be careful with your text editor! (resource=" + input + ")"); } }
From source file:org.codelibs.elasticsearch.common.Strings.java
License:Apache License
public static byte[] toUTF8Bytes(CharSequence charSequence, BytesRefBuilder spare) { spare.copyChars(charSequence);/*from w w w.j a va 2s . c om*/ return Arrays.copyOf(spare.bytes(), spare.length()); }
From source file:org.elasticsearch.common.blobstore.BlobStoreTests.java
License:Apache License
@Test public void testWriteRead() throws IOException { final BlobStore store = newBlobStore(); final BlobContainer container = store.blobContainer(new BlobPath()); byte[] data = randomBytes(randomIntBetween(10, scaledRandomIntBetween(1024, 1 << 16))); container.writeBlob("foobar", new BytesArray(data)); 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)); }// w ww . j av a2s . c o m assertEquals(data.length, target.length()); assertArrayEquals(data, Arrays.copyOfRange(target.bytes(), 0, target.length())); } store.close(); }
From source file:org.elasticsearch.common.bytes.AbstractBytesReferenceTestCase.java
License:Apache License
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:// w w w. j a va 2s .c om 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)); }
From source file:org.elasticsearch.common.bytes.PagedBytesReferenceTest.java
License:Apache License
public void testRandomReads() throws IOException { int length = randomIntBetween(10, scaledRandomIntBetween(PAGE_SIZE * 2, PAGE_SIZE * 20)); BytesReference pbr = getRandomizedPagedBytesReference(length); StreamInput streamInput = pbr.streamInput(); BytesRefBuilder target = new BytesRefBuilder(); while (target.length() < pbr.length()) { switch (randomIntBetween(0, 10)) { case 6:/* ww w. j a va 2 s . co m*/ 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(pbr.toBytes(), Arrays.copyOfRange(targetBytes.bytes, targetBytes.offset, targetBytes.length)); }
From source file:org.elasticsearch.repositories.ESBlobStoreContainerTestCase.java
License:Apache License
public void testWriteRead() throws IOException { try (final 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)); 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)); }// w w w.j a v a 2 s . co m assertEquals(data.length, target.length()); assertArrayEquals(data, Arrays.copyOfRange(target.bytes(), 0, target.length())); } } }
From source file:org.elasticsearch.test.ESBlobStoreContainerTestCase.java
License:Apache License
public void testWriteRead() throws IOException { try (final BlobStore store = newBlobStore()) { final BlobContainer container = store.blobContainer(new BlobPath()); byte[] data = randomBytes(randomIntBetween(10, scaledRandomIntBetween(1024, 1 << 16))); container.writeBlob("foobar", new BytesArray(data)); 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)); }/*from w w w . jav a2 s . c o m*/ assertEquals(data.length, target.length()); assertArrayEquals(data, Arrays.copyOfRange(target.bytes(), 0, target.length())); } } }