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! HivePersistenceConfig) {
    throw ArgumentError(
        'HiveAtPersistenceFactory expects HivePersistenceConfig, '
        'got ${config.runtimeType}');
  }

  // Reuse an open bundle if we already have one. A closed entry
  // is treated as absent — drop the stale reference and build a
  // fresh bundle below. Covers the case where a caller closed
  // the bundle directly via [AtPersistenceBundle.close] without
  // going through [closeFor] / [close].
  final existing = _bundles[atSign];
  if (existing != null) {
    if (!existing.isClosed) {
      final held = _configs[atSign];
      if (held != null && !sameStorageLocations(held, config)) {
        throw conflictingStorageError(
            factory: 'HiveAtPersistenceFactory',
            atSign: atSign,
            held: held,
            requested: config);
      }
      return existing;
    }
    _bundles.remove(atSign);
    _configs.remove(atSign);
  }

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

  // 1. Commit log (optional capability). Server bundles append
  //    every write to it for sync; commit-log-free client bundles
  //    skip it entirely and never open `config.commitLogPath`.
  HiveAtCommitLog? commitLog;
  if (config.enableCommitLog) {
    final commitLogKeyStore = HiveCommitLogKeyStore(atSign);
    await commitLogKeyStore.init(config.commitLogPath, isLazy: false);
    commitLog = HiveAtCommitLog(commitLogKeyStore);
  }

  // 2. Access log (optional capability).
  HiveAtAccessLog? accessLog;
  if (config.enableAccessLog) {
    final accessLogKeyStore = HiveAccessLogKeyStore(atSign);
    await accessLogKeyStore.init(config.accessLogPath);
    accessLog = HiveAtAccessLog(accessLogKeyStore);
  }

  // 3. Notification keystore (optional capability).
  HiveAtNotificationKeystore? notificationKeystore;
  if (config.enableNotificationKeystore) {
    notificationKeystore = HiveAtNotificationKeystore(atSign);
    await notificationKeystore.init(config.notificationStoragePath);
  }

  // 4. Secondary keystore. Owns its own Hive box, encryption
  //    secret, and cron-driven key-expiry sweep (former roles
  //    of the now-retired HivePersistenceManager). The commit
  //    log lives inside the keystore: when present, every write
  //    appends to it for sync. Server bundles always have one;
  //    commit-log-free client bundles leave `commitLog` null.
  final keyValueStore = HiveAtKeyValueStore(atSign);
  keyValueStore.commitLog = commitLog;
  await keyValueStore.init(config.storagePath);

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