Search in sources :

Example 11 with MutableDocument

use of com.google.firebase.firestore.model.MutableDocument 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 12 with MutableDocument

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

the class MemoryRemoteDocumentCache method getAll.

@Override
public Map<DocumentKey, MutableDocument> getAll(ResourcePath collection, IndexOffset offset) {
    Map<DocumentKey, MutableDocument> result = new HashMap<>();
    // Documents are ordered by key, so we can use a prefix scan to narrow down the documents
    // we need to match the query against.
    DocumentKey prefix = DocumentKey.fromPath(collection.append(""));
    Iterator<Map.Entry<DocumentKey, Document>> iterator = docs.iteratorFrom(prefix);
    while (iterator.hasNext()) {
        Map.Entry<DocumentKey, Document> entry = iterator.next();
        Document doc = entry.getValue();
        DocumentKey key = entry.getKey();
        if (!collection.isPrefixOf(key.getPath())) {
            // We are now scanning the next collection. Abort.
            break;
        }
        if (key.getPath().length() > collection.length() + 1) {
            // Exclude entries from subcollections.
            continue;
        }
        if (IndexOffset.fromDocument(doc).compareTo(offset) <= 0) {
            // The document sorts before the offset.
            continue;
        }
        result.put(doc.getKey(), doc.mutableCopy());
    }
    return result;
}
Also used : HashMap(java.util.HashMap) DocumentKey(com.google.firebase.firestore.model.DocumentKey) MutableDocument(com.google.firebase.firestore.model.MutableDocument) Document(com.google.firebase.firestore.model.Document) MutableDocument(com.google.firebase.firestore.model.MutableDocument) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableSortedMap(com.google.firebase.database.collection.ImmutableSortedMap) DocumentCollections.emptyDocumentMap(com.google.firebase.firestore.model.DocumentCollections.emptyDocumentMap)

Example 13 with MutableDocument

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

the class Transaction method getAsync.

/**
 * Reads the document referenced by the provided {@code DocumentReference}
 *
 * @param documentRef The {@code DocumentReference} to read.
 * @return A Task that will be resolved with the contents of the Document at this {@code
 *     DocumentReference}.
 */
private Task<DocumentSnapshot> getAsync(DocumentReference documentRef) {
    return transaction.lookup(Collections.singletonList(documentRef.getKey())).continueWith(Executors.DIRECT_EXECUTOR, task -> {
        if (!task.isSuccessful()) {
            throw task.getException();
        }
        List<MutableDocument> docs = task.getResult();
        if (docs.size() != 1) {
            throw fail("Mismatch in docs returned from document lookup.");
        }
        MutableDocument doc = docs.get(0);
        if (doc.isFoundDocument()) {
            return DocumentSnapshot.fromDocument(firestore, doc, /*fromCache=*/
            false, /*hasPendingWrites=*/
            false);
        } else if (doc.isNoDocument()) {
            return DocumentSnapshot.fromNoDocument(firestore, doc.getKey(), /*fromCache=*/
            false);
        } else {
            throw fail("BatchGetDocumentsRequest returned unexpected document type: " + doc.getClass().getCanonicalName());
        }
    });
}
Also used : MutableDocument(com.google.firebase.firestore.model.MutableDocument)

Example 14 with MutableDocument

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

the class SpecTestCase method parseChange.

private DocumentViewChange parseChange(JSONObject jsonDoc, DocumentViewChange.Type type) throws JSONException {
    long version = jsonDoc.getLong("version");
    JSONObject options = jsonDoc.getJSONObject("options");
    Map<String, Object> values = parseMap(jsonDoc.getJSONObject("value"));
    MutableDocument doc = doc(jsonDoc.getString("key"), version, values);
    if (options.optBoolean("hasLocalMutations")) {
        doc.setHasLocalMutations();
    }
    if (options.optBoolean("hasCommittedMutations")) {
        doc.setHasCommittedMutations();
    }
    return DocumentViewChange.create(type, doc);
}
Also used : JSONObject(org.json.JSONObject) MutableDocument(com.google.firebase.firestore.model.MutableDocument) JSONObject(org.json.JSONObject) ByteString(com.google.protobuf.ByteString)

Example 15 with MutableDocument

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

the class SpecTestCase method doWatchEntity.

private void doWatchEntity(JSONObject watchEntity) throws Exception {
    if (watchEntity.has("docs")) {
        Assert.hardAssert(!watchEntity.has("doc"), "Exactly one of |doc| or |docs| needs to be set.");
        JSONArray docs = watchEntity.getJSONArray("docs");
        for (int i = 0; i < docs.length(); ++i) {
            JSONObject doc = docs.getJSONObject(i);
            JSONObject watchSpec = new JSONObject();
            watchSpec.put("doc", doc);
            if (watchEntity.has("targets")) {
                watchSpec.put("targets", watchEntity.get("targets"));
            }
            if (watchEntity.has("removedTargets")) {
                watchSpec.put("removedTargets", watchEntity.get("removedTargets"));
            }
            doWatchEntity(watchSpec);
        }
    } else if (watchEntity.has("doc")) {
        JSONObject docSpec = watchEntity.getJSONObject("doc");
        String key = docSpec.getString("key");
        @Nullable Map<String, Object> value = !docSpec.isNull("value") ? parseMap(docSpec.getJSONObject("value")) : null;
        long version = docSpec.getLong("version");
        MutableDocument doc = value != null ? doc(key, version, value) : deletedDoc(key, version);
        List<Integer> updated = parseIntList(watchEntity.optJSONArray("targets"));
        List<Integer> removed = parseIntList(watchEntity.optJSONArray("removedTargets"));
        WatchChange change = new DocumentChange(updated, removed, doc.getKey(), doc);
        writeWatchChange(change, SnapshotVersion.NONE);
    } else if (watchEntity.has("key")) {
        String key = watchEntity.getString("key");
        List<Integer> removed = parseIntList(watchEntity.optJSONArray("removedTargets"));
        WatchChange change = new DocumentChange(Collections.emptyList(), removed, key(key), null);
        writeWatchChange(change, SnapshotVersion.NONE);
    } else {
        throw Assert.fail("Either key, doc or docs must be set.");
    }
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) MutableDocument(com.google.firebase.firestore.model.MutableDocument) WatchChange(com.google.firebase.firestore.remote.WatchChange) ExistenceFilterWatchChange(com.google.firebase.firestore.remote.WatchChange.ExistenceFilterWatchChange) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) DocumentChange(com.google.firebase.firestore.remote.WatchChange.DocumentChange) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

MutableDocument (com.google.firebase.firestore.model.MutableDocument)166 Test (org.junit.Test)125 DocumentKey (com.google.firebase.firestore.model.DocumentKey)43 Mutation.calculateOverlayMutation (com.google.firebase.firestore.model.mutation.Mutation.calculateOverlayMutation)30 TestUtil.deleteMutation (com.google.firebase.firestore.testutil.TestUtil.deleteMutation)30 TestUtil.mergeMutation (com.google.firebase.firestore.testutil.TestUtil.mergeMutation)30 TestUtil.patchMutation (com.google.firebase.firestore.testutil.TestUtil.patchMutation)30 TestUtil.setMutation (com.google.firebase.firestore.testutil.TestUtil.setMutation)30 HashMap (java.util.HashMap)22 TestUtil.wrapObject (com.google.firebase.firestore.testutil.TestUtil.wrapObject)18 ArrayList (java.util.ArrayList)18 TargetData (com.google.firebase.firestore.local.TargetData)15 WatchTargetChange (com.google.firebase.firestore.remote.WatchChange.WatchTargetChange)14 DocumentChange (com.google.firebase.firestore.remote.WatchChange.DocumentChange)13 ResourcePath (com.google.firebase.firestore.model.ResourcePath)12 Query (com.google.firebase.firestore.core.Query)10 Map (java.util.Map)10 SnapshotVersion (com.google.firebase.firestore.model.SnapshotVersion)8 Document (com.google.firebase.firestore.model.Document)7 Timestamp (com.google.firebase.Timestamp)6