@soulcraft/brainy 6.2.2 → 6.2.3

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,252 @@
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 { HNSWNoun, HNSWVerb, HNSWVerbWithMetadata, NounMetadata, VerbMetadata, 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 cache
72
+ *
73
+ * v4.0.0: Metadata is stored separately, so we rely on the cache
74
+ * which is populated when saveNounMetadata is called
75
+ */
76
+ private getNounType;
77
+ /**
78
+ * Get verb type from verb object
79
+ *
80
+ * ARCHITECTURAL FIX (v3.50.1): Simplified - verb field is now always present
81
+ */
82
+ private getVerbType;
83
+ /**
84
+ * Save noun (type-first path)
85
+ */
86
+ protected saveNoun_internal(noun: HNSWNoun): Promise<void>;
87
+ /**
88
+ * Get noun (type-first path)
89
+ */
90
+ protected getNoun_internal(id: string): Promise<HNSWNoun | null>;
91
+ /**
92
+ * Get nouns by noun type (O(1) with type-first paths!)
93
+ */
94
+ protected getNounsByNounType_internal(nounType: string): Promise<HNSWNoun[]>;
95
+ /**
96
+ * Delete noun (type-first path)
97
+ */
98
+ protected deleteNoun_internal(id: string): Promise<void>;
99
+ /**
100
+ * Save verb (type-first path)
101
+ *
102
+ * ARCHITECTURAL FIX (v3.50.1): No more caching hack needed!
103
+ * HNSWVerb now includes verb field, so type is always available
104
+ */
105
+ protected saveVerb_internal(verb: HNSWVerb): Promise<void>;
106
+ /**
107
+ * Get verb (type-first path)
108
+ *
109
+ * ARCHITECTURAL FIX (v3.50.1): Cache still useful for performance
110
+ * Once we know where a verb is, we can retrieve it O(1) instead of searching all types
111
+ */
112
+ protected getVerb_internal(id: string): Promise<HNSWVerb | null>;
113
+ /**
114
+ * Get verbs by source
115
+ */
116
+ protected getVerbsBySource_internal(sourceId: string): Promise<HNSWVerbWithMetadata[]>;
117
+ /**
118
+ * Get verbs by target
119
+ */
120
+ protected getVerbsByTarget_internal(targetId: string): Promise<HNSWVerbWithMetadata[]>;
121
+ /**
122
+ * Get verbs by type (O(1) with type-first paths!)
123
+ *
124
+ * v4.0.0: Load verbs and combine with metadata
125
+ */
126
+ protected getVerbsByType_internal(verbType: string): Promise<HNSWVerbWithMetadata[]>;
127
+ /**
128
+ * Delete verb (type-first path)
129
+ */
130
+ protected deleteVerb_internal(id: string): Promise<void>;
131
+ /**
132
+ * Save noun metadata (override to cache type for type-aware routing)
133
+ *
134
+ * v4.0.0: Extract and cache noun type when metadata is saved
135
+ */
136
+ saveNounMetadata(id: string, metadata: NounMetadata): Promise<void>;
137
+ /**
138
+ * Get noun metadata (override to use type-aware paths)
139
+ */
140
+ getNounMetadata(id: string): Promise<NounMetadata | null>;
141
+ /**
142
+ * Delete noun metadata (override to use type-aware paths)
143
+ */
144
+ deleteNounMetadata(id: string): Promise<void>;
145
+ /**
146
+ * Save verb metadata (override to use type-aware paths)
147
+ *
148
+ * Note: Verb type comes from HNSWVerb.verb field, not metadata
149
+ * We need to read the verb to get the type for path routing
150
+ */
151
+ saveVerbMetadata(id: string, metadata: VerbMetadata): Promise<void>;
152
+ /**
153
+ * Get verb metadata (override to use type-aware paths)
154
+ */
155
+ getVerbMetadata(id: string): Promise<VerbMetadata | null>;
156
+ /**
157
+ * Delete verb metadata (override to use type-aware paths)
158
+ */
159
+ deleteVerbMetadata(id: string): Promise<void>;
160
+ /**
161
+ * Write object to path (delegate to underlying storage)
162
+ */
163
+ protected writeObjectToPath(path: string, data: any): Promise<void>;
164
+ /**
165
+ * Read object from path (delegate to underlying storage)
166
+ */
167
+ protected readObjectFromPath(path: string): Promise<any | null>;
168
+ /**
169
+ * Delete object from path (delegate to underlying storage)
170
+ */
171
+ protected deleteObjectFromPath(path: string): Promise<void>;
172
+ /**
173
+ * List objects under path (delegate to underlying storage)
174
+ */
175
+ protected listObjectsUnderPath(prefix: string): Promise<string[]>;
176
+ /**
177
+ * Save statistics data
178
+ */
179
+ protected saveStatisticsData(statistics: StatisticsData): Promise<void>;
180
+ /**
181
+ * Get statistics data
182
+ */
183
+ protected getStatisticsData(): Promise<StatisticsData | null>;
184
+ /**
185
+ * Clear all data
186
+ */
187
+ clear(): Promise<void>;
188
+ /**
189
+ * Get storage status
190
+ */
191
+ getStorageStatus(): Promise<{
192
+ type: string;
193
+ used: number;
194
+ quota: number | null;
195
+ details?: Record<string, any>;
196
+ }>;
197
+ /**
198
+ * Initialize counts from storage
199
+ */
200
+ protected initializeCounts(): Promise<void>;
201
+ /**
202
+ * Persist counts to storage
203
+ */
204
+ protected persistCounts(): Promise<void>;
205
+ /**
206
+ * Get noun vector (delegate to underlying storage)
207
+ */
208
+ getNounVector(id: string): Promise<number[] | null>;
209
+ /**
210
+ * Save HNSW data for a noun
211
+ */
212
+ saveHNSWData(nounId: string, hnswData: {
213
+ level: number;
214
+ connections: Record<string, string[]>;
215
+ }): Promise<void>;
216
+ /**
217
+ * Get HNSW data for a noun
218
+ */
219
+ getHNSWData(nounId: string): Promise<{
220
+ level: number;
221
+ connections: Record<string, string[]>;
222
+ } | null>;
223
+ /**
224
+ * Save HNSW system data (entry point, max level)
225
+ */
226
+ saveHNSWSystem(systemData: {
227
+ entryPointId: string | null;
228
+ maxLevel: number;
229
+ }): Promise<void>;
230
+ /**
231
+ * Get HNSW system data
232
+ */
233
+ getHNSWSystem(): Promise<{
234
+ entryPointId: string | null;
235
+ maxLevel: number;
236
+ } | null>;
237
+ /**
238
+ * Get type statistics
239
+ * Useful for analytics and optimization
240
+ */
241
+ getTypeStatistics(): {
242
+ nouns: Array<{
243
+ type: NounType;
244
+ count: number;
245
+ }>;
246
+ verbs: Array<{
247
+ type: VerbType;
248
+ count: number;
249
+ }>;
250
+ totalMemory: number;
251
+ };
252
+ }