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: Filters should be serializable #397

Merged
merged 7 commits into from
Sep 22, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import com.google.cloud.bigtable.data.v2.models.Range.AbstractTimestampRange;
import com.google.common.base.Preconditions;
import com.google.protobuf.ByteString;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import javax.annotation.Nonnull;

Expand Down Expand Up @@ -200,7 +203,19 @@ public Filter label(@Nonnull String label) {
// Implementations of target specific filters.
/** DSL for adding filters to a chain. */
public static final class ChainFilter implements Filter {
private RowFilter.Chain.Builder builder;
private static final long serialVersionUID = -6756759448656768478L;
private transient RowFilter.Chain.Builder builder;

private void writeObject(ObjectOutputStream s) throws IOException {
s.defaultWriteObject();
s.writeObject(builder.build());
}

private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
s.defaultReadObject();
RowFilter.Chain chain = (RowFilter.Chain) s.readObject();
this.builder = chain.toBuilder();
}

private ChainFilter() {
this.builder = RowFilter.Chain.newBuilder();
Expand Down Expand Up @@ -241,7 +256,19 @@ public ChainFilter clone() {

/** DSL for adding filters to the interleave list. */
public static final class InterleaveFilter implements Filter {
private RowFilter.Interleave.Builder builder;
private static final long serialVersionUID = -6356151037337889421L;
private transient RowFilter.Interleave.Builder builder;

private void writeObject(ObjectOutputStream s) throws IOException {
s.defaultWriteObject();
s.writeObject(builder.build());
}

private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
s.defaultReadObject();
RowFilter.Interleave interleave = (RowFilter.Interleave) s.readObject();
this.builder = interleave.toBuilder();
}

private InterleaveFilter() {
builder = RowFilter.Interleave.newBuilder();
Expand Down Expand Up @@ -281,7 +308,19 @@ public InterleaveFilter clone() {

/** DSL for configuring a conditional filter. */
public static final class ConditionFilter implements Filter {
private RowFilter.Condition.Builder builder;
private static final long serialVersionUID = -2720899822014446776L;
private transient RowFilter.Condition.Builder builder;

private void writeObject(ObjectOutputStream s) throws IOException {
s.defaultWriteObject();
s.writeObject(builder.build());
}

private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
s.defaultReadObject();
RowFilter.Condition condition = (RowFilter.Condition) s.readObject();
this.builder = condition.toBuilder();
}

private ConditionFilter(@Nonnull Filter predicate) {
Preconditions.checkNotNull(predicate);
Expand Down Expand Up @@ -323,7 +362,9 @@ public ConditionFilter clone() {
}
}

public static final class KeyFilter {
public static final class KeyFilter implements Serializable {
private static final long serialVersionUID = 5137765114285539458L;

private KeyFilter() {}

/**
Expand Down Expand Up @@ -383,7 +424,9 @@ public Filter sample(double probability) {
}
}

public static final class FamilyFilter {
public static final class FamilyFilter implements Serializable {
private static final long serialVersionUID = -4470936841191831553L;

private FamilyFilter() {}

/**
Expand All @@ -405,7 +448,9 @@ public Filter exactMatch(@Nonnull String value) {
}
}

public static final class QualifierFilter {
public static final class QualifierFilter implements Serializable {
private static final long serialVersionUID = -1274850022909506559L;

private QualifierFilter() {}

/**
Expand Down Expand Up @@ -459,7 +504,8 @@ public QualifierRangeFilter rangeWithinFamily(@Nonnull String family) {

/** Matches only cells from columns within the given range. */
public static final class QualifierRangeFilter
extends AbstractByteStringRange<QualifierRangeFilter> implements Filter, Serializable {
extends AbstractByteStringRange<QualifierRangeFilter> implements Filter {
private static final long serialVersionUID = -1909319911147913630L;
private final String family;

private QualifierRangeFilter(String family) {
Expand Down Expand Up @@ -505,7 +551,9 @@ public QualifierRangeFilter clone() {
}
}

public static final class TimestampFilter {
public static final class TimestampFilter implements Serializable {
private static final long serialVersionUID = 5284219722591464991L;

private TimestampFilter() {}

/**
Expand All @@ -529,7 +577,9 @@ public TimestampRangeFilter exact(Long exactTimestamp) {

/** Matches only cells with microsecond timestamps within the given range. */
public static final class TimestampRangeFilter
extends AbstractTimestampRange<TimestampRangeFilter> implements Filter, Serializable {
extends AbstractTimestampRange<TimestampRangeFilter> implements Filter {
private static final long serialVersionUID = 8410980338603335276L;

private TimestampRangeFilter() {}

@InternalApi
Expand Down Expand Up @@ -571,7 +621,9 @@ public TimestampRangeFilter clone() {
}
}

public static final class ValueFilter {
public static final class ValueFilter implements Serializable {
private static final long serialVersionUID = 6722715229238811179L;

private ValueFilter() {}

/**
Expand Down Expand Up @@ -628,7 +680,9 @@ public Filter strip() {

/** Matches only cells with values that fall within the given value range. */
public static final class ValueRangeFilter extends AbstractByteStringRange<ValueRangeFilter>
implements Filter, Serializable {
implements Filter {
private static final long serialVersionUID = -2452360677825047088L;

private ValueRangeFilter() {}

@InternalApi
Expand Down Expand Up @@ -668,7 +722,9 @@ public ValueRangeFilter clone() {
}
}

public static final class OffsetFilter {
public static final class OffsetFilter implements Serializable {
private static final long serialVersionUID = 3228791236971884041L;

private OffsetFilter() {}

/**
Expand All @@ -681,7 +737,9 @@ public Filter cellsPerRow(int count) {
}
}

public static final class LimitFilter {
public static final class LimitFilter implements Serializable {
private static final long serialVersionUID = -794915549003008940L;

private LimitFilter() {}

/**
Expand All @@ -705,6 +763,7 @@ public Filter cellsPerColumn(int count) {
}

private static final class SimpleFilter implements Filter {
private static final long serialVersionUID = 3595911451325189833L;
private final RowFilter proto;

private SimpleFilter(@Nonnull RowFilter proto) {
Expand All @@ -729,7 +788,7 @@ public SimpleFilter clone() {
}

@InternalExtensionOnly
public interface Filter extends Cloneable {
public interface Filter extends Cloneable, Serializable {
@InternalApi
RowFilter toProto();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import static com.google.cloud.bigtable.data.v2.models.Filters.FILTERS;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import com.google.bigtable.v2.ColumnRange;
import com.google.bigtable.v2.RowFilter;
Expand All @@ -26,6 +28,14 @@
import com.google.bigtable.v2.TimestampRange;
import com.google.bigtable.v2.ValueRange;
import com.google.protobuf.ByteString;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand Down Expand Up @@ -528,4 +538,66 @@ public void labelTest() {

assertThat(actualFilter).isEqualTo(expectedFilter);
}

@Test
public void serializationTest() throws InvocationTargetException, IllegalAccessException {
// checks that the all objects returned by the all methods of the Filters class
// can be serialized/deserialized.

for (Method m : Filters.class.getDeclaredMethods()) {
String name = m.getName();
if (Modifier.isPublic(m.getModifiers())) {
switch (name) {
case "condition":
checkSerialization(
name,
FILTERS
.condition(
FILTERS
.chain()
.filter(FILTERS.qualifier().exactMatch("data_plan_10gb"))
.filter(FILTERS.value().exactMatch("true")))
.then(FILTERS.label("passed-filter"))
.otherwise(FILTERS.label("filtered-out")));
break;
case "label":
checkSerialization(name, FILTERS.label("label"));
break;
case "fromProto":
checkSerialization(name, FILTERS.label("label").toProto());
break;
default:
checkSerialization(name, m.invoke(FILTERS));
}
}
}
}

private void checkSerialization(String name, Object filter) {
igorbernstein2 marked this conversation as resolved.
Show resolved Hide resolved
try {
Object deserialized = serializeDeserialize(filter);
if (filter instanceof Filters.Filter) {
igorbernstein2 marked this conversation as resolved.
Show resolved Hide resolved
RowFilter protoBefore = ((Filters.Filter) filter).toProto();
RowFilter protoAfter = ((Filters.Filter) deserialized).toProto();
assertEquals(
dmitry-fa marked this conversation as resolved.
Show resolved Hide resolved
"'" + name + "' filter protoBuf mismatch after deserialization",
protoBefore,
protoAfter);
}
} catch (IOException | ClassNotFoundException e) {
fail(name + ": " + e);
igorbernstein2 marked this conversation as resolved.
Show resolved Hide resolved
}
}

private Object serializeDeserialize(Object obj) throws IOException, ClassNotFoundException {
igorbernstein2 marked this conversation as resolved.
Show resolved Hide resolved
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();
}
}
}