Search in sources :

Example 1 with Target

use of com.google.firebase.firestore.proto.Target in project firebase-android-sdk by firebase.

the class SQLiteSchemaTest method dropsLastLimboFreeSnapshotIfValuesCannotBeReliedUpon.

@Test
public void dropsLastLimboFreeSnapshotIfValuesCannotBeReliedUpon() {
    schema.runSchemaUpgrades(0, 9);
    db.execSQL("INSERT INTO targets (target_id, canonical_id, target_proto) VALUES (?,?, ?)", new Object[] { 1, "foo", createDummyQueryTargetWithLimboFreeVersion(1).toByteArray() });
    schema.runSchemaUpgrades(9, 10);
    new SQLitePersistence.Query(db, "SELECT target_proto FROM targets WHERE target_id = 1").first(cursor -> {
        byte[] targetProtoBytes = cursor.getBlob(0);
        try {
            Target targetProto = Target.parseFrom(targetProtoBytes);
            assertFalse(targetProto.hasLastLimboFreeSnapshotVersion());
        } catch (InvalidProtocolBufferException e) {
            fail("Failed to decode Query data");
        }
    });
}
Also used : Target(com.google.firebase.firestore.proto.Target) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) Test(org.junit.Test)

Example 2 with Target

use of com.google.firebase.firestore.proto.Target in project firebase-android-sdk by firebase.

the class SQLiteSchemaTest method rewritesCanonicalIds.

@Test
public void rewritesCanonicalIds() {
    schema.runSchemaUpgrades(0, 10);
    Query filteredQuery = query("colletion").filter(filter("foo", "==", "bar"));
    TargetData initialTargetData = new TargetData(filteredQuery.toTarget(), /* targetId= */
    2, /* sequenceNumber= */
    1, QueryPurpose.LISTEN);
    db.execSQL("INSERT INTO targets (target_id, canonical_id, target_proto) VALUES (? ,?, ?)", new Object[] { 2, "invalid_canonical_id", serializer.encodeTargetData(initialTargetData).toByteArray() });
    schema.runSchemaUpgrades(10, 11);
    new SQLitePersistence.Query(db, "SELECT canonical_id, target_proto, canonical_id FROM targets").forEach(cursor -> {
        String actualCanonicalId = cursor.getString(0);
        byte[] targetProtoBytes = cursor.getBlob(1);
        try {
            Target targetProto = Target.parseFrom(targetProtoBytes);
            TargetData targetData = serializer.decodeTargetData(targetProto);
            String expectedCanonicalId = targetData.getTarget().getCanonicalId();
            assertEquals(expectedCanonicalId, actualCanonicalId);
        } catch (InvalidProtocolBufferException e) {
            fail("Failed to decode Target data");
        }
    });
}
Also used : Target(com.google.firebase.firestore.proto.Target) Query(com.google.firebase.firestore.core.Query) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) Test(org.junit.Test)

Example 3 with Target

use of com.google.firebase.firestore.proto.Target in project firebase-android-sdk by firebase.

the class SQLiteSchemaTest method dropsLastLimboFreeSnapshotIfPreviouslyDowngraded.

@Test
public void dropsLastLimboFreeSnapshotIfPreviouslyDowngraded() {
    schema.runSchemaUpgrades(0, 9);
    db.execSQL("INSERT INTO targets (target_id, canonical_id, target_proto) VALUES (?,?, ?)", new Object[] { 1, "foo", createDummyQueryTargetWithLimboFreeVersion(1).toByteArray() });
    db.execSQL("INSERT INTO targets (target_id, canonical_id, target_proto) VALUES (?, ?, ?)", new Object[] { 2, "bar", createDummyQueryTargetWithLimboFreeVersion(2).toByteArray() });
    db.execSQL("INSERT INTO targets (target_id, canonical_id, target_proto) VALUES (?,?, ?)", new Object[] { 3, "baz", createDummyQueryTargetWithLimboFreeVersion(3).toByteArray() });
    schema.runSchemaUpgrades(0, 8);
    schema.runSchemaUpgrades(8, 10);
    int rowCount = new SQLitePersistence.Query(db, "SELECT target_id, target_proto FROM targets").forEach(cursor -> {
        int targetId = cursor.getInt(0);
        byte[] targetProtoBytes = cursor.getBlob(1);
        try {
            Target targetProto = Target.parseFrom(targetProtoBytes);
            assertEquals(targetId, targetProto.getTargetId());
            assertFalse(targetProto.hasLastLimboFreeSnapshotVersion());
        } catch (InvalidProtocolBufferException e) {
            fail("Failed to decode Query data");
        }
    });
    assertEquals(3, rowCount);
}
Also used : Target(com.google.firebase.firestore.proto.Target) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) Test(org.junit.Test)

Example 4 with Target

use of com.google.firebase.firestore.proto.Target in project firebase-android-sdk by firebase.

the class SQLiteSchema method dropLastLimboFreeSnapshotVersion.

private void dropLastLimboFreeSnapshotVersion() {
    new SQLitePersistence.Query(db, "SELECT target_id, target_proto FROM targets").forEach(cursor -> {
        int targetId = cursor.getInt(0);
        byte[] targetProtoBytes = cursor.getBlob(1);
        try {
            Target targetProto = Target.parseFrom(targetProtoBytes);
            targetProto = targetProto.toBuilder().clearLastLimboFreeSnapshotVersion().build();
            db.execSQL("UPDATE targets SET target_proto = ? WHERE target_id = ?", new Object[] { targetProto.toByteArray(), targetId });
        } catch (InvalidProtocolBufferException e) {
            throw fail("Failed to decode Query data for target %s", targetId);
        }
    });
}
Also used : Target(com.google.firebase.firestore.proto.Target) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException)

Example 5 with Target

use of com.google.firebase.firestore.proto.Target in project firebase-android-sdk by firebase.

the class SQLiteSchema method rewriteCanonicalIds.

private void rewriteCanonicalIds() {
    new SQLitePersistence.Query(db, "SELECT target_id, target_proto FROM targets").forEach(cursor -> {
        int targetId = cursor.getInt(0);
        byte[] targetProtoBytes = cursor.getBlob(1);
        try {
            Target targetProto = Target.parseFrom(targetProtoBytes);
            TargetData targetData = serializer.decodeTargetData(targetProto);
            String updatedCanonicalId = targetData.getTarget().getCanonicalId();
            db.execSQL("UPDATE targets SET canonical_id  = ? WHERE target_id = ?", new Object[] { updatedCanonicalId, targetId });
        } catch (InvalidProtocolBufferException e) {
            throw fail("Failed to decode Query data for target %s", targetId);
        }
    });
}
Also used : Target(com.google.firebase.firestore.proto.Target) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException)

Aggregations

Target (com.google.firebase.firestore.proto.Target)6 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)6 Test (org.junit.Test)4 Query (com.google.firebase.firestore.core.Query)1