velesdb-wasm 0.2.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.
- package/README.md +112 -0
- package/package.json +40 -0
- package/velesdb_wasm.d.ts +115 -0
- package/velesdb_wasm.js +436 -0
- package/velesdb_wasm_bg.wasm +0 -0
- package/velesdb_wasm_bg.wasm.d.ts +16 -0
package/README.md
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# VelesDB WASM
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/velesdb-wasm)
|
|
4
|
+
[](https://github.com/cyberlife-coder/VelesDB/blob/main/LICENSE)
|
|
5
|
+
|
|
6
|
+
WebAssembly build of [VelesDB](https://github.com/cyberlife-coder/VelesDB) - vector search in the browser.
|
|
7
|
+
|
|
8
|
+
## Features
|
|
9
|
+
|
|
10
|
+
- **In-browser vector search** - No server required
|
|
11
|
+
- **SIMD optimized** - Uses WASM SIMD128 for fast distance calculations
|
|
12
|
+
- **Multiple metrics** - Cosine, Euclidean, Dot Product
|
|
13
|
+
- **Lightweight** - Minimal bundle size
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install velesdb-wasm
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
```javascript
|
|
24
|
+
import init, { VectorStore } from 'velesdb-wasm';
|
|
25
|
+
|
|
26
|
+
async function main() {
|
|
27
|
+
// Initialize WASM module
|
|
28
|
+
await init();
|
|
29
|
+
|
|
30
|
+
// Create a vector store (768 dimensions, cosine similarity)
|
|
31
|
+
const store = new VectorStore(768, 'cosine');
|
|
32
|
+
|
|
33
|
+
// Insert vectors
|
|
34
|
+
store.insert(1, new Float32Array([0.1, 0.2, ...]));
|
|
35
|
+
store.insert(2, new Float32Array([0.3, 0.4, ...]));
|
|
36
|
+
|
|
37
|
+
// Search for similar vectors
|
|
38
|
+
const query = new Float32Array([0.15, 0.25, ...]);
|
|
39
|
+
const results = store.search(query, 5); // Top 5 results
|
|
40
|
+
|
|
41
|
+
// Results: [[id, score], [id, score], ...]
|
|
42
|
+
console.log(results);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
main();
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## API
|
|
49
|
+
|
|
50
|
+
### VectorStore
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
class VectorStore {
|
|
54
|
+
// Create a new store
|
|
55
|
+
constructor(dimension: number, metric: 'cosine' | 'euclidean' | 'dot');
|
|
56
|
+
|
|
57
|
+
// Properties
|
|
58
|
+
readonly len: number;
|
|
59
|
+
readonly is_empty: boolean;
|
|
60
|
+
readonly dimension: number;
|
|
61
|
+
|
|
62
|
+
// Methods
|
|
63
|
+
insert(id: number, vector: Float32Array): void;
|
|
64
|
+
search(query: Float32Array, k: number): Array<[number, number]>;
|
|
65
|
+
remove(id: number): boolean;
|
|
66
|
+
clear(): void;
|
|
67
|
+
memory_usage(): number;
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Distance Metrics
|
|
72
|
+
|
|
73
|
+
| Metric | Description | Best For |
|
|
74
|
+
|--------|-------------|----------|
|
|
75
|
+
| `cosine` | Cosine similarity | Text embeddings (BERT, GPT) |
|
|
76
|
+
| `euclidean` | L2 distance | Image features, spatial data |
|
|
77
|
+
| `dot` | Dot product | Pre-normalized vectors |
|
|
78
|
+
|
|
79
|
+
## Use Cases
|
|
80
|
+
|
|
81
|
+
- **Browser-based RAG** - 100% client-side semantic search
|
|
82
|
+
- **Offline-first apps** - Works without internet
|
|
83
|
+
- **Privacy-preserving AI** - Data never leaves the browser
|
|
84
|
+
- **Electron/Tauri apps** - Desktop AI without a server
|
|
85
|
+
|
|
86
|
+
## Building from Source
|
|
87
|
+
|
|
88
|
+
```bash
|
|
89
|
+
# Install wasm-pack
|
|
90
|
+
cargo install wasm-pack
|
|
91
|
+
|
|
92
|
+
# Build for browser
|
|
93
|
+
wasm-pack build --target web
|
|
94
|
+
|
|
95
|
+
# Build for Node.js
|
|
96
|
+
wasm-pack build --target nodejs
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Performance
|
|
100
|
+
|
|
101
|
+
Typical latencies on modern browsers:
|
|
102
|
+
|
|
103
|
+
| Operation | 768D vectors | 10K vectors |
|
|
104
|
+
|-----------|--------------|-------------|
|
|
105
|
+
| Insert | ~1 µs | ~10 ms |
|
|
106
|
+
| Search | ~50 µs | ~5 ms |
|
|
107
|
+
|
|
108
|
+
## License
|
|
109
|
+
|
|
110
|
+
Business Source License 1.1 (BSL-1.1)
|
|
111
|
+
|
|
112
|
+
See [LICENSE](https://github.com/cyberlife-coder/VelesDB/blob/main/LICENSE) for details.
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "velesdb-wasm",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"description": "VelesDB for WebAssembly - Vector search in the browser. Microsecond latency similarity search with no server required.",
|
|
5
|
+
"version": "0.2.0",
|
|
6
|
+
"license": "BUSL-1.1",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/cyberlife-coder/VelesDB.git",
|
|
10
|
+
"directory": "crates/velesdb-wasm"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/cyberlife-coder/VelesDB",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/cyberlife-coder/VelesDB/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"wasm",
|
|
18
|
+
"webassembly",
|
|
19
|
+
"vector",
|
|
20
|
+
"database",
|
|
21
|
+
"search",
|
|
22
|
+
"embeddings",
|
|
23
|
+
"ai",
|
|
24
|
+
"machine-learning",
|
|
25
|
+
"similarity",
|
|
26
|
+
"browser"
|
|
27
|
+
],
|
|
28
|
+
"author": "Cyberlife Coder",
|
|
29
|
+
"files": [
|
|
30
|
+
"velesdb_wasm_bg.wasm",
|
|
31
|
+
"velesdb_wasm.js",
|
|
32
|
+
"velesdb_wasm.d.ts",
|
|
33
|
+
"velesdb_wasm_bg.wasm.d.ts"
|
|
34
|
+
],
|
|
35
|
+
"main": "velesdb_wasm.js",
|
|
36
|
+
"types": "velesdb_wasm.d.ts",
|
|
37
|
+
"sideEffects": [
|
|
38
|
+
"./snippets/*"
|
|
39
|
+
]
|
|
40
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
|
|
4
|
+
export class VectorStore {
|
|
5
|
+
free(): void;
|
|
6
|
+
[Symbol.dispose](): void;
|
|
7
|
+
/**
|
|
8
|
+
* Returns memory usage estimate in bytes.
|
|
9
|
+
*/
|
|
10
|
+
memory_usage(): number;
|
|
11
|
+
/**
|
|
12
|
+
* Creates a new vector store with the specified dimension and distance metric.
|
|
13
|
+
*
|
|
14
|
+
* # Arguments
|
|
15
|
+
*
|
|
16
|
+
* * `dimension` - Vector dimension (e.g., 768 for BERT, 1536 for GPT)
|
|
17
|
+
* * `metric` - Distance metric: "cosine", "euclidean", or "dot"
|
|
18
|
+
*
|
|
19
|
+
* # Errors
|
|
20
|
+
*
|
|
21
|
+
* Returns an error if the metric is not recognized.
|
|
22
|
+
*/
|
|
23
|
+
constructor(dimension: number, metric: string);
|
|
24
|
+
/**
|
|
25
|
+
* Clears all vectors from the store.
|
|
26
|
+
*/
|
|
27
|
+
clear(): void;
|
|
28
|
+
/**
|
|
29
|
+
* Inserts a vector with the given ID.
|
|
30
|
+
*
|
|
31
|
+
* # Arguments
|
|
32
|
+
*
|
|
33
|
+
* * `id` - Unique identifier for the vector
|
|
34
|
+
* * `vector` - `Float32Array` of the vector data
|
|
35
|
+
*
|
|
36
|
+
* # Errors
|
|
37
|
+
*
|
|
38
|
+
* Returns an error if vector dimension doesn't match store dimension.
|
|
39
|
+
*/
|
|
40
|
+
insert(id: bigint, vector: Float32Array): void;
|
|
41
|
+
/**
|
|
42
|
+
* Removes a vector by ID.
|
|
43
|
+
*/
|
|
44
|
+
remove(id: bigint): boolean;
|
|
45
|
+
/**
|
|
46
|
+
* Searches for the k nearest neighbors to the query vector.
|
|
47
|
+
*
|
|
48
|
+
* # Arguments
|
|
49
|
+
*
|
|
50
|
+
* * `query` - Query vector as `Float32Array`
|
|
51
|
+
* * `k` - Number of results to return
|
|
52
|
+
*
|
|
53
|
+
* # Returns
|
|
54
|
+
*
|
|
55
|
+
* Array of [id, score] pairs sorted by relevance.
|
|
56
|
+
*
|
|
57
|
+
* # Errors
|
|
58
|
+
*
|
|
59
|
+
* Returns an error if query dimension doesn't match store dimension.
|
|
60
|
+
*/
|
|
61
|
+
search(query: Float32Array, k: number): any;
|
|
62
|
+
/**
|
|
63
|
+
* Returns the number of vectors in the store.
|
|
64
|
+
*/
|
|
65
|
+
readonly len: number;
|
|
66
|
+
/**
|
|
67
|
+
* Returns true if the store is empty.
|
|
68
|
+
*/
|
|
69
|
+
readonly is_empty: boolean;
|
|
70
|
+
/**
|
|
71
|
+
* Returns the vector dimension.
|
|
72
|
+
*/
|
|
73
|
+
readonly dimension: number;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
|
|
77
|
+
|
|
78
|
+
export interface InitOutput {
|
|
79
|
+
readonly memory: WebAssembly.Memory;
|
|
80
|
+
readonly __wbg_vectorstore_free: (a: number, b: number) => void;
|
|
81
|
+
readonly vectorstore_clear: (a: number) => void;
|
|
82
|
+
readonly vectorstore_dimension: (a: number) => number;
|
|
83
|
+
readonly vectorstore_insert: (a: number, b: number, c: bigint, d: number, e: number) => void;
|
|
84
|
+
readonly vectorstore_is_empty: (a: number) => number;
|
|
85
|
+
readonly vectorstore_len: (a: number) => number;
|
|
86
|
+
readonly vectorstore_memory_usage: (a: number) => number;
|
|
87
|
+
readonly vectorstore_new: (a: number, b: number, c: number, d: number) => void;
|
|
88
|
+
readonly vectorstore_remove: (a: number, b: bigint) => number;
|
|
89
|
+
readonly vectorstore_search: (a: number, b: number, c: number, d: number, e: number) => void;
|
|
90
|
+
readonly __wbindgen_export: (a: number, b: number) => number;
|
|
91
|
+
readonly __wbindgen_export2: (a: number, b: number, c: number, d: number) => number;
|
|
92
|
+
readonly __wbindgen_add_to_stack_pointer: (a: number) => number;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export type SyncInitInput = BufferSource | WebAssembly.Module;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Instantiates the given `module`, which can either be bytes or
|
|
99
|
+
* a precompiled `WebAssembly.Module`.
|
|
100
|
+
*
|
|
101
|
+
* @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
|
|
102
|
+
*
|
|
103
|
+
* @returns {InitOutput}
|
|
104
|
+
*/
|
|
105
|
+
export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* If `module_or_path` is {RequestInfo} or {URL}, makes a request and
|
|
109
|
+
* for everything else, calls `WebAssembly.instantiate` directly.
|
|
110
|
+
*
|
|
111
|
+
* @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
|
|
112
|
+
*
|
|
113
|
+
* @returns {Promise<InitOutput>}
|
|
114
|
+
*/
|
|
115
|
+
export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;
|
package/velesdb_wasm.js
ADDED
|
@@ -0,0 +1,436 @@
|
|
|
1
|
+
let wasm;
|
|
2
|
+
|
|
3
|
+
function addHeapObject(obj) {
|
|
4
|
+
if (heap_next === heap.length) heap.push(heap.length + 1);
|
|
5
|
+
const idx = heap_next;
|
|
6
|
+
heap_next = heap[idx];
|
|
7
|
+
|
|
8
|
+
heap[idx] = obj;
|
|
9
|
+
return idx;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function dropObject(idx) {
|
|
13
|
+
if (idx < 132) return;
|
|
14
|
+
heap[idx] = heap_next;
|
|
15
|
+
heap_next = idx;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
let cachedDataViewMemory0 = null;
|
|
19
|
+
function getDataViewMemory0() {
|
|
20
|
+
if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
|
|
21
|
+
cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
|
|
22
|
+
}
|
|
23
|
+
return cachedDataViewMemory0;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
let cachedFloat32ArrayMemory0 = null;
|
|
27
|
+
function getFloat32ArrayMemory0() {
|
|
28
|
+
if (cachedFloat32ArrayMemory0 === null || cachedFloat32ArrayMemory0.byteLength === 0) {
|
|
29
|
+
cachedFloat32ArrayMemory0 = new Float32Array(wasm.memory.buffer);
|
|
30
|
+
}
|
|
31
|
+
return cachedFloat32ArrayMemory0;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function getStringFromWasm0(ptr, len) {
|
|
35
|
+
ptr = ptr >>> 0;
|
|
36
|
+
return decodeText(ptr, len);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
let cachedUint8ArrayMemory0 = null;
|
|
40
|
+
function getUint8ArrayMemory0() {
|
|
41
|
+
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
|
|
42
|
+
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
|
|
43
|
+
}
|
|
44
|
+
return cachedUint8ArrayMemory0;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function getObject(idx) { return heap[idx]; }
|
|
48
|
+
|
|
49
|
+
let heap = new Array(128).fill(undefined);
|
|
50
|
+
heap.push(undefined, null, true, false);
|
|
51
|
+
|
|
52
|
+
let heap_next = heap.length;
|
|
53
|
+
|
|
54
|
+
function passArrayF32ToWasm0(arg, malloc) {
|
|
55
|
+
const ptr = malloc(arg.length * 4, 4) >>> 0;
|
|
56
|
+
getFloat32ArrayMemory0().set(arg, ptr / 4);
|
|
57
|
+
WASM_VECTOR_LEN = arg.length;
|
|
58
|
+
return ptr;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function passStringToWasm0(arg, malloc, realloc) {
|
|
62
|
+
if (realloc === undefined) {
|
|
63
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
64
|
+
const ptr = malloc(buf.length, 1) >>> 0;
|
|
65
|
+
getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
|
|
66
|
+
WASM_VECTOR_LEN = buf.length;
|
|
67
|
+
return ptr;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
let len = arg.length;
|
|
71
|
+
let ptr = malloc(len, 1) >>> 0;
|
|
72
|
+
|
|
73
|
+
const mem = getUint8ArrayMemory0();
|
|
74
|
+
|
|
75
|
+
let offset = 0;
|
|
76
|
+
|
|
77
|
+
for (; offset < len; offset++) {
|
|
78
|
+
const code = arg.charCodeAt(offset);
|
|
79
|
+
if (code > 0x7F) break;
|
|
80
|
+
mem[ptr + offset] = code;
|
|
81
|
+
}
|
|
82
|
+
if (offset !== len) {
|
|
83
|
+
if (offset !== 0) {
|
|
84
|
+
arg = arg.slice(offset);
|
|
85
|
+
}
|
|
86
|
+
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
|
|
87
|
+
const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
|
|
88
|
+
const ret = cachedTextEncoder.encodeInto(arg, view);
|
|
89
|
+
|
|
90
|
+
offset += ret.written;
|
|
91
|
+
ptr = realloc(ptr, len, offset, 1) >>> 0;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
WASM_VECTOR_LEN = offset;
|
|
95
|
+
return ptr;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function takeObject(idx) {
|
|
99
|
+
const ret = getObject(idx);
|
|
100
|
+
dropObject(idx);
|
|
101
|
+
return ret;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
105
|
+
cachedTextDecoder.decode();
|
|
106
|
+
const MAX_SAFARI_DECODE_BYTES = 2146435072;
|
|
107
|
+
let numBytesDecoded = 0;
|
|
108
|
+
function decodeText(ptr, len) {
|
|
109
|
+
numBytesDecoded += len;
|
|
110
|
+
if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
|
|
111
|
+
cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
112
|
+
cachedTextDecoder.decode();
|
|
113
|
+
numBytesDecoded = len;
|
|
114
|
+
}
|
|
115
|
+
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const cachedTextEncoder = new TextEncoder();
|
|
119
|
+
|
|
120
|
+
if (!('encodeInto' in cachedTextEncoder)) {
|
|
121
|
+
cachedTextEncoder.encodeInto = function (arg, view) {
|
|
122
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
123
|
+
view.set(buf);
|
|
124
|
+
return {
|
|
125
|
+
read: arg.length,
|
|
126
|
+
written: buf.length
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
let WASM_VECTOR_LEN = 0;
|
|
132
|
+
|
|
133
|
+
const VectorStoreFinalization = (typeof FinalizationRegistry === 'undefined')
|
|
134
|
+
? { register: () => {}, unregister: () => {} }
|
|
135
|
+
: new FinalizationRegistry(ptr => wasm.__wbg_vectorstore_free(ptr >>> 0, 1));
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* A vector store for in-memory vector search.
|
|
139
|
+
*/
|
|
140
|
+
export class VectorStore {
|
|
141
|
+
__destroy_into_raw() {
|
|
142
|
+
const ptr = this.__wbg_ptr;
|
|
143
|
+
this.__wbg_ptr = 0;
|
|
144
|
+
VectorStoreFinalization.unregister(this);
|
|
145
|
+
return ptr;
|
|
146
|
+
}
|
|
147
|
+
free() {
|
|
148
|
+
const ptr = this.__destroy_into_raw();
|
|
149
|
+
wasm.__wbg_vectorstore_free(ptr, 0);
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Returns memory usage estimate in bytes.
|
|
153
|
+
* @returns {number}
|
|
154
|
+
*/
|
|
155
|
+
memory_usage() {
|
|
156
|
+
const ret = wasm.vectorstore_memory_usage(this.__wbg_ptr);
|
|
157
|
+
return ret >>> 0;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Returns the number of vectors in the store.
|
|
161
|
+
* @returns {number}
|
|
162
|
+
*/
|
|
163
|
+
get len() {
|
|
164
|
+
const ret = wasm.vectorstore_len(this.__wbg_ptr);
|
|
165
|
+
return ret >>> 0;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Creates a new vector store with the specified dimension and distance metric.
|
|
169
|
+
*
|
|
170
|
+
* # Arguments
|
|
171
|
+
*
|
|
172
|
+
* * `dimension` - Vector dimension (e.g., 768 for BERT, 1536 for GPT)
|
|
173
|
+
* * `metric` - Distance metric: "cosine", "euclidean", or "dot"
|
|
174
|
+
*
|
|
175
|
+
* # Errors
|
|
176
|
+
*
|
|
177
|
+
* Returns an error if the metric is not recognized.
|
|
178
|
+
* @param {number} dimension
|
|
179
|
+
* @param {string} metric
|
|
180
|
+
*/
|
|
181
|
+
constructor(dimension, metric) {
|
|
182
|
+
try {
|
|
183
|
+
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
|
184
|
+
const ptr0 = passStringToWasm0(metric, wasm.__wbindgen_export, wasm.__wbindgen_export2);
|
|
185
|
+
const len0 = WASM_VECTOR_LEN;
|
|
186
|
+
wasm.vectorstore_new(retptr, dimension, ptr0, len0);
|
|
187
|
+
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
|
188
|
+
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
|
|
189
|
+
var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
|
|
190
|
+
if (r2) {
|
|
191
|
+
throw takeObject(r1);
|
|
192
|
+
}
|
|
193
|
+
this.__wbg_ptr = r0 >>> 0;
|
|
194
|
+
VectorStoreFinalization.register(this, this.__wbg_ptr, this);
|
|
195
|
+
return this;
|
|
196
|
+
} finally {
|
|
197
|
+
wasm.__wbindgen_add_to_stack_pointer(16);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Clears all vectors from the store.
|
|
202
|
+
*/
|
|
203
|
+
clear() {
|
|
204
|
+
wasm.vectorstore_clear(this.__wbg_ptr);
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Inserts a vector with the given ID.
|
|
208
|
+
*
|
|
209
|
+
* # Arguments
|
|
210
|
+
*
|
|
211
|
+
* * `id` - Unique identifier for the vector
|
|
212
|
+
* * `vector` - `Float32Array` of the vector data
|
|
213
|
+
*
|
|
214
|
+
* # Errors
|
|
215
|
+
*
|
|
216
|
+
* Returns an error if vector dimension doesn't match store dimension.
|
|
217
|
+
* @param {bigint} id
|
|
218
|
+
* @param {Float32Array} vector
|
|
219
|
+
*/
|
|
220
|
+
insert(id, vector) {
|
|
221
|
+
try {
|
|
222
|
+
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
|
223
|
+
const ptr0 = passArrayF32ToWasm0(vector, wasm.__wbindgen_export);
|
|
224
|
+
const len0 = WASM_VECTOR_LEN;
|
|
225
|
+
wasm.vectorstore_insert(retptr, this.__wbg_ptr, id, ptr0, len0);
|
|
226
|
+
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
|
227
|
+
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
|
|
228
|
+
if (r1) {
|
|
229
|
+
throw takeObject(r0);
|
|
230
|
+
}
|
|
231
|
+
} finally {
|
|
232
|
+
wasm.__wbindgen_add_to_stack_pointer(16);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Removes a vector by ID.
|
|
237
|
+
* @param {bigint} id
|
|
238
|
+
* @returns {boolean}
|
|
239
|
+
*/
|
|
240
|
+
remove(id) {
|
|
241
|
+
const ret = wasm.vectorstore_remove(this.__wbg_ptr, id);
|
|
242
|
+
return ret !== 0;
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Searches for the k nearest neighbors to the query vector.
|
|
246
|
+
*
|
|
247
|
+
* # Arguments
|
|
248
|
+
*
|
|
249
|
+
* * `query` - Query vector as `Float32Array`
|
|
250
|
+
* * `k` - Number of results to return
|
|
251
|
+
*
|
|
252
|
+
* # Returns
|
|
253
|
+
*
|
|
254
|
+
* Array of [id, score] pairs sorted by relevance.
|
|
255
|
+
*
|
|
256
|
+
* # Errors
|
|
257
|
+
*
|
|
258
|
+
* Returns an error if query dimension doesn't match store dimension.
|
|
259
|
+
* @param {Float32Array} query
|
|
260
|
+
* @param {number} k
|
|
261
|
+
* @returns {any}
|
|
262
|
+
*/
|
|
263
|
+
search(query, k) {
|
|
264
|
+
try {
|
|
265
|
+
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
|
266
|
+
const ptr0 = passArrayF32ToWasm0(query, wasm.__wbindgen_export);
|
|
267
|
+
const len0 = WASM_VECTOR_LEN;
|
|
268
|
+
wasm.vectorstore_search(retptr, this.__wbg_ptr, ptr0, len0, k);
|
|
269
|
+
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
|
270
|
+
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
|
|
271
|
+
var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
|
|
272
|
+
if (r2) {
|
|
273
|
+
throw takeObject(r1);
|
|
274
|
+
}
|
|
275
|
+
return takeObject(r0);
|
|
276
|
+
} finally {
|
|
277
|
+
wasm.__wbindgen_add_to_stack_pointer(16);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Returns true if the store is empty.
|
|
282
|
+
* @returns {boolean}
|
|
283
|
+
*/
|
|
284
|
+
get is_empty() {
|
|
285
|
+
const ret = wasm.vectorstore_is_empty(this.__wbg_ptr);
|
|
286
|
+
return ret !== 0;
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Returns the vector dimension.
|
|
290
|
+
* @returns {number}
|
|
291
|
+
*/
|
|
292
|
+
get dimension() {
|
|
293
|
+
const ret = wasm.vectorstore_dimension(this.__wbg_ptr);
|
|
294
|
+
return ret >>> 0;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
if (Symbol.dispose) VectorStore.prototype[Symbol.dispose] = VectorStore.prototype.free;
|
|
298
|
+
|
|
299
|
+
const EXPECTED_RESPONSE_TYPES = new Set(['basic', 'cors', 'default']);
|
|
300
|
+
|
|
301
|
+
async function __wbg_load(module, imports) {
|
|
302
|
+
if (typeof Response === 'function' && module instanceof Response) {
|
|
303
|
+
if (typeof WebAssembly.instantiateStreaming === 'function') {
|
|
304
|
+
try {
|
|
305
|
+
return await WebAssembly.instantiateStreaming(module, imports);
|
|
306
|
+
} catch (e) {
|
|
307
|
+
const validResponse = module.ok && EXPECTED_RESPONSE_TYPES.has(module.type);
|
|
308
|
+
|
|
309
|
+
if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') {
|
|
310
|
+
console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
|
|
311
|
+
|
|
312
|
+
} else {
|
|
313
|
+
throw e;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
const bytes = await module.arrayBuffer();
|
|
319
|
+
return await WebAssembly.instantiate(bytes, imports);
|
|
320
|
+
} else {
|
|
321
|
+
const instance = await WebAssembly.instantiate(module, imports);
|
|
322
|
+
|
|
323
|
+
if (instance instanceof WebAssembly.Instance) {
|
|
324
|
+
return { instance, module };
|
|
325
|
+
} else {
|
|
326
|
+
return instance;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function __wbg_get_imports() {
|
|
332
|
+
const imports = {};
|
|
333
|
+
imports.wbg = {};
|
|
334
|
+
imports.wbg.__wbg_Error_52673b7de5a0ca89 = function(arg0, arg1) {
|
|
335
|
+
const ret = Error(getStringFromWasm0(arg0, arg1));
|
|
336
|
+
return addHeapObject(ret);
|
|
337
|
+
};
|
|
338
|
+
imports.wbg.__wbg_String_8f0eb39a4a4c2f66 = function(arg0, arg1) {
|
|
339
|
+
const ret = String(getObject(arg1));
|
|
340
|
+
const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_export, wasm.__wbindgen_export2);
|
|
341
|
+
const len1 = WASM_VECTOR_LEN;
|
|
342
|
+
getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
|
|
343
|
+
getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
|
|
344
|
+
};
|
|
345
|
+
imports.wbg.__wbg___wbindgen_throw_dd24417ed36fc46e = function(arg0, arg1) {
|
|
346
|
+
throw new Error(getStringFromWasm0(arg0, arg1));
|
|
347
|
+
};
|
|
348
|
+
imports.wbg.__wbg_new_25f239778d6112b9 = function() {
|
|
349
|
+
const ret = new Array();
|
|
350
|
+
return addHeapObject(ret);
|
|
351
|
+
};
|
|
352
|
+
imports.wbg.__wbg_set_7df433eea03a5c14 = function(arg0, arg1, arg2) {
|
|
353
|
+
getObject(arg0)[arg1 >>> 0] = takeObject(arg2);
|
|
354
|
+
};
|
|
355
|
+
imports.wbg.__wbindgen_cast_2241b6af4c4b2941 = function(arg0, arg1) {
|
|
356
|
+
// Cast intrinsic for `Ref(String) -> Externref`.
|
|
357
|
+
const ret = getStringFromWasm0(arg0, arg1);
|
|
358
|
+
return addHeapObject(ret);
|
|
359
|
+
};
|
|
360
|
+
imports.wbg.__wbindgen_cast_4625c577ab2ec9ee = function(arg0) {
|
|
361
|
+
// Cast intrinsic for `U64 -> Externref`.
|
|
362
|
+
const ret = BigInt.asUintN(64, arg0);
|
|
363
|
+
return addHeapObject(ret);
|
|
364
|
+
};
|
|
365
|
+
imports.wbg.__wbindgen_cast_d6cd19b81560fd6e = function(arg0) {
|
|
366
|
+
// Cast intrinsic for `F64 -> Externref`.
|
|
367
|
+
const ret = arg0;
|
|
368
|
+
return addHeapObject(ret);
|
|
369
|
+
};
|
|
370
|
+
imports.wbg.__wbindgen_object_drop_ref = function(arg0) {
|
|
371
|
+
takeObject(arg0);
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
return imports;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
function __wbg_finalize_init(instance, module) {
|
|
378
|
+
wasm = instance.exports;
|
|
379
|
+
__wbg_init.__wbindgen_wasm_module = module;
|
|
380
|
+
cachedDataViewMemory0 = null;
|
|
381
|
+
cachedFloat32ArrayMemory0 = null;
|
|
382
|
+
cachedUint8ArrayMemory0 = null;
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
return wasm;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
function initSync(module) {
|
|
390
|
+
if (wasm !== undefined) return wasm;
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
if (typeof module !== 'undefined') {
|
|
394
|
+
if (Object.getPrototypeOf(module) === Object.prototype) {
|
|
395
|
+
({module} = module)
|
|
396
|
+
} else {
|
|
397
|
+
console.warn('using deprecated parameters for `initSync()`; pass a single object instead')
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
const imports = __wbg_get_imports();
|
|
402
|
+
if (!(module instanceof WebAssembly.Module)) {
|
|
403
|
+
module = new WebAssembly.Module(module);
|
|
404
|
+
}
|
|
405
|
+
const instance = new WebAssembly.Instance(module, imports);
|
|
406
|
+
return __wbg_finalize_init(instance, module);
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
async function __wbg_init(module_or_path) {
|
|
410
|
+
if (wasm !== undefined) return wasm;
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
if (typeof module_or_path !== 'undefined') {
|
|
414
|
+
if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
|
|
415
|
+
({module_or_path} = module_or_path)
|
|
416
|
+
} else {
|
|
417
|
+
console.warn('using deprecated parameters for the initialization function; pass a single object instead')
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
if (typeof module_or_path === 'undefined') {
|
|
422
|
+
module_or_path = new URL('velesdb_wasm_bg.wasm', import.meta.url);
|
|
423
|
+
}
|
|
424
|
+
const imports = __wbg_get_imports();
|
|
425
|
+
|
|
426
|
+
if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
|
|
427
|
+
module_or_path = fetch(module_or_path);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
const { instance, module } = await __wbg_load(await module_or_path, imports);
|
|
431
|
+
|
|
432
|
+
return __wbg_finalize_init(instance, module);
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
export { initSync };
|
|
436
|
+
export default __wbg_init;
|
|
Binary file
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
export const memory: WebAssembly.Memory;
|
|
4
|
+
export const __wbg_vectorstore_free: (a: number, b: number) => void;
|
|
5
|
+
export const vectorstore_clear: (a: number) => void;
|
|
6
|
+
export const vectorstore_dimension: (a: number) => number;
|
|
7
|
+
export const vectorstore_insert: (a: number, b: number, c: bigint, d: number, e: number) => void;
|
|
8
|
+
export const vectorstore_is_empty: (a: number) => number;
|
|
9
|
+
export const vectorstore_len: (a: number) => number;
|
|
10
|
+
export const vectorstore_memory_usage: (a: number) => number;
|
|
11
|
+
export const vectorstore_new: (a: number, b: number, c: number, d: number) => void;
|
|
12
|
+
export const vectorstore_remove: (a: number, b: bigint) => number;
|
|
13
|
+
export const vectorstore_search: (a: number, b: number, c: number, d: number, e: number) => void;
|
|
14
|
+
export const __wbindgen_export: (a: number, b: number) => number;
|
|
15
|
+
export const __wbindgen_export2: (a: number, b: number, c: number, d: number) => number;
|
|
16
|
+
export const __wbindgen_add_to_stack_pointer: (a: number) => number;
|