brainy/src/storage/storageFactory.ts
David Snelling 92c96246fb feat(v4.0.0): Complete metadata/vector separation architecture with Azure support
This commit completes the core v4.0.0 architecture changes for billion-scale
performance with metadata/vector separation. NO RELEASE YET - remaining optimizations
and testing required before production release.

## Core v4.0.0 Architecture Changes

### Type System Updates
- Fixed all TypeScript compilation errors (zero errors achieved)
- Updated HNSWNoun/HNSWVerb to separate core fields from metadata
- Implemented HNSWNounWithMetadata/HNSWVerbWithMetadata for API boundaries
- Added required 'noun' field to NounMetadata for semantic structure
- Renamed verb.type to verb.verb for consistency

### Storage Adapter Updates
**All adapters updated for v4.0.0 two-file storage pattern:**
- memoryStorage: Proper metadata/vector separation
- fileSystemStorage: Two-file pattern with sharding
- opfsStorage: Browser persistent storage updated
- s3CompatibleStorage: AWS/MinIO/DigitalOcean support
- r2Storage: Cloudflare R2 optimization
- gcsStorage: Google Cloud with ADC support
- **azureBlobStorage: NEW - Full Azure Blob Storage support**

### Storage Features
- BaseStorage: Internal vs public method separation (_getNoun vs getNoun)
- Two-file storage: Vectors in one file, metadata in another
- Change tracking: getChangesSince return type updated
- Pagination: getNounsWithPagination returns WithMetadata types

### Azure Blob Storage Integration (NEW)
- Native @azure/storage-blob SDK integration
- Four authentication methods:
  * DefaultAzureCredential (Managed Identity) - recommended
  * Connection String - simplest setup
  * Account Name + Key - traditional auth
  * SAS Token - delegated access
- High-volume mode with write buffering
- Adaptive backpressure for throttling
- UUID-based sharding for billion-scale
- Full HNSW support with graph persistence

### Utility Updates
- EmbeddingManager: Updated to accept Record<string, unknown>
- LSMTree: Wrapped data in NounMetadata structure with 'noun' field
- EntityIdMapper: Fixed nested metadata.data structure access
- MetadataIndex: Fixed field type inference integration
- PeriodicCleanup: Updated for new metadata structure

### Core API Updates
- Brainy: Updated verb property access from v.type to v.verb
- ConfigAPI: Fixed NounMetadata access patterns
- DataAPI: Updated metadata handling

### Documentation Updates
- CREATING-AUGMENTATIONS.md: v4.0.0 breaking changes guide
- DEVELOPER-GUIDE.md: Migration checklist and examples
- COMPLETE-REFERENCE.md: v4.0.0 architecture improvements
- **finite-type-system.md: NEW - Revolutionary type system benefits**

### Build & Dependencies
- Zero TypeScript compilation errors
- Added @azure/storage-blob and @azure/identity
- 591 tests passing (23 timeout in long-running neural tests)

## What's NOT in This Release
This is a work-in-progress commit. Before v4.0.0 release we need:
- Storage adapter optimizations (batch operations, compression)
- Azure blob tier management (Hot/Cool/Archive)
- Cost optimization implementations
- Additional performance testing at billion-scale
- Migration guides for v3.x users

## Testing
- Clean build: 
- Type checking:  (zero errors)
- Test suite:  (591/614 passing, timeouts in neural tests only)

🔐 Generated with Claude Code
https://claude.com/claude-code

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-17 12:29:27 -07:00

704 lines
20 KiB
TypeScript

/**
* Storage Factory
* Creates the appropriate storage adapter based on the environment and configuration
*/
import { StorageAdapter } from '../coreTypes.js'
import { MemoryStorage } from './adapters/memoryStorage.js'
import { OPFSStorage } from './adapters/opfsStorage.js'
import { S3CompatibleStorage } from './adapters/s3CompatibleStorage.js'
import { R2Storage } from './adapters/r2Storage.js'
import { GcsStorage } from './adapters/gcsStorage.js'
import { AzureBlobStorage } from './adapters/azureBlobStorage.js'
import { TypeAwareStorageAdapter } from './adapters/typeAwareStorageAdapter.js'
// FileSystemStorage is dynamically imported to avoid issues in browser environments
import { isBrowser } from '../utils/environment.js'
import { OperationConfig } from '../utils/operationUtils.js'
/**
* Options for creating a storage adapter
*/
export interface StorageOptions {
/**
* The type of storage to use
* - 'auto': Automatically select the best storage adapter based on the environment
* - 'memory': Use in-memory storage
* - 'opfs': Use Origin Private File System storage (browser only)
* - 'filesystem': Use file system storage (Node.js only)
* - 's3': Use Amazon S3 storage
* - 'r2': Use Cloudflare R2 storage
* - 'gcs': Use Google Cloud Storage (S3-compatible with HMAC keys)
* - 'gcs-native': Use Google Cloud Storage (native SDK with ADC)
* - 'azure': Use Azure Blob Storage (native SDK with Managed Identity)
* - 'type-aware': Use type-first storage adapter (wraps another adapter)
*/
type?: 'auto' | 'memory' | 'opfs' | 'filesystem' | 's3' | 'r2' | 'gcs' | 'gcs-native' | 'azure' | 'type-aware'
/**
* Force the use of memory storage even if other storage types are available
*/
forceMemoryStorage?: boolean
/**
* Force the use of file system storage even if other storage types are available
*/
forceFileSystemStorage?: boolean
/**
* Request persistent storage permission from the user (browser only)
*/
requestPersistentStorage?: boolean
/**
* Root directory for file system storage (Node.js only)
*/
rootDirectory?: string
/**
* Nested options object for backward compatibility with BrainyConfig.storage.options
* Supports flexible API patterns
*/
options?: {
rootDirectory?: string
path?: string
[key: string]: any
}
/**
* Configuration for Amazon S3 storage
*/
s3Storage?: {
/**
* S3 bucket name
*/
bucketName: string
/**
* AWS region (e.g., 'us-east-1')
*/
region?: string
/**
* AWS access key ID
*/
accessKeyId: string
/**
* AWS secret access key
*/
secretAccessKey: string
/**
* AWS session token (optional)
*/
sessionToken?: string
}
/**
* Configuration for Cloudflare R2 storage
*/
r2Storage?: {
/**
* R2 bucket name
*/
bucketName: string
/**
* Cloudflare account ID
*/
accountId: string
/**
* R2 access key ID
*/
accessKeyId: string
/**
* R2 secret access key
*/
secretAccessKey: string
}
/**
* Configuration for Google Cloud Storage (S3-compatible with HMAC keys)
*/
gcsStorage?: {
/**
* GCS bucket name
*/
bucketName: string
/**
* GCS region (e.g., 'us-central1')
*/
region?: string
/**
* GCS access key ID
*/
accessKeyId: string
/**
* GCS secret access key
*/
secretAccessKey: string
/**
* GCS endpoint (e.g., 'https://storage.googleapis.com')
*/
endpoint?: string
}
/**
* Configuration for Google Cloud Storage (native SDK with ADC)
*/
gcsNativeStorage?: {
/**
* GCS bucket name
*/
bucketName: string
/**
* Service account key file path (optional, uses ADC if not provided)
*/
keyFilename?: string
/**
* Service account credentials object (optional, uses ADC if not provided)
*/
credentials?: object
/**
* HMAC access key ID (backward compatibility, not recommended)
*/
accessKeyId?: string
/**
* HMAC secret access key (backward compatibility, not recommended)
*/
secretAccessKey?: string
}
/**
* Configuration for Azure Blob Storage (native SDK with Managed Identity)
*/
azureStorage?: {
/**
* Azure container name
*/
containerName: string
/**
* Azure Storage account name (for Managed Identity or SAS)
*/
accountName?: string
/**
* Azure Storage account key (optional, uses Managed Identity if not provided)
*/
accountKey?: string
/**
* Azure connection string (highest priority if provided)
*/
connectionString?: string
/**
* SAS token (optional, alternative to account key)
*/
sasToken?: string
}
/**
* Configuration for Type-Aware Storage (type-first architecture)
* Wraps another storage adapter and adds type-first routing
*/
typeAwareStorage?: {
/**
* Underlying storage adapter to use
* Can be any of: 'memory', 'filesystem', 's3', 'r2', 'gcs', 'gcs-native'
*/
underlyingType?: 'memory' | 'filesystem' | 's3' | 'r2' | 'gcs' | 'gcs-native'
/**
* Options for the underlying storage adapter
*/
underlyingOptions?: StorageOptions
/**
* Enable verbose logging for debugging
*/
verbose?: boolean
}
/**
* Configuration for custom S3-compatible storage
*/
customS3Storage?: {
/**
* S3-compatible bucket name
*/
bucketName: string
/**
* S3-compatible region
*/
region?: string
/**
* S3-compatible endpoint URL
*/
endpoint: string
/**
* S3-compatible access key ID
*/
accessKeyId: string
/**
* S3-compatible secret access key
*/
secretAccessKey: string
/**
* S3-compatible service type (for logging and error messages)
*/
serviceType?: string
}
/**
* Operation configuration for timeout and retry behavior
*/
operationConfig?: OperationConfig
/**
* Cache configuration for optimizing data access
* Particularly important for S3 and other remote storage
*/
cacheConfig?: {
/**
* Maximum size of the hot cache (most frequently accessed items)
* For large datasets, consider values between 5000-50000 depending on available memory
*/
hotCacheMaxSize?: number
/**
* Threshold at which to start evicting items from the hot cache
* Expressed as a fraction of hotCacheMaxSize (0.0 to 1.0)
* Default: 0.8 (start evicting when cache is 80% full)
*/
hotCacheEvictionThreshold?: number
/**
* Time-to-live for items in the warm cache in milliseconds
* Default: 3600000 (1 hour)
*/
warmCacheTTL?: number
/**
* Batch size for operations like prefetching
* Larger values improve throughput but use more memory
*/
batchSize?: number
/**
* Whether to enable auto-tuning of cache parameters
* When true, the system will automatically adjust cache sizes based on usage patterns
* Default: true
*/
autoTune?: boolean
/**
* The interval (in milliseconds) at which to auto-tune cache parameters
* Only applies when autoTune is true
* Default: 60000 (1 minute)
*/
autoTuneInterval?: number
/**
* Whether the storage is in read-only mode
* This affects cache sizing and prefetching strategies
*/
readOnly?: boolean
}
}
/**
* Extract filesystem root directory from options
* Single source of truth for path resolution - supports all API variants
* Zero-config philosophy: flexible input, predictable output
*/
function getFileSystemPath(options: StorageOptions): string {
return (
options.rootDirectory || // Official storageFactory API
(options as any).path || // User-friendly API
options.options?.rootDirectory || // Nested options API
options.options?.path || // Nested path API
'./brainy-data' // Zero-config fallback
)
}
/**
* Create a storage adapter based on the environment and configuration
* @param options Options for creating the storage adapter
* @returns Promise that resolves to a storage adapter
*/
export async function createStorage(
options: StorageOptions = {}
): Promise<StorageAdapter> {
// If memory storage is forced, use it regardless of other options
if (options.forceMemoryStorage) {
console.log('Using memory storage (forced)')
return new MemoryStorage()
}
// If file system storage is forced, use it regardless of other options
if (options.forceFileSystemStorage) {
if (isBrowser()) {
console.warn(
'FileSystemStorage is not available in browser environments, falling back to memory storage'
)
return new MemoryStorage()
}
const fsPath = getFileSystemPath(options)
console.log(`Using file system storage (forced): ${fsPath}`)
try {
const { FileSystemStorage } = await import(
'./adapters/fileSystemStorage.js'
)
return new FileSystemStorage(fsPath)
} catch (error) {
console.warn(
'Failed to load FileSystemStorage, falling back to memory storage:',
error
)
return new MemoryStorage()
}
}
// If a specific storage type is specified, use it
if (options.type && options.type !== 'auto') {
switch (options.type) {
case 'memory':
console.log('Using memory storage')
return new MemoryStorage()
case 'opfs': {
// Check if OPFS is available
const opfsStorage = new OPFSStorage()
if (opfsStorage.isOPFSAvailable()) {
console.log('Using OPFS storage')
await opfsStorage.init()
// Request persistent storage if specified
if (options.requestPersistentStorage) {
const isPersistent = await opfsStorage.requestPersistentStorage()
console.log(
`Persistent storage ${isPersistent ? 'granted' : 'denied'}`
)
}
return opfsStorage
} else {
console.warn(
'OPFS storage is not available, falling back to memory storage'
)
return new MemoryStorage()
}
}
case 'filesystem': {
if (isBrowser()) {
console.warn(
'FileSystemStorage is not available in browser environments, falling back to memory storage'
)
return new MemoryStorage()
}
const fsPath = getFileSystemPath(options)
console.log(`Using file system storage: ${fsPath}`)
try {
const { FileSystemStorage } = await import(
'./adapters/fileSystemStorage.js'
)
return new FileSystemStorage(fsPath)
} catch (error) {
console.warn(
'Failed to load FileSystemStorage, falling back to memory storage:',
error
)
return new MemoryStorage()
}
}
case 's3':
if (options.s3Storage) {
console.log('Using Amazon S3 storage')
return new S3CompatibleStorage({
bucketName: options.s3Storage.bucketName,
region: options.s3Storage.region,
accessKeyId: options.s3Storage.accessKeyId,
secretAccessKey: options.s3Storage.secretAccessKey,
sessionToken: options.s3Storage.sessionToken,
serviceType: 's3',
operationConfig: options.operationConfig,
cacheConfig: options.cacheConfig
})
} else {
console.warn(
'S3 storage configuration is missing, falling back to memory storage'
)
return new MemoryStorage()
}
case 'r2':
if (options.r2Storage) {
console.log('Using Cloudflare R2 storage (dedicated adapter)')
return new R2Storage({
bucketName: options.r2Storage.bucketName,
accountId: options.r2Storage.accountId,
accessKeyId: options.r2Storage.accessKeyId,
secretAccessKey: options.r2Storage.secretAccessKey,
cacheConfig: options.cacheConfig
})
} else {
console.warn(
'R2 storage configuration is missing, falling back to memory storage'
)
return new MemoryStorage()
}
case 'gcs':
if (options.gcsStorage) {
console.log('Using Google Cloud Storage (S3-compatible)')
return new S3CompatibleStorage({
bucketName: options.gcsStorage.bucketName,
region: options.gcsStorage.region,
endpoint:
options.gcsStorage.endpoint || 'https://storage.googleapis.com',
accessKeyId: options.gcsStorage.accessKeyId,
secretAccessKey: options.gcsStorage.secretAccessKey,
serviceType: 'gcs',
cacheConfig: options.cacheConfig
})
} else {
console.warn(
'GCS storage configuration is missing, falling back to memory storage'
)
return new MemoryStorage()
}
case 'gcs-native':
if (options.gcsNativeStorage) {
console.log('Using Google Cloud Storage (native SDK)')
return new GcsStorage({
bucketName: options.gcsNativeStorage.bucketName,
keyFilename: options.gcsNativeStorage.keyFilename,
credentials: options.gcsNativeStorage.credentials,
accessKeyId: options.gcsNativeStorage.accessKeyId,
secretAccessKey: options.gcsNativeStorage.secretAccessKey,
cacheConfig: options.cacheConfig
})
} else {
console.warn(
'GCS native storage configuration is missing, falling back to memory storage'
)
return new MemoryStorage()
}
case 'azure':
if (options.azureStorage) {
console.log('Using Azure Blob Storage (native SDK)')
return new AzureBlobStorage({
containerName: options.azureStorage.containerName,
accountName: options.azureStorage.accountName,
accountKey: options.azureStorage.accountKey,
connectionString: options.azureStorage.connectionString,
sasToken: options.azureStorage.sasToken,
cacheConfig: options.cacheConfig
})
} else {
console.warn(
'Azure storage configuration is missing, falling back to memory storage'
)
return new MemoryStorage()
}
case 'type-aware': {
console.log('Using Type-Aware Storage (type-first architecture)')
// Create underlying storage adapter
const underlyingType = options.typeAwareStorage?.underlyingType || 'auto'
const underlyingOptions = options.typeAwareStorage?.underlyingOptions || {}
// Recursively create the underlying storage
const underlying = await createStorage({
...underlyingOptions,
type: underlyingType
})
// Wrap with TypeAwareStorageAdapter
// Cast to BaseStorage since all concrete storage adapters extend BaseStorage
return new TypeAwareStorageAdapter({
underlyingStorage: underlying as any,
verbose: options.typeAwareStorage?.verbose || false
}) as any
}
default:
console.warn(
`Unknown storage type: ${options.type}, falling back to memory storage`
)
return new MemoryStorage()
}
}
// If custom S3-compatible storage is specified, use it
if (options.customS3Storage) {
console.log(
`Using custom S3-compatible storage: ${options.customS3Storage.serviceType || 'custom'}`
)
return new S3CompatibleStorage({
bucketName: options.customS3Storage.bucketName,
region: options.customS3Storage.region,
endpoint: options.customS3Storage.endpoint,
accessKeyId: options.customS3Storage.accessKeyId,
secretAccessKey: options.customS3Storage.secretAccessKey,
serviceType: options.customS3Storage.serviceType || 'custom',
cacheConfig: options.cacheConfig
})
}
// If R2 storage is specified, use it
if (options.r2Storage) {
console.log('Using Cloudflare R2 storage (dedicated adapter)')
return new R2Storage({
bucketName: options.r2Storage.bucketName,
accountId: options.r2Storage.accountId,
accessKeyId: options.r2Storage.accessKeyId,
secretAccessKey: options.r2Storage.secretAccessKey,
cacheConfig: options.cacheConfig
})
}
// If S3 storage is specified, use it
if (options.s3Storage) {
console.log('Using Amazon S3 storage')
return new S3CompatibleStorage({
bucketName: options.s3Storage.bucketName,
region: options.s3Storage.region,
accessKeyId: options.s3Storage.accessKeyId,
secretAccessKey: options.s3Storage.secretAccessKey,
sessionToken: options.s3Storage.sessionToken,
serviceType: 's3',
cacheConfig: options.cacheConfig
})
}
// If GCS native storage is specified, use it (prioritize native over S3-compatible)
if (options.gcsNativeStorage) {
console.log('Using Google Cloud Storage (native SDK)')
return new GcsStorage({
bucketName: options.gcsNativeStorage.bucketName,
keyFilename: options.gcsNativeStorage.keyFilename,
credentials: options.gcsNativeStorage.credentials,
accessKeyId: options.gcsNativeStorage.accessKeyId,
secretAccessKey: options.gcsNativeStorage.secretAccessKey,
cacheConfig: options.cacheConfig
})
}
// If GCS storage is specified, use it (S3-compatible)
if (options.gcsStorage) {
console.log('Using Google Cloud Storage (S3-compatible)')
return new S3CompatibleStorage({
bucketName: options.gcsStorage.bucketName,
region: options.gcsStorage.region,
endpoint: options.gcsStorage.endpoint || 'https://storage.googleapis.com',
accessKeyId: options.gcsStorage.accessKeyId,
secretAccessKey: options.gcsStorage.secretAccessKey,
serviceType: 'gcs',
cacheConfig: options.cacheConfig
})
}
// If Azure storage is specified, use it
if (options.azureStorage) {
console.log('Using Azure Blob Storage (native SDK)')
return new AzureBlobStorage({
containerName: options.azureStorage.containerName,
accountName: options.azureStorage.accountName,
accountKey: options.azureStorage.accountKey,
connectionString: options.azureStorage.connectionString,
sasToken: options.azureStorage.sasToken,
cacheConfig: options.cacheConfig
})
}
// Auto-detect the best storage adapter based on the environment
// First, check if we're in Node.js (prioritize for test environments)
if (!isBrowser()) {
try {
// Check if we're in a Node.js environment
if (
typeof process !== 'undefined' &&
process.versions &&
process.versions.node
) {
const fsPath = getFileSystemPath(options)
console.log(`Using file system storage (auto-detected): ${fsPath}`)
try {
const { FileSystemStorage } = await import(
'./adapters/fileSystemStorage.js'
)
return new FileSystemStorage(fsPath)
} catch (fsError) {
console.warn(
'Failed to load FileSystemStorage, falling back to memory storage:',
fsError
)
}
}
} catch (error) {
// Not in a Node.js environment or file system is not available
console.warn('Not in a Node.js environment:', error)
}
}
// Next, try OPFS (browser only)
if (isBrowser()) {
const opfsStorage = new OPFSStorage()
if (opfsStorage.isOPFSAvailable()) {
console.log('Using OPFS storage (auto-detected)')
await opfsStorage.init()
// Request persistent storage if specified
if (options.requestPersistentStorage) {
const isPersistent = await opfsStorage.requestPersistentStorage()
console.log(`Persistent storage ${isPersistent ? 'granted' : 'denied'}`)
}
return opfsStorage
}
}
// Finally, fall back to memory storage
console.log('Using memory storage (auto-detected)')
return new MemoryStorage()
}
/**
* Export storage adapters
*/
export {
MemoryStorage,
OPFSStorage,
S3CompatibleStorage,
R2Storage,
GcsStorage,
AzureBlobStorage,
TypeAwareStorageAdapter
}
// Export FileSystemStorage conditionally
// NOTE: FileSystemStorage is now only imported dynamically to avoid fs imports in browser builds
// export { FileSystemStorage } from './adapters/fileSystemStorage.js'