use of com.google.storage.v2.ReadObjectRequest in project hadoop-connectors by GoogleCloudDataproc.
the class GoogleCloudStorageGrpcReadChannelTest method randomReadRequestsExpectedBytes.
@Test
public void randomReadRequestsExpectedBytes() throws Exception {
int objectSize = GoogleCloudStorageReadOptions.DEFAULT_MIN_RANGE_REQUEST_SIZE * 10;
storageObject.setSize(BigInteger.valueOf(objectSize));
fakeService.setObject(DEFAULT_OBJECT.toBuilder().setSize(objectSize).build());
verify(fakeService, times(1)).setObject(any());
GoogleCloudStorageReadOptions options = GoogleCloudStorageReadOptions.builder().setFadvise(Fadvise.RANDOM).setGrpcChecksumsEnabled(true).setInplaceSeekLimit(5).build();
GoogleCloudStorageGrpcReadChannel readChannel = newReadChannel(options);
// Request bytes less than minimum request size.
ByteBuffer buffer = ByteBuffer.allocate(50);
readChannel.position(10);
readChannel.read(buffer);
assertArrayEquals(fakeService.data.substring(10, 60).toByteArray(), buffer.array());
// Request bytes larger than minimum request size.
buffer = ByteBuffer.allocate(GoogleCloudStorageReadOptions.DEFAULT_MIN_RANGE_REQUEST_SIZE + 1);
readChannel.position(0);
readChannel.read(buffer);
assertArrayEquals(fakeService.data.substring(0, GoogleCloudStorageReadOptions.DEFAULT_MIN_RANGE_REQUEST_SIZE + 1).toByteArray(), buffer.array());
ReadObjectRequest firstExpectedRequest = ReadObjectRequest.newBuilder().setBucket(BUCKET_NAME).setObject(OBJECT_NAME).setGeneration(OBJECT_GENERATION).setReadLimit(GoogleCloudStorageReadOptions.DEFAULT_MIN_RANGE_REQUEST_SIZE).setReadOffset(10).build();
ReadObjectRequest secondExpectedRequest = ReadObjectRequest.newBuilder().setBucket(BUCKET_NAME).setObject(OBJECT_NAME).setGeneration(OBJECT_GENERATION).setReadLimit(GoogleCloudStorageReadOptions.DEFAULT_MIN_RANGE_REQUEST_SIZE + 1).setReadOffset(0).build();
verify(get).setFields(METADATA_FIELDS);
verify(get).execute();
int footerOffset = objectSize - (GoogleCloudStorageReadOptions.DEFAULT_MIN_RANGE_REQUEST_SIZE / 2);
verify(fakeService, times(1)).readObject(eq(ReadObjectRequest.newBuilder().setBucket(BUCKET_NAME).setObject(OBJECT_NAME).setGeneration(OBJECT_GENERATION).setReadOffset(footerOffset).build()), any());
verify(fakeService, times(1)).readObject(eq(firstExpectedRequest), any());
verify(fakeService, times(1)).readObject(eq(secondExpectedRequest), any());
verifyNoMoreInteractions(fakeService);
}
use of com.google.storage.v2.ReadObjectRequest in project hadoop-connectors by GoogleCloudDataproc.
the class GoogleCloudStorageGrpcReadChannelTest method randomReadRequestsExactBytes.
@Test
public void randomReadRequestsExactBytes() throws Exception {
int objectSize = GoogleCloudStorageReadOptions.DEFAULT_MIN_RANGE_REQUEST_SIZE * 10;
storageObject.setSize(BigInteger.valueOf(objectSize));
fakeService.setObject(DEFAULT_OBJECT.toBuilder().setSize(objectSize).build());
verify(fakeService, times(1)).setObject(any());
GoogleCloudStorageReadOptions options = GoogleCloudStorageReadOptions.builder().setFadvise(Fadvise.RANDOM).setGrpcChecksumsEnabled(true).setInplaceSeekLimit(5).build();
GoogleCloudStorageGrpcReadChannel readChannel = newReadChannel(options);
ByteBuffer buffer = ByteBuffer.allocate(50);
readChannel.position(10);
readChannel.read(buffer);
verify(get).setFields(METADATA_FIELDS);
verify(get).execute();
ReadObjectRequest expectedRequest = ReadObjectRequest.newBuilder().setBucket(BUCKET_NAME).setObject(OBJECT_NAME).setGeneration(OBJECT_GENERATION).setReadLimit(GoogleCloudStorageReadOptions.DEFAULT_MIN_RANGE_REQUEST_SIZE).setReadOffset(10).build();
verify(fakeService, times(1)).readObject(eq(expectedRequest), any());
assertArrayEquals(fakeService.data.substring(10, 60).toByteArray(), buffer.array());
int footerOffset = objectSize - (GoogleCloudStorageReadOptions.DEFAULT_MIN_RANGE_REQUEST_SIZE / 2);
verify(fakeService, times(1)).readObject(eq(ReadObjectRequest.newBuilder().setBucket(BUCKET_NAME).setObject(OBJECT_NAME).setGeneration(OBJECT_GENERATION).setReadOffset(footerOffset).build()), any());
verifyNoMoreInteractions(fakeService);
}
use of com.google.storage.v2.ReadObjectRequest in project hadoop-connectors by GoogleCloudDataproc.
the class GoogleCloudStorageGrpcReadChannelTest method readWithLatestGenerationReadConsistencySucceeds.
@Test
public void readWithLatestGenerationReadConsistencySucceeds() throws Exception {
int objectSize = 100;
fakeService.setObject(DEFAULT_OBJECT.toBuilder().setSize(objectSize).setGeneration(1).build());
storageObject.setSize(BigInteger.valueOf(objectSize));
GoogleCloudStorageReadOptions options = GoogleCloudStorageReadOptions.builder().setMinRangeRequestSize(4).build();
GoogleCloudStorageGrpcReadChannel readChannel = newReadChannel(options);
ByteBuffer buffer = ByteBuffer.allocate(10);
readChannel.read(buffer);
fakeService.setObject(DEFAULT_OBJECT.toBuilder().setSize(objectSize).setGeneration(2).build());
readChannel.position(0);
buffer.clear();
readChannel.read(buffer);
ArgumentCaptor<ReadObjectRequest> requestCaptor = ArgumentCaptor.forClass(ReadObjectRequest.class);
verify(get).setFields(METADATA_FIELDS);
verify(get).execute();
verify(fakeService, times(3)).readObject(requestCaptor.capture(), any());
}
use of com.google.storage.v2.ReadObjectRequest in project hadoop-connectors by GoogleCloudDataproc.
the class GoogleCloudStorageGrpcReadChannelTest method repeatedRandomReadsWorkAsExpected.
@Test
public void repeatedRandomReadsWorkAsExpected() throws Exception {
int objectSize = GoogleCloudStorageReadOptions.DEFAULT_MIN_RANGE_REQUEST_SIZE * 10;
storageObject.setSize(BigInteger.valueOf(objectSize));
fakeService.setObject(DEFAULT_OBJECT.toBuilder().setSize(objectSize).build());
verify(fakeService, times(1)).setObject(any());
GoogleCloudStorageReadOptions options = GoogleCloudStorageReadOptions.builder().setFadvise(Fadvise.RANDOM).setGrpcChecksumsEnabled(true).setInplaceSeekLimit(5).build();
GoogleCloudStorageGrpcReadChannel readChannel = newReadChannel(options);
ByteBuffer buffer = ByteBuffer.allocate(50);
readChannel.position(10);
readChannel.read(buffer);
assertArrayEquals(fakeService.data.substring(10, 60).toByteArray(), buffer.array());
buffer = ByteBuffer.allocate(25);
readChannel.position(20);
readChannel.read(buffer);
assertArrayEquals(fakeService.data.substring(20, 45).toByteArray(), buffer.array());
ReadObjectRequest firstExpectedRequest = ReadObjectRequest.newBuilder().setBucket(BUCKET_NAME).setObject(OBJECT_NAME).setGeneration(OBJECT_GENERATION).setReadLimit(GoogleCloudStorageReadOptions.DEFAULT_MIN_RANGE_REQUEST_SIZE).setReadOffset(10).build();
ReadObjectRequest secondExpectedRequest = ReadObjectRequest.newBuilder().setBucket(BUCKET_NAME).setObject(OBJECT_NAME).setGeneration(OBJECT_GENERATION).setReadLimit(GoogleCloudStorageReadOptions.DEFAULT_MIN_RANGE_REQUEST_SIZE).setReadOffset(20).build();
verify(get).setFields(METADATA_FIELDS);
verify(get).execute();
int footerOffset = objectSize - (GoogleCloudStorageReadOptions.DEFAULT_MIN_RANGE_REQUEST_SIZE / 2);
verify(fakeService, times(1)).readObject(eq(ReadObjectRequest.newBuilder().setBucket(BUCKET_NAME).setObject(OBJECT_NAME).setGeneration(OBJECT_GENERATION).setReadOffset(footerOffset).build()), any());
verify(fakeService, times(1)).readObject(eq(firstExpectedRequest), any());
verify(fakeService, times(1)).readObject(eq(secondExpectedRequest), any());
verifyNoMoreInteractions(fakeService);
}
use of com.google.storage.v2.ReadObjectRequest in project gapic-generator-java by googleapis.
the class AsyncReadObject method asyncReadObject.
public static void asyncReadObject() throws Exception {
// It may require modifications to work in your environment.
try (StorageClient storageClient = StorageClient.create()) {
ReadObjectRequest request = ReadObjectRequest.newBuilder().setBucket("bucket-1378203158").setObject("object-1023368385").setGeneration(305703192).setReadOffset(-715377828).setReadLimit(-164298798).setIfGenerationMatch(-1086241088).setIfGenerationNotMatch(1475720404).setIfMetagenerationMatch(1043427781).setIfMetagenerationNotMatch(1025430873).setCommonObjectRequestParams(CommonObjectRequestParams.newBuilder().build()).setCommonRequestParams(CommonRequestParams.newBuilder().build()).setReadMask(FieldMask.newBuilder().build()).build();
ServerStream<ReadObjectResponse> stream = storageClient.readObjectCallable().call(request);
for (ReadObjectResponse response : stream) {
// Do something when a response is received.
}
}
}
Aggregations