Search in sources :

Example 1 with BundleMetadata

use of com.google.firebase.firestore.bundle.BundleMetadata in project firebase-android-sdk by firebase.

the class LocalStoreTestCase method assertHasNewerBundle.

private void assertHasNewerBundle(String bundleId, int createTime) {
    boolean hasNewerBundle = localStore.hasNewerBundle(new BundleMetadata(bundleId, /* version= */
    1, version(createTime), /* totalDocuments= */
    1, /* totalBytes= */
    10));
    assertTrue(hasNewerBundle);
}
Also used : BundleMetadata(com.google.firebase.firestore.bundle.BundleMetadata)

Example 2 with BundleMetadata

use of com.google.firebase.firestore.bundle.BundleMetadata in project firebase-android-sdk by firebase.

the class SyncEngine method loadBundle.

public void loadBundle(BundleReader bundleReader, LoadBundleTask resultTask) {
    try {
        BundleMetadata bundleMetadata = bundleReader.getBundleMetadata();
        boolean hasNewerBundle = localStore.hasNewerBundle(bundleMetadata);
        if (hasNewerBundle) {
            resultTask.setResult(LoadBundleTaskProgress.forSuccess(bundleMetadata));
            return;
        }
        @Nullable LoadBundleTaskProgress progress = LoadBundleTaskProgress.forInitial(bundleMetadata);
        resultTask.updateProgress(progress);
        BundleLoader bundleLoader = new BundleLoader(localStore, bundleMetadata);
        long currentBytesRead = 0;
        BundleElement bundleElement;
        while ((bundleElement = bundleReader.getNextElement()) != null) {
            long oldBytesRead = currentBytesRead;
            currentBytesRead = bundleReader.getBytesRead();
            progress = bundleLoader.addElement(bundleElement, currentBytesRead - oldBytesRead);
            if (progress != null) {
                resultTask.updateProgress(progress);
            }
        }
        ImmutableSortedMap<DocumentKey, Document> changes = bundleLoader.applyChanges();
        // TODO(b/160876443): This currently raises snapshots with `fromCache=false` if users already
        // listen to some queries and bundles has newer version.
        emitNewSnapsAndNotifyLocalStore(changes, /* remoteEvent= */
        null);
        // Save metadata, so loading the same bundle will skip.
        localStore.saveBundle(bundleMetadata);
        resultTask.setResult(LoadBundleTaskProgress.forSuccess(bundleMetadata));
    } catch (Exception e) {
        Logger.warn("Firestore", "Loading bundle failed : %s", e);
        resultTask.setException(new FirebaseFirestoreException("Bundle failed to load", FirebaseFirestoreException.Code.INVALID_ARGUMENT, e));
    } finally {
        try {
            bundleReader.close();
        } catch (IOException e) {
            Logger.warn("SyncEngine", "Exception while closing bundle", e);
        }
    }
}
Also used : BundleElement(com.google.firebase.firestore.bundle.BundleElement) FirebaseFirestoreException(com.google.firebase.firestore.FirebaseFirestoreException) IOException(java.io.IOException) Document(com.google.firebase.firestore.model.Document) MutableDocument(com.google.firebase.firestore.model.MutableDocument) FirebaseFirestoreException(com.google.firebase.firestore.FirebaseFirestoreException) IOException(java.io.IOException) BundleMetadata(com.google.firebase.firestore.bundle.BundleMetadata) DocumentKey(com.google.firebase.firestore.model.DocumentKey) LoadBundleTaskProgress(com.google.firebase.firestore.LoadBundleTaskProgress) Nullable(androidx.annotation.Nullable) BundleLoader(com.google.firebase.firestore.bundle.BundleLoader)

Example 3 with BundleMetadata

use of com.google.firebase.firestore.bundle.BundleMetadata in project firebase-android-sdk by firebase.

the class BundleCacheTestCase method testReturnsSavedBundle.

@Test
public void testReturnsSavedBundle() {
    BundleMetadata expectedBundleMetadata = new BundleMetadata("bundle-1", 1, new SnapshotVersion(Timestamp.now()), /* totalDocuments= */
    1, /* totalBytes= */
    10);
    bundleCache.saveBundleMetadata(expectedBundleMetadata);
    BundleMetadata actualBundleMetadata = bundleCache.getBundleMetadata("bundle-1");
    assertEquals(expectedBundleMetadata, actualBundleMetadata);
    // Overwrite
    expectedBundleMetadata = new BundleMetadata("bundle-1", 2, new SnapshotVersion(Timestamp.now()), /* totalDocuments= */
    1, /* totalBytes= */
    10);
    bundleCache.saveBundleMetadata(expectedBundleMetadata);
    actualBundleMetadata = bundleCache.getBundleMetadata("bundle-1");
    assertEquals(expectedBundleMetadata, actualBundleMetadata);
}
Also used : SnapshotVersion(com.google.firebase.firestore.model.SnapshotVersion) BundleMetadata(com.google.firebase.firestore.bundle.BundleMetadata) Test(org.junit.Test)

Example 4 with BundleMetadata

use of com.google.firebase.firestore.bundle.BundleMetadata in project firebase-android-sdk by firebase.

the class LocalStoreTestCase method assertNoNewerBundle.

private void assertNoNewerBundle(String bundleId, int createTime) {
    boolean hasNewerBundle = localStore.hasNewerBundle(new BundleMetadata(bundleId, /* version= */
    1, version(createTime), /* totalDocuments= */
    1, /* totalBytes= */
    10));
    assertFalse(hasNewerBundle);
}
Also used : BundleMetadata(com.google.firebase.firestore.bundle.BundleMetadata)

Aggregations

BundleMetadata (com.google.firebase.firestore.bundle.BundleMetadata)4 Nullable (androidx.annotation.Nullable)1 FirebaseFirestoreException (com.google.firebase.firestore.FirebaseFirestoreException)1 LoadBundleTaskProgress (com.google.firebase.firestore.LoadBundleTaskProgress)1 BundleElement (com.google.firebase.firestore.bundle.BundleElement)1 BundleLoader (com.google.firebase.firestore.bundle.BundleLoader)1 Document (com.google.firebase.firestore.model.Document)1 DocumentKey (com.google.firebase.firestore.model.DocumentKey)1 MutableDocument (com.google.firebase.firestore.model.MutableDocument)1 SnapshotVersion (com.google.firebase.firestore.model.SnapshotVersion)1 IOException (java.io.IOException)1 Test (org.junit.Test)1