Search in sources :

Example 1 with BackgroundQueue

use of com.google.firebase.firestore.util.BackgroundQueue 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 2 with BackgroundQueue

use of com.google.firebase.firestore.util.BackgroundQueue 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 3 with BackgroundQueue

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

the class SQLiteRemoteDocumentCache method getAll.

/**
 * Returns the next {@code count} documents from the provided collections, ordered by read time.
 */
private Map<DocumentKey, MutableDocument> getAll(List<ResourcePath> collections, IndexOffset offset, int count) {
    Timestamp readTime = offset.getReadTime().getTimestamp();
    DocumentKey documentKey = offset.getDocumentKey();
    StringBuilder sql = repeatSequence("SELECT contents, read_time_seconds, read_time_nanos, path " + "FROM remote_documents " + "WHERE path >= ? AND path < ? AND path_length = ? " + "AND (read_time_seconds > ? OR ( " + "read_time_seconds = ? AND read_time_nanos > ?) OR ( " + "read_time_seconds = ? AND read_time_nanos = ? and path > ?)) ", collections.size(), " UNION ");
    sql.append("ORDER BY read_time_seconds, read_time_nanos, path LIMIT ?");
    Object[] bindVars = new Object[BINDS_PER_STATEMENT * collections.size() + 1];
    int i = 0;
    for (ResourcePath collection : collections) {
        String prefixPath = EncodedPath.encode(collection);
        bindVars[i++] = prefixPath;
        bindVars[i++] = EncodedPath.prefixSuccessor(prefixPath);
        bindVars[i++] = collection.length() + 1;
        bindVars[i++] = readTime.getSeconds();
        bindVars[i++] = readTime.getSeconds();
        bindVars[i++] = readTime.getNanoseconds();
        bindVars[i++] = readTime.getSeconds();
        bindVars[i++] = readTime.getNanoseconds();
        bindVars[i++] = EncodedPath.encode(documentKey.getPath());
    }
    bindVars[i] = count;
    BackgroundQueue backgroundQueue = new BackgroundQueue();
    Map<DocumentKey, MutableDocument> results = new HashMap<>();
    db.query(sql.toString()).binding(bindVars).forEach(row -> processRowInBackground(backgroundQueue, results, row));
    backgroundQueue.drain();
    return results;
}
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) MutableDocument(com.google.firebase.firestore.model.MutableDocument) Timestamp(com.google.firebase.Timestamp)

Example 4 with BackgroundQueue

use of com.google.firebase.firestore.util.BackgroundQueue 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 5 with BackgroundQueue

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

the class SQLiteRemoteDocumentCache method getAll.

@Override
public Map<DocumentKey, MutableDocument> getAll(Iterable<DocumentKey> documentKeys) {
    Map<DocumentKey, MutableDocument> results = new HashMap<>();
    List<Object> bindVars = new ArrayList<>();
    for (DocumentKey key : documentKeys) {
        bindVars.add(EncodedPath.encode(key.getPath()));
        // Make sure each key has a corresponding entry, which is null in case the document is not
        // found.
        results.put(key, MutableDocument.newInvalidDocument(key));
    }
    SQLitePersistence.LongQuery longQuery = new SQLitePersistence.LongQuery(db, "SELECT contents, read_time_seconds, read_time_nanos FROM remote_documents " + "WHERE path IN (", bindVars, ") ORDER BY path");
    BackgroundQueue backgroundQueue = new BackgroundQueue();
    while (longQuery.hasMoreSubqueries()) {
        longQuery.performNextSubquery().forEach(row -> processRowInBackground(backgroundQueue, results, row));
    }
    backgroundQueue.drain();
    return results;
}
Also used : BackgroundQueue(com.google.firebase.firestore.util.BackgroundQueue) HashMap(java.util.HashMap) DocumentKey(com.google.firebase.firestore.model.DocumentKey) MutableDocument(com.google.firebase.firestore.model.MutableDocument) ArrayList(java.util.ArrayList)

Aggregations

DocumentKey (com.google.firebase.firestore.model.DocumentKey)5 BackgroundQueue (com.google.firebase.firestore.util.BackgroundQueue)5 HashMap (java.util.HashMap)5 Overlay (com.google.firebase.firestore.model.mutation.Overlay)3 MutableDocument (com.google.firebase.firestore.model.MutableDocument)2 ResourcePath (com.google.firebase.firestore.model.ResourcePath)2 ArrayList (java.util.ArrayList)2 Timestamp (com.google.firebase.Timestamp)1