Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: RowCells are not actually serializeable #407

Merged
merged 3 commits into from Sep 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -20,6 +20,7 @@
import com.google.auto.value.AutoValue;
import com.google.cloud.bigtable.data.v2.internal.ByteStringComparator;
import com.google.common.collect.ComparisonChain;
import com.google.common.collect.ImmutableList;
import com.google.protobuf.ByteString;
import java.io.Serializable;
import java.util.Comparator;
Expand Down Expand Up @@ -62,7 +63,13 @@ public static RowCell create(
long timestamp,
@Nonnull List<String> labels,
@Nonnull ByteString value) {
return new AutoValue_RowCell(family, qualifier, timestamp, value, labels);
// Ensure that the list is serializable and optimize for the common case
if (labels.isEmpty()) {
labels = ImmutableList.of();
} else {
labels = ImmutableList.copyOf(labels);
}
return new AutoValue_RowCell(family, qualifier, timestamp, value, ImmutableList.copyOf(labels));
igorbernstein2 marked this conversation as resolved.
Show resolved Hide resolved
}

/** The cell's family */
Expand Down
Expand Up @@ -19,6 +19,14 @@

import com.google.common.collect.ImmutableList;
import com.google.protobuf.ByteString;
import com.google.protobuf.LazyStringArrayList;
import com.google.protobuf.UnmodifiableLazyStringList;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import org.junit.Test;
Expand Down Expand Up @@ -98,4 +106,46 @@ public void compareTest() {
RowCell.create("family1", col1, timestamp2, labels1, value1)))
.isEqualTo(1);
}

@Test
public void testSerialization() throws IOException, ClassNotFoundException {
LazyStringArrayList lazyList = new LazyStringArrayList();
lazyList.add("lazy");
lazyList.add("very lazy");
List[] labelLists = {
Arrays.asList("str1", "str2", "str3"),
ImmutableList.of("string1", "string2"),
new UnmodifiableLazyStringList(lazyList),
new UnmodifiableLazyStringList(LazyStringArrayList.EMPTY)
};

for (int i = 0; i < labelLists.length; i++) {
String family = "family_" + i;
ByteString col = ByteString.copyFromUtf8("col_" + i);
long timestamp = 1000L * (i + 1);
List<String> labels = labelLists[i];
ByteString value = ByteString.copyFromUtf8("value_" + i);
RowCell cell = RowCell.create(family, col, timestamp, labels, value);
RowCell deserialized = (RowCell) serializeDeserialize(cell);

assertThat(cell.getFamily()).isEqualTo(deserialized.getFamily());
assertThat(cell.getQualifier()).isEqualTo(deserialized.getQualifier());
assertThat(cell.getTimestamp()).isEqualTo(deserialized.getTimestamp());
assertThat(cell.getLabels()).isEqualTo(deserialized.getLabels());
assertThat(cell.getValue()).isEqualTo(deserialized.getValue());
}
}

private static Object serializeDeserialize(Object obj)
throws IOException, ClassNotFoundException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try (ObjectOutputStream outStream = new ObjectOutputStream(bos)) {
outStream.writeObject(obj);
}

ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
try (ObjectInputStream inStream = new ObjectInputStream(bis)) {
return inStream.readObject();
}
}
}