@ruvector/wasm 0.1.16 → 0.1.22
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.
- package/package.json +22 -33
- package/README.md +0 -969
- package/pkg/README.md +0 -969
- package/src/indexeddb.js +0 -355
- package/src/worker-pool.js +0 -254
- package/src/worker.js +0 -184
package/src/worker.js
DELETED
|
@@ -1,184 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Web Worker for parallel vector search operations
|
|
3
|
-
*
|
|
4
|
-
* This worker handles:
|
|
5
|
-
* - Vector search operations in parallel
|
|
6
|
-
* - Batch insert operations
|
|
7
|
-
* - Zero-copy transfers via transferable objects
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
// Import the WASM module
|
|
11
|
-
let wasmModule = null;
|
|
12
|
-
let vectorDB = null;
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Initialize the worker with WASM module
|
|
16
|
-
*/
|
|
17
|
-
self.onmessage = async function(e) {
|
|
18
|
-
const { type, data } = e.data;
|
|
19
|
-
|
|
20
|
-
try {
|
|
21
|
-
switch (type) {
|
|
22
|
-
case 'init':
|
|
23
|
-
await initWorker(data);
|
|
24
|
-
self.postMessage({ type: 'init', success: true });
|
|
25
|
-
break;
|
|
26
|
-
|
|
27
|
-
case 'insert':
|
|
28
|
-
await handleInsert(data);
|
|
29
|
-
break;
|
|
30
|
-
|
|
31
|
-
case 'insertBatch':
|
|
32
|
-
await handleInsertBatch(data);
|
|
33
|
-
break;
|
|
34
|
-
|
|
35
|
-
case 'search':
|
|
36
|
-
await handleSearch(data);
|
|
37
|
-
break;
|
|
38
|
-
|
|
39
|
-
case 'delete':
|
|
40
|
-
await handleDelete(data);
|
|
41
|
-
break;
|
|
42
|
-
|
|
43
|
-
case 'get':
|
|
44
|
-
await handleGet(data);
|
|
45
|
-
break;
|
|
46
|
-
|
|
47
|
-
case 'len':
|
|
48
|
-
const length = vectorDB.len();
|
|
49
|
-
self.postMessage({ type: 'len', data: length });
|
|
50
|
-
break;
|
|
51
|
-
|
|
52
|
-
default:
|
|
53
|
-
throw new Error(`Unknown message type: ${type}`);
|
|
54
|
-
}
|
|
55
|
-
} catch (error) {
|
|
56
|
-
self.postMessage({
|
|
57
|
-
type: 'error',
|
|
58
|
-
error: {
|
|
59
|
-
message: error.message,
|
|
60
|
-
stack: error.stack
|
|
61
|
-
}
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Initialize WASM module and VectorDB
|
|
68
|
-
*/
|
|
69
|
-
async function initWorker(config) {
|
|
70
|
-
const { wasmUrl, dimensions, metric, useHnsw } = config;
|
|
71
|
-
|
|
72
|
-
// Import WASM module
|
|
73
|
-
wasmModule = await import(wasmUrl);
|
|
74
|
-
|
|
75
|
-
// Initialize WASM
|
|
76
|
-
await wasmModule.default();
|
|
77
|
-
|
|
78
|
-
// Create VectorDB instance
|
|
79
|
-
vectorDB = new wasmModule.VectorDB(dimensions, metric, useHnsw);
|
|
80
|
-
|
|
81
|
-
console.log(`Worker initialized with dimensions=${dimensions}, metric=${metric}, SIMD=${wasmModule.detectSIMD()}`);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Handle single vector insert
|
|
86
|
-
*/
|
|
87
|
-
async function handleInsert(data) {
|
|
88
|
-
const { vector, id, metadata, requestId } = data;
|
|
89
|
-
|
|
90
|
-
// Convert array to Float32Array if needed
|
|
91
|
-
const vectorArray = new Float32Array(vector);
|
|
92
|
-
|
|
93
|
-
const resultId = vectorDB.insert(vectorArray, id, metadata);
|
|
94
|
-
|
|
95
|
-
self.postMessage({
|
|
96
|
-
type: 'insert',
|
|
97
|
-
requestId,
|
|
98
|
-
data: resultId
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Handle batch insert
|
|
104
|
-
*/
|
|
105
|
-
async function handleInsertBatch(data) {
|
|
106
|
-
const { entries, requestId } = data;
|
|
107
|
-
|
|
108
|
-
// Convert vectors to Float32Array
|
|
109
|
-
const processedEntries = entries.map(entry => ({
|
|
110
|
-
vector: new Float32Array(entry.vector),
|
|
111
|
-
id: entry.id,
|
|
112
|
-
metadata: entry.metadata
|
|
113
|
-
}));
|
|
114
|
-
|
|
115
|
-
const ids = vectorDB.insertBatch(processedEntries);
|
|
116
|
-
|
|
117
|
-
self.postMessage({
|
|
118
|
-
type: 'insertBatch',
|
|
119
|
-
requestId,
|
|
120
|
-
data: ids
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* Handle vector search
|
|
126
|
-
*/
|
|
127
|
-
async function handleSearch(data) {
|
|
128
|
-
const { query, k, filter, requestId } = data;
|
|
129
|
-
|
|
130
|
-
// Convert query to Float32Array
|
|
131
|
-
const queryArray = new Float32Array(query);
|
|
132
|
-
|
|
133
|
-
const results = vectorDB.search(queryArray, k, filter);
|
|
134
|
-
|
|
135
|
-
// Convert results to plain objects
|
|
136
|
-
const plainResults = results.map(result => ({
|
|
137
|
-
id: result.id,
|
|
138
|
-
score: result.score,
|
|
139
|
-
vector: result.vector ? Array.from(result.vector) : null,
|
|
140
|
-
metadata: result.metadata
|
|
141
|
-
}));
|
|
142
|
-
|
|
143
|
-
self.postMessage({
|
|
144
|
-
type: 'search',
|
|
145
|
-
requestId,
|
|
146
|
-
data: plainResults
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* Handle delete operation
|
|
152
|
-
*/
|
|
153
|
-
async function handleDelete(data) {
|
|
154
|
-
const { id, requestId } = data;
|
|
155
|
-
|
|
156
|
-
const deleted = vectorDB.delete(id);
|
|
157
|
-
|
|
158
|
-
self.postMessage({
|
|
159
|
-
type: 'delete',
|
|
160
|
-
requestId,
|
|
161
|
-
data: deleted
|
|
162
|
-
});
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* Handle get operation
|
|
167
|
-
*/
|
|
168
|
-
async function handleGet(data) {
|
|
169
|
-
const { id, requestId } = data;
|
|
170
|
-
|
|
171
|
-
const entry = vectorDB.get(id);
|
|
172
|
-
|
|
173
|
-
const plainEntry = entry ? {
|
|
174
|
-
id: entry.id,
|
|
175
|
-
vector: Array.from(entry.vector),
|
|
176
|
-
metadata: entry.metadata
|
|
177
|
-
} : null;
|
|
178
|
-
|
|
179
|
-
self.postMessage({
|
|
180
|
-
type: 'get',
|
|
181
|
-
requestId,
|
|
182
|
-
data: plainEntry
|
|
183
|
-
});
|
|
184
|
-
}
|