@soulcraft/brainy 3.44.0 → 3.46.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,210 @@
1
+ /**
2
+ * Type-Aware Storage Adapter
3
+ *
4
+ * Implements type-first storage architecture for billion-scale optimization
5
+ *
6
+ * Key Features:
7
+ * - Type-first paths: entities/nouns/{type}/vectors/{shard}/{uuid}.json
8
+ * - Fixed-size type tracking: Uint32Array(31) for nouns, Uint32Array(40) for verbs
9
+ * - O(1) type filtering: Can list entities by type via directory structure
10
+ * - Zero technical debt: Clean implementation, no legacy paths
11
+ *
12
+ * Memory Impact @ 1B Scale:
13
+ * - Type tracking: 284 bytes (vs ~120KB with Maps) = -99.76%
14
+ * - Metadata index: 3GB (vs 5GB) = -40% (when combined with TypeFirstMetadataIndex)
15
+ * - Total system: 69GB (vs 557GB) = -88%
16
+ *
17
+ * @version 3.45.0
18
+ * @since Phase 1 - Type-First Implementation
19
+ */
20
+ import { BaseStorage } from '../baseStorage.js';
21
+ import { GraphVerb, HNSWNoun, HNSWVerb, StatisticsData } from '../../coreTypes.js';
22
+ import { NounType, VerbType } from '../../types/graphTypes.js';
23
+ /**
24
+ * Options for TypeAwareStorageAdapter
25
+ */
26
+ export interface TypeAwareStorageOptions {
27
+ /**
28
+ * Underlying storage adapter to delegate file operations to
29
+ * (e.g., FileSystemStorage, S3CompatibleStorage, MemoryStorage)
30
+ */
31
+ underlyingStorage: BaseStorage;
32
+ /**
33
+ * Optional: Enable verbose logging for debugging
34
+ */
35
+ verbose?: boolean;
36
+ }
37
+ /**
38
+ * Type-Aware Storage Adapter
39
+ *
40
+ * Wraps an underlying storage adapter and adds type-first routing
41
+ * Tracks types with fixed-size arrays for billion-scale efficiency
42
+ */
43
+ export declare class TypeAwareStorageAdapter extends BaseStorage {
44
+ private underlying;
45
+ private verbose;
46
+ private nounCountsByType;
47
+ private verbCountsByType;
48
+ private nounTypeCache;
49
+ private verbTypeCache;
50
+ constructor(options: TypeAwareStorageOptions);
51
+ /**
52
+ * Helper to access protected methods on underlying storage
53
+ * TypeScript doesn't allow calling protected methods across instances,
54
+ * so we cast to any to bypass this restriction
55
+ */
56
+ private get u();
57
+ /**
58
+ * Initialize storage adapter
59
+ */
60
+ init(): Promise<void>;
61
+ /**
62
+ * Load type statistics from storage
63
+ * Rebuilds type counts if needed
64
+ */
65
+ private loadTypeStatistics;
66
+ /**
67
+ * Save type statistics to storage
68
+ */
69
+ private saveTypeStatistics;
70
+ /**
71
+ * Get noun type from noun object or cache
72
+ */
73
+ private getNounType;
74
+ /**
75
+ * Get verb type from verb object or cache
76
+ */
77
+ private getVerbType;
78
+ /**
79
+ * Save noun (type-first path)
80
+ */
81
+ protected saveNoun_internal(noun: HNSWNoun): Promise<void>;
82
+ /**
83
+ * Get noun (type-first path)
84
+ */
85
+ protected getNoun_internal(id: string): Promise<HNSWNoun | null>;
86
+ /**
87
+ * Get nouns by noun type (O(1) with type-first paths!)
88
+ */
89
+ protected getNounsByNounType_internal(nounType: string): Promise<HNSWNoun[]>;
90
+ /**
91
+ * Delete noun (type-first path)
92
+ */
93
+ protected deleteNoun_internal(id: string): Promise<void>;
94
+ /**
95
+ * Save verb (type-first path)
96
+ */
97
+ protected saveVerb_internal(verb: HNSWVerb): Promise<void>;
98
+ /**
99
+ * Get verb (type-first path)
100
+ */
101
+ protected getVerb_internal(id: string): Promise<HNSWVerb | null>;
102
+ /**
103
+ * Get verbs by source
104
+ */
105
+ protected getVerbsBySource_internal(sourceId: string): Promise<GraphVerb[]>;
106
+ /**
107
+ * Get verbs by target
108
+ */
109
+ protected getVerbsByTarget_internal(targetId: string): Promise<GraphVerb[]>;
110
+ /**
111
+ * Get verbs by type (O(1) with type-first paths!)
112
+ */
113
+ protected getVerbsByType_internal(verbType: string): Promise<GraphVerb[]>;
114
+ /**
115
+ * Delete verb (type-first path)
116
+ */
117
+ protected deleteVerb_internal(id: string): Promise<void>;
118
+ /**
119
+ * Write object to path (delegate to underlying storage)
120
+ */
121
+ protected writeObjectToPath(path: string, data: any): Promise<void>;
122
+ /**
123
+ * Read object from path (delegate to underlying storage)
124
+ */
125
+ protected readObjectFromPath(path: string): Promise<any | null>;
126
+ /**
127
+ * Delete object from path (delegate to underlying storage)
128
+ */
129
+ protected deleteObjectFromPath(path: string): Promise<void>;
130
+ /**
131
+ * List objects under path (delegate to underlying storage)
132
+ */
133
+ protected listObjectsUnderPath(prefix: string): Promise<string[]>;
134
+ /**
135
+ * Save statistics data
136
+ */
137
+ protected saveStatisticsData(statistics: StatisticsData): Promise<void>;
138
+ /**
139
+ * Get statistics data
140
+ */
141
+ protected getStatisticsData(): Promise<StatisticsData | null>;
142
+ /**
143
+ * Clear all data
144
+ */
145
+ clear(): Promise<void>;
146
+ /**
147
+ * Get storage status
148
+ */
149
+ getStorageStatus(): Promise<{
150
+ type: string;
151
+ used: number;
152
+ quota: number | null;
153
+ details?: Record<string, any>;
154
+ }>;
155
+ /**
156
+ * Initialize counts from storage
157
+ */
158
+ protected initializeCounts(): Promise<void>;
159
+ /**
160
+ * Persist counts to storage
161
+ */
162
+ protected persistCounts(): Promise<void>;
163
+ /**
164
+ * Get noun vector (delegate to underlying storage)
165
+ */
166
+ getNounVector(id: string): Promise<number[] | null>;
167
+ /**
168
+ * Save HNSW data for a noun
169
+ */
170
+ saveHNSWData(nounId: string, hnswData: {
171
+ level: number;
172
+ connections: Record<string, string[]>;
173
+ }): Promise<void>;
174
+ /**
175
+ * Get HNSW data for a noun
176
+ */
177
+ getHNSWData(nounId: string): Promise<{
178
+ level: number;
179
+ connections: Record<string, string[]>;
180
+ } | null>;
181
+ /**
182
+ * Save HNSW system data (entry point, max level)
183
+ */
184
+ saveHNSWSystem(systemData: {
185
+ entryPointId: string | null;
186
+ maxLevel: number;
187
+ }): Promise<void>;
188
+ /**
189
+ * Get HNSW system data
190
+ */
191
+ getHNSWSystem(): Promise<{
192
+ entryPointId: string | null;
193
+ maxLevel: number;
194
+ } | null>;
195
+ /**
196
+ * Get type statistics
197
+ * Useful for analytics and optimization
198
+ */
199
+ getTypeStatistics(): {
200
+ nouns: Array<{
201
+ type: NounType;
202
+ count: number;
203
+ }>;
204
+ verbs: Array<{
205
+ type: VerbType;
206
+ count: number;
207
+ }>;
208
+ totalMemory: number;
209
+ };
210
+ }