Search in sources :

Example 6 with Overlay

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

the class SQLiteDocumentOverlayCache method getOverlays.

@Override
public Map<DocumentKey, Overlay> getOverlays(ResourcePath collection, int sinceBatchId) {
    Map<DocumentKey, Overlay> result = new HashMap<>();
    BackgroundQueue backgroundQueue = new BackgroundQueue();
    db.query("SELECT overlay_mutation, largest_batch_id FROM document_overlays " + "WHERE uid = ? AND collection_path = ? AND largest_batch_id > ?").binding(uid, EncodedPath.encode(collection), sinceBatchId).forEach(row -> processOverlaysInBackground(backgroundQueue, result, row));
    backgroundQueue.drain();
    return result;
}
Also used : BackgroundQueue(com.google.firebase.firestore.util.BackgroundQueue) HashMap(java.util.HashMap) DocumentKey(com.google.firebase.firestore.model.DocumentKey) Overlay(com.google.firebase.firestore.model.mutation.Overlay)

Example 7 with Overlay

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

the class SQLiteDocumentOverlayCache method getOverlays.

@Override
public Map<DocumentKey, Overlay> getOverlays(String collectionGroup, int sinceBatchId, int count) {
    Map<DocumentKey, Overlay> result = new HashMap<>();
    String[] lastCollectionPath = new String[1];
    String[] lastDocumentPath = new String[1];
    int[] lastLargestBatchId = new int[1];
    BackgroundQueue backgroundQueue = new BackgroundQueue();
    db.query("SELECT overlay_mutation, largest_batch_id, collection_path, document_id " + " FROM document_overlays " + "WHERE uid = ? AND collection_group = ? AND largest_batch_id > ? " + "ORDER BY largest_batch_id, collection_path, document_id LIMIT ?").binding(uid, collectionGroup, sinceBatchId, count).forEach(row -> {
        lastLargestBatchId[0] = row.getInt(1);
        lastCollectionPath[0] = row.getString(2);
        lastDocumentPath[0] = row.getString(3);
        processOverlaysInBackground(backgroundQueue, result, row);
    });
    if (lastCollectionPath[0] == null) {
        return result;
    }
    // This function should not return partial batch overlays, even if the number of overlays in the
    // result set exceeds the given `count` argument. Since the `LIMIT` in the above query might
    // result in a partial batch, the following query appends any remaining overlays for the last
    // batch.
    db.query("SELECT overlay_mutation, largest_batch_id FROM document_overlays " + "WHERE uid = ? AND collection_group = ? " + "AND (collection_path > ? OR (collection_path = ? AND document_id > ?)) " + "AND largest_batch_id = ?").binding(uid, collectionGroup, lastCollectionPath[0], lastCollectionPath[0], lastDocumentPath[0], lastLargestBatchId[0]).forEach(row -> processOverlaysInBackground(backgroundQueue, result, row));
    backgroundQueue.drain();
    return result;
}
Also used : BackgroundQueue(com.google.firebase.firestore.util.BackgroundQueue) HashMap(java.util.HashMap) DocumentKey(com.google.firebase.firestore.model.DocumentKey) Overlay(com.google.firebase.firestore.model.mutation.Overlay)

Example 8 with Overlay

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

the class SQLiteDocumentOverlayCache method getOverlays.

@Override
public Map<DocumentKey, Overlay> getOverlays(SortedSet<DocumentKey> keys) {
    hardAssert(keys.comparator() == null, "getOverlays() requires natural order");
    Map<DocumentKey, Overlay> result = new HashMap<>();
    BackgroundQueue backgroundQueue = new BackgroundQueue();
    ResourcePath currentCollection = ResourcePath.EMPTY;
    List<Object> accumulatedDocumentIds = new ArrayList<>();
    for (DocumentKey key : keys) {
        if (!currentCollection.equals(key.getCollectionPath())) {
            processSingleCollection(result, backgroundQueue, currentCollection, accumulatedDocumentIds);
            currentCollection = key.getCollectionPath();
            accumulatedDocumentIds.clear();
        }
        accumulatedDocumentIds.add(key.getDocumentId());
    }
    processSingleCollection(result, backgroundQueue, currentCollection, accumulatedDocumentIds);
    backgroundQueue.drain();
    return result;
}
Also used : BackgroundQueue(com.google.firebase.firestore.util.BackgroundQueue) ResourcePath(com.google.firebase.firestore.model.ResourcePath) HashMap(java.util.HashMap) DocumentKey(com.google.firebase.firestore.model.DocumentKey) ArrayList(java.util.ArrayList) Overlay(com.google.firebase.firestore.model.mutation.Overlay)

Example 9 with Overlay

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

the class SQLiteDocumentOverlayCache method processOverlaysInBackground.

private void processOverlaysInBackground(BackgroundQueue backgroundQueue, Map<DocumentKey, Overlay> results, Cursor row) {
    byte[] rawMutation = row.getBlob(0);
    int largestBatchId = row.getInt(1);
    // Since scheduling background tasks incurs overhead, we only dispatch to a
    // background thread if there are still some documents remaining.
    Executor executor = row.isLast() ? Executors.DIRECT_EXECUTOR : backgroundQueue;
    executor.execute(() -> {
        Overlay overlay = decodeOverlay(rawMutation, largestBatchId);
        synchronized (results) {
            results.put(overlay.getKey(), overlay);
        }
    });
}
Also used : Executor(java.util.concurrent.Executor) Overlay(com.google.firebase.firestore.model.mutation.Overlay)

Example 10 with Overlay

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

the class LocalDocumentsView method getDocument.

/**
 * Returns the the local view of the document identified by {@code key}.
 *
 * @return Local view of the document or a an invalid document if we don't have any cached state
 *     for it.
 */
Document getDocument(DocumentKey key) {
    Overlay overlay = documentOverlayCache.getOverlay(key);
    MutableDocument document = getBaseDocument(key, overlay);
    if (overlay != null) {
        overlay.getMutation().applyToLocalView(document, null, Timestamp.now());
    }
    return document;
}
Also used : MutableDocument(com.google.firebase.firestore.model.MutableDocument) Overlay(com.google.firebase.firestore.model.mutation.Overlay)

Aggregations

Overlay (com.google.firebase.firestore.model.mutation.Overlay)14 DocumentKey (com.google.firebase.firestore.model.DocumentKey)10 HashMap (java.util.HashMap)7 MutableDocument (com.google.firebase.firestore.model.MutableDocument)4 Document (com.google.firebase.firestore.model.Document)3 Mutation (com.google.firebase.firestore.model.mutation.Mutation)3 TestUtil.deleteMutation (com.google.firebase.firestore.testutil.TestUtil.deleteMutation)3 TestUtil.patchMutation (com.google.firebase.firestore.testutil.TestUtil.patchMutation)3 TestUtil.setMutation (com.google.firebase.firestore.testutil.TestUtil.setMutation)3 BackgroundQueue (com.google.firebase.firestore.util.BackgroundQueue)3 Map (java.util.Map)3 TreeMap (java.util.TreeMap)3 Test (org.junit.Test)3 ImmutableSortedMap (com.google.firebase.database.collection.ImmutableSortedMap)2 DocumentCollections.emptyDocumentMap (com.google.firebase.firestore.model.DocumentCollections.emptyDocumentMap)2 ResourcePath (com.google.firebase.firestore.model.ResourcePath)1 PatchMutation (com.google.firebase.firestore.model.mutation.PatchMutation)1 ArrayList (java.util.ArrayList)1 SortedMap (java.util.SortedMap)1 Executor (java.util.concurrent.Executor)1