18
18
import com .google .firestore .v1 .Value ;
19
19
import java .util .Comparator ;
20
20
21
- public class MutableDocument implements Cloneable {
21
+ public final class MutableDocument implements Cloneable {
22
22
private static final Comparator <MutableDocument > KEY_COMPARATOR =
23
23
(left , right ) -> left .getKey ().compareTo (right .getKey ());
24
24
@@ -27,81 +27,99 @@ public static Comparator<MutableDocument> keyComparator() {
27
27
return KEY_COMPARATOR ;
28
28
}
29
29
30
- private enum Type {
30
+ private enum DocumentType {
31
+ /**
32
+ * Represents the initial state of a MutableDocument when only the document key is known.
33
+ * Invalid documents transition to other states as mutations are applied. If a document remain
34
+ * invalids after applying mutations, it should be discarded.
35
+ */
31
36
INVALID ,
37
+ /**
38
+ * Represents a document in Firestore with a key, version, data and whether the data has local
39
+ * mutations applied to it.
40
+ */
32
41
FOUND_DOCUMENT ,
42
+ /** Represents that no documents exists for the key at the given version. */
33
43
NO_DOCUMENT ,
44
+ /**
45
+ * Represents an existing document whose data is unknown (e.g. a document that was updated
46
+ * without a known base document).
47
+ */
34
48
UNKNOWN_DOCUMENT ;
35
49
}
36
50
51
+ /** Describes the `hasPendingWrites` state of a document. */
52
+ private enum DocumentState {
53
+ /** Local mutations applied via the mutation queue. Document is potentially inconsistent. */
54
+ LOCAL_MUTATIONS ,
55
+ /** Mutations applied based on a write acknowledgment. Document is potentially inconsistent. */
56
+ COMMITTED_MUTATIONS ,
57
+ /** No mutations applied. Document was sent to us by Watch. */
58
+ SYNCED
59
+ }
60
+
37
61
private final DocumentKey key ;
38
- private Type type ;
62
+ private DocumentType documentType ;
39
63
private SnapshotVersion version ;
40
64
private ObjectValue value ;
41
- boolean hasLocalMutations ;
42
- boolean hasCommittedMutations ;
65
+ private DocumentState documentState ;
43
66
44
67
public MutableDocument (DocumentKey key ) {
45
- this .key = key ;
46
- this .version = SnapshotVersion .NONE ;
47
- this .type = Type .INVALID ;
48
- this .value = new ObjectValue ();
68
+ this (key , DocumentType .INVALID , SnapshotVersion .NONE , new ObjectValue (), DocumentState .SYNCED );
49
69
}
50
70
51
71
private MutableDocument (
52
72
DocumentKey key ,
53
- Type type ,
73
+ DocumentType documentType ,
54
74
SnapshotVersion version ,
55
75
ObjectValue value ,
56
- boolean hasLocalMutations ,
57
- boolean hasCommittedMutations ) {
76
+ DocumentState documentState ) {
58
77
this .key = key ;
59
78
this .version = version ;
60
- this .type = type ;
61
- this .hasLocalMutations = hasLocalMutations ;
62
- this .hasCommittedMutations = hasCommittedMutations ;
79
+ this .documentType = documentType ;
80
+ this .documentState = documentState ;
63
81
this .value = value ;
64
82
}
65
83
66
- /** Changes the document type to FOUND_DOCUMENT and sets the given version and data. */
84
+ /**
85
+ * Changes the document type to indicate that it exists and that its version and data are known.
86
+ */
67
87
public MutableDocument setFoundDocument (SnapshotVersion version , ObjectValue value ) {
68
88
this .version = version ;
69
- this .type = Type .FOUND_DOCUMENT ;
89
+ this .documentType = DocumentType .FOUND_DOCUMENT ;
70
90
this .value = value ;
71
- this .hasLocalMutations = false ;
72
- this .hasCommittedMutations = false ;
91
+ this .documentState = DocumentState .SYNCED ;
73
92
return this ;
74
93
}
75
94
76
- /** Changes the document type to NO_DOCUMENT and sets the given version. */
95
+ /** Changes the document type to indicate that it doesn't exist at the given version. */
77
96
public MutableDocument setNoDocument (SnapshotVersion version ) {
78
97
this .version = version ;
79
- this .type = Type .NO_DOCUMENT ;
98
+ this .documentType = DocumentType .NO_DOCUMENT ;
80
99
this .value = new ObjectValue ();
81
- this .hasLocalMutations = false ;
82
- this .hasCommittedMutations = false ;
100
+ this .documentState = DocumentState .SYNCED ;
83
101
return this ;
84
102
}
85
103
86
- /** Changes the document type to UNKNOWN_DOCUMENT and sets the given version. */
104
+ /**
105
+ * Changes the document type to indicate that it exists at a given version but that is data is not
106
+ * known (e.g. a document that was updated without a known base document).
107
+ */
87
108
public MutableDocument setUnknownDocument (SnapshotVersion version ) {
88
109
this .version = version ;
89
- this .type = Type .UNKNOWN_DOCUMENT ;
110
+ this .documentType = DocumentType .UNKNOWN_DOCUMENT ;
90
111
this .value = new ObjectValue ();
91
- this .hasLocalMutations = false ;
92
- this .hasCommittedMutations = true ;
112
+ this .documentState = DocumentState .COMMITTED_MUTATIONS ;
93
113
return this ;
94
114
}
95
115
96
116
public MutableDocument setCommittedMutations () {
97
- this .hasLocalMutations = false ;
98
- this .hasCommittedMutations = true ;
117
+ this .documentState = DocumentState .COMMITTED_MUTATIONS ;
99
118
return this ;
100
119
}
101
120
102
121
public MutableDocument setLocalMutations () {
103
- this .hasLocalMutations = true ;
104
- this .hasCommittedMutations = true ;
122
+ this .documentState = DocumentState .LOCAL_MUTATIONS ;
105
123
return this ;
106
124
}
107
125
@@ -120,12 +138,12 @@ public SnapshotVersion getVersion() {
120
138
121
139
/** Returns whether local mutations were applied via the mutation queue. */
122
140
public boolean hasLocalMutations () {
123
- return hasLocalMutations ;
141
+ return documentState . equals ( DocumentState . LOCAL_MUTATIONS ) ;
124
142
}
125
143
126
144
/** Returns whether mutations were applied based on a write acknowledgment. */
127
145
public boolean hasCommittedMutations () {
128
- return hasCommittedMutations ;
146
+ return documentState . equals ( DocumentState . COMMITTED_MUTATIONS ) ;
129
147
}
130
148
131
149
/**
@@ -143,27 +161,33 @@ public Value getField(FieldPath field) {
143
161
return getData ().get (field );
144
162
}
145
163
164
+ /**
165
+ * Returns whether this document is valid (i.e. it is an entry in the RemoteDocumentCache, was
166
+ * created by a mutation or read from the backend).
167
+ */
146
168
public boolean isValidDocument () {
147
- return !type .equals (Type .INVALID );
169
+ return !documentType .equals (DocumentType .INVALID );
148
170
}
149
171
172
+ /** Returns whether the document exists and its data is known at the current version. */
150
173
public boolean isFoundDocument () {
151
- return type .equals (Type .FOUND_DOCUMENT );
174
+ return documentType .equals (DocumentType .FOUND_DOCUMENT );
152
175
}
153
176
177
+ /** Returns whether the document is known to not exist at the current version. */
154
178
public boolean isNoDocument () {
155
- return type .equals (Type .NO_DOCUMENT );
179
+ return documentType .equals (DocumentType .NO_DOCUMENT );
156
180
}
157
181
182
+ /** Returns whether the document exists and its data is unknown at the current version. */
158
183
public boolean isUnknownDocument () {
159
- return type .equals (Type .UNKNOWN_DOCUMENT );
184
+ return documentType .equals (DocumentType .UNKNOWN_DOCUMENT );
160
185
}
161
186
162
187
@ Override
163
188
@ NonNull
164
189
public MutableDocument clone () {
165
- return new MutableDocument (
166
- key , type , version , value .clone (), hasLocalMutations , hasCommittedMutations );
190
+ return new MutableDocument (key , documentType , version , value .clone (), documentState );
167
191
}
168
192
169
193
@ Override
@@ -173,11 +197,9 @@ public boolean equals(Object o) {
173
197
174
198
MutableDocument document = (MutableDocument ) o ;
175
199
176
- if (hasLocalMutations != document .hasLocalMutations ) return false ;
177
- if (hasCommittedMutations != document .hasCommittedMutations ) return false ;
178
200
if (!key .equals (document .key )) return false ;
179
201
if (!version .equals (document .version )) return false ;
180
- if (type != document .type ) return false ;
202
+ if (documentType != document .documentType ) return false ;
181
203
return value .equals (document .value );
182
204
}
183
205
@@ -194,11 +216,9 @@ public String toString() {
194
216
+ ", version="
195
217
+ version
196
218
+ ", type="
197
- + type
198
- + ", hasLocalMutations="
199
- + hasLocalMutations
200
- + ", hasCommittedMutations="
201
- + hasCommittedMutations
219
+ + documentType
220
+ + ", documentState="
221
+ + documentState
202
222
+ ", value="
203
223
+ value
204
224
+ '}' ;
0 commit comments