1616
1717import static com .google .firebase .firestore .util .Preconditions .checkNotNull ;
1818
19+ import androidx .annotation .Nullable ;
1920import com .google .firebase .firestore .core .Target ;
2021import com .google .firebase .firestore .model .SnapshotVersion ;
2122import com .google .firebase .firestore .remote .WatchStream ;
2223import com .google .protobuf .ByteString ;
24+ import java .util .Objects ;
2325
2426/** An immutable set of metadata that the store will need to keep track of for each target. */
2527public final class TargetData {
@@ -30,6 +32,7 @@ public final class TargetData {
3032 private final SnapshotVersion snapshotVersion ;
3133 private final SnapshotVersion lastLimboFreeSnapshotVersion ;
3234 private final ByteString resumeToken ;
35+ private final @ Nullable Integer expectedCount ;
3336
3437 /**
3538 * Creates a new TargetData with the given values.
@@ -45,6 +48,9 @@ public final class TargetData {
4548 * @param resumeToken An opaque, server-assigned token that allows watching a target to be resumed
4649 * after disconnecting without retransmitting all the data that matches the target. The resume
4750 * 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.
4854 */
4955 TargetData (
5056 Target target ,
@@ -53,14 +59,16 @@ public final class TargetData {
5359 QueryPurpose purpose ,
5460 SnapshotVersion snapshotVersion ,
5561 SnapshotVersion lastLimboFreeSnapshotVersion ,
56- ByteString resumeToken ) {
62+ ByteString resumeToken ,
63+ @ Nullable Integer expectedCount ) {
5764 this .target = checkNotNull (target );
5865 this .targetId = targetId ;
5966 this .sequenceNumber = sequenceNumber ;
6067 this .lastLimboFreeSnapshotVersion = lastLimboFreeSnapshotVersion ;
6168 this .purpose = purpose ;
6269 this .snapshotVersion = checkNotNull (snapshotVersion );
6370 this .resumeToken = checkNotNull (resumeToken );
71+ this .expectedCount = expectedCount ;
6472 }
6573
6674 /** 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
7280 purpose ,
7381 SnapshotVersion .NONE ,
7482 SnapshotVersion .NONE ,
75- WatchStream .EMPTY_RESUME_TOKEN );
83+ WatchStream .EMPTY_RESUME_TOKEN ,
84+ null );
7685 }
7786
7887 /** Creates a new target data instance with an updated sequence number. */
@@ -84,7 +93,8 @@ public TargetData withSequenceNumber(long sequenceNumber) {
8493 purpose ,
8594 snapshotVersion ,
8695 lastLimboFreeSnapshotVersion ,
87- resumeToken );
96+ resumeToken ,
97+ /* expectedCount= */ null );
8898 }
8999
90100 /** 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
96106 purpose ,
97107 snapshotVersion ,
98108 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 );
100124 }
101125
102126 /** Creates a new target data instance with an updated last limbo free snapshot version number. */
@@ -108,7 +132,8 @@ public TargetData withLastLimboFreeSnapshotVersion(SnapshotVersion lastLimboFree
108132 purpose ,
109133 snapshotVersion ,
110134 lastLimboFreeSnapshotVersion ,
111- resumeToken );
135+ resumeToken ,
136+ expectedCount );
112137 }
113138
114139 public Target getTarget () {
@@ -135,6 +160,11 @@ public ByteString getResumeToken() {
135160 return resumeToken ;
136161 }
137162
163+ @ Nullable
164+ public Integer getExpectedCount () {
165+ return expectedCount ;
166+ }
167+
138168 /**
139169 * Returns the last snapshot version for which the associated view contained no limbo documents.
140170 */
@@ -158,7 +188,8 @@ public boolean equals(Object o) {
158188 && purpose .equals (targetData .purpose )
159189 && snapshotVersion .equals (targetData .snapshotVersion )
160190 && lastLimboFreeSnapshotVersion .equals (targetData .lastLimboFreeSnapshotVersion )
161- && resumeToken .equals (targetData .resumeToken );
191+ && resumeToken .equals (targetData .resumeToken )
192+ && Objects .equals (expectedCount , targetData .expectedCount );
162193 }
163194
164195 @ Override
@@ -170,6 +201,7 @@ public int hashCode() {
170201 result = 31 * result + snapshotVersion .hashCode ();
171202 result = 31 * result + lastLimboFreeSnapshotVersion .hashCode ();
172203 result = 31 * result + resumeToken .hashCode ();
204+ result = 31 * result + Objects .hashCode (expectedCount );
173205 return result ;
174206 }
175207
@@ -190,6 +222,8 @@ public String toString() {
190222 + lastLimboFreeSnapshotVersion
191223 + ", resumeToken="
192224 + resumeToken
225+ + ", expectedCount="
226+ + expectedCount
193227 + '}' ;
194228 }
195229}
0 commit comments