agentdb 1.0.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/LICENSE +38 -0
- package/LICENSE-APACHE +190 -0
- package/LICENSE-MIT +21 -0
- package/README.md +953 -0
- package/bin/agentdb.js +485 -0
- package/bin/plugin-cli-wrapper.mjs +21 -0
- package/dist/cache/query-cache.d.ts +105 -0
- package/dist/cache/query-cache.d.ts.map +1 -0
- package/dist/cache/query-cache.js +224 -0
- package/dist/cache/query-cache.js.map +1 -0
- package/dist/cache/query-cache.mjs +219 -0
- package/dist/cli/cache/query-cache.d.ts +104 -0
- package/dist/cli/cache/query-cache.js +244 -0
- package/dist/cli/cli/db-commands.d.ts +48 -0
- package/dist/cli/cli/db-commands.js +613 -0
- package/dist/cli/commands.d.ts +7 -0
- package/dist/cli/commands.d.ts.map +1 -0
- package/dist/cli/commands.js +113 -0
- package/dist/cli/commands.js.map +1 -0
- package/dist/cli/commands.mjs +104 -0
- package/dist/cli/core/backend-interface.d.ts +70 -0
- package/dist/cli/core/backend-interface.js +15 -0
- package/dist/cli/core/native-backend.d.ts +140 -0
- package/dist/cli/core/native-backend.js +432 -0
- package/dist/cli/core/vector-db.d.ts +126 -0
- package/dist/cli/core/vector-db.js +338 -0
- package/dist/cli/core/wasm-backend.d.ts +95 -0
- package/dist/cli/core/wasm-backend.js +418 -0
- package/dist/cli/db-commands.d.ts +49 -0
- package/dist/cli/db-commands.d.ts.map +1 -0
- package/dist/cli/db-commands.js +533 -0
- package/dist/cli/db-commands.js.map +1 -0
- package/dist/cli/db-commands.mjs +522 -0
- package/dist/cli/generator.d.ts +11 -0
- package/dist/cli/generator.d.ts.map +1 -0
- package/dist/cli/generator.js +567 -0
- package/dist/cli/generator.js.map +1 -0
- package/dist/cli/generator.mjs +527 -0
- package/dist/cli/help.d.ts +18 -0
- package/dist/cli/help.d.ts.map +1 -0
- package/dist/cli/help.js +676 -0
- package/dist/cli/help.js.map +1 -0
- package/dist/cli/help.mjs +667 -0
- package/dist/cli/index/hnsw.d.ts +164 -0
- package/dist/cli/index/hnsw.js +558 -0
- package/dist/cli/plugin-cli.d.ts +7 -0
- package/dist/cli/plugin-cli.d.ts.map +1 -0
- package/dist/cli/plugin-cli.js +295 -0
- package/dist/cli/plugin-cli.js.map +1 -0
- package/dist/cli/plugin-cli.mjs +289 -0
- package/dist/cli/quantization/product-quantization.d.ts +108 -0
- package/dist/cli/quantization/product-quantization.js +350 -0
- package/dist/cli/query/query-builder.d.ts +322 -0
- package/dist/cli/query/query-builder.js +600 -0
- package/dist/cli/templates.d.ts +14 -0
- package/dist/cli/templates.d.ts.map +1 -0
- package/dist/cli/templates.js +182 -0
- package/dist/cli/templates.js.map +1 -0
- package/dist/cli/templates.mjs +176 -0
- package/dist/cli/types/index.d.ts +116 -0
- package/dist/cli/types/index.js +5 -0
- package/dist/cli/types.d.ts +91 -0
- package/dist/cli/types.d.ts.map +1 -0
- package/dist/cli/types.js +6 -0
- package/dist/cli/types.js.map +1 -0
- package/dist/cli/types.mjs +4 -0
- package/dist/cli/wizard/index.d.ts +6 -0
- package/dist/cli/wizard/index.d.ts.map +1 -0
- package/dist/cli/wizard/index.js +138 -0
- package/dist/cli/wizard/index.js.map +1 -0
- package/dist/cli/wizard/index.mjs +131 -0
- package/dist/cli/wizard/prompts.d.ts +11 -0
- package/dist/cli/wizard/prompts.d.ts.map +1 -0
- package/dist/cli/wizard/prompts.js +482 -0
- package/dist/cli/wizard/prompts.js.map +1 -0
- package/dist/cli/wizard/prompts.mjs +470 -0
- package/dist/cli/wizard/validator.d.ts +13 -0
- package/dist/cli/wizard/validator.d.ts.map +1 -0
- package/dist/cli/wizard/validator.js +234 -0
- package/dist/cli/wizard/validator.js.map +1 -0
- package/dist/cli/wizard/validator.mjs +224 -0
- package/dist/core/backend-interface.d.ts +71 -0
- package/dist/core/backend-interface.d.ts.map +1 -0
- package/dist/core/backend-interface.js +16 -0
- package/dist/core/backend-interface.js.map +1 -0
- package/dist/core/backend-interface.mjs +12 -0
- package/dist/core/native-backend.d.ts +141 -0
- package/dist/core/native-backend.d.ts.map +1 -0
- package/dist/core/native-backend.js +457 -0
- package/dist/core/native-backend.js.map +1 -0
- package/dist/core/native-backend.mjs +449 -0
- package/dist/core/vector-db.d.ts +127 -0
- package/dist/core/vector-db.d.ts.map +1 -0
- package/dist/core/vector-db.js +266 -0
- package/dist/core/vector-db.js.map +1 -0
- package/dist/core/vector-db.mjs +261 -0
- package/dist/core/wasm-backend.d.ts +96 -0
- package/dist/core/wasm-backend.d.ts.map +1 -0
- package/dist/core/wasm-backend.js +393 -0
- package/dist/core/wasm-backend.js.map +1 -0
- package/dist/core/wasm-backend.mjs +385 -0
- package/dist/index/hnsw-optimized.d.ts +75 -0
- package/dist/index/hnsw-optimized.d.ts.map +1 -0
- package/dist/index/hnsw-optimized.js +412 -0
- package/dist/index/hnsw-optimized.js.map +1 -0
- package/dist/index/hnsw-optimized.mjs +407 -0
- package/dist/index/hnsw.d.ts +165 -0
- package/dist/index/hnsw.d.ts.map +1 -0
- package/dist/index/hnsw.js +521 -0
- package/dist/index/hnsw.js.map +1 -0
- package/dist/index/hnsw.mjs +516 -0
- package/dist/index.d.ts +57 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +82 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +63 -0
- package/dist/mcp-server.d.ts +27 -0
- package/dist/mcp-server.d.ts.map +1 -0
- package/dist/mcp-server.js +789 -0
- package/dist/mcp-server.js.map +1 -0
- package/dist/mcp-server.mjs +784 -0
- package/dist/plugins/base-plugin.d.ts +114 -0
- package/dist/plugins/base-plugin.d.ts.map +1 -0
- package/dist/plugins/base-plugin.js +313 -0
- package/dist/plugins/base-plugin.js.map +1 -0
- package/dist/plugins/base-plugin.mjs +275 -0
- package/dist/plugins/implementations/active-learning.d.ts +135 -0
- package/dist/plugins/implementations/active-learning.d.ts.map +1 -0
- package/dist/plugins/implementations/active-learning.js +372 -0
- package/dist/plugins/implementations/active-learning.js.map +1 -0
- package/dist/plugins/implementations/active-learning.mjs +367 -0
- package/dist/plugins/implementations/actor-critic.d.ts +64 -0
- package/dist/plugins/implementations/actor-critic.d.ts.map +1 -0
- package/dist/plugins/implementations/actor-critic.js +363 -0
- package/dist/plugins/implementations/actor-critic.js.map +1 -0
- package/dist/plugins/implementations/actor-critic.mjs +358 -0
- package/dist/plugins/implementations/adversarial-training.d.ts +133 -0
- package/dist/plugins/implementations/adversarial-training.d.ts.map +1 -0
- package/dist/plugins/implementations/adversarial-training.js +409 -0
- package/dist/plugins/implementations/adversarial-training.js.map +1 -0
- package/dist/plugins/implementations/adversarial-training.mjs +404 -0
- package/dist/plugins/implementations/curriculum-learning.d.ts +132 -0
- package/dist/plugins/implementations/curriculum-learning.d.ts.map +1 -0
- package/dist/plugins/implementations/curriculum-learning.js +354 -0
- package/dist/plugins/implementations/curriculum-learning.js.map +1 -0
- package/dist/plugins/implementations/curriculum-learning.mjs +349 -0
- package/dist/plugins/implementations/decision-transformer.d.ts +77 -0
- package/dist/plugins/implementations/decision-transformer.d.ts.map +1 -0
- package/dist/plugins/implementations/decision-transformer.js +422 -0
- package/dist/plugins/implementations/decision-transformer.js.map +1 -0
- package/dist/plugins/implementations/decision-transformer.mjs +417 -0
- package/dist/plugins/implementations/federated-learning.d.ts +126 -0
- package/dist/plugins/implementations/federated-learning.d.ts.map +1 -0
- package/dist/plugins/implementations/federated-learning.js +436 -0
- package/dist/plugins/implementations/federated-learning.js.map +1 -0
- package/dist/plugins/implementations/federated-learning.mjs +431 -0
- package/dist/plugins/implementations/index.d.ts +30 -0
- package/dist/plugins/implementations/index.d.ts.map +1 -0
- package/dist/plugins/implementations/index.js +45 -0
- package/dist/plugins/implementations/index.js.map +1 -0
- package/dist/plugins/implementations/index.mjs +31 -0
- package/dist/plugins/implementations/multi-task-learning.d.ts +115 -0
- package/dist/plugins/implementations/multi-task-learning.d.ts.map +1 -0
- package/dist/plugins/implementations/multi-task-learning.js +369 -0
- package/dist/plugins/implementations/multi-task-learning.js.map +1 -0
- package/dist/plugins/implementations/multi-task-learning.mjs +364 -0
- package/dist/plugins/implementations/neural-architecture-search.d.ts +148 -0
- package/dist/plugins/implementations/neural-architecture-search.d.ts.map +1 -0
- package/dist/plugins/implementations/neural-architecture-search.js +379 -0
- package/dist/plugins/implementations/neural-architecture-search.js.map +1 -0
- package/dist/plugins/implementations/neural-architecture-search.mjs +374 -0
- package/dist/plugins/implementations/q-learning.d.ts +98 -0
- package/dist/plugins/implementations/q-learning.d.ts.map +1 -0
- package/dist/plugins/implementations/q-learning.js +435 -0
- package/dist/plugins/implementations/q-learning.js.map +1 -0
- package/dist/plugins/implementations/q-learning.mjs +430 -0
- package/dist/plugins/implementations/sarsa.d.ts +103 -0
- package/dist/plugins/implementations/sarsa.d.ts.map +1 -0
- package/dist/plugins/implementations/sarsa.js +347 -0
- package/dist/plugins/implementations/sarsa.js.map +1 -0
- package/dist/plugins/implementations/sarsa.mjs +342 -0
- package/dist/plugins/index.d.ts +107 -0
- package/dist/plugins/index.d.ts.map +1 -0
- package/dist/plugins/index.js +179 -0
- package/dist/plugins/index.js.map +1 -0
- package/dist/plugins/index.mjs +168 -0
- package/dist/plugins/interface.d.ts +439 -0
- package/dist/plugins/interface.d.ts.map +1 -0
- package/dist/plugins/interface.js +12 -0
- package/dist/plugins/interface.js.map +1 -0
- package/dist/plugins/interface.mjs +10 -0
- package/dist/plugins/learning-plugin.interface.d.ts +257 -0
- package/dist/plugins/learning-plugin.interface.d.ts.map +1 -0
- package/dist/plugins/learning-plugin.interface.js +7 -0
- package/dist/plugins/learning-plugin.interface.js.map +1 -0
- package/dist/plugins/learning-plugin.interface.mjs +5 -0
- package/dist/plugins/plugin-exports.d.ts +71 -0
- package/dist/plugins/plugin-exports.d.ts.map +1 -0
- package/dist/plugins/plugin-exports.js +78 -0
- package/dist/plugins/plugin-exports.js.map +1 -0
- package/dist/plugins/plugin-exports.mjs +69 -0
- package/dist/plugins/registry.d.ts +206 -0
- package/dist/plugins/registry.d.ts.map +1 -0
- package/dist/plugins/registry.js +365 -0
- package/dist/plugins/registry.js.map +1 -0
- package/dist/plugins/registry.mjs +356 -0
- package/dist/plugins/validator.d.ts +63 -0
- package/dist/plugins/validator.d.ts.map +1 -0
- package/dist/plugins/validator.js +464 -0
- package/dist/plugins/validator.js.map +1 -0
- package/dist/plugins/validator.mjs +458 -0
- package/dist/quantization/binary-quantization.d.ts +104 -0
- package/dist/quantization/binary-quantization.d.ts.map +1 -0
- package/dist/quantization/binary-quantization.js +246 -0
- package/dist/quantization/binary-quantization.js.map +1 -0
- package/dist/quantization/binary-quantization.mjs +240 -0
- package/dist/quantization/optimized-pq.d.ts +138 -0
- package/dist/quantization/optimized-pq.d.ts.map +1 -0
- package/dist/quantization/optimized-pq.js +320 -0
- package/dist/quantization/optimized-pq.js.map +1 -0
- package/dist/quantization/optimized-pq.mjs +313 -0
- package/dist/quantization/product-quantization.d.ts +109 -0
- package/dist/quantization/product-quantization.d.ts.map +1 -0
- package/dist/quantization/product-quantization.js +287 -0
- package/dist/quantization/product-quantization.js.map +1 -0
- package/dist/quantization/product-quantization.mjs +282 -0
- package/dist/quantization/scalar-quantization.d.ts +100 -0
- package/dist/quantization/scalar-quantization.d.ts.map +1 -0
- package/dist/quantization/scalar-quantization.js +324 -0
- package/dist/quantization/scalar-quantization.js.map +1 -0
- package/dist/quantization/scalar-quantization.mjs +319 -0
- package/dist/query/index.d.ts +6 -0
- package/dist/query/index.d.ts.map +1 -0
- package/dist/query/index.js +9 -0
- package/dist/query/index.js.map +1 -0
- package/dist/query/index.mjs +4 -0
- package/dist/query/query-builder.d.ts +323 -0
- package/dist/query/query-builder.d.ts.map +1 -0
- package/dist/query/query-builder.js +524 -0
- package/dist/query/query-builder.js.map +1 -0
- package/dist/query/query-builder.mjs +519 -0
- package/dist/reasoning/context-synthesizer.d.ts +57 -0
- package/dist/reasoning/context-synthesizer.d.ts.map +1 -0
- package/dist/reasoning/context-synthesizer.js +224 -0
- package/dist/reasoning/context-synthesizer.js.map +1 -0
- package/dist/reasoning/context-synthesizer.mjs +219 -0
- package/dist/reasoning/experience-curator.d.ts +66 -0
- package/dist/reasoning/experience-curator.d.ts.map +1 -0
- package/dist/reasoning/experience-curator.js +288 -0
- package/dist/reasoning/experience-curator.js.map +1 -0
- package/dist/reasoning/experience-curator.mjs +283 -0
- package/dist/reasoning/memory-optimizer.d.ts +69 -0
- package/dist/reasoning/memory-optimizer.d.ts.map +1 -0
- package/dist/reasoning/memory-optimizer.js +331 -0
- package/dist/reasoning/memory-optimizer.js.map +1 -0
- package/dist/reasoning/memory-optimizer.mjs +326 -0
- package/dist/reasoning/pattern-matcher.d.ts +59 -0
- package/dist/reasoning/pattern-matcher.d.ts.map +1 -0
- package/dist/reasoning/pattern-matcher.js +229 -0
- package/dist/reasoning/pattern-matcher.js.map +1 -0
- package/dist/reasoning/pattern-matcher.mjs +224 -0
- package/dist/reasoningbank/adapter/agentdb-adapter.d.ts +118 -0
- package/dist/reasoningbank/adapter/agentdb-adapter.d.ts.map +1 -0
- package/dist/reasoningbank/adapter/agentdb-adapter.js +477 -0
- package/dist/reasoningbank/adapter/agentdb-adapter.js.map +1 -0
- package/dist/reasoningbank/adapter/types.d.ts +113 -0
- package/dist/reasoningbank/adapter/types.d.ts.map +1 -0
- package/dist/reasoningbank/adapter/types.js +9 -0
- package/dist/reasoningbank/adapter/types.js.map +1 -0
- package/dist/reasoningbank/cli/commands.d.ts +16 -0
- package/dist/reasoningbank/cli/commands.d.ts.map +1 -0
- package/dist/reasoningbank/cli/commands.js +272 -0
- package/dist/reasoningbank/cli/commands.js.map +1 -0
- package/dist/reasoningbank/mcp/agentdb-tools.d.ts +319 -0
- package/dist/reasoningbank/mcp/agentdb-tools.d.ts.map +1 -0
- package/dist/reasoningbank/mcp/agentdb-tools.js +301 -0
- package/dist/reasoningbank/mcp/agentdb-tools.js.map +1 -0
- package/dist/reasoningbank/migration/migrate.d.ts +25 -0
- package/dist/reasoningbank/migration/migrate.d.ts.map +1 -0
- package/dist/reasoningbank/migration/migrate.js +178 -0
- package/dist/reasoningbank/migration/migrate.js.map +1 -0
- package/dist/reasoningbank/reasoning/context-synthesizer.d.ts +37 -0
- package/dist/reasoningbank/reasoning/context-synthesizer.d.ts.map +1 -0
- package/dist/reasoningbank/reasoning/context-synthesizer.js +114 -0
- package/dist/reasoningbank/reasoning/context-synthesizer.js.map +1 -0
- package/dist/reasoningbank/reasoning/experience-curator.d.ts +39 -0
- package/dist/reasoningbank/reasoning/experience-curator.d.ts.map +1 -0
- package/dist/reasoningbank/reasoning/experience-curator.js +98 -0
- package/dist/reasoningbank/reasoning/experience-curator.js.map +1 -0
- package/dist/reasoningbank/reasoning/memory-optimizer.d.ts +44 -0
- package/dist/reasoningbank/reasoning/memory-optimizer.d.ts.map +1 -0
- package/dist/reasoningbank/reasoning/memory-optimizer.js +184 -0
- package/dist/reasoningbank/reasoning/memory-optimizer.js.map +1 -0
- package/dist/reasoningbank/reasoning/pattern-matcher.d.ts +40 -0
- package/dist/reasoningbank/reasoning/pattern-matcher.d.ts.map +1 -0
- package/dist/reasoningbank/reasoning/pattern-matcher.js +87 -0
- package/dist/reasoningbank/reasoning/pattern-matcher.js.map +1 -0
- package/dist/reasoningbank/sync/quic-sync.d.ts +77 -0
- package/dist/reasoningbank/sync/quic-sync.d.ts.map +1 -0
- package/dist/reasoningbank/sync/quic-sync.js +165 -0
- package/dist/reasoningbank/sync/quic-sync.js.map +1 -0
- package/dist/sync/conflict.d.ts +78 -0
- package/dist/sync/conflict.d.ts.map +1 -0
- package/dist/sync/conflict.js +202 -0
- package/dist/sync/conflict.js.map +1 -0
- package/dist/sync/conflict.mjs +196 -0
- package/dist/sync/coordinator.d.ts +111 -0
- package/dist/sync/coordinator.d.ts.map +1 -0
- package/dist/sync/coordinator.js +256 -0
- package/dist/sync/coordinator.js.map +1 -0
- package/dist/sync/coordinator.mjs +250 -0
- package/dist/sync/delta.d.ts +81 -0
- package/dist/sync/delta.d.ts.map +1 -0
- package/dist/sync/delta.js +245 -0
- package/dist/sync/delta.js.map +1 -0
- package/dist/sync/delta.mjs +238 -0
- package/dist/sync/index.d.ts +11 -0
- package/dist/sync/index.d.ts.map +1 -0
- package/dist/sync/index.js +22 -0
- package/dist/sync/index.js.map +1 -0
- package/dist/sync/index.mjs +9 -0
- package/dist/sync/quic-sync.d.ts +81 -0
- package/dist/sync/quic-sync.d.ts.map +1 -0
- package/dist/sync/quic-sync.js +329 -0
- package/dist/sync/quic-sync.js.map +1 -0
- package/dist/sync/quic-sync.mjs +323 -0
- package/dist/sync/types.d.ts +168 -0
- package/dist/sync/types.d.ts.map +1 -0
- package/dist/sync/types.js +8 -0
- package/dist/sync/types.js.map +1 -0
- package/dist/sync/types.mjs +6 -0
- package/dist/types/index.d.ts +117 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +6 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/index.mjs +4 -0
- package/dist/wasm-loader.d.ts +32 -0
- package/dist/wasm-loader.d.ts.map +1 -0
- package/dist/wasm-loader.js +75 -0
- package/dist/wasm-loader.js.map +1 -0
- package/dist/wasm-loader.mjs +64 -0
- package/examples/adaptive-learning.ts +284 -0
- package/examples/browser/README.md +732 -0
- package/examples/browser/adaptive-recommendations/index.html +427 -0
- package/examples/browser/collaborative-filtering/index.html +310 -0
- package/examples/browser/continual-learning/index.html +736 -0
- package/examples/browser/experience-replay/index.html +616 -0
- package/examples/browser/index.html +369 -0
- package/examples/browser/meta-learning/index.html +789 -0
- package/examples/browser/neuro-symbolic/index.html +692 -0
- package/examples/browser/pattern-learning/index.html +620 -0
- package/examples/browser/quantum-inspired/index.html +728 -0
- package/examples/browser/rag/index.html +624 -0
- package/examples/browser/swarm-intelligence/index.html +811 -0
- package/examples/browser-basic.html +170 -0
- package/examples/hnsw-example.ts +148 -0
- package/examples/node-basic.js +70 -0
- package/examples/quic-sync-example.ts +310 -0
- package/examples/quick-start.js +68 -0
- package/examples/wasm-example.ts +222 -0
- package/package.json +118 -0
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
|
+
<title>SQLiteVector Browser Example</title>
|
|
7
|
+
<style>
|
|
8
|
+
body {
|
|
9
|
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
|
10
|
+
max-width: 800px;
|
|
11
|
+
margin: 50px auto;
|
|
12
|
+
padding: 20px;
|
|
13
|
+
background: #f5f5f5;
|
|
14
|
+
}
|
|
15
|
+
h1 {
|
|
16
|
+
color: #333;
|
|
17
|
+
border-bottom: 2px solid #4CAF50;
|
|
18
|
+
padding-bottom: 10px;
|
|
19
|
+
}
|
|
20
|
+
.controls {
|
|
21
|
+
background: white;
|
|
22
|
+
padding: 20px;
|
|
23
|
+
border-radius: 8px;
|
|
24
|
+
margin-bottom: 20px;
|
|
25
|
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
|
26
|
+
}
|
|
27
|
+
button {
|
|
28
|
+
background: #4CAF50;
|
|
29
|
+
color: white;
|
|
30
|
+
border: none;
|
|
31
|
+
padding: 10px 20px;
|
|
32
|
+
border-radius: 4px;
|
|
33
|
+
cursor: pointer;
|
|
34
|
+
margin-right: 10px;
|
|
35
|
+
font-size: 14px;
|
|
36
|
+
}
|
|
37
|
+
button:hover {
|
|
38
|
+
background: #45a049;
|
|
39
|
+
}
|
|
40
|
+
#output {
|
|
41
|
+
background: white;
|
|
42
|
+
padding: 20px;
|
|
43
|
+
border-radius: 8px;
|
|
44
|
+
font-family: 'Courier New', monospace;
|
|
45
|
+
font-size: 13px;
|
|
46
|
+
white-space: pre-wrap;
|
|
47
|
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
|
48
|
+
max-height: 500px;
|
|
49
|
+
overflow-y: auto;
|
|
50
|
+
}
|
|
51
|
+
.success { color: #4CAF50; }
|
|
52
|
+
.error { color: #f44336; }
|
|
53
|
+
.info { color: #2196F3; }
|
|
54
|
+
</style>
|
|
55
|
+
</head>
|
|
56
|
+
<body>
|
|
57
|
+
<h1>🚀 SQLiteVector Browser Example</h1>
|
|
58
|
+
|
|
59
|
+
<div class="controls">
|
|
60
|
+
<button onclick="initDatabase()">Initialize DB</button>
|
|
61
|
+
<button onclick="insertVectors()">Insert Vectors</button>
|
|
62
|
+
<button onclick="searchVectors()">Search Similar</button>
|
|
63
|
+
<button onclick="getStats()">Show Stats</button>
|
|
64
|
+
<button onclick="clearOutput()">Clear Output</button>
|
|
65
|
+
</div>
|
|
66
|
+
|
|
67
|
+
<div id="output">Click buttons above to interact with SQLiteVector...</div>
|
|
68
|
+
|
|
69
|
+
<script type="module">
|
|
70
|
+
import { createDb, Vector } from '../dist/index.mjs';
|
|
71
|
+
|
|
72
|
+
let db = null;
|
|
73
|
+
|
|
74
|
+
function log(message, type = 'info') {
|
|
75
|
+
const output = document.getElementById('output');
|
|
76
|
+
const timestamp = new Date().toLocaleTimeString();
|
|
77
|
+
const colorClass = type === 'error' ? 'error' : type === 'success' ? 'success' : 'info';
|
|
78
|
+
output.innerHTML += `<span class="${colorClass}">[${timestamp}] ${message}</span>\n`;
|
|
79
|
+
output.scrollTop = output.scrollHeight;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
window.initDatabase = async function() {
|
|
83
|
+
try {
|
|
84
|
+
log('Initializing SQLiteVector database...', 'info');
|
|
85
|
+
db = await createDb({ memoryMode: true, dimension: 384 });
|
|
86
|
+
log('✓ Database initialized successfully!', 'success');
|
|
87
|
+
} catch (error) {
|
|
88
|
+
log(`✗ Error: ${error.message}`, 'error');
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
window.insertVectors = async function() {
|
|
93
|
+
if (!db) {
|
|
94
|
+
log('Please initialize database first!', 'error');
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
try {
|
|
99
|
+
log('Inserting sample vectors...', 'info');
|
|
100
|
+
|
|
101
|
+
const vectors = [
|
|
102
|
+
new Vector([0.1, 0.2, 0.3], { doc: 'Document 1', category: 'tech' }),
|
|
103
|
+
new Vector([0.4, 0.5, 0.6], { doc: 'Document 2', category: 'science' }),
|
|
104
|
+
new Vector([0.15, 0.25, 0.35], { doc: 'Document 3', category: 'tech' }),
|
|
105
|
+
new Vector([0.7, 0.8, 0.9], { doc: 'Document 4', category: 'math' }),
|
|
106
|
+
];
|
|
107
|
+
|
|
108
|
+
const ids = await db.insertBatch(vectors);
|
|
109
|
+
log(`✓ Inserted ${ids.length} vectors with IDs: ${ids.join(', ')}`, 'success');
|
|
110
|
+
} catch (error) {
|
|
111
|
+
log(`✗ Error: ${error.message}`, 'error');
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
window.searchVectors = async function() {
|
|
116
|
+
if (!db) {
|
|
117
|
+
log('Please initialize database first!', 'error');
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
try {
|
|
122
|
+
log('Searching for similar vectors...', 'info');
|
|
123
|
+
|
|
124
|
+
const query = new Vector([0.12, 0.22, 0.32]);
|
|
125
|
+
const results = await db.search(query, 3, { metric: 'cosine', threshold: 0.5 });
|
|
126
|
+
|
|
127
|
+
log(`Found ${results.length} similar vectors:`, 'info');
|
|
128
|
+
results.forEach((result, i) => {
|
|
129
|
+
log(` ${i + 1}. ID: ${result.id}, Score: ${result.score.toFixed(4)}, ` +
|
|
130
|
+
`Metadata: ${JSON.stringify(result.metadata)}`, 'info');
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
log('✓ Search completed', 'success');
|
|
134
|
+
} catch (error) {
|
|
135
|
+
log(`✗ Error: ${error.message}`, 'error');
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
window.getStats = async function() {
|
|
140
|
+
if (!db) {
|
|
141
|
+
log('Please initialize database first!', 'error');
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
try {
|
|
146
|
+
log('Fetching database statistics...', 'info');
|
|
147
|
+
|
|
148
|
+
const stats = await db.getStats();
|
|
149
|
+
log('Database Statistics:', 'info');
|
|
150
|
+
log(` Total Vectors: ${stats.totalVectors}`, 'info');
|
|
151
|
+
log(` Dimension: ${stats.dimension}`, 'info');
|
|
152
|
+
log(` Memory Usage: ${(stats.memoryUsage / 1024).toFixed(2)} KB`, 'info');
|
|
153
|
+
log(` DB Size: ${(stats.dbSize / 1024).toFixed(2)} KB`, 'info');
|
|
154
|
+
|
|
155
|
+
log('✓ Stats retrieved', 'success');
|
|
156
|
+
} catch (error) {
|
|
157
|
+
log(`✗ Error: ${error.message}`, 'error');
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
window.clearOutput = function() {
|
|
162
|
+
document.getElementById('output').innerHTML = 'Output cleared. Ready for new operations...\n';
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
// Auto-initialize on load
|
|
166
|
+
log('SQLiteVector Browser Example Ready', 'success');
|
|
167
|
+
log('Click "Initialize DB" to begin', 'info');
|
|
168
|
+
</script>
|
|
169
|
+
</body>
|
|
170
|
+
</html>
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HNSW Index Example
|
|
3
|
+
* Demonstrates high-performance vector search with HNSW indexing
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { NativeBackend } from '../src/core/native-backend';
|
|
7
|
+
import { Vector } from '../src/types';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Generate random vector with specified dimensions
|
|
11
|
+
*/
|
|
12
|
+
function generateRandomVector(dim: number): number[] {
|
|
13
|
+
const vector = new Array(dim);
|
|
14
|
+
for (let i = 0; i < dim; i++) {
|
|
15
|
+
vector[i] = Math.random() * 2 - 1;
|
|
16
|
+
}
|
|
17
|
+
const norm = Math.sqrt(vector.reduce((sum, val) => sum + val * val, 0));
|
|
18
|
+
return vector.map(v => v / norm);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Main example
|
|
23
|
+
*/
|
|
24
|
+
async function main() {
|
|
25
|
+
console.log('=== HNSW Index Performance Demo ===\n');
|
|
26
|
+
|
|
27
|
+
// Create backend with HNSW enabled
|
|
28
|
+
const backend = new NativeBackend();
|
|
29
|
+
backend.initialize({
|
|
30
|
+
memoryMode: true,
|
|
31
|
+
hnsw: {
|
|
32
|
+
enabled: true,
|
|
33
|
+
M: 16, // Connections per node
|
|
34
|
+
efConstruction: 200, // Build quality
|
|
35
|
+
efSearch: 50, // Search quality
|
|
36
|
+
minVectorsForIndex: 1000 // Minimum vectors to enable index
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// Generate test dataset
|
|
41
|
+
console.log('Generating 10,000 test vectors (128 dimensions)...');
|
|
42
|
+
const VECTOR_DIM = 128;
|
|
43
|
+
const NUM_VECTORS = 10000;
|
|
44
|
+
const vectors: Vector[] = [];
|
|
45
|
+
|
|
46
|
+
for (let i = 0; i < NUM_VECTORS; i++) {
|
|
47
|
+
vectors.push({
|
|
48
|
+
id: `vec_${i}`,
|
|
49
|
+
embedding: generateRandomVector(VECTOR_DIM),
|
|
50
|
+
metadata: {
|
|
51
|
+
index: i,
|
|
52
|
+
category: `group_${Math.floor(i / 100)}`
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Insert vectors and build index
|
|
58
|
+
console.log('Inserting vectors and building HNSW index...');
|
|
59
|
+
const insertStart = Date.now();
|
|
60
|
+
backend.insertBatch(vectors);
|
|
61
|
+
const insertTime = Date.now() - insertStart;
|
|
62
|
+
|
|
63
|
+
console.log(`✓ Inserted ${NUM_VECTORS} vectors in ${insertTime}ms`);
|
|
64
|
+
console.log(` (${(insertTime / NUM_VECTORS).toFixed(2)}ms per vector)\n`);
|
|
65
|
+
|
|
66
|
+
// Check index stats
|
|
67
|
+
const hnswStats = backend.getHNSWStats();
|
|
68
|
+
if (hnswStats) {
|
|
69
|
+
console.log('HNSW Index Statistics:');
|
|
70
|
+
console.log(` - Enabled: ${hnswStats.enabled}`);
|
|
71
|
+
console.log(` - Ready: ${hnswStats.ready}`);
|
|
72
|
+
console.log(` - Nodes: ${hnswStats.nodeCount}`);
|
|
73
|
+
console.log(` - Edges: ${hnswStats.edgeCount}`);
|
|
74
|
+
console.log(` - Max Level: ${hnswStats.maxLevel}`);
|
|
75
|
+
console.log(` - Avg Connections: ${hnswStats.avgDegree.toFixed(2)}\n`);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Perform search benchmarks
|
|
79
|
+
console.log('Benchmarking search performance...');
|
|
80
|
+
const NUM_QUERIES = 100;
|
|
81
|
+
const K = 10;
|
|
82
|
+
|
|
83
|
+
// Generate query vectors
|
|
84
|
+
const queries = Array.from({ length: NUM_QUERIES }, () =>
|
|
85
|
+
generateRandomVector(VECTOR_DIM)
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
// Warm up
|
|
89
|
+
backend.search(queries[0], K, 'euclidean', 0);
|
|
90
|
+
|
|
91
|
+
// Benchmark HNSW search
|
|
92
|
+
const hnswStart = performance.now();
|
|
93
|
+
for (const query of queries) {
|
|
94
|
+
backend.search(query, K, 'euclidean', 0);
|
|
95
|
+
}
|
|
96
|
+
const hnswTime = performance.now() - hnswStart;
|
|
97
|
+
const avgHnswTime = hnswTime / NUM_QUERIES;
|
|
98
|
+
|
|
99
|
+
console.log(`\n✓ HNSW Search (${NUM_QUERIES} queries):`);
|
|
100
|
+
console.log(` - Total time: ${hnswTime.toFixed(2)}ms`);
|
|
101
|
+
console.log(` - Average: ${avgHnswTime.toFixed(2)}ms per query`);
|
|
102
|
+
|
|
103
|
+
// Compare with brute-force
|
|
104
|
+
console.log('\nComparing with brute-force search...');
|
|
105
|
+
const bruteBackend = new NativeBackend();
|
|
106
|
+
bruteBackend.initialize({
|
|
107
|
+
memoryMode: true,
|
|
108
|
+
hnsw: { enabled: false }
|
|
109
|
+
});
|
|
110
|
+
bruteBackend.insertBatch(vectors);
|
|
111
|
+
|
|
112
|
+
const bruteStart = performance.now();
|
|
113
|
+
for (const query of queries) {
|
|
114
|
+
bruteBackend.search(query, K, 'euclidean', 0);
|
|
115
|
+
}
|
|
116
|
+
const bruteTime = performance.now() - bruteStart;
|
|
117
|
+
const avgBruteTime = bruteTime / NUM_QUERIES;
|
|
118
|
+
|
|
119
|
+
console.log(`\n✓ Brute-force Search (${NUM_QUERIES} queries):`);
|
|
120
|
+
console.log(` - Total time: ${bruteTime.toFixed(2)}ms`);
|
|
121
|
+
console.log(` - Average: ${avgBruteTime.toFixed(2)}ms per query`);
|
|
122
|
+
|
|
123
|
+
const speedup = bruteTime / hnswTime;
|
|
124
|
+
console.log(`\n✓ Performance Improvement:`);
|
|
125
|
+
console.log(` - Speedup: ${speedup.toFixed(2)}x faster`);
|
|
126
|
+
console.log(` - Time saved: ${(bruteTime - hnswTime).toFixed(2)}ms`);
|
|
127
|
+
|
|
128
|
+
// Demonstrate search results
|
|
129
|
+
console.log('\n=== Sample Search Results ===');
|
|
130
|
+
const sampleQuery = generateRandomVector(VECTOR_DIM);
|
|
131
|
+
const results = backend.search(sampleQuery, 5, 'euclidean', 0);
|
|
132
|
+
|
|
133
|
+
console.log('\nTop 5 nearest neighbors:');
|
|
134
|
+
results.forEach((result, idx) => {
|
|
135
|
+
console.log(` ${idx + 1}. ID: ${result.id}`);
|
|
136
|
+
console.log(` Distance: ${result.score.toFixed(4)}`);
|
|
137
|
+
console.log(` Metadata: ${JSON.stringify(result.metadata)}`);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
// Cleanup
|
|
141
|
+
backend.close();
|
|
142
|
+
bruteBackend.close();
|
|
143
|
+
|
|
144
|
+
console.log('\n=== Demo Complete ===');
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// Run example
|
|
148
|
+
main().catch(console.error);
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Basic Node.js usage example
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
const { createDb, Vector } = require('../dist/index.js');
|
|
6
|
+
|
|
7
|
+
async function main() {
|
|
8
|
+
console.log('SQLiteVector - Node.js Example\n');
|
|
9
|
+
|
|
10
|
+
// Create in-memory database
|
|
11
|
+
console.log('Creating database...');
|
|
12
|
+
const db = await createDb({ memoryMode: true });
|
|
13
|
+
|
|
14
|
+
// Insert vectors
|
|
15
|
+
console.log('Inserting vectors...');
|
|
16
|
+
const vector1 = new Vector([0.1, 0.2, 0.3], { doc: 'First document' });
|
|
17
|
+
const vector2 = new Vector([0.4, 0.5, 0.6], { doc: 'Second document' });
|
|
18
|
+
const vector3 = new Vector([0.15, 0.25, 0.35], { doc: 'Similar to first' });
|
|
19
|
+
|
|
20
|
+
const id1 = await db.insert(vector1);
|
|
21
|
+
const id2 = await db.insert(vector2);
|
|
22
|
+
const id3 = await db.insert(vector3);
|
|
23
|
+
|
|
24
|
+
console.log(`Inserted vectors with IDs: ${id1}, ${id2}, ${id3}`);
|
|
25
|
+
|
|
26
|
+
// Batch insert
|
|
27
|
+
console.log('\nBatch inserting vectors...');
|
|
28
|
+
const batchVectors = [
|
|
29
|
+
new Vector([0.7, 0.8, 0.9], { doc: 'Batch 1' }),
|
|
30
|
+
new Vector([0.2, 0.3, 0.4], { doc: 'Batch 2' }),
|
|
31
|
+
];
|
|
32
|
+
const batchIds = await db.insertBatch(batchVectors);
|
|
33
|
+
console.log(`Batch inserted IDs: ${batchIds.join(', ')}`);
|
|
34
|
+
|
|
35
|
+
// Search for similar vectors
|
|
36
|
+
console.log('\nSearching for similar vectors...');
|
|
37
|
+
const query = new Vector([0.12, 0.22, 0.32]);
|
|
38
|
+
const results = await db.search(query, 3, { metric: 'cosine' });
|
|
39
|
+
|
|
40
|
+
console.log('Search results:');
|
|
41
|
+
results.forEach((result, i) => {
|
|
42
|
+
console.log(` ${i + 1}. ID: ${result.id}, Score: ${result.score.toFixed(4)}, Metadata:`, result.metadata);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
// Get statistics
|
|
46
|
+
console.log('\nDatabase statistics:');
|
|
47
|
+
const stats = await db.getStats();
|
|
48
|
+
console.log(` Total vectors: ${stats.totalVectors}`);
|
|
49
|
+
console.log(` Dimension: ${stats.dimension}`);
|
|
50
|
+
console.log(` Memory usage: ${(stats.memoryUsage / 1024).toFixed(2)} KB`);
|
|
51
|
+
console.log(` DB size: ${(stats.dbSize / 1024).toFixed(2)} KB`);
|
|
52
|
+
|
|
53
|
+
// Update a vector
|
|
54
|
+
console.log('\nUpdating vector...');
|
|
55
|
+
const updated = await db.update(id1, new Vector([0.11, 0.21, 0.31], { doc: 'Updated first' }));
|
|
56
|
+
console.log(`Update successful: ${updated}`);
|
|
57
|
+
|
|
58
|
+
// Delete a vector
|
|
59
|
+
console.log('\nDeleting vector...');
|
|
60
|
+
const deleted = await db.delete(id2);
|
|
61
|
+
console.log(`Delete successful: ${deleted}`);
|
|
62
|
+
|
|
63
|
+
// Final stats
|
|
64
|
+
const finalStats = await db.getStats();
|
|
65
|
+
console.log(`\nFinal vector count: ${finalStats.totalVectors}`);
|
|
66
|
+
|
|
67
|
+
console.log('\nExample completed successfully!');
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
main().catch(console.error);
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SQLiteVector QUIC Sync - Complete Example
|
|
3
|
+
*
|
|
4
|
+
* Demonstrates real-time vector synchronization using QUIC
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import Database from 'better-sqlite3';
|
|
8
|
+
import { createVectorSync, createShardCoordinator } from '../src/sync';
|
|
9
|
+
|
|
10
|
+
// Initialize database with changelog schema
|
|
11
|
+
function setupDatabase(dbPath: string): any {
|
|
12
|
+
const db = new Database(dbPath);
|
|
13
|
+
|
|
14
|
+
// Create vector changelog table
|
|
15
|
+
db.exec(`
|
|
16
|
+
CREATE TABLE IF NOT EXISTS vector_changelog (
|
|
17
|
+
change_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
18
|
+
shard_id TEXT NOT NULL,
|
|
19
|
+
vector_id TEXT NOT NULL,
|
|
20
|
+
operation TEXT NOT NULL,
|
|
21
|
+
vector_data TEXT,
|
|
22
|
+
metadata TEXT,
|
|
23
|
+
timestamp INTEGER NOT NULL,
|
|
24
|
+
source_node TEXT NOT NULL,
|
|
25
|
+
version_vector TEXT NOT NULL,
|
|
26
|
+
INDEX idx_shard_change (shard_id, change_id)
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
CREATE TABLE IF NOT EXISTS shard_metadata (
|
|
30
|
+
shard_id TEXT PRIMARY KEY,
|
|
31
|
+
version_vector TEXT NOT NULL,
|
|
32
|
+
updated_at INTEGER NOT NULL
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
CREATE TABLE IF NOT EXISTS vectors (
|
|
36
|
+
id TEXT PRIMARY KEY,
|
|
37
|
+
shard_id TEXT NOT NULL,
|
|
38
|
+
vector BLOB NOT NULL,
|
|
39
|
+
metadata TEXT,
|
|
40
|
+
created_at INTEGER NOT NULL,
|
|
41
|
+
updated_at INTEGER NOT NULL
|
|
42
|
+
);
|
|
43
|
+
`);
|
|
44
|
+
|
|
45
|
+
return db;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Example 1: Basic Two-Node Sync
|
|
49
|
+
async function basicSyncExample() {
|
|
50
|
+
console.log('🚀 Example 1: Basic Two-Node Sync\n');
|
|
51
|
+
|
|
52
|
+
const db1 = setupDatabase(':memory:');
|
|
53
|
+
const db2 = setupDatabase(':memory:');
|
|
54
|
+
|
|
55
|
+
// Create sync instances
|
|
56
|
+
const sync1 = await createVectorSync(db1, 'node-1', {
|
|
57
|
+
conflictStrategy: 'last-write-wins',
|
|
58
|
+
batchSize: 100,
|
|
59
|
+
compression: true
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
const sync2 = await createVectorSync(db2, 'node-2', {
|
|
63
|
+
conflictStrategy: 'last-write-wins',
|
|
64
|
+
batchSize: 100,
|
|
65
|
+
compression: true
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
console.log('✓ Initialized sync on node-1 and node-2');
|
|
69
|
+
|
|
70
|
+
// Simulate changes on node-1
|
|
71
|
+
db1.prepare(`
|
|
72
|
+
INSERT INTO vector_changelog
|
|
73
|
+
(shard_id, vector_id, operation, vector_data, timestamp, source_node, version_vector)
|
|
74
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
75
|
+
`).run(
|
|
76
|
+
'shard-1',
|
|
77
|
+
'vec-1',
|
|
78
|
+
'insert',
|
|
79
|
+
JSON.stringify([1.0, 2.0, 3.0]),
|
|
80
|
+
Date.now() * 1000,
|
|
81
|
+
'node-1',
|
|
82
|
+
JSON.stringify({ 'node-1': 1 })
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
console.log('✓ Created change on node-1');
|
|
86
|
+
|
|
87
|
+
// Note: This would require a running QUIC server
|
|
88
|
+
// For demonstration, we show the API:
|
|
89
|
+
// const result = await sync1.sync('shard-1', '192.168.1.100:4433');
|
|
90
|
+
// console.log(`✓ Synced ${result.changesApplied} changes`);
|
|
91
|
+
|
|
92
|
+
await sync1.close();
|
|
93
|
+
await sync2.close();
|
|
94
|
+
|
|
95
|
+
console.log('\n✅ Example 1 complete\n');
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Example 2: Multi-Shard Mesh Network
|
|
99
|
+
async function meshNetworkExample() {
|
|
100
|
+
console.log('🌐 Example 2: Multi-Shard Mesh Network\n');
|
|
101
|
+
|
|
102
|
+
const db = setupDatabase(':memory:');
|
|
103
|
+
const coordinator = await createShardCoordinator(db, 'coordinator-node', {}, 10);
|
|
104
|
+
|
|
105
|
+
// Register 4 peers in mesh topology
|
|
106
|
+
const peers = [
|
|
107
|
+
{ nodeId: 'node-1', address: '192.168.1.100:4433', shards: ['shard-1', 'shard-2'] },
|
|
108
|
+
{ nodeId: 'node-2', address: '192.168.1.101:4433', shards: ['shard-2', 'shard-3'] },
|
|
109
|
+
{ nodeId: 'node-3', address: '192.168.1.102:4433', shards: ['shard-1', 'shard-3'] },
|
|
110
|
+
{ nodeId: 'node-4', address: '192.168.1.103:4433', shards: ['shard-1', 'shard-2', 'shard-3'] }
|
|
111
|
+
];
|
|
112
|
+
|
|
113
|
+
for (const peer of peers) {
|
|
114
|
+
coordinator.registerPeer({
|
|
115
|
+
...peer,
|
|
116
|
+
lastContact: Date.now(),
|
|
117
|
+
status: 'online'
|
|
118
|
+
});
|
|
119
|
+
console.log(`✓ Registered peer: ${peer.nodeId} (${peer.shards.join(', ')})`);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
console.log('\n📊 Mesh Network:');
|
|
123
|
+
console.log(' - 4 peers');
|
|
124
|
+
console.log(' - 3 shards');
|
|
125
|
+
console.log(' - Replicated across all nodes');
|
|
126
|
+
|
|
127
|
+
// Show stats
|
|
128
|
+
const stats = coordinator.getStats();
|
|
129
|
+
console.log(`\n📈 Stats:`);
|
|
130
|
+
console.log(` - Total shards: ${stats.totalShards}`);
|
|
131
|
+
console.log(` - Total syncs: ${stats.totalSyncs}`);
|
|
132
|
+
|
|
133
|
+
// Get all shard states
|
|
134
|
+
const states = await coordinator.getAllShardStates();
|
|
135
|
+
console.log(`\n📋 Shard States:`);
|
|
136
|
+
for (const state of states) {
|
|
137
|
+
console.log(` - ${state.shardId}: ${state.status} (change ID: ${state.currentChangeId})`);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
console.log('\n✅ Example 2 complete\n');
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Example 3: Conflict Resolution Strategies
|
|
144
|
+
async function conflictResolutionExample() {
|
|
145
|
+
console.log('⚔️ Example 3: Conflict Resolution Strategies\n');
|
|
146
|
+
|
|
147
|
+
const db = setupDatabase(':memory:');
|
|
148
|
+
|
|
149
|
+
// Last-Write-Wins
|
|
150
|
+
console.log('Strategy 1: Last-Write-Wins');
|
|
151
|
+
const syncLWW = await createVectorSync(db, 'node-lww', {
|
|
152
|
+
conflictStrategy: 'last-write-wins'
|
|
153
|
+
});
|
|
154
|
+
console.log(' ✓ Newest change always wins');
|
|
155
|
+
await syncLWW.close();
|
|
156
|
+
|
|
157
|
+
// First-Write-Wins
|
|
158
|
+
console.log('\nStrategy 2: First-Write-Wins');
|
|
159
|
+
const syncFWW = await createVectorSync(db, 'node-fww', {
|
|
160
|
+
conflictStrategy: 'first-write-wins'
|
|
161
|
+
});
|
|
162
|
+
console.log(' ✓ Oldest change always wins');
|
|
163
|
+
await syncFWW.close();
|
|
164
|
+
|
|
165
|
+
// Merge
|
|
166
|
+
console.log('\nStrategy 3: Merge');
|
|
167
|
+
const syncMerge = await createVectorSync(db, 'node-merge', {
|
|
168
|
+
conflictStrategy: 'merge'
|
|
169
|
+
});
|
|
170
|
+
console.log(' ✓ Metadata merged, vectors averaged');
|
|
171
|
+
await syncMerge.close();
|
|
172
|
+
|
|
173
|
+
// Manual
|
|
174
|
+
console.log('\nStrategy 4: Manual');
|
|
175
|
+
const syncManual = await createVectorSync(db, 'node-manual', {
|
|
176
|
+
conflictStrategy: 'manual'
|
|
177
|
+
});
|
|
178
|
+
console.log(' ✓ Conflicts tracked for custom resolution');
|
|
179
|
+
|
|
180
|
+
// Show unresolved conflicts
|
|
181
|
+
const conflicts = syncManual.getUnresolvedConflicts();
|
|
182
|
+
console.log(` ✓ Unresolved conflicts: ${conflicts.length}`);
|
|
183
|
+
|
|
184
|
+
await syncManual.close();
|
|
185
|
+
|
|
186
|
+
console.log('\n✅ Example 3 complete\n');
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Example 4: Real-Time Auto-Sync
|
|
190
|
+
async function realTimeSyncExample() {
|
|
191
|
+
console.log('⚡ Example 4: Real-Time Auto-Sync\n');
|
|
192
|
+
|
|
193
|
+
const db = setupDatabase(':memory:');
|
|
194
|
+
|
|
195
|
+
const sync = await createVectorSync(db, 'realtime-node', {
|
|
196
|
+
conflictStrategy: 'last-write-wins',
|
|
197
|
+
syncIntervalMs: 1000 // Sync every second
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
console.log('✓ Initialized real-time sync (1 second interval)');
|
|
201
|
+
|
|
202
|
+
// Note: This would start automatic background sync
|
|
203
|
+
// sync.startAutoSync('shard-1', '192.168.1.100:4433');
|
|
204
|
+
// console.log('✓ Started automatic sync for shard-1');
|
|
205
|
+
|
|
206
|
+
// Monitor sync progress
|
|
207
|
+
console.log('\n📊 Monitoring (simulated):');
|
|
208
|
+
const state = await sync.getShardState('shard-1');
|
|
209
|
+
console.log(` - Shard: ${state.shardId}`);
|
|
210
|
+
console.log(` - Status: ${state.status}`);
|
|
211
|
+
console.log(` - Current change ID: ${state.currentChangeId}`);
|
|
212
|
+
console.log(` - Vector count: ${state.vectorCount}`);
|
|
213
|
+
|
|
214
|
+
await sync.close();
|
|
215
|
+
|
|
216
|
+
console.log('\n✅ Example 4 complete\n');
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// Example 5: Performance Demonstration
|
|
220
|
+
async function performanceExample() {
|
|
221
|
+
console.log('🚀 Example 5: Performance Demonstration\n');
|
|
222
|
+
|
|
223
|
+
const { DeltaEncoder } = await import('../src/sync/delta');
|
|
224
|
+
const { ConflictResolver } = await import('../src/sync/conflict');
|
|
225
|
+
|
|
226
|
+
// Create test data
|
|
227
|
+
const changes = Array.from({ length: 100 }, (_, i) => ({
|
|
228
|
+
id: i + 1,
|
|
229
|
+
operation: 'insert' as const,
|
|
230
|
+
shardId: 'perf-shard',
|
|
231
|
+
vectorId: `vec-${i}`,
|
|
232
|
+
vector: new Float32Array(128).fill(Math.random()),
|
|
233
|
+
metadata: { index: i },
|
|
234
|
+
timestamp: Date.now() * 1000 + i,
|
|
235
|
+
sourceNode: 'perf-node',
|
|
236
|
+
versionVector: new Map([['perf-node', i + 1]])
|
|
237
|
+
}));
|
|
238
|
+
|
|
239
|
+
// Benchmark delta encoding
|
|
240
|
+
console.log('📊 Delta Encoding (100 vectors):');
|
|
241
|
+
const encodeStart = performance.now();
|
|
242
|
+
const delta = DeltaEncoder.encode('perf-shard', changes, 'msgpack');
|
|
243
|
+
const encodeTime = performance.now() - encodeStart;
|
|
244
|
+
console.log(` ✓ Encode: ${encodeTime.toFixed(2)}ms (target: <10ms)`);
|
|
245
|
+
|
|
246
|
+
// Benchmark serialization
|
|
247
|
+
const serializeStart = performance.now();
|
|
248
|
+
const bytes = DeltaEncoder.serialize(delta);
|
|
249
|
+
const serializeTime = performance.now() - serializeStart;
|
|
250
|
+
console.log(` ✓ Serialize: ${serializeTime.toFixed(2)}ms (target: <5ms)`);
|
|
251
|
+
console.log(` ✓ Compressed size: ${bytes.length} bytes`);
|
|
252
|
+
|
|
253
|
+
// Benchmark deserialization
|
|
254
|
+
const deserializeStart = performance.now();
|
|
255
|
+
const deserialized = DeltaEncoder.deserialize(bytes);
|
|
256
|
+
const deserializeTime = performance.now() - deserializeStart;
|
|
257
|
+
console.log(` ✓ Deserialize: ${deserializeTime.toFixed(2)}ms (target: <5ms)`);
|
|
258
|
+
|
|
259
|
+
// Benchmark conflict resolution
|
|
260
|
+
console.log('\n⚔️ Conflict Resolution (100 conflicts):');
|
|
261
|
+
const resolver = new ConflictResolver('last-write-wins');
|
|
262
|
+
const remote = changes.map(c => ({
|
|
263
|
+
...c,
|
|
264
|
+
id: c.id + 1000,
|
|
265
|
+
sourceNode: 'remote-node'
|
|
266
|
+
}));
|
|
267
|
+
|
|
268
|
+
const resolveStart = performance.now();
|
|
269
|
+
const result = resolver.resolveAll(changes, remote);
|
|
270
|
+
const resolveTime = performance.now() - resolveStart;
|
|
271
|
+
console.log(` ✓ Resolve: ${resolveTime.toFixed(2)}ms (target: <5ms)`);
|
|
272
|
+
console.log(` ✓ Resolved: ${result.resolved.length} changes`);
|
|
273
|
+
|
|
274
|
+
console.log('\n✅ Example 5 complete\n');
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// Main execution
|
|
278
|
+
async function main() {
|
|
279
|
+
console.log('═══════════════════════════════════════════════════════');
|
|
280
|
+
console.log(' SQLiteVector QUIC Sync - Complete Examples');
|
|
281
|
+
console.log('═══════════════════════════════════════════════════════\n');
|
|
282
|
+
|
|
283
|
+
try {
|
|
284
|
+
await basicSyncExample();
|
|
285
|
+
await meshNetworkExample();
|
|
286
|
+
await conflictResolutionExample();
|
|
287
|
+
await realTimeSyncExample();
|
|
288
|
+
await performanceExample();
|
|
289
|
+
|
|
290
|
+
console.log('═══════════════════════════════════════════════════════');
|
|
291
|
+
console.log(' ✅ All examples completed successfully!');
|
|
292
|
+
console.log('═══════════════════════════════════════════════════════\n');
|
|
293
|
+
} catch (error) {
|
|
294
|
+
console.error('❌ Error running examples:', error);
|
|
295
|
+
process.exit(1);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// Run if executed directly
|
|
300
|
+
if (require.main === module) {
|
|
301
|
+
main();
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
export {
|
|
305
|
+
basicSyncExample,
|
|
306
|
+
meshNetworkExample,
|
|
307
|
+
conflictResolutionExample,
|
|
308
|
+
realTimeSyncExample,
|
|
309
|
+
performanceExample
|
|
310
|
+
};
|