Skip to content

Commit 4fbb46a

Browse files
author
Mairbek Khadikov
committed
Javadocs and formatting.
1 parent 3329759 commit 4fbb46a

4 files changed

Lines changed: 55 additions & 45 deletions

File tree

google-cloud-spanner/src/main/java/com/google/cloud/spanner/GrpcStruct.java

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
import static com.google.cloud.spanner.SpannerExceptionFactory.newSpannerException;
2929
import static com.google.common.base.Preconditions.checkArgument;
3030

31+
/**
32+
* A gRPC/proto-based struct.
33+
*/
3134
class GrpcStruct extends Struct {
3235
private final Type type;
3336
private final List<Object> rowData;
@@ -37,23 +40,7 @@ class GrpcStruct extends Struct {
3740
this.rowData = rowData;
3841
}
3942

40-
static com.google.cloud.spanner.Value valueFromTypedProto(com.google.protobuf.Value proto) {
41-
if (proto.getKindCase() != Value.KindCase.LIST_VALUE && proto.getListValue().getValuesCount()
42-
!= 2) {
43-
throw new AssertionError("Expected a list of two elements: type and value");
44-
}
45-
Type type;
46-
try {
47-
type = Type.fromProto(
48-
com.google.spanner.v1.Type.parseFrom(
49-
proto.getListValue().getValues(0).getStringValueBytes()));
50-
} catch (InvalidProtocolBufferException e) {
51-
throw new AssertionError("Expected serialized type.");
52-
}
53-
return asSpannerValue(type, proto.getListValue().getValues(1));
54-
}
55-
56-
static Object fromTypedProto(com.google.protobuf.Value proto) {
43+
static com.google.cloud.spanner.Value valueFromTypedProto(Value proto) {
5744
if (proto.getKindCase() != Value.KindCase.LIST_VALUE && proto.getListValue().getValuesCount()
5845
!= 2) {
5946
throw new AssertionError("Expected a list of two elements: type and value");
@@ -66,13 +53,8 @@ static Object fromTypedProto(com.google.protobuf.Value proto) {
6653
} catch (InvalidProtocolBufferException e) {
6754
throw new AssertionError("Expected serialized type.");
6855
}
69-
return decodeValue(type, proto.getListValue().getValues(1));
70-
}
71-
72-
static com.google.cloud.spanner.Value asSpannerValue(
73-
Type fieldType, com.google.protobuf.Value proto) {
74-
Object value = decodeValue(fieldType, proto);
75-
switch (fieldType.getCode()) {
56+
Object value = decodeValue(type, proto.getListValue().getValues(1));
57+
switch (type.getCode()) {
7658
case BOOL:
7759
return com.google.cloud.spanner.Value.bool((Boolean) value);
7860
case INT64:
@@ -88,9 +70,9 @@ static com.google.cloud.spanner.Value asSpannerValue(
8870
(com.google.cloud.Timestamp) value);
8971
case DATE:
9072
return com.google.cloud.spanner.Value.date(
91-
(com.google.cloud.Date) value);
73+
(Date) value);
9274
case ARRAY:
93-
Type elementType = fieldType.getArrayElementType();
75+
Type elementType = type.getArrayElementType();
9476
switch (elementType.getCode()) {
9577
case BOOL:
9678
return com.google.cloud.spanner.Value.boolArray((Iterable<Boolean>) value);
@@ -107,7 +89,7 @@ static com.google.cloud.spanner.Value asSpannerValue(
10789
(Iterable<com.google.cloud.Timestamp>) value);
10890
case DATE:
10991
return com.google.cloud.spanner.Value.dateArray(
110-
(Iterable<com.google.cloud.Date>) value);
92+
(Iterable<Date>) value);
11193

11294
case STRUCT:
11395
List<Type.StructField> fields = elementType.getStructFields();
@@ -119,11 +101,27 @@ static com.google.cloud.spanner.Value asSpannerValue(
119101
}
120102
case STRUCT: // Not a legal top-level field type.
121103
default:
122-
throw new AssertionError("Unhandled type code: " + fieldType.getCode());
104+
throw new AssertionError("Unhandled type code: " + type.getCode());
123105
}
124106
}
125107

126-
static Object decodeValue(Type fieldType, com.google.protobuf.Value proto) {
108+
static Object fromTypedProto(Value proto) {
109+
if (proto.getKindCase() != Value.KindCase.LIST_VALUE && proto.getListValue().getValuesCount()
110+
!= 2) {
111+
throw new AssertionError("Expected a list of two elements: type and value");
112+
}
113+
Type type;
114+
try {
115+
type = Type.fromProto(
116+
com.google.spanner.v1.Type.parseFrom(
117+
proto.getListValue().getValues(0).getStringValueBytes()));
118+
} catch (InvalidProtocolBufferException e) {
119+
throw new AssertionError("Expected serialized type.");
120+
}
121+
return decodeValue(type, proto.getListValue().getValues(1));
122+
}
123+
124+
private static Object decodeValue(Type fieldType, com.google.protobuf.Value proto) {
127125
if (proto.getKindCase() == Value.KindCase.NULL_VALUE) {
128126
return null;
129127
}

google-cloud-spanner/src/main/java/com/google/cloud/spanner/Key.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import com.google.common.base.Function;
2323
import com.google.common.base.Joiner;
2424
import com.google.common.collect.Iterables;
25-
import com.google.common.collect.Lists;
2625
import com.google.protobuf.ListValue;
2726
import com.google.protobuf.NullValue;
2827
import com.google.protobuf.Value;
@@ -230,16 +229,6 @@ public Iterable<Object> getParts() {
230229
}
231230

232231

233-
public Iterable<com.google.cloud.spanner.Value> getPartsAsValues() {
234-
return Iterables.transform(parts, new Function<Object, com.google.cloud.spanner.Value>() {
235-
@Nullable
236-
@Override
237-
public com.google.cloud.spanner.Value apply(@Nullable Object part) {
238-
return null;
239-
}
240-
});
241-
}
242-
243232
/** Returns a builder initialized with the value of this key. */
244233
public Builder toBuilder() {
245234
return new Builder(this);
@@ -278,13 +267,17 @@ public int hashCode() {
278267
return parts.hashCode();
279268
}
280269

281-
Iterable<com.google.cloud.spanner.Value> toValues() {
282-
return Lists.transform(parts, new Function<Object, com.google.cloud.spanner.Value>() {
270+
/**
271+
* Returns the parts in this key represented as Cloud Spanner
272+
* {@link com.google.cloud.spanner.Value}.
273+
*/
274+
Iterable<com.google.cloud.spanner.Value> toValues() {
275+
return Iterables.transform(parts, new Function<Object, com.google.cloud.spanner.Value>() {
283276
@Nullable
284277
@Override
285278
public com.google.cloud.spanner.Value apply(@Nullable Object value) {
286279
if (value == null) {
287-
return com.google.cloud.spanner.Value.bool((Boolean) null);
280+
return com.google.cloud.spanner.Value.bool(null);
288281
} else if (value instanceof Boolean) {
289282
return com.google.cloud.spanner.Value.bool((Boolean) value);
290283
} else if (value instanceof Integer) {

google-cloud-spanner/src/main/java/com/google/cloud/spanner/Mutation.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,10 @@ static void toProto(Iterable<Mutation> mutations, List<com.google.spanner.v1.Mut
403403
}
404404
}
405405

406+
/**
407+
* A proxy class that handles {@link Mutation} serialization.
408+
*/
406409
private static class SerializationProxy implements Serializable {
407-
// TODO(mairbek): custom reading bytes instead...
408410
private com.google.spanner.v1.Mutation proto;
409411

410412
private SerializationProxy(Mutation mutation) {
@@ -476,7 +478,7 @@ public com.google.protobuf.Value apply(Value input) {
476478

477479
private static Key keyFromListValues(ListValue value) {
478480
Key.Builder builder = Key.newBuilder();
479-
for(com.google.protobuf.Value part :value.getValuesList()){
481+
for (com.google.protobuf.Value part : value.getValuesList()) {
480482
builder.appendObject(fromTypedProto(part));
481483
}
482484
return builder.build();
@@ -502,6 +504,12 @@ private static KeyRange keyRangeFromProto(com.google.spanner.v1.KeyRange proto)
502504
return builder.build();
503505
}
504506

507+
/**
508+
* Is called right after the serialization, and substitutes itself with the {@link Mutation}
509+
* object.
510+
*
511+
* @return an instance of {@link Mutation}.
512+
*/
505513
private Object readResolve() throws ObjectStreamException {
506514
com.google.spanner.v1.Mutation.Write write;
507515
Mutation.WriteBuilder builder;
@@ -551,6 +559,11 @@ private Object readResolve() throws ObjectStreamException {
551559
}
552560

553561

562+
/**
563+
* Delegates serialization to the {@link SerializationProxy} class.
564+
*
565+
* @return an instance of {@link SerializationProxy} that is safe to serialize.
566+
*/
554567
private Object writeReplace() {
555568
return new SerializationProxy(this);
556569
}

google-cloud-spanner/src/main/java/com/google/cloud/spanner/Value.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,12 @@ static Value structArray(Iterable<Type.StructField> fieldTypes, @Nullable Iterab
437437

438438
abstract com.google.protobuf.Value toProto();
439439

440+
/**
441+
* Returns a com.google.protobuf.Value that preserves type information, so it is possible ot
442+
* recover all the original value from it.
443+
*
444+
* @return com.google.protobuf.Value of a list [serialized type proto value, value]
445+
*/
440446
com.google.protobuf.Value toTypedProto() {
441447
com.google.protobuf.Value.Builder valueBuilder = com.google.protobuf.Value.newBuilder();
442448
com.google.protobuf.ListValue.Builder listBuilder = valueBuilder.getListValueBuilder();

0 commit comments

Comments
 (0)