hydrate method

Implementation

@override
hydrate() async {
  await isInitialized;

  final files = await _readDataStoreFiles();
  final fileDataStores =
      files.map((file) => parseFileDataStore(file: file)).toList();

  await Future.wait(
    fileDataStores.map((dataStore) => dataStore.hydrate()),
  );

  for (final fileDataStore in fileDataStores) {
    final documentIds = fileDataStore.data.keys.toList();

    for (final documentId in documentIds) {
      final indexId = _getIndexId(fileDataStore.collection, documentId);
      _documentFileDataStoreIndex[indexId] = fileDataStore;
    }
  }

  _fileDataStoreIndex = fileDataStores.fold({}, (acc, store) {
    return {
      ...acc,
      store.filename: store,
    };
  });

  return fileDataStores.fold<SerializedCollectionStore>(
    {},
    (acc, fileDataStore) {
      final existingCollectionData = acc[fileDataStore.collection];
      final fileDataStoreCollectionData = fileDataStore.data;

      // Multiple data stores map to the same collection across different shards
      // and they should all aggregate their data into a single collection store.
      if (existingCollectionData != null) {
        return {
          ...acc,
          fileDataStore.collection: {
            ...existingCollectionData,
            ...fileDataStoreCollectionData,
          }
        };
      }
      return {
        ...acc,
        fileDataStore.collection: fileDataStoreCollectionData,
      };
    },
  );
}