Search in sources :

Example 16 with FieldIndex

use of com.google.firebase.firestore.model.FieldIndex in project firebase-android-sdk by firebase.

the class IndexBackfillerTest method addFieldIndex.

private void addFieldIndex(String collectionGroup, String fieldName, long sequenceNumber) {
    FieldIndex fieldIndex = fieldIndex(collectionGroup, FieldIndex.UNKNOWN_ID, FieldIndex.IndexState.create(sequenceNumber, IndexOffset.NONE), fieldName, FieldIndex.Segment.Kind.ASCENDING);
    indexManager.addFieldIndex(fieldIndex);
}
Also used : FieldIndex(com.google.firebase.firestore.model.FieldIndex)

Example 17 with FieldIndex

use of com.google.firebase.firestore.model.FieldIndex in project firebase-android-sdk by firebase.

the class IndexBackfillerTest method testBackfillWritesLatestReadTimeToFieldIndexOnCompletion.

@Test
public void testBackfillWritesLatestReadTimeToFieldIndexOnCompletion() {
    addFieldIndex("coll1", "foo");
    addFieldIndex("coll2", "bar");
    addDoc("coll1/docA", version(10), "foo", 1);
    addDoc("coll2/docA", version(20), "bar", 1);
    int documentsProcessed = backfiller.backfill();
    assertEquals(2, documentsProcessed);
    FieldIndex fieldIndex1 = indexManager.getFieldIndexes("coll1").iterator().next();
    FieldIndex fieldIndex2 = indexManager.getFieldIndexes("coll2").iterator().next();
    assertEquals(version(10), fieldIndex1.getIndexState().getOffset().getReadTime());
    assertEquals(version(20), fieldIndex2.getIndexState().getOffset().getReadTime());
    addDoc("coll1/docB", version(50, 10), "foo", 1);
    addDoc("coll1/docC", version(50), "foo", 1);
    addDoc("coll2/docB", version(60), "bar", 1);
    addDoc("coll2/docC", version(60, 10), "bar", 1);
    documentsProcessed = backfiller.backfill();
    assertEquals(4, documentsProcessed);
    fieldIndex1 = indexManager.getFieldIndexes("coll1").iterator().next();
    fieldIndex2 = indexManager.getFieldIndexes("coll2").iterator().next();
    assertEquals(version(50, 10), fieldIndex1.getIndexState().getOffset().getReadTime());
    assertEquals(version(60, 10), fieldIndex2.getIndexState().getOffset().getReadTime());
}
Also used : FieldIndex(com.google.firebase.firestore.model.FieldIndex) Test(org.junit.Test)

Example 18 with FieldIndex

use of com.google.firebase.firestore.model.FieldIndex in project firebase-android-sdk by firebase.

the class IndexBackfillerTest method testBackfillDoesNotProcessSameDocumentTwice.

@Test
public void testBackfillDoesNotProcessSameDocumentTwice() {
    addFieldIndex("coll", "foo");
    addDoc("coll/doc", version(5), "foo", 1);
    addSetMutationsToOverlay(1, "coll/doc");
    int documentsProcessed = backfiller.backfill();
    assertEquals(1, documentsProcessed);
    FieldIndex fieldIndex = indexManager.getFieldIndexes("coll").iterator().next();
    assertEquals(version(5), fieldIndex.getIndexState().getOffset().getReadTime());
    assertEquals(1, fieldIndex.getIndexState().getOffset().getLargestBatchId());
}
Also used : FieldIndex(com.google.firebase.firestore.model.FieldIndex) Test(org.junit.Test)

Example 19 with FieldIndex

use of com.google.firebase.firestore.model.FieldIndex in project firebase-android-sdk by firebase.

the class FirebaseFirestore method setIndexConfiguration.

/**
 * Configures indexing for local query execution. Any previous index configuration is overridden.
 * The Task resolves once the index configuration has been persisted.
 *
 * <p>The index entries themselves are created asynchronously. You can continue to use queries
 * that require indexing even if the indices are not yet available. Query execution will
 * automatically start using the index once the index entries have been written.
 *
 * <p>The method accepts the JSON format exported by the Firebase CLI (`firebase
 * firestore:indexes`). If the JSON format is invalid, this method throws an exception.
 *
 * @param json The JSON format exported by the Firebase CLI.
 * @return A task that resolves once all indices are successfully configured.
 * @throws IllegalArgumentException if the JSON format is invalid
 */
@VisibleForTesting
Task<Void> setIndexConfiguration(String json) {
    ensureClientConfigured();
    Preconditions.checkState(settings.isPersistenceEnabled(), "Cannot enable indexes when persistence is disabled");
    List<FieldIndex> parsedIndexes = new ArrayList<>();
    try {
        JSONObject jsonObject = new JSONObject(json);
        if (jsonObject.has("indexes")) {
            JSONArray indexes = jsonObject.getJSONArray("indexes");
            for (int i = 0; i < indexes.length(); ++i) {
                JSONObject definition = indexes.getJSONObject(i);
                String collectionGroup = definition.getString("collectionGroup");
                List<FieldIndex.Segment> segments = new ArrayList<>();
                JSONArray fields = definition.optJSONArray("fields");
                for (int f = 0; fields != null && f < fields.length(); ++f) {
                    JSONObject field = fields.getJSONObject(f);
                    FieldPath fieldPath = FieldPath.fromServerFormat(field.getString("fieldPath"));
                    if ("CONTAINS".equals(field.optString("arrayConfig"))) {
                        segments.add(FieldIndex.Segment.create(fieldPath, FieldIndex.Segment.Kind.CONTAINS));
                    } else if ("ASCENDING".equals(field.optString("order"))) {
                        segments.add(FieldIndex.Segment.create(fieldPath, FieldIndex.Segment.Kind.ASCENDING));
                    } else {
                        segments.add(FieldIndex.Segment.create(fieldPath, FieldIndex.Segment.Kind.DESCENDING));
                    }
                }
                parsedIndexes.add(FieldIndex.create(FieldIndex.UNKNOWN_ID, collectionGroup, segments, FieldIndex.INITIAL_STATE));
            }
        }
    } catch (JSONException e) {
        throw new IllegalArgumentException("Failed to parse index configuration", e);
    }
    return client.configureFieldIndexes(parsedIndexes);
}
Also used : FieldIndex(com.google.firebase.firestore.model.FieldIndex) JSONObject(org.json.JSONObject) FieldPath(com.google.firebase.firestore.model.FieldPath) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) VisibleForTesting(androidx.annotation.VisibleForTesting)

Example 20 with FieldIndex

use of com.google.firebase.firestore.model.FieldIndex in project firebase-android-sdk by firebase.

the class TargetTest method emptyQueryBound.

@Test
public void emptyQueryBound() {
    Target target = query("c").toTarget();
    FieldIndex index = fieldIndex("c");
    Bound lowerBound = target.getLowerBound(index);
    verifyBound(lowerBound, true);
    Bound upperBound = target.getUpperBound(index);
    verifyBound(upperBound, true);
}
Also used : FieldIndex(com.google.firebase.firestore.model.FieldIndex) Test(org.junit.Test)

Aggregations

FieldIndex (com.google.firebase.firestore.model.FieldIndex)49 Test (org.junit.Test)32 Nullable (androidx.annotation.Nullable)8 Value (com.google.firestore.v1.Value)8 Query (com.google.firebase.firestore.core.Query)6 ArrayList (java.util.ArrayList)6 DocumentKey (com.google.firebase.firestore.model.DocumentKey)3 DirectionalIndexByteEncoder (com.google.firebase.firestore.index.DirectionalIndexByteEncoder)2 IndexByteEncoder (com.google.firebase.firestore.index.IndexByteEncoder)2 IndexEntry (com.google.firebase.firestore.index.IndexEntry)2 IndexOffset (com.google.firebase.firestore.model.FieldIndex.IndexOffset)2 HashMap (java.util.HashMap)2 VisibleForTesting (androidx.annotation.VisibleForTesting)1 Timestamp (com.google.firebase.Timestamp)1 ImmutableSortedMap (com.google.firebase.database.collection.ImmutableSortedMap)1 Bound (com.google.firebase.firestore.core.Bound)1 Target (com.google.firebase.firestore.core.Target)1 Document (com.google.firebase.firestore.model.Document)1 FieldPath (com.google.firebase.firestore.model.FieldPath)1 SnapshotVersion (com.google.firebase.firestore.model.SnapshotVersion)1