Search in sources :

Example 21 with ResourcePath

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

the class RemoteSerializer method decodeKey.

public DocumentKey decodeKey(String name) {
    ResourcePath resource = decodeResourceName(name);
    Assert.hardAssert(resource.getSegment(1).equals(databaseId.getProjectId()), "Tried to deserialize key from different project.");
    Assert.hardAssert(resource.getSegment(3).equals(databaseId.getDatabaseId()), "Tried to deserialize key from different database.");
    return DocumentKey.fromPath(extractLocalPathFromResourceName(resource));
}
Also used : ResourcePath(com.google.firebase.firestore.model.ResourcePath)

Example 22 with ResourcePath

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

the class SQLiteSchemaTest method usesMultipleBatchesToAddPathLengths.

@Test
public void usesMultipleBatchesToAddPathLengths() {
    schema.runSchemaUpgrades(0, 12);
    for (int i = 0; i < SQLiteSchema.MIGRATION_BATCH_SIZE + 1; ++i) {
        ResourcePath path = path(String.format("coll/doc%03d", i));
        db.execSQL("INSERT INTO remote_documents (path) VALUES (?)", new Object[] { EncodedPath.encode(path) });
    }
    schema.runSchemaUpgrades(12, 13);
    int[] current = new int[] { 0 };
    new SQLitePersistence.Query(db, "SELECT path_length FROM remote_documents ORDER by path").forEach(cursor -> {
        assertEquals(2, cursor.getInt(0));
        ++current[0];
    });
    assertEquals(SQLiteSchema.MIGRATION_BATCH_SIZE + 1, current[0]);
}
Also used : ResourcePath(com.google.firebase.firestore.model.ResourcePath) EncodedPath.decodeResourcePath(com.google.firebase.firestore.local.EncodedPath.decodeResourcePath) Test(org.junit.Test)

Example 23 with ResourcePath

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

the class SQLiteSchemaTest method addPathLengths.

@Test
public void addPathLengths() {
    schema.runSchemaUpgrades(0, 12);
    ResourcePath[] paths = new ResourcePath[] { path("collA/doc"), path("collA/doc/collB/doc") };
    for (ResourcePath path : paths) {
        db.execSQL("INSERT INTO remote_documents (path, read_time_seconds, read_time_nanos, contents) VALUES (?, ?, ?, ?)", new Object[] { EncodedPath.encode(path) });
    }
    schema.runSchemaUpgrades(12, 13);
    int[] current = new int[] { 0 };
    new SQLitePersistence.Query(db, "SELECT path_length FROM remote_documents ORDER BY path").forEach(cursor -> {
        assertEquals(paths[current[0]].length(), cursor.getInt(0));
        ++current[0];
    });
    assertEquals(2, current[0]);
}
Also used : ResourcePath(com.google.firebase.firestore.model.ResourcePath) EncodedPath.decodeResourcePath(com.google.firebase.firestore.local.EncodedPath.decodeResourcePath) Test(org.junit.Test)

Example 24 with ResourcePath

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

the class RemoteDocumentCacheTestCase method testGetAllFromSinceReadTimeAndSeconds.

@Test
public void testGetAllFromSinceReadTimeAndSeconds() {
    addTestDocumentAtPath("b/old", /* updateTime= */
    1, /* readTime= */
    11);
    addTestDocumentAtPath("b/current", /* updateTime= */
    2, /*  readTime= = */
    12);
    addTestDocumentAtPath("b/new", /* updateTime= */
    3, /*  readTime= = */
    13);
    ResourcePath collection = path("b");
    Map<DocumentKey, MutableDocument> results = remoteDocumentCache.getAll(collection, IndexOffset.createSuccessor(version(12), -1));
    assertThat(results.values()).containsExactly(doc("b/new", 3, DOC_DATA));
}
Also used : ResourcePath(com.google.firebase.firestore.model.ResourcePath) DocumentKey(com.google.firebase.firestore.model.DocumentKey) MutableDocument(com.google.firebase.firestore.model.MutableDocument) Test(org.junit.Test)

Example 25 with ResourcePath

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

the class Query method parseDocumentIdValue.

/**
 * Parses the given documentIdValue into a ReferenceValue, throwing appropriate errors if the
 * value is anything other than a DocumentReference or String, or if the string is malformed.
 */
private Value parseDocumentIdValue(Object documentIdValue) {
    if (documentIdValue instanceof String) {
        String documentId = (String) documentIdValue;
        if (documentId.isEmpty()) {
            throw new IllegalArgumentException("Invalid query. When querying with FieldPath.documentId() you must provide a valid " + "document ID, but it was an empty string.");
        }
        if (!query.isCollectionGroupQuery() && documentId.contains("/")) {
            throw new IllegalArgumentException("Invalid query. When querying a collection by FieldPath.documentId() you must " + "provide a plain document ID, but '" + documentId + "' contains a '/' character.");
        }
        ResourcePath path = query.getPath().append(ResourcePath.fromString(documentId));
        if (!DocumentKey.isDocumentKey(path)) {
            throw new IllegalArgumentException("Invalid query. When querying a collection group by FieldPath.documentId(), the " + "value provided must result in a valid document path, but '" + path + "' is not because it has an odd number of segments (" + path.length() + ").");
        }
        return Values.refValue(this.getFirestore().getDatabaseId(), DocumentKey.fromPath(path));
    } else if (documentIdValue instanceof DocumentReference) {
        DocumentReference ref = (DocumentReference) documentIdValue;
        return Values.refValue(this.getFirestore().getDatabaseId(), ref.getKey());
    } else {
        throw new IllegalArgumentException("Invalid query. When querying with FieldPath.documentId() you must provide a valid " + "String or DocumentReference, but it was of type: " + Util.typeName(documentIdValue));
    }
}
Also used : ResourcePath(com.google.firebase.firestore.model.ResourcePath)

Aggregations

ResourcePath (com.google.firebase.firestore.model.ResourcePath)34 MutableDocument (com.google.firebase.firestore.model.MutableDocument)13 DocumentKey (com.google.firebase.firestore.model.DocumentKey)12 Test (org.junit.Test)11 ArrayList (java.util.ArrayList)8 OrderBy (com.google.firebase.firestore.core.OrderBy)4 HashMap (java.util.HashMap)4 Bound (com.google.firebase.firestore.core.Bound)3 Cursor (android.database.Cursor)2 SQLiteStatement (android.database.sqlite.SQLiteStatement)2 ImmutableSortedMap (com.google.firebase.database.collection.ImmutableSortedMap)2 ImmutableSortedSet (com.google.firebase.database.collection.ImmutableSortedSet)2 FieldFilter (com.google.firebase.firestore.core.FieldFilter)2 Filter (com.google.firebase.firestore.core.Filter)2 Query (com.google.firebase.firestore.core.Query)2 EncodedPath.decodeResourcePath (com.google.firebase.firestore.local.EncodedPath.decodeResourcePath)2 DocumentCollections.emptyDocumentMap (com.google.firebase.firestore.model.DocumentCollections.emptyDocumentMap)2 SnapshotVersion (com.google.firebase.firestore.model.SnapshotVersion)2 BackgroundQueue (com.google.firebase.firestore.util.BackgroundQueue)2 CollectionSelector (com.google.firestore.v1.StructuredQuery.CollectionSelector)2