/
Order.java
240 lines (215 loc) · 7.6 KB
/
Order.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.firestore;
import com.google.firestore.v1.Value;
import com.google.firestore.v1.Value.ValueTypeCase;
import com.google.protobuf.ByteString;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.SortedMap;
import java.util.TreeMap;
import javax.annotation.Nonnull;
/** Order implements the ordering semantics of the backend. */
class Order implements Comparator<Value> {
enum TypeOrder implements Comparable<TypeOrder> {
// NOTE: This order is defined by the backend and cannot be changed.
NULL,
BOOLEAN,
NUMBER,
TIMESTAMP,
STRING,
BLOB,
REF,
GEO_POINT,
ARRAY,
OBJECT;
static TypeOrder fromValue(Value value) {
switch (value.getValueTypeCase()) {
case NULL_VALUE:
return NULL;
case BOOLEAN_VALUE:
return BOOLEAN;
case INTEGER_VALUE:
return NUMBER;
case DOUBLE_VALUE:
return NUMBER;
case TIMESTAMP_VALUE:
return TIMESTAMP;
case STRING_VALUE:
return STRING;
case BYTES_VALUE:
return BLOB;
case REFERENCE_VALUE:
return REF;
case GEO_POINT_VALUE:
return GEO_POINT;
case ARRAY_VALUE:
return ARRAY;
case MAP_VALUE:
return OBJECT;
default:
throw new IllegalArgumentException("Could not detect value type for " + value);
}
}
}
static final Order INSTANCE = new Order();
private Order() {}
/**
* Main comparison function for all Firestore types.
*
* @return -1 is left < right, 0 if left == right, otherwise 1
*/
public int compare(@Nonnull Value left, @Nonnull Value right) {
// First compare the types.
TypeOrder leftType = TypeOrder.fromValue(left);
TypeOrder rightType = TypeOrder.fromValue(right);
int typeComparison = leftType.compareTo(rightType);
if (typeComparison != 0) {
return typeComparison;
}
// So they are the same type.
switch (leftType) {
case NULL:
return 0; // Nulls are all equal.
case BOOLEAN:
return Boolean.compare(left.getBooleanValue(), right.getBooleanValue());
case NUMBER:
return compareNumbers(left, right);
case TIMESTAMP:
return compareTimestamps(left, right);
case STRING:
return compareStrings(left, right);
case BLOB:
return compareBlobs(left, right);
case REF:
return compareResourcePaths(left, right);
case GEO_POINT:
return compareGeoPoints(left, right);
case ARRAY:
return compareArrays(
left.getArrayValue().getValuesList(), right.getArrayValue().getValuesList());
case OBJECT:
return compareObjects(left, right);
default:
throw new IllegalArgumentException("Cannot compare " + leftType);
}
}
private int compareStrings(Value left, Value right) {
return left.getStringValue().compareTo(right.getStringValue());
}
private int compareBlobs(Value left, Value right) {
ByteString leftBytes = left.getBytesValue();
ByteString rightBytes = right.getBytesValue();
int size = Math.min(leftBytes.size(), rightBytes.size());
for (int i = 0; i < size; i++) {
// Make sure the bytes are unsigned
int thisByte = leftBytes.byteAt(i) & 0xff;
int otherByte = rightBytes.byteAt(i) & 0xff;
if (thisByte < otherByte) {
return -1;
} else if (thisByte > otherByte) {
return 1;
}
// Byte values are equal, continue with comparison
}
return Integer.compare(leftBytes.size(), rightBytes.size());
}
private static int compareTimestamps(Value left, Value right) {
int cmp =
Long.compare(left.getTimestampValue().getSeconds(), right.getTimestampValue().getSeconds());
if (cmp != 0) {
return cmp;
} else {
return Integer.compare(
left.getTimestampValue().getNanos(), right.getTimestampValue().getNanos());
}
}
private static int compareGeoPoints(Value left, Value right) {
int cmp =
Double.compare(
left.getGeoPointValue().getLatitude(), right.getGeoPointValue().getLatitude());
if (cmp != 0) {
return cmp;
} else {
return Double.compare(
left.getGeoPointValue().getLongitude(), right.getGeoPointValue().getLongitude());
}
}
private int compareResourcePaths(Value left, Value right) {
ResourcePath leftPath = ResourcePath.create(left.getReferenceValue());
ResourcePath rightPath = ResourcePath.create(right.getReferenceValue());
return leftPath.compareTo(rightPath);
}
public int compareArrays(List<Value> left, List<Value> right) {
int minLength = Math.min(left.size(), right.size());
for (int i = 0; i < minLength; i++) {
int cmp = compare(left.get(i), right.get(i));
if (cmp != 0) {
return cmp;
}
}
return Integer.compare(left.size(), right.size());
}
private int compareObjects(Value left, Value right) {
// This requires iterating over the keys in the object in order and doing a
// deep comparison.
SortedMap<String, Value> leftMap = new TreeMap<>(left.getMapValue().getFieldsMap());
SortedMap<String, Value> rightMap = new TreeMap<>(right.getMapValue().getFieldsMap());
Iterator<Entry<String, Value>> leftIterator = leftMap.entrySet().iterator();
Iterator<Entry<String, Value>> rightIterator = rightMap.entrySet().iterator();
while (leftIterator.hasNext() && rightIterator.hasNext()) {
Entry<String, Value> leftEntry = leftIterator.next();
Entry<String, Value> rightEntry = rightIterator.next();
int keyCompare = leftEntry.getKey().compareTo(rightEntry.getKey());
if (keyCompare != 0) {
return keyCompare;
}
int valueCompare = compare(leftEntry.getValue(), rightEntry.getValue());
if (valueCompare != 0) {
return valueCompare;
}
}
// Only equal if both iterators are exhausted.
return Boolean.compare(leftIterator.hasNext(), rightIterator.hasNext());
}
private int compareNumbers(Value left, Value right) {
if (left.getValueTypeCase() == ValueTypeCase.DOUBLE_VALUE) {
if (right.getValueTypeCase() == ValueTypeCase.DOUBLE_VALUE) {
return compareDoubles(left.getDoubleValue(), right.getDoubleValue());
} else {
return compareDoubles(left.getDoubleValue(), right.getIntegerValue());
}
} else {
if (right.getValueTypeCase() == ValueTypeCase.INTEGER_VALUE) {
return Long.compare(left.getIntegerValue(), right.getIntegerValue());
} else {
return compareDoubles(left.getIntegerValue(), right.getDoubleValue());
}
}
}
private int compareDoubles(double left, double right) {
// Firestore orders NaNs before all other numbers and treats -0.0, 0.0 and +0.0 as equal.
if (Double.isNaN(left)) {
return Double.isNaN(right) ? 0 : -1;
}
if (Double.isNaN(right)) {
return 1;
}
return Double.compare(left == -0.0 ? 0 : left, right == -0.0 ? 0 : right);
}
}