UNPKG

@sailboat-computer/data-storage

Version:

Shared data storage library for sailboat computer v3

426 lines 15.7 kB
"use strict"; /** * Local file storage provider implementation */ Object.defineProperty(exports, "__esModule", { value: true }); exports.createLocalFileProvider = exports.LocalFileProvider = void 0; const types_1 = require("../../types"); const errors_1 = require("../../utils/errors"); /** * Local file storage provider implementation */ class LocalFileProvider { /** * Create a new local file storage provider * * @param options - Provider options */ constructor(options) { this.options = options; this.initialized = false; this.index = {}; // In-memory index of data this.lastOperation = new Date(); this.metrics = { operationsPerSecond: 0, averageLatency: 0, errorRate: 0, averageQueryTime: 0, queriesPerSecond: 0 }; this.indexPath = `${options.directory}/index.json`; } /** * Initialize the provider */ async initialize() { if (this.initialized) { return; } try { // In a real implementation, this would create the directory if it doesn't exist // and load the index from disk this.initialized = true; console.log('Local file provider initialized'); } catch (error) { console.error('Failed to initialize local file provider:', error); throw new errors_1.StorageError(errors_1.StorageErrorCode.CONNECTION_FAILED, 'Failed to initialize local file provider', { error }); } } /** * Store data * * @param data - Data to store * @param metadata - Data metadata * @returns Data ID */ async store(data, metadata) { this.ensureInitialized(); try { const id = metadata.id || crypto.randomUUID(); const filePath = `${this.options.directory}/${metadata.category}/${id}.json`; // In a real implementation, this would create the directory if it doesn't exist // and write the data to disk // Update index this.index[id] = filePath; // In a real implementation, this would write the index to disk return id; } catch (error) { console.error('Failed to store data in local file:', error); throw new errors_1.StorageError(errors_1.StorageErrorCode.STORE_FAILED, 'Failed to store data in local file', { error }); } } /** * Retrieve data * * @param query - Data query * @returns Stored data */ async retrieve(query) { this.ensureInitialized(); try { // In a real implementation, this would read data from disk based on the query // For now, just return an empty array return []; } catch (error) { console.error('Failed to retrieve data from local file:', error); throw new errors_1.StorageError(errors_1.StorageErrorCode.RETRIEVE_FAILED, 'Failed to retrieve data from local file', { error }); } } /** * Update data * * @param data - Data to update */ async update(data) { this.ensureInitialized(); try { const id = data.metadata.id; if (!id) { throw new Error('Data ID is required for update'); } // Check if data exists if (!this.index[id]) { throw new Error(`Data with ID ${id} not found`); } // In a real implementation, this would write the data to disk } catch (error) { console.error('Failed to update data in local file:', error); throw new errors_1.StorageError(errors_1.StorageErrorCode.UPDATE_FAILED, 'Failed to update data in local file', { error }); } } /** * Delete data * * @param id - Data ID */ async delete(id) { this.ensureInitialized(); try { // Check if data exists if (!this.index[id]) { throw new Error(`Data with ID ${id} not found`); } // In a real implementation, this would delete the file from disk // Update index delete this.index[id]; // In a real implementation, this would write the index to disk } catch (error) { console.error('Failed to delete data from local file:', error); throw new errors_1.StorageError(errors_1.StorageErrorCode.DELETE_FAILED, 'Failed to delete data from local file', { error }); } } /** * Store multiple data items * * @param items - Data items to store * @returns Data IDs */ async storeBatch(items) { this.ensureInitialized(); try { // Store each item const ids = await Promise.all(items.map(item => this.store(item.data, item.metadata))); return ids; } catch (error) { console.error('Failed to store batch data in local file:', error); throw new errors_1.StorageError(errors_1.StorageErrorCode.STORE_FAILED, 'Failed to store batch data in local file', { error }); } } /** * Clean up old data * * @param category - Data category * @param retentionDays - Retention period in days * @returns Cleanup result */ async cleanup(category, retentionDays) { this.ensureInitialized(); try { // In a real implementation, this would delete old files from disk return { itemsRemoved: 0, bytesFreed: 0, duration: 0 }; } catch (error) { console.error('Failed to clean up data in local file:', error); throw new errors_1.StorageError(errors_1.StorageErrorCode.CLEANUP_FAILED, 'Failed to clean up data in local file', { error }); } } /** * Get storage status * * @returns Storage status */ async getStatus() { try { // In a real implementation, this would get disk usage statistics return { connected: this.initialized, healthy: this.initialized, lastOperation: this.lastOperation, metrics: { operationsPerSecond: this.metrics.operationsPerSecond, averageLatency: this.metrics.averageLatency, errorRate: this.metrics.errorRate, storageUsed: 0, storageAvailable: this.options.maxSizeBytes || 0 }, queryPerformance: { hot: { averageQueryTime: this.metrics.averageQueryTime, queriesPerSecond: this.metrics.queriesPerSecond }, warm: { averageQueryTime: 0, queriesPerSecond: 0 }, cold: { averageQueryTime: 0, queriesPerSecond: 0 }, pendingItems: 0, averageBatchSize: 0, totalBatches: 0, failedBatches: 0, itemsRemoved: 0, bytesFreed: 0, duration: 0, hotStorageUsage: 0, warmStorageUsage: 0, coldStorageUsage: 0, totalStorageUsage: 0, hotStorageCapacity: 0, warmStorageCapacity: 0, coldStorageCapacity: 0, totalStorageCapacity: 0, operationsPerSecond: 0, averageLatency: 0, errorRate: 0, queryPerformance: { hot: { averageQueryTime: this.metrics.averageQueryTime, queriesPerSecond: this.metrics.queriesPerSecond }, warm: { averageQueryTime: 0, queriesPerSecond: 0 }, cold: { averageQueryTime: 0, queriesPerSecond: 0 }, overall: types_1.HealthStatus.HEALTHY, providers: { hot: types_1.HealthStatus.HEALTHY, warm: types_1.HealthStatus.HEALTHY, cold: types_1.HealthStatus.HEALTHY }, issues: { severity: types_1.AlertSeverity.INFO, message: 'Local file storage', component: 'local-file', timestamp: new Date() }, circuitBreaker: { state: {}, failures: {}, lastFailure: {} }, retry: { retryCount: {}, successCount: {}, failureCount: {}, averageDelay: {} }, fallback: { fallbackCount: {}, successCount: {}, failureCount: {} }, timeout: { timeoutCount: {}, averageDuration: {} }, bulkhead: { concurrentOperations: {}, queueSize: {}, rejectionCount: {} }, rateLimit: { limitedCount: {}, bypassCount: {}, waitTimeMs: {}, avgTokensPerSecond: {}, currentTokens: {} } } } }; } catch (error) { console.error('Failed to get local file status:', error); return { connected: false, healthy: false, error: `Failed to get local file status: ${error}`, queryPerformance: { hot: { averageQueryTime: 0, queriesPerSecond: 0 }, warm: { averageQueryTime: 0, queriesPerSecond: 0 }, cold: { averageQueryTime: 0, queriesPerSecond: 0 }, pendingItems: 0, averageBatchSize: 0, totalBatches: 0, failedBatches: 0, itemsRemoved: 0, bytesFreed: 0, duration: 0, hotStorageUsage: 0, warmStorageUsage: 0, coldStorageUsage: 0, totalStorageUsage: 0, hotStorageCapacity: 0, warmStorageCapacity: 0, coldStorageCapacity: 0, totalStorageCapacity: 0, operationsPerSecond: 0, averageLatency: 0, errorRate: 0, queryPerformance: { hot: { averageQueryTime: 0, queriesPerSecond: 0 }, warm: { averageQueryTime: 0, queriesPerSecond: 0 }, cold: { averageQueryTime: 0, queriesPerSecond: 0 }, overall: types_1.HealthStatus.UNHEALTHY, providers: { hot: types_1.HealthStatus.UNHEALTHY, warm: types_1.HealthStatus.UNHEALTHY, cold: types_1.HealthStatus.UNHEALTHY }, issues: { severity: types_1.AlertSeverity.ALARM, message: `Failed to get local file status: ${error}`, component: 'local-file', timestamp: new Date() }, circuitBreaker: { state: {}, failures: {}, lastFailure: {} }, retry: { retryCount: {}, successCount: {}, failureCount: {}, averageDelay: {} }, fallback: { fallbackCount: {}, successCount: {}, failureCount: {} }, timeout: { timeoutCount: {}, averageDuration: {} }, bulkhead: { concurrentOperations: {}, queueSize: {}, rejectionCount: {} }, rateLimit: { limitedCount: {}, bypassCount: {}, waitTimeMs: {}, avgTokensPerSecond: {}, currentTokens: {} } } } }; } } /** * Shutdown the provider */ async shutdown() { if (!this.initialized) { return; } try { // In a real implementation, this would flush any pending writes to disk this.initialized = false; console.log('Local file provider closed'); } catch (error) { console.error('Failed to close local file provider:', error); throw new errors_1.StorageError(errors_1.StorageErrorCode.CONNECTION_FAILED, 'Failed to close local file provider', { error }); } } /** * Ensure provider is initialized * * @throws StorageError if not initialized */ ensureInitialized() { if (!this.initialized) { throw new errors_1.StorageError(errors_1.StorageErrorCode.CONNECTION_FAILED, 'Local file provider not initialized', {}); } } } exports.LocalFileProvider = LocalFileProvider; /** * Create a new local file storage provider * * @param options - Provider options * @returns Local file storage provider */ function createLocalFileProvider(options) { return new LocalFileProvider(options); } exports.createLocalFileProvider = createLocalFileProvider; //# sourceMappingURL=local-file.js.map