use of com.google.firebase.firestore.bundle.BundleElement 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);
}
}
}
Aggregations