Skip to content

Commit 39e17cc

Browse files
authored
fix: RowCells are not actually serializeable (#407)
* fix: make RowCell serializable regardless given type of the labels parameter * fix: make RowCell serializable regardless given type of the labels parameter * optimization for an empty labels list
1 parent 0e0e802 commit 39e17cc

File tree

2 files changed

+58
-1
lines changed
  • google-cloud-bigtable/src

2 files changed

+58
-1
lines changed

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/models/RowCell.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.google.auto.value.AutoValue;
2121
import com.google.cloud.bigtable.data.v2.internal.ByteStringComparator;
2222
import com.google.common.collect.ComparisonChain;
23+
import com.google.common.collect.ImmutableList;
2324
import com.google.protobuf.ByteString;
2425
import java.io.Serializable;
2526
import java.util.Comparator;
@@ -62,7 +63,13 @@ public static RowCell create(
6263
long timestamp,
6364
@Nonnull List<String> labels,
6465
@Nonnull ByteString value) {
65-
return new AutoValue_RowCell(family, qualifier, timestamp, value, labels);
66+
// Ensure that the list is serializable and optimize for the common case
67+
if (labels.isEmpty()) {
68+
labels = ImmutableList.of();
69+
} else {
70+
labels = ImmutableList.copyOf(labels);
71+
}
72+
return new AutoValue_RowCell(family, qualifier, timestamp, value, ImmutableList.copyOf(labels));
6673
}
6774

6875
/** The cell's family */

google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/models/RowCellTest.java

+50
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@
1919

2020
import com.google.common.collect.ImmutableList;
2121
import com.google.protobuf.ByteString;
22+
import com.google.protobuf.LazyStringArrayList;
23+
import com.google.protobuf.UnmodifiableLazyStringList;
24+
import java.io.ByteArrayInputStream;
25+
import java.io.ByteArrayOutputStream;
26+
import java.io.IOException;
27+
import java.io.ObjectInputStream;
28+
import java.io.ObjectOutputStream;
29+
import java.util.Arrays;
2230
import java.util.Comparator;
2331
import java.util.List;
2432
import org.junit.Test;
@@ -98,4 +106,46 @@ public void compareTest() {
98106
RowCell.create("family1", col1, timestamp2, labels1, value1)))
99107
.isEqualTo(1);
100108
}
109+
110+
@Test
111+
public void testSerialization() throws IOException, ClassNotFoundException {
112+
LazyStringArrayList lazyList = new LazyStringArrayList();
113+
lazyList.add("lazy");
114+
lazyList.add("very lazy");
115+
List[] labelLists = {
116+
Arrays.asList("str1", "str2", "str3"),
117+
ImmutableList.of("string1", "string2"),
118+
new UnmodifiableLazyStringList(lazyList),
119+
new UnmodifiableLazyStringList(LazyStringArrayList.EMPTY)
120+
};
121+
122+
for (int i = 0; i < labelLists.length; i++) {
123+
String family = "family_" + i;
124+
ByteString col = ByteString.copyFromUtf8("col_" + i);
125+
long timestamp = 1000L * (i + 1);
126+
List<String> labels = labelLists[i];
127+
ByteString value = ByteString.copyFromUtf8("value_" + i);
128+
RowCell cell = RowCell.create(family, col, timestamp, labels, value);
129+
RowCell deserialized = (RowCell) serializeDeserialize(cell);
130+
131+
assertThat(cell.getFamily()).isEqualTo(deserialized.getFamily());
132+
assertThat(cell.getQualifier()).isEqualTo(deserialized.getQualifier());
133+
assertThat(cell.getTimestamp()).isEqualTo(deserialized.getTimestamp());
134+
assertThat(cell.getLabels()).isEqualTo(deserialized.getLabels());
135+
assertThat(cell.getValue()).isEqualTo(deserialized.getValue());
136+
}
137+
}
138+
139+
private static Object serializeDeserialize(Object obj)
140+
throws IOException, ClassNotFoundException {
141+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
142+
try (ObjectOutputStream outStream = new ObjectOutputStream(bos)) {
143+
outStream.writeObject(obj);
144+
}
145+
146+
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
147+
try (ObjectInputStream inStream = new ObjectInputStream(bis)) {
148+
return inStream.readObject();
149+
}
150+
}
101151
}

0 commit comments

Comments
 (0)