use of org.elasticsearch.common.bytes.BytesArray in project crate by crate.
the class RemoteDigestBlob method chunk.
private Status chunk(ChannelBuffer buffer, boolean last) {
assert transferId != null : "transferId should not be null";
PutChunkRequest request = new PutChunkRequest(index, Hex.decodeHex(digest), transferId, new BytesArray(buffer.array()), size, last);
size += buffer.readableBytes();
PutChunkResponse putChunkResponse = client.execute(PutChunkAction.INSTANCE, request).actionGet();
return putChunkResponse.status();
}
use of org.elasticsearch.common.bytes.BytesArray in project elasticsearch by elastic.
the class RandomObjects method randomStoredFieldValues.
/**
* Returns a tuple containing random stored field values and their corresponding expected values once printed out
* via {@link org.elasticsearch.common.xcontent.ToXContent#toXContent(XContentBuilder, ToXContent.Params)} and parsed back via
* {@link org.elasticsearch.common.xcontent.XContentParser#objectText()}.
* Generates values based on what can get printed out. Stored fields values are retrieved from lucene and converted via
* {@link org.elasticsearch.index.mapper.MappedFieldType#valueForDisplay(Object)} to either strings, numbers or booleans.
*
* @param random Random generator
* @param xContentType the content type, used to determine what the expected values are for float numbers.
*/
public static Tuple<List<Object>, List<Object>> randomStoredFieldValues(Random random, XContentType xContentType) {
int numValues = randomIntBetween(random, 1, 5);
List<Object> originalValues = new ArrayList<>();
List<Object> expectedParsedValues = new ArrayList<>();
int dataType = randomIntBetween(random, 0, 8);
for (int i = 0; i < numValues; i++) {
switch(dataType) {
case 0:
long randomLong = random.nextLong();
originalValues.add(randomLong);
expectedParsedValues.add(randomLong);
break;
case 1:
int randomInt = random.nextInt();
originalValues.add(randomInt);
expectedParsedValues.add(randomInt);
break;
case 2:
Short randomShort = (short) random.nextInt();
originalValues.add(randomShort);
expectedParsedValues.add(randomShort.intValue());
break;
case 3:
Byte randomByte = (byte) random.nextInt();
originalValues.add(randomByte);
expectedParsedValues.add(randomByte.intValue());
break;
case 4:
double randomDouble = random.nextDouble();
originalValues.add(randomDouble);
expectedParsedValues.add(randomDouble);
break;
case 5:
Float randomFloat = random.nextFloat();
originalValues.add(randomFloat);
if (xContentType == XContentType.CBOR) {
//with CBOR we get back a float
expectedParsedValues.add(randomFloat);
} else if (xContentType == XContentType.SMILE) {
//with SMILE we get back a double
expectedParsedValues.add(randomFloat.doubleValue());
} else {
//with JSON AND YAML we get back a double, but with float precision.
expectedParsedValues.add(Double.parseDouble(randomFloat.toString()));
}
break;
case 6:
boolean randomBoolean = random.nextBoolean();
originalValues.add(randomBoolean);
expectedParsedValues.add(randomBoolean);
break;
case 7:
String randomString = random.nextBoolean() ? RandomStrings.randomAsciiOfLengthBetween(random, 3, 10) : randomUnicodeOfLengthBetween(random, 3, 10);
originalValues.add(randomString);
expectedParsedValues.add(randomString);
break;
case 8:
byte[] randomBytes = RandomStrings.randomUnicodeOfLengthBetween(random, 10, 50).getBytes(StandardCharsets.UTF_8);
BytesArray randomBytesArray = new BytesArray(randomBytes);
originalValues.add(randomBytesArray);
if (xContentType == XContentType.JSON || xContentType == XContentType.YAML) {
//JSON and YAML write the base64 format
expectedParsedValues.add(Base64.getEncoder().encodeToString(randomBytes));
} else {
//SMILE and CBOR write the original bytes as they support binary format
expectedParsedValues.add(randomBytesArray);
}
break;
default:
throw new UnsupportedOperationException();
}
}
return Tuple.tuple(originalValues, expectedParsedValues);
}
use of org.elasticsearch.common.bytes.BytesArray in project elasticsearch by elastic.
the class ESBlobStoreContainerTestCase method testDeleteBlob.
public void testDeleteBlob() throws IOException {
try (BlobStore store = newBlobStore()) {
final String blobName = "foobar";
final BlobContainer container = store.blobContainer(new BlobPath());
expectThrows(IOException.class, () -> container.deleteBlob(blobName));
byte[] data = randomBytes(randomIntBetween(10, scaledRandomIntBetween(1024, 1 << 16)));
final BytesArray bytesArray = new BytesArray(data);
writeBlob(container, blobName, bytesArray);
// should not raise
container.deleteBlob(blobName);
// blob deleted, so should raise again
expectThrows(IOException.class, () -> container.deleteBlob(blobName));
}
}
use of org.elasticsearch.common.bytes.BytesArray in project elasticsearch by elastic.
the class ESBlobStoreContainerTestCase method testVerifyOverwriteFails.
public void testVerifyOverwriteFails() throws IOException {
try (BlobStore store = newBlobStore()) {
final String blobName = "foobar";
final BlobContainer container = store.blobContainer(new BlobPath());
byte[] data = randomBytes(randomIntBetween(10, scaledRandomIntBetween(1024, 1 << 16)));
final BytesArray bytesArray = new BytesArray(data);
writeBlob(container, blobName, bytesArray);
// should not be able to overwrite existing blob
expectThrows(IOException.class, () -> writeBlob(container, blobName, bytesArray));
container.deleteBlob(blobName);
// after deleting the previous blob, we should be able to write to it again
writeBlob(container, blobName, bytesArray);
}
}
use of org.elasticsearch.common.bytes.BytesArray in project elasticsearch by elastic.
the class ChannelsTests method testPartialReadWriteThroughBuffers.
public void testPartialReadWriteThroughBuffers() throws IOException {
int length = randomIntBetween(1, randomBytes.length / 2);
int offset = randomIntBetween(0, randomBytes.length - length);
ByteBuffer source;
if (randomBoolean()) {
source = ByteBuffer.wrap(randomBytes, offset, length);
} else {
source = ByteBuffer.allocateDirect(length);
source.put(randomBytes, offset, length);
source.flip();
}
Channels.writeToChannel(source, fileChannel);
int lengthToRead = randomIntBetween(1, length);
int offsetToRead = randomIntBetween(0, length - lengthToRead);
ByteBuffer copy;
if (randomBoolean()) {
copy = ByteBuffer.allocate(lengthToRead);
} else {
copy = ByteBuffer.allocateDirect(lengthToRead);
}
int read = Channels.readFromFileChannel(fileChannel, offsetToRead, copy);
assertThat(read, Matchers.equalTo(lengthToRead));
copy.flip();
BytesReference sourceRef = new BytesArray(randomBytes, offset + offsetToRead, lengthToRead);
byte[] tmp = new byte[copy.remaining()];
copy.duplicate().get(tmp);
BytesReference copyRef = new BytesArray(tmp);
assertTrue("read bytes didn't match written bytes", sourceRef.equals(copyRef));
}
Aggregations