16
16
17
17
import static com .google .firebase .firestore .util .Preconditions .checkNotNull ;
18
18
19
+ import androidx .annotation .Nullable ;
19
20
import com .google .firebase .firestore .core .Target ;
20
21
import com .google .firebase .firestore .model .SnapshotVersion ;
21
22
import com .google .firebase .firestore .remote .WatchStream ;
22
23
import com .google .protobuf .ByteString ;
24
+ import java .util .Objects ;
23
25
24
26
/** An immutable set of metadata that the store will need to keep track of for each target. */
25
27
public final class TargetData {
@@ -30,6 +32,7 @@ public final class TargetData {
30
32
private final SnapshotVersion snapshotVersion ;
31
33
private final SnapshotVersion lastLimboFreeSnapshotVersion ;
32
34
private final ByteString resumeToken ;
35
+ private final @ Nullable Integer expectedCount ;
33
36
34
37
/**
35
38
* Creates a new TargetData with the given values.
@@ -45,6 +48,9 @@ public final class TargetData {
45
48
* @param resumeToken An opaque, server-assigned token that allows watching a target to be resumed
46
49
* after disconnecting without retransmitting all the data that matches the target. The resume
47
50
* token essentially identifies a point in time from which the server should resume sending
51
+ * @param expectedCount The number of documents that last matched the query at the resume token or
52
+ * read time. Documents are counted only when making a listen request with resume token or
53
+ * read time, otherwise, keep it null.
48
54
*/
49
55
TargetData (
50
56
Target target ,
@@ -53,14 +59,16 @@ public final class TargetData {
53
59
QueryPurpose purpose ,
54
60
SnapshotVersion snapshotVersion ,
55
61
SnapshotVersion lastLimboFreeSnapshotVersion ,
56
- ByteString resumeToken ) {
62
+ ByteString resumeToken ,
63
+ @ Nullable Integer expectedCount ) {
57
64
this .target = checkNotNull (target );
58
65
this .targetId = targetId ;
59
66
this .sequenceNumber = sequenceNumber ;
60
67
this .lastLimboFreeSnapshotVersion = lastLimboFreeSnapshotVersion ;
61
68
this .purpose = purpose ;
62
69
this .snapshotVersion = checkNotNull (snapshotVersion );
63
70
this .resumeToken = checkNotNull (resumeToken );
71
+ this .expectedCount = expectedCount ;
64
72
}
65
73
66
74
/** Convenience constructor for use when creating a TargetData for the first time. */
@@ -72,7 +80,8 @@ public TargetData(Target target, int targetId, long sequenceNumber, QueryPurpose
72
80
purpose ,
73
81
SnapshotVersion .NONE ,
74
82
SnapshotVersion .NONE ,
75
- WatchStream .EMPTY_RESUME_TOKEN );
83
+ WatchStream .EMPTY_RESUME_TOKEN ,
84
+ null );
76
85
}
77
86
78
87
/** Creates a new target data instance with an updated sequence number. */
@@ -84,7 +93,8 @@ public TargetData withSequenceNumber(long sequenceNumber) {
84
93
purpose ,
85
94
snapshotVersion ,
86
95
lastLimboFreeSnapshotVersion ,
87
- resumeToken );
96
+ resumeToken ,
97
+ /* expectedCount= */ null );
88
98
}
89
99
90
100
/** Creates a new target data instance with an updated resume token and snapshot version. */
@@ -96,7 +106,21 @@ public TargetData withResumeToken(ByteString resumeToken, SnapshotVersion snapsh
96
106
purpose ,
97
107
snapshotVersion ,
98
108
lastLimboFreeSnapshotVersion ,
99
- resumeToken );
109
+ resumeToken ,
110
+ expectedCount );
111
+ }
112
+
113
+ /** Creates a new target data instance with an updated expected count. */
114
+ public TargetData withExpectedCount (@ Nullable Integer expectedCount ) {
115
+ return new TargetData (
116
+ target ,
117
+ targetId ,
118
+ sequenceNumber ,
119
+ purpose ,
120
+ snapshotVersion ,
121
+ lastLimboFreeSnapshotVersion ,
122
+ resumeToken ,
123
+ expectedCount );
100
124
}
101
125
102
126
/** Creates a new target data instance with an updated last limbo free snapshot version number. */
@@ -108,7 +132,8 @@ public TargetData withLastLimboFreeSnapshotVersion(SnapshotVersion lastLimboFree
108
132
purpose ,
109
133
snapshotVersion ,
110
134
lastLimboFreeSnapshotVersion ,
111
- resumeToken );
135
+ resumeToken ,
136
+ expectedCount );
112
137
}
113
138
114
139
public Target getTarget () {
@@ -135,6 +160,11 @@ public ByteString getResumeToken() {
135
160
return resumeToken ;
136
161
}
137
162
163
+ @ Nullable
164
+ public Integer getExpectedCount () {
165
+ return expectedCount ;
166
+ }
167
+
138
168
/**
139
169
* Returns the last snapshot version for which the associated view contained no limbo documents.
140
170
*/
@@ -158,7 +188,8 @@ public boolean equals(Object o) {
158
188
&& purpose .equals (targetData .purpose )
159
189
&& snapshotVersion .equals (targetData .snapshotVersion )
160
190
&& lastLimboFreeSnapshotVersion .equals (targetData .lastLimboFreeSnapshotVersion )
161
- && resumeToken .equals (targetData .resumeToken );
191
+ && resumeToken .equals (targetData .resumeToken )
192
+ && Objects .equals (expectedCount , targetData .expectedCount );
162
193
}
163
194
164
195
@ Override
@@ -170,6 +201,7 @@ public int hashCode() {
170
201
result = 31 * result + snapshotVersion .hashCode ();
171
202
result = 31 * result + lastLimboFreeSnapshotVersion .hashCode ();
172
203
result = 31 * result + resumeToken .hashCode ();
204
+ result = 31 * result + Objects .hashCode (expectedCount );
173
205
return result ;
174
206
}
175
207
@@ -190,6 +222,8 @@ public String toString() {
190
222
+ lastLimboFreeSnapshotVersion
191
223
+ ", resumeToken="
192
224
+ resumeToken
225
+ + ", expectedCount="
226
+ + expectedCount
193
227
+ '}' ;
194
228
}
195
229
}
0 commit comments