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;
}
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;
}
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());
}
});
}
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);
}
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.");
}
}
Aggregations