use of com.google.firebase.firestore.model.FieldIndex in project firebase-android-sdk by firebase.
the class SQLiteIndexManager method updateCollectionGroup.
@Override
public void updateCollectionGroup(String collectionGroup, IndexOffset offset) {
hardAssert(started, "IndexManager not started");
++memoizedMaxSequenceNumber;
for (FieldIndex fieldIndex : getFieldIndexes(collectionGroup)) {
FieldIndex updatedIndex = FieldIndex.create(fieldIndex.getIndexId(), fieldIndex.getCollectionGroup(), fieldIndex.getSegments(), FieldIndex.IndexState.create(memoizedMaxSequenceNumber, offset));
db.execute("REPLACE INTO index_state (index_id, uid, sequence_number, " + "read_time_seconds, read_time_nanos, document_key, largest_batch_id) " + "VALUES(?, ?, ?, ?, ?, ?, ?)", fieldIndex.getIndexId(), uid, memoizedMaxSequenceNumber, offset.getReadTime().getTimestamp().getSeconds(), offset.getReadTime().getTimestamp().getNanoseconds(), EncodedPath.encode(offset.getDocumentKey().getPath()), offset.getLargestBatchId());
memoizeIndex(updatedIndex);
}
}
use of com.google.firebase.firestore.model.FieldIndex in project firebase-android-sdk by firebase.
the class SQLiteIndexManager method getFieldIndex.
@Nullable
@Override
public FieldIndex getFieldIndex(Target target) {
hardAssert(started, "IndexManager not started");
TargetIndexMatcher targetIndexMatcher = new TargetIndexMatcher(target);
String collectionGroup = target.getCollectionGroup() != null ? target.getCollectionGroup() : target.getPath().getLastSegment();
Collection<FieldIndex> collectionIndexes = getFieldIndexes(collectionGroup);
if (collectionIndexes.isEmpty()) {
return null;
}
List<FieldIndex> matchingIndexes = new ArrayList<>();
for (FieldIndex fieldIndex : collectionIndexes) {
boolean matches = targetIndexMatcher.servedByIndex(fieldIndex);
if (matches) {
matchingIndexes.add(fieldIndex);
}
}
if (matchingIndexes.isEmpty()) {
return null;
}
// Return the index with the most number of segments
return Collections.max(matchingIndexes, (l, r) -> Integer.compare(l.getSegments().size(), r.getSegments().size()));
}
use of com.google.firebase.firestore.model.FieldIndex in project firebase-android-sdk by firebase.
the class SQLiteIndexManager method getMinOffset.
private IndexOffset getMinOffset(Collection<FieldIndex> fieldIndexes) {
hardAssert(!fieldIndexes.isEmpty(), "Found empty index group when looking for least recent index offset.");
Iterator<FieldIndex> it = fieldIndexes.iterator();
IndexOffset minOffset = it.next().getIndexState().getOffset();
int minBatchId = minOffset.getLargestBatchId();
while (it.hasNext()) {
IndexOffset newOffset = it.next().getIndexState().getOffset();
if (newOffset.compareTo(minOffset) < 0) {
minOffset = newOffset;
}
minBatchId = Math.max(newOffset.getLargestBatchId(), minBatchId);
}
return IndexOffset.create(minOffset.getReadTime(), minOffset.getDocumentKey(), minBatchId);
}
use of com.google.firebase.firestore.model.FieldIndex in project firebase-android-sdk by firebase.
the class SQLiteIndexManager method getNextCollectionGroupToUpdate.
@Override
@Nullable
public String getNextCollectionGroupToUpdate() {
hardAssert(started, "IndexManager not started");
FieldIndex nextIndex = nextIndexToUpdate.peek();
return nextIndex != null ? nextIndex.getCollectionGroup() : null;
}
use of com.google.firebase.firestore.model.FieldIndex in project firebase-android-sdk by firebase.
the class SQLiteIndexManager method encodeValues.
/**
* Encodes the given field values according to the specification in {@code target}. For IN
* queries, a list of possible values is returned.
*/
@Nullable
private Object[] encodeValues(FieldIndex fieldIndex, Target target, @Nullable Collection<Value> values) {
if (values == null)
return null;
List<IndexByteEncoder> encoders = new ArrayList<>();
encoders.add(new IndexByteEncoder());
Iterator<Value> position = values.iterator();
for (FieldIndex.Segment segment : fieldIndex.getDirectionalSegments()) {
Value value = position.next();
for (IndexByteEncoder encoder : encoders) {
if (isInFilter(target, segment.getFieldPath()) && isArray(value)) {
encoders = expandIndexValues(encoders, segment, value);
} else {
DirectionalIndexByteEncoder directionalEncoder = encoder.forKind(segment.getKind());
FirestoreIndexValueWriter.INSTANCE.writeIndexValue(value, directionalEncoder);
}
}
}
return getEncodedBytes(encoders);
}
Aggregations