initialize method

  1. @override
Future<AtPersistenceBundle> initialize(
  1. String atSign,
  2. AtPersistenceConfig config
)
override

Build a fully-initialised AtPersistenceBundle for atSign.

Calling this twice for the same atSign with the same storage locations returns the same bundle — the factory owns the per-atSign lifecycle. Callers should not try to manage it themselves.

Calling it for an atSign that already has an open bundle rooted somewhere else throws a StateError. One bundle per atSign is this interface's contract and bundleFor and closeFor both key on the atSign alone, so there is nowhere for a second bundle to live; returning the one it holds would answer a caller with another store's records, and the caller has no way to notice. Close the first bundle, or use a separate factory instance.

Two spellings of one location are one location: the comparison is lexical first and resolves symlinks on disagreement, so foo/bar and foo/./bar, and a link and its target, are not a conflict.

If a previous bundle for atSign has been closed (whether directly via AtPersistenceBundle.close or via closeFor), the stale entry is dropped and a fresh bundle is built — at whatever locations the new config names.

Throws ArgumentError if config does not match backendId.

Implementation

@override
Future<AtPersistenceBundle> initialize(
    String atSign, AtPersistenceConfig config) async {
  if (config is! SqlitePersistenceConfig) {
    throw ArgumentError(
        'SqliteAtPersistenceFactory expects SqlitePersistenceConfig, '
        'got ${config.runtimeType}');
  }

  final existing = _bundles[atSign];
  if (existing != null) {
    if (!existing.isClosed) {
      final held = _configs[atSign];
      if (held != null && !sameStorageLocations(held, config)) {
        throw conflictingStorageError(
            factory: 'SqliteAtPersistenceFactory',
            atSign: atSign,
            held: held,
            requested: config);
      }
      return existing;
    }
    _bundles.remove(atSign);
    _configs.remove(atSign);
  }

  _logger.info('Initialising SQLite persistence for $atSign');

  final db = SqliteDatabase.open(atSign, config.dbPathFor(atSign));

  final commitLog = config.enableCommitLog ? SqliteAtCommitLog(db) : null;
  final accessLog = config.enableAccessLog ? SqliteAtAccessLog(db) : null;
  final notificationKeystore = config.enableNotificationKeystore
      ? SqliteAtNotificationKeystore(db)
      : null;

  final keyValueStore = SqliteAtKeyValueStore(db, atSign)
    ..commitLog = commitLog;
  await keyValueStore.initialize();

  final bundle = SqliteAtPersistenceBundle._(
    atSign: atSign,
    db: db,
    keyValueStore: keyValueStore,
    accessLog: accessLog,
    notificationKeystore: notificationKeystore,
  );
  _bundles[atSign] = bundle;
  _configs[atSign] = config;
  return bundle;
}