@@ -40,18 +40,22 @@ public class Values {
4040 public static final Value NULL_VALUE =
4141 Value .newBuilder ().setNullValue (NullValue .NULL_VALUE ).build ();
4242
43- /** The order of types in Firestore; this order is defined by the backend. */
43+ /**
44+ * The order of types in Firestore. This order is based on the backend's ordering, but modified to
45+ * support server timestamps.
46+ */
4447 public static final int TYPE_ORDER_NULL = 0 ;
4548
4649 public static final int TYPE_ORDER_BOOLEAN = 1 ;
4750 public static final int TYPE_ORDER_NUMBER = 2 ;
4851 public static final int TYPE_ORDER_TIMESTAMP = 3 ;
49- public static final int TYPE_ORDER_STRING = 4 ;
50- public static final int TYPE_ORDER_BLOB = 5 ;
51- public static final int TYPE_ORDER_REFERENCE = 6 ;
52- public static final int TYPE_ORDER_GEOPOINT = 7 ;
53- public static final int TYPE_ORDER_ARRAY = 8 ;
54- public static final int TYPE_ORDER_MAP = 9 ;
52+ public static final int TYPE_ORDER_SERVER_TIMESTAMP = 4 ;
53+ public static final int TYPE_ORDER_STRING = 5 ;
54+ public static final int TYPE_ORDER_BLOB = 6 ;
55+ public static final int TYPE_ORDER_REFERENCE = 7 ;
56+ public static final int TYPE_ORDER_GEOPOINT = 8 ;
57+ public static final int TYPE_ORDER_ARRAY = 9 ;
58+ public static final int TYPE_ORDER_MAP = 10 ;
5559
5660 /** Returns the backend's type order of the given Value type. */
5761 public static int typeOrder (Value value ) {
@@ -78,7 +82,7 @@ public static int typeOrder(Value value) {
7882 return TYPE_ORDER_ARRAY ;
7983 case MAP_VALUE :
8084 if (isServerTimestamp (value )) {
81- return TYPE_ORDER_TIMESTAMP ;
85+ return TYPE_ORDER_SERVER_TIMESTAMP ;
8286 }
8387 return TYPE_ORDER_MAP ;
8488 default :
@@ -106,23 +110,13 @@ public static boolean equals(Value left, Value right) {
106110 return arrayEquals (left , right );
107111 case TYPE_ORDER_MAP :
108112 return objectEquals (left , right );
109- case TYPE_ORDER_TIMESTAMP :
110- return timestampEquals (left , right );
113+ case TYPE_ORDER_SERVER_TIMESTAMP :
114+ return getLocalWriteTime (left ). equals ( getLocalWriteTime ( right ) );
111115 default :
112116 return left .equals (right );
113117 }
114118 }
115119
116- private static boolean timestampEquals (Value left , Value right ) {
117- if (isServerTimestamp (left ) && isServerTimestamp (right )) {
118- return getLocalWriteTime (left ).equals (getLocalWriteTime (right ));
119- } else if (isServerTimestamp (left ) || isServerTimestamp (right )) {
120- return false ;
121- }
122-
123- return left .getTimestampValue ().equals (right .getTimestampValue ());
124- }
125-
126120 private static boolean numberEquals (Value left , Value right ) {
127121 if (left .getValueTypeCase () == Value .ValueTypeCase .INTEGER_VALUE
128122 && right .getValueTypeCase () == Value .ValueTypeCase .INTEGER_VALUE ) {
@@ -197,7 +191,9 @@ public static int compare(Value left, Value right) {
197191 case TYPE_ORDER_NUMBER :
198192 return compareNumbers (left , right );
199193 case TYPE_ORDER_TIMESTAMP :
200- return compareTimestamps (left , right );
194+ return compareTimestamps (left .getTimestampValue (), right .getTimestampValue ());
195+ case TYPE_ORDER_SERVER_TIMESTAMP :
196+ return compareTimestamps (getLocalWriteTime (left ), getLocalWriteTime (right ));
201197 case TYPE_ORDER_STRING :
202198 return left .getStringValue ().compareTo (right .getStringValue ());
203199 case TYPE_ORDER_BLOB :
@@ -235,30 +231,11 @@ private static int compareNumbers(Value left, Value right) {
235231 throw fail ("Unexpected values: %s vs %s" , left , right );
236232 }
237233
238- private static int compareTimestamps (Value left , Value right ) {
239- if (isServerTimestamp (left )) {
240- if (isServerTimestamp (right )) {
241- return compareTimestamps (getLocalWriteTime (left ), getLocalWriteTime (right ));
242- } else {
243- // Server timestamps come after all concrete timestamps.
244- return 1 ;
245- }
246- } else {
247- if (isServerTimestamp (right )) {
248- // Server timestamps come after all concrete timestamps.
249- return -1 ;
250- } else {
251- return compareTimestamps (left .getTimestampValue (), right .getTimestampValue ());
252- }
253- }
254- }
255-
256234 private static int compareTimestamps (Timestamp left , Timestamp right ) {
257235 int cmp = Util .compareLongs (left .getSeconds (), right .getSeconds ());
258236 if (cmp != 0 ) {
259237 return cmp ;
260238 }
261-
262239 return Util .compareIntegers (left .getNanos (), right .getNanos ());
263240 }
264241
0 commit comments