32
32
* group-scoped indices. Every index can be used for both single collection and collection group
33
33
* queries.
34
34
*/
35
- public final class FieldIndex {
35
+ @ AutoValue
36
+ public abstract class FieldIndex {
36
37
37
38
/** Compares indexes by collection group and segments. Ignores update time and index ID. */
38
39
public static final Comparator <FieldIndex > SEMANTIC_COMPARATOR =
39
40
(left , right ) -> {
40
- int cmp = left .collectionGroup .compareTo (right .collectionGroup );
41
+ int cmp = left .getCollectionGroup () .compareTo (right .getCollectionGroup () );
41
42
if (cmp != 0 ) return cmp ;
42
43
43
- Iterator <Segment > leftIt = left .segments .iterator ();
44
- Iterator <Segment > rightIt = right .segments .iterator ();
44
+ Iterator <Segment > leftIt = left .getSegments () .iterator ();
45
+ Iterator <Segment > rightIt = right .getSegments () .iterator ();
45
46
while (leftIt .hasNext () && rightIt .hasNext ()) {
46
47
cmp = leftIt .next ().compareTo (rightIt .next ());
47
48
if (cmp != 0 ) return cmp ;
@@ -62,17 +63,16 @@ public enum Kind {
62
63
CONTAINS
63
64
}
64
65
66
+ public static Segment create (FieldPath fieldPath , Kind kind ) {
67
+ return new AutoValue_FieldIndex_Segment (fieldPath , kind );
68
+ }
69
+
65
70
/** The field path of the component. */
66
71
public abstract FieldPath getFieldPath ();
67
72
68
73
/** The indexes sorting order. */
69
74
public abstract Kind getKind ();
70
75
71
- @ Override
72
- public String toString () {
73
- return String .format ("Segment{fieldPath=%s, kind=%s}" , getFieldPath (), getKind ());
74
- }
75
-
76
76
@ Override
77
77
public int compareTo (Segment other ) {
78
78
int cmp = getFieldPath ().compareTo (other .getFieldPath ());
@@ -81,121 +81,45 @@ public int compareTo(Segment other) {
81
81
}
82
82
}
83
83
84
- private final String collectionGroup ;
85
- private final int indexId ;
86
- private final List <Segment > segments ;
87
- private final SnapshotVersion updateTime ;
88
-
89
- public FieldIndex (String collectionGroup , int indexId ) {
90
- this .collectionGroup = collectionGroup ;
91
- this .segments = new ArrayList <>();
92
- this .indexId = indexId ;
93
- this .updateTime = SnapshotVersion .NONE ;
94
- }
95
-
96
- public FieldIndex (String collectionId ) {
97
- this (collectionId , -1 );
98
- }
99
-
100
- FieldIndex (
101
- String collectionGroup , int indexId , List <Segment > segments , SnapshotVersion updateTime ) {
102
- this .collectionGroup = collectionGroup ;
103
- this .segments = segments ;
104
- this .indexId = indexId ;
105
- this .updateTime = updateTime ;
106
- }
107
-
108
- /** The collection ID this index applies to. */
109
- public String getCollectionGroup () {
110
- return collectionGroup ;
84
+ public static FieldIndex create (
85
+ int indexId , String collectionGroup , List <Segment > segments , SnapshotVersion updateTime ) {
86
+ return new AutoValue_FieldIndex (indexId , collectionGroup , segments , updateTime );
111
87
}
112
88
113
89
/**
114
90
* The index ID. Returns -1 if the index ID is not available (e.g. the index has not yet been
115
91
* persisted).
116
92
*/
117
- public int getIndexId () {
118
- return indexId ;
119
- }
93
+ public abstract int getIndexId ();
120
94
121
- public Segment getSegment (int index ) {
122
- return segments .get (index );
123
- }
95
+ /** The collection ID this index applies to. */
96
+ public abstract String getCollectionGroup ();
124
97
125
- public int segmentCount () {
126
- return segments .size ();
127
- }
98
+ /** Returns all field segments for this index. */
99
+ public abstract List <Segment > getSegments ();
128
100
129
- /**
130
- * Returns the latest read time version that has been indexed by Firestore for this field index.
131
- */
132
- public SnapshotVersion getUpdateTime () {
133
- return updateTime ;
134
- }
101
+ /** Returns when this index was last updated. */
102
+ public abstract SnapshotVersion getUpdateTime ();
135
103
104
+ /** Returns all directional (ascending/descending) segments for this index. */
136
105
public List <Segment > getDirectionalSegments () {
137
106
List <Segment > filteredSegments = new ArrayList <>();
138
- for (Segment segment : segments ) {
107
+ for (Segment segment : getSegments () ) {
139
108
if (!segment .getKind ().equals (Segment .Kind .CONTAINS )) {
140
109
filteredSegments .add (segment );
141
110
}
142
111
}
143
112
return filteredSegments ;
144
113
}
145
114
115
+ /** Returns the ArrayContains/ArrayContainsAny segment for this index. */
146
116
public @ Nullable Segment getArraySegment () {
147
- for (Segment segment : segments ) {
117
+ for (Segment segment : getSegments () ) {
148
118
if (segment .getKind ().equals (Segment .Kind .CONTAINS )) {
149
119
// Firestore queries can only have a single ArrayContains/ArrayContainsAny statements.
150
120
return segment ;
151
121
}
152
122
}
153
123
return null ;
154
124
}
155
-
156
- /** Returns a new field index with additional index segment. */
157
- public FieldIndex withAddedField (FieldPath fieldPath , Segment .Kind kind ) {
158
- List <Segment > newSegments = new ArrayList <>(segments );
159
- newSegments .add (new AutoValue_FieldIndex_Segment (fieldPath , kind ));
160
- return new FieldIndex (collectionGroup , indexId , newSegments , updateTime );
161
- }
162
-
163
- /** Returns a new field index with the updated version. */
164
- public FieldIndex withUpdateTime (SnapshotVersion updateTime ) {
165
- return new FieldIndex (collectionGroup , indexId , segments , updateTime );
166
- }
167
-
168
- /** Returns a new field index with the provided index id. */
169
- public FieldIndex withIndexId (int indexId ) {
170
- return new FieldIndex (collectionGroup , indexId , segments , updateTime );
171
- }
172
-
173
- @ Override
174
- public boolean equals (Object o ) {
175
- if (this == o ) return true ;
176
- if (o == null || getClass () != o .getClass ()) return false ;
177
-
178
- FieldIndex fieldIndex = (FieldIndex ) o ;
179
-
180
- if (indexId != fieldIndex .indexId ) return false ;
181
- if (!segments .equals (fieldIndex .segments )) return false ;
182
- if (!updateTime .equals (fieldIndex .updateTime )) return false ;
183
- return collectionGroup .equals (fieldIndex .collectionGroup );
184
- }
185
-
186
- @ Override
187
- public int hashCode () {
188
- int result = collectionGroup .hashCode ();
189
- result = 31 * result + indexId ;
190
- result = 31 * result + segments .hashCode ();
191
- result = 31 * result + updateTime .hashCode ();
192
- return result ;
193
- }
194
-
195
- @ Override
196
- public String toString () {
197
- return String .format (
198
- "FieldIndex{indexId=%s, collectionGroup='%s', segments=%s, updateTime=%s}" ,
199
- indexId , collectionGroup , segments , updateTime );
200
- }
201
125
}
0 commit comments