Search in sources :

Example 1 with BundleLoader

use of com.google.firebase.firestore.bundle.BundleLoader 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)

Aggregations

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 BundleMetadata (com.google.firebase.firestore.bundle.BundleMetadata)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 IOException (java.io.IOException)1