Skip to content

Add explicit FieldValue canonicalization #1178

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

Merged
merged 7 commits into from
Feb 1, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -27,6 +27,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

// TODO(mrschmidt): Make package-private
Expand Down Expand Up @@ -249,4 +250,92 @@ private static int compareMaps(MapValue left, MapValue right) {
// Only equal if both iterators are exhausted.
return Util.compareBooleans(iterator1.hasNext(), iterator2.hasNext());
}

/** Generate the canonical ID for the provided field value (as used in Target serialization). */
public static String canonicalId(Value value) {
StringBuilder builder = new StringBuilder();
stringifyValue(builder, value);
return builder.toString();
}

// TODO(mrschmidt): Use in target serialization and migrate all existing TargetData
private static void stringifyValue(StringBuilder builder, Value value) {
switch (value.getValueTypeCase()) {
case NULL_VALUE:
builder.append("null");
break;
case BOOLEAN_VALUE:
builder.append(value.getBooleanValue());
break;
case INTEGER_VALUE:
builder.append(value.getIntegerValue());
break;
case DOUBLE_VALUE:
builder.append(value.getDoubleValue());
break;
case TIMESTAMP_VALUE:
stringifyTimestamp(builder, value.getTimestampValue());
break;
case STRING_VALUE:
builder.append(value.getStringValue());
break;
case BYTES_VALUE:
builder.append(Util.toDebugString(value.getBytesValue()));
break;
case REFERENCE_VALUE:
// TODO(mrschmidt): Use document key only
builder.append(value.getReferenceValue());
break;
case GEO_POINT_VALUE:
stringifyGeoPoint(builder, value.getGeoPointValue());
break;
case ARRAY_VALUE:
stringifyArray(builder, value.getArrayValue());
break;
case MAP_VALUE:
stringifyObject(builder, value.getMapValue());
break;
default:
throw fail("Invalid value type: " + value.getValueTypeCase());
}
}

private static void stringifyTimestamp(StringBuilder builder, Timestamp timestamp) {
builder.append(String.format("time(%s,%s)", timestamp.getSeconds(), timestamp.getNanos()));
}

private static void stringifyGeoPoint(StringBuilder builder, LatLng latLng) {
builder.append(String.format("geo(%s,%s)", latLng.getLatitude(), latLng.getLongitude()));
}

private static void stringifyObject(StringBuilder builder, MapValue mapValue) {
// Even though MapValue are likely sorted correctly based on their insertion order (e.g. when
// received from the backend), local modifications can bring elements out of order. We need to
// re-sort the elements to ensure that canonical IDs are independent of insertion order.
SortedMap<String, Value> sortedMap = new TreeMap<>(mapValue.getFieldsMap());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Building a binary tree to sort the entries is pretty wasteful in terms of memory allocation. It's also pessimistic because building the tree is always going to cost O(n lg(n)). An alternative would be to collect the keys into an ArrayList and sort that. That has the benefit of allocating less memory and also runs in O(n) if the list is already sorted.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went through different implementations for optimize for both runtime complexity and readability. While I still think that using large Maps here is kind of an anti-pattern (especially given our lack of truncation support), adding an extra line here to pre-sort the keys (and performing a key-based lookup on the HashMap) might be a worthy tradeoff. Updated.


builder.append("{");
boolean first = true;
for (Map.Entry<String, Value> entry : sortedMap.entrySet()) {
if (!first) {
builder.append(",");
} else {
first = false;
}
builder.append(entry.getKey()).append(":");
stringifyValue(builder, entry.getValue());
}
builder.append("}");
}

private static void stringifyArray(StringBuilder builder, ArrayValue arrayValue) {
builder.append("[");
for (int i = 0; i < arrayValue.getValuesCount(); ++i) {
stringifyValue(builder, arrayValue.getValues(i));
if (i != arrayValue.getValuesCount() - 1) {
builder.append(",");
}
}
builder.append("]");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.google.firebase.firestore.model.protovalue.ObjectValue;
import com.google.firebase.firestore.model.protovalue.PrimitiveValue;
import com.google.firebase.firestore.model.value.FieldValue;
import com.google.firebase.firestore.model.value.ProtoValues;
import com.google.firebase.firestore.model.value.ServerTimestampValue;
import com.google.firebase.firestore.testutil.ComparatorTester;
import com.google.firestore.v1.Value;
Expand Down Expand Up @@ -366,6 +367,36 @@ public void testValueOrdering() {
.testCompare();
}

@Test
public void testCanonicalIds() {
assertCanonicalId(wrap(null), "null");
assertCanonicalId(wrap(true), "true");
assertCanonicalId(wrap(false), "false");
assertCanonicalId(wrap(1), "1");
assertCanonicalId(wrap(1.0), "1.0");
assertCanonicalId(wrap(new Timestamp(30, 60)), "time(30,60)");
assertCanonicalId(wrap("a"), "a");
assertCanonicalId(wrap(blob(1, 2, 3)), "010203");
assertCanonicalId(
wrapRef(dbId("p1", "d1"), key("c1/doc1")), "projects/p1/databases/d1/documents/c1/doc1");
assertCanonicalId(wrap(new GeoPoint(30, 60)), "geo(30.0,60.0)");
assertCanonicalId(wrap(Arrays.asList(1, 2, 3)), "[1,2,3]");
assertCanonicalId(wrap(map("a", 1, "b", 2, "c", "3")), "{a:1,b:2,c:3}");
assertCanonicalId(
wrap(map("a", Arrays.asList("b", map("c", new GeoPoint(30, 60))))),
"{a:[b,{c:geo(30.0,60.0)}]}");
}

@Test
public void testObjectCanonicalIdsIgnoreSortOrder() {
assertCanonicalId(wrap(map("a", 1, "b", 2, "c", "3")), "{a:1,b:2,c:3}");
assertCanonicalId(wrap(map("c", 3, "b", 2, "a", "1")), "{a:1,b:2,c:3}");
}

private void assertCanonicalId(PrimitiveValue fieldValue, String expectedCanonicalId) {
assertEquals(expectedCanonicalId, ProtoValues.canonicalId(fieldValue.toProto()));
}

private ObjectValue setField(ObjectValue objectValue, String fieldPath, PrimitiveValue value) {
return objectValue.toBuilder().set(field(fieldPath), value.toProto()).build();
}
Expand Down