Java Code Examples for com.google.protobuf.UnsafeByteOperations#unsafeWriteTo()

The following examples show how to use com.google.protobuf.UnsafeByteOperations#unsafeWriteTo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestOptimisticByteOutput.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Test
public void testRopeByteString() throws Exception {
  ByteString literal = ByteString.copyFrom(smallData);

  ByteString data = literal;
  for (int i = 0; i < 3; i++) {
    data = data.concat(literal);
  }

  final byte[] expected;
  try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
    for (int i = 0; i < 4; i++) {
      baos.write(smallData);
    }
    expected = baos.toByteArray();
  }

  OptimisticByteOutput byteOutput = new OptimisticByteOutput(smallData.length * 4);
  UnsafeByteOperations.unsafeWriteTo(data, byteOutput);

  assertArrayEquals(expected, byteOutput.toByteArray());
}
 
Example 2
Source File: BigtableToAvro.java    From DataflowTemplates with Apache License 2.0 5 votes vote down vote up
/**
 * Extracts the byte array from the given {@link ByteString} without copy.
 *
 * @param byteString A {@link ByteString} from which to extract the array.
 * @return an array of byte.
 */
protected static byte[] toByteArray(final ByteString byteString) {
  try {
    ZeroCopyByteOutput byteOutput = new ZeroCopyByteOutput();
    UnsafeByteOperations.unsafeWriteTo(byteString, byteOutput);
    return byteOutput.bytes;
  } catch (IOException e) {
    return byteString.toByteArray();
  }
}
 
Example 3
Source File: TestOptimisticByteOutput.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Test
public void testLiteralByteString() throws Exception {
  ByteString literal = ByteString.copyFrom(smallData);

  OptimisticByteOutput byteOutput = new OptimisticByteOutput(literal.size());
  UnsafeByteOperations.unsafeWriteTo(literal, byteOutput);

  byte[] array = (byte[]) FieldUtils.readField(literal, "bytes", true);
  assertArrayEquals(smallData, byteOutput.toByteArray());
  assertSame(array, byteOutput.toByteArray());
}
 
Example 4
Source File: TestOptimisticByteOutput.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Test
public void testRopeByteStringWithZeroOnLeft() throws Exception {
  ByteString literal = ByteString.copyFrom(smallData);

  ByteString data = (ByteString) NEW_ROPE_BYTE_STRING_INSTANCE.invoke(null, ByteString.EMPTY, literal);

  OptimisticByteOutput byteOutput = new OptimisticByteOutput(smallData.length);
  UnsafeByteOperations.unsafeWriteTo(data, byteOutput);

  byte[] array = (byte[]) FieldUtils.readField(literal, "bytes", true);
  assertArrayEquals(smallData, byteOutput.toByteArray());
  assertSame(array, byteOutput.toByteArray());
}
 
Example 5
Source File: TestOptimisticByteOutput.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Test
public void testRopeByteStringWithZeroOnRight() throws Exception {
  ByteString literal = ByteString.copyFrom(smallData);

  ByteString data = (ByteString) NEW_ROPE_BYTE_STRING_INSTANCE.invoke(null, literal, ByteString.EMPTY);

  OptimisticByteOutput byteOutput = new OptimisticByteOutput(smallData.length);
  UnsafeByteOperations.unsafeWriteTo(data, byteOutput);

  byte[] array = (byte[]) FieldUtils.readField(literal, "bytes", true);
  assertArrayEquals(smallData, byteOutput.toByteArray());
  assertSame(array, byteOutput.toByteArray());
}
 
Example 6
Source File: TestOptimisticByteOutputUnsafeByteOperations.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Test
public void testUnsafeByteOperations() throws IOException {
  // Arrange
  final ByteString bytes = ByteString.copyFrom(data);
  final OptimisticByteOutput byteOutput = new OptimisticByteOutput(bytes.size());

  // Act
  UnsafeByteOperations.unsafeWriteTo(bytes, byteOutput);

  // Assert
  assertNotSame(data, byteOutput.toByteArray());
  assertArrayEquals(data, byteOutput.toByteArray());
}
 
Example 7
Source File: CoreIndexedStoreImpl.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
private Document toDoc(KVStoreTuple<K> key, PutOption option) {
  final Document doc = new Document();
  final SimpleDocumentWriter documentWriter = new SimpleDocumentWriter(doc);

  RemoteDataStoreProtobuf.PutOptionInfo putOptionInfo = option.getPutOptionInfo();

  for (RemoteDataStoreProtobuf.PutRequestIndexField indexField : putOptionInfo.getIndexFieldsList()) {
    IndexKey indexKey = RemoteDataStoreUtils.toIndexKey(indexField.getKey());

    if (indexField.getValueDoubleCount() > 0) {
      indexField.getValueDoubleList().forEach(v -> documentWriter.write(indexKey, v));
    } else if (indexField.getValueInt32Count() > 0) {
      indexField.getValueInt32List().forEach(v -> documentWriter.write(indexKey, v));
    } else if (indexField.getValueInt64Count() > 0) {
      indexField.getValueInt64List().forEach(v -> documentWriter.write(indexKey, v));
    } else if (indexField.getValueBytesCount() > 0) {
      final byte[][] byteArray = new byte[indexField.getValueBytesList().size()][];
      for (int i = 0; i < indexField.getValueBytesList().size(); i++) {
        byteArray[i] = indexField.getValueBytes(i).toByteArray();
        final ByteString byteString = indexField.getValueBytes(i);

        final OptimisticByteOutput byteOutput = new OptimisticByteOutput(byteString.size());
        try {
          UnsafeByteOperations.unsafeWriteTo(byteString, byteOutput);
        } catch (IOException e) {
          throw new IllegalStateException(String.format("Problem reading binary data from field: %s", indexKey.getIndexFieldName()), e);
        }
        byteArray[i] = byteOutput.toByteArray();
      }

      documentWriter.write(indexKey, byteArray);
    } else if (indexField.getValueStringCount() > 0) {
      documentWriter.write(indexKey, indexField.getValueStringList().toArray(new String[indexField.getValueStringList().size()]));
    } else {
      throw new IllegalStateException(String.format("Unknown index field type for field name: %s", indexField.getKey().getIndexFieldName()));
    }
  }

  if (doc.getFields().isEmpty()) {
    return null;
  }

  documentWriter.write(ID_KEY, key.getSerializedBytes());

  return doc;
}