@@ -39,12 +39,15 @@ public abstract class FieldIndex {
39
39
/** An ID for an index that has not yet been added to persistence. */
40
40
public static final int UNKNOWN_ID = -1 ;
41
41
42
+ /** The initial mutation batch id for each index. Gets updated during index backfill. */
43
+ public static final int INITIAL_LARGEST_BATCH_ID = -1 ;
44
+
42
45
/** The initial sequence number for each index. Gets updated during index backfill. */
43
46
public static final int INITIAL_SEQUENCE_NUMBER = 0 ;
44
47
45
48
/** The state of an index that has not yet been backfilled. */
46
49
public static IndexState INITIAL_STATE =
47
- IndexState .create (INITIAL_SEQUENCE_NUMBER , SnapshotVersion .NONE , DocumentKey . empty () );
50
+ IndexState .create (INITIAL_SEQUENCE_NUMBER , IndexOffset .NONE );
48
51
49
52
/** Compares indexes by collection group and segments. Ignores update time and index ID. */
50
53
public static final Comparator <FieldIndex > SEMANTIC_COMPARATOR =
@@ -100,8 +103,11 @@ public static IndexState create(long sequenceNumber, IndexOffset offset) {
100
103
}
101
104
102
105
public static IndexState create (
103
- long sequenceNumber , SnapshotVersion readTime , DocumentKey documentKey ) {
104
- return create (sequenceNumber , IndexOffset .create (readTime , documentKey ));
106
+ long sequenceNumber ,
107
+ SnapshotVersion readTime ,
108
+ DocumentKey documentKey ,
109
+ int largestBatchId ) {
110
+ return create (sequenceNumber , IndexOffset .create (readTime , documentKey , largestBatchId ));
105
111
}
106
112
107
113
/**
@@ -116,17 +122,20 @@ public static IndexState create(
116
122
/** Stores the latest read time and document that were processed for an index. */
117
123
@ AutoValue
118
124
public abstract static class IndexOffset implements Comparable <IndexOffset > {
125
+ public static final IndexOffset NONE =
126
+ create (SnapshotVersion .NONE , DocumentKey .empty (), INITIAL_LARGEST_BATCH_ID );
127
+
119
128
public static final Comparator <MutableDocument > DOCUMENT_COMPARATOR =
120
129
(l , r ) -> IndexOffset .fromDocument (l ).compareTo (IndexOffset .fromDocument (r ));
121
130
122
- public static final IndexOffset NONE = create (SnapshotVersion .NONE , DocumentKey .empty ());
123
-
124
131
/**
125
132
* Creates an offset that matches all documents with a read time higher than {@code readTime} or
126
- * with a key higher than {@code documentKey} for equal read times.
133
+ * with a key higher than {@code documentKey} for equal read times. The largest batch ID is used
134
+ * as a final tie breaker.
127
135
*/
128
- public static IndexOffset create (SnapshotVersion readTime , DocumentKey documentKey ) {
129
- return new AutoValue_FieldIndex_IndexOffset (readTime , documentKey );
136
+ public static IndexOffset create (
137
+ SnapshotVersion readTime , DocumentKey key , int largestBatchId ) {
138
+ return new AutoValue_FieldIndex_IndexOffset (readTime , key , largestBatchId );
130
139
}
131
140
132
141
/**
@@ -145,12 +154,12 @@ public static IndexOffset create(SnapshotVersion readTime) {
145
154
successorNanos == 1e9
146
155
? new Timestamp (successorSeconds + 1 , 0 )
147
156
: new Timestamp (successorSeconds , successorNanos ));
148
- return new AutoValue_FieldIndex_IndexOffset (successor , DocumentKey .empty ());
157
+ return create (successor , DocumentKey .empty (), INITIAL_LARGEST_BATCH_ID );
149
158
}
150
159
151
160
/** Creates a new offset based on the provided document. */
152
161
public static IndexOffset fromDocument (Document document ) {
153
- return new AutoValue_FieldIndex_IndexOffset (document .getReadTime (), document .getKey ());
162
+ return create (document .getReadTime (), document .getKey (), INITIAL_LARGEST_BATCH_ID );
154
163
}
155
164
156
165
/**
@@ -164,10 +173,17 @@ public static IndexOffset fromDocument(Document document) {
164
173
*/
165
174
public abstract DocumentKey getDocumentKey ();
166
175
176
+ /*
177
+ * Returns the largest mutation batch id that's been processed by Firestore.
178
+ */
179
+ public abstract int getLargestBatchId ();
180
+
167
181
public int compareTo (IndexOffset other ) {
168
182
int cmp = getReadTime ().compareTo (other .getReadTime ());
169
183
if (cmp != 0 ) return cmp ;
170
- return getDocumentKey ().compareTo (other .getDocumentKey ());
184
+ cmp = getDocumentKey ().compareTo (other .getDocumentKey ());
185
+ if (cmp != 0 ) return cmp ;
186
+ return Integer .compare (getLargestBatchId (), other .getLargestBatchId ());
171
187
}
172
188
}
173
189
0 commit comments