Search in sources :

Example 16 with ByteArray

use of com.google.cloud.ByteArray in project beam by apache.

the class MutationKeyEncoder method encodeKey.

private void encodeKey(OrderedCode orderedCode, String tableName, Key key) {
    List<SpannerSchema.KeyPart> parts = schema.getKeyParts(tableName);
    Iterator<Object> it = key.getParts().iterator();
    for (SpannerSchema.KeyPart part : parts) {
        Object value = it.next();
        if (value == null) {
            if (part.isDesc()) {
                orderedCode.writeInfinityDecreasing();
            } else {
                orderedCode.writeInfinity();
            }
        } else {
            if (value instanceof Boolean) {
                writeNumber(orderedCode, part, (long) ((Boolean) value ? 0 : 1));
            } else if (value instanceof Long) {
                writeNumber(orderedCode, part, (long) value);
            } else if (value instanceof Double) {
                writeNumber(orderedCode, part, Double.doubleToLongBits((double) value));
            } else if (value instanceof String) {
                writeString(orderedCode, part, (String) value);
            } else if (value instanceof ByteArray) {
                writeBytes(orderedCode, part, (ByteArray) value);
            } else if (value instanceof Timestamp) {
                writeTimestamp(orderedCode, part, (Timestamp) value);
            } else if (value instanceof Date) {
                writeNumber(orderedCode, part, encodeDate((Date) value));
            } else {
                throw new IllegalArgumentException("Unknown key part " + value);
            }
        }
    }
}
Also used : KeyPart(org.apache.beam.sdk.io.gcp.spanner.SpannerSchema.KeyPart) Timestamp(com.google.cloud.Timestamp) Date(com.google.cloud.Date) KeyPart(org.apache.beam.sdk.io.gcp.spanner.SpannerSchema.KeyPart) ByteArray(com.google.cloud.ByteArray)

Example 17 with ByteArray

use of com.google.cloud.ByteArray in project spanner-jdbc by olavloite.

the class CloudSpannerConversionUtilTest method testToCloudSpannerBytes.

@Test
public void testToCloudSpannerBytes() {
    byte[][] input = new byte[][] { "AA".getBytes(), "BB".getBytes() };
    List<ByteArray> output = CloudSpannerConversionUtil.toCloudSpannerBytes(input);
    ByteArray inp1 = ByteArray.copyFrom("AA".getBytes());
    ByteArray inp2 = ByteArray.copyFrom("BB".getBytes());
    assertArrayEquals(new ByteArray[] { inp1, inp2 }, output.toArray());
}
Also used : ByteArray(com.google.cloud.ByteArray) Test(org.junit.Test) UnitTest(nl.topicus.jdbc.test.category.UnitTest)

Aggregations

ByteArray (com.google.cloud.ByteArray)17 Test (org.junit.Test)15 IntegrationTest (com.google.cloud.spanner.IntegrationTest)6 Struct (com.google.cloud.spanner.Struct)5 UnitTest (nl.topicus.jdbc.test.category.UnitTest)2 Date (com.google.cloud.Date)1 Timestamp (com.google.cloud.Timestamp)1 KeySet (com.google.cloud.spanner.KeySet)1 Mutation (com.google.cloud.spanner.Mutation)1 ResultSet (com.google.cloud.spanner.ResultSet)1 ByteString (com.google.protobuf.ByteString)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Random (java.util.Random)1 KeyPart (org.apache.beam.sdk.io.gcp.spanner.SpannerSchema.KeyPart)1