UNPKG

@darlean/fs-persistence-suite

Version:

File System Persistence Suite that uses a physical or shared file system to persist data.

114 lines (113 loc) 4.85 kB
"use strict"; /** * Suite that provides the File System Persistency service. * * The file system persistence service provides persistency by storing data in SQLite databases on regular or * shared filesystem. * * @packageDocumentation */ Object.defineProperty(exports, "__esModule", { value: true }); exports.createFsPersistenceSuiteFromConfig = exports.createFsPersistenceSuite = exports.FS_PERSISTENCE_SERVICE = void 0; const base_1 = require("@darlean/base"); const utils_1 = require("@darlean/utils"); const syncactor_impl_1 = require("./syncactor.impl"); const service_impl_1 = require("./service.impl"); exports.FS_PERSISTENCE_SERVICE = base_1.FS_PERSISTENCE_SERVICE; const FS_PERSISTENCE_ACTOR = 'io.darlean.FsPersistenceActor'; const DEFAULT_SHARD_COUNT = 8; const DEFAULT_MAX_READERS = 1; /** * Iterates through the list of compartments in options and merges all record for which the * compartment mask matches with the provided compartment. * @param options The options object that contains the compartments * @param compartment The name of the compartment to look for * @returns The merged compartment options. */ function findOptions(options, compartment) { let result; for (const comp of options.compartments) { if ((0, utils_1.wildcardMatch)(compartment, comp.compartment)) { if (result) { result = { basePath: comp.basePath ?? result.basePath, compartment: comp.compartment ?? result.compartment, nodes: comp.nodes ?? result.nodes, shardCount: comp.shardCount ?? result.shardCount, subPath: comp.subPath ?? result.subPath }; } else { result = comp; } } } if (result) { return result; } throw new base_1.ApplicationError('NO_COMPARTMENT', 'There is no compartment configured for [Compartment]', { Compartment: compartment }); } function createFsPersistenceSuite(options) { return new base_1.ActorSuite([ { type: FS_PERSISTENCE_ACTOR, kind: 'singular', creator: (context) => { const compartment = context.id[0]; const opts = findOptions(options, compartment); const boundNode = context.id[context.id.length - 1] || 'unbound'; const shard = context.id[context.id.length - 2]; const shardCount = opts.shardCount ?? DEFAULT_SHARD_COUNT; const path = [opts.basePath, opts.subPath, compartment, shardCount, shard, boundNode].join('/'); return new syncactor_impl_1.FsPersistenceActor(path, opts.maxReaders ?? DEFAULT_MAX_READERS, context.deser); } }, { type: exports.FS_PERSISTENCE_SERVICE, kind: 'multiplar', creator: (context) => { const compartment = context.id[0]; const opts = findOptions(options, compartment); const portal = context.portal.typed(FS_PERSISTENCE_ACTOR).prefix([compartment]); return new service_impl_1.FsPersistenceService({ nodes: opts.nodes ?? [], shardCount: opts.shardCount ?? DEFAULT_SHARD_COUNT }, portal, context.deser); } } ]); } exports.createFsPersistenceSuite = createFsPersistenceSuite; function createFsPersistenceSuiteFromConfig(env, runtimeEnabled) { if (env.fetchBoolean('enabled') ?? runtimeEnabled) { const options = { compartments: [] }; const maxShardCount = env.fetchNumber('maxShardCount'); const DEFAULT_COMPARTMENT = { compartment: 'fs.*', basePath: env.fetchString('basePath') ?? './persistence/', shardCount: limit(env.fetchNumber('shardCount') ?? DEFAULT_SHARD_COUNT, maxShardCount) }; for (const comp of [DEFAULT_COMPARTMENT, ...(env.fetchRaw('compartments') ?? [])]) { options.compartments.push({ compartment: comp.compartment, basePath: comp.basePath, subPath: comp.subPath, nodes: comp.nodes, shardCount: limit(comp.shardCount ?? DEFAULT_SHARD_COUNT, maxShardCount), maxReaders: comp.maxReaders ?? env.fetchNumber('maxReaders') }); } return createFsPersistenceSuite(options); } } exports.createFsPersistenceSuiteFromConfig = createFsPersistenceSuiteFromConfig; function limit(n, max) { if (n === undefined) { return undefined; } return max === undefined || n <= max ? n : max; }