build static method
Future<FileStorage>
build({
- required HydratedStorageDirectory storageDirectory,
- HydratedEncoding encoding = HydratedEncoding.gzipJson,
- int cacheBytes = defaultCacheBytes,
- Duration? maxAge,
- int? maxEntries,
- bool evictable(
- String key
- Duration useResolution = HydratedStorage.defaultUseResolution,
- @visibleForTesting DateTime clock() = DateTime.now,
Opens the storage kept in storageDirectory, creating it if needed.
encodingis how values are written: HydratedEncoding.gzipJson (the default, smallest) or HydratedEncoding.json. Files written either way read back whatever the setting.cacheBytesbounds the encoded bytes kept in memory, most recently used first. A value larger than that is never cached.maxAge,maxEntriesandevictableevict as withHydratedStorage.build, when the storage opens: see evict.useResolution: how often a file's use time is rewritten at most, by a write or by markUsed.
Opening lists the directory and reads no file. With maxAge or
maxEntries it also evicts, which reads the modification time of every
file evictable accepts: one stat per file, at each launch, run in
a background isolate.
Throws a StateError when another storage, in this isolate or another process, has the directory open. Another isolate of this process is not detected: keep each directory to one isolate.
Implementation
static Future<FileStorage> build({
required HydratedStorageDirectory storageDirectory,
HydratedEncoding encoding = HydratedEncoding.gzipJson,
int cacheBytes = defaultCacheBytes,
Duration? maxAge,
int? maxEntries,
bool Function(String key)? evictable,
Duration useResolution = HydratedStorage.defaultUseResolution,
@visibleForTesting DateTime Function() clock = DateTime.now,
}) async {
if (storageDirectory == HydratedStorageDirectory.web) {
throw ArgumentError.value(
storageDirectory,
'storageDirectory',
'must be a directory; FileStorage is not available on the web',
);
}
if (encoding == HydratedEncoding.tree) {
throw ArgumentError.value(
encoding,
'encoding',
'must be json or gzipJson; files hold bytes',
);
}
if (cacheBytes < 0) {
throw ArgumentError.value(cacheBytes, 'cacheBytes', 'must be >= 0');
}
checkEvictArguments(
olderThan: maxAge,
maxEntries: maxEntries,
olderThanName: 'maxAge',
);
final dir = Directory(storageDirectory.path);
await dir.create(recursive: true);
final path = await dir.resolveSymbolicLinks();
if (!_openDirectories.add(path)) {
throw StateError('A FileStorage already has $path open');
}
final RandomAccessFile lockFile;
try {
lockFile = await _lockDirectory(path);
} catch (_) {
_openDirectories.remove(path);
rethrow;
}
final keys = <String>{};
final storage = FileStorage._(
Directory(path),
keys,
Lock(),
lockFile,
encoding: encoding,
cacheBytes: cacheBytes,
useResolution: useResolution,
clock: clock,
);
try {
await for (final entry in storage._directory.list()) {
if (entry is! File) continue;
final name = entry.uri.pathSegments.last;
if (name.endsWith(_tempExtension)) {
// A write the process did not finish; the old value is intact.
// Only this storage's own: the directory may hold other files.
final target = name.substring(0, name.length - _tempExtension.length);
if (_keyOf(target) != null) await entry.delete();
} else if (_keyOf(name) case final key?) {
keys.add(key);
}
}
if (maxAge != null || maxEntries != null) {
await storage.evict(
olderThan: maxAge,
maxEntries: maxEntries,
where: evictable,
);
}
} catch (_) {
await storage.close();
rethrow;
}
return storage;
}