Search in sources :

Example 1 with Consumer

use of com.google.firebase.firestore.util.Consumer in project firebase-android-sdk by firebase.

the class SQLiteSchema method createV8CollectionParentsIndex.

private void createV8CollectionParentsIndex() {
    ifTablesDontExist(new String[] { "collection_parents" }, () -> {
        // A table storing associations between a Collection ID (e.g. 'messages') to a parent path
        // (e.g. '/chats/123') that contains it as a (sub)collection. This is used to efficiently
        // find all collections to query when performing a Collection Group query. Note that the
        // parent path will be an empty path in the case of root-level collections.
        db.execSQL("CREATE TABLE collection_parents (" + "collection_id TEXT, " + "parent TEXT, " + "PRIMARY KEY(collection_id, parent))");
    });
    // Helper to add an index entry iff we haven't already written it.
    MemoryIndexManager.MemoryCollectionParentIndex cache = new MemoryIndexManager.MemoryCollectionParentIndex();
    SQLiteStatement addIndexEntry = db.compileStatement("INSERT OR REPLACE INTO collection_parents (collection_id, parent) VALUES (?, ?)");
    Consumer<ResourcePath> addEntry = collectionPath -> {
        if (cache.add(collectionPath)) {
            String collectionId = collectionPath.getLastSegment();
            ResourcePath parentPath = collectionPath.popLast();
            addIndexEntry.clearBindings();
            addIndexEntry.bindString(1, collectionId);
            addIndexEntry.bindString(2, EncodedPath.encode(parentPath));
            addIndexEntry.execute();
        }
    };
    // Index existing remote documents.
    SQLitePersistence.Query remoteDocumentsQuery = new SQLitePersistence.Query(db, "SELECT path FROM remote_documents");
    remoteDocumentsQuery.forEach(row -> {
        ResourcePath path = EncodedPath.decodeResourcePath(row.getString(0));
        addEntry.accept(path.popLast());
    });
    // Index existing mutations.
    SQLitePersistence.Query documentMutationsQuery = new SQLitePersistence.Query(db, "SELECT path FROM document_mutations");
    documentMutationsQuery.forEach(row -> {
        ResourcePath path = EncodedPath.decodeResourcePath(row.getString(0));
        addEntry.accept(path.popLast());
    });
}
Also used : InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) Assert.hardAssert(com.google.firebase.firestore.util.Assert.hardAssert) Logger(com.google.firebase.firestore.util.Logger) TextUtils(android.text.TextUtils) ResourcePath(com.google.firebase.firestore.model.ResourcePath) ArrayList(java.util.ArrayList) Target(com.google.firebase.firestore.proto.Target) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Consumer(com.google.firebase.firestore.util.Consumer) DatabaseUtils(android.database.DatabaseUtils) List(java.util.List) Assert.fail(com.google.firebase.firestore.util.Assert.fail) ContentValues(android.content.ContentValues) SQLiteStatement(android.database.sqlite.SQLiteStatement) VisibleForTesting(androidx.annotation.VisibleForTesting) Cursor(android.database.Cursor) ResourcePath(com.google.firebase.firestore.model.ResourcePath) SQLiteStatement(android.database.sqlite.SQLiteStatement)

Aggregations

ContentValues (android.content.ContentValues)1 Cursor (android.database.Cursor)1 DatabaseUtils (android.database.DatabaseUtils)1 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)1 SQLiteStatement (android.database.sqlite.SQLiteStatement)1 TextUtils (android.text.TextUtils)1 VisibleForTesting (androidx.annotation.VisibleForTesting)1 ResourcePath (com.google.firebase.firestore.model.ResourcePath)1 Target (com.google.firebase.firestore.proto.Target)1 Assert.fail (com.google.firebase.firestore.util.Assert.fail)1 Assert.hardAssert (com.google.firebase.firestore.util.Assert.hardAssert)1 Consumer (com.google.firebase.firestore.util.Consumer)1 Logger (com.google.firebase.firestore.util.Logger)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1