initialize method
Build a fully-initialised AtPersistenceBundle for atSign.
Calling this twice for the same atSign returns the same bundle — the factory owns the per-atSign lifecycle. Callers should not try to manage it themselves.
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.
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) return existing;
_bundles.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;
return bundle;
}