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,196 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SQLiteVector QUIC Sync - Conflict Resolution
|
|
3
|
+
*
|
|
4
|
+
* Strategies for resolving synchronization conflicts
|
|
5
|
+
*/
|
|
6
|
+
import { VersionVector } from './delta.mjs';
|
|
7
|
+
/**
|
|
8
|
+
* Conflict resolver
|
|
9
|
+
*/
|
|
10
|
+
export class ConflictResolver {
|
|
11
|
+
constructor(strategy = 'last-write-wins') {
|
|
12
|
+
this.strategy = strategy;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Resolve conflict between local and remote changes
|
|
16
|
+
*/
|
|
17
|
+
resolve(local, remote) {
|
|
18
|
+
// Check if it's actually a conflict
|
|
19
|
+
if (!this.isConflict(local, remote)) {
|
|
20
|
+
// Use version vector to determine ordering
|
|
21
|
+
const comparison = VersionVector.compare(local.versionVector, remote.versionVector);
|
|
22
|
+
return {
|
|
23
|
+
winner: comparison >= 0 ? local : remote
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
// Detect conflict
|
|
27
|
+
const conflict = {
|
|
28
|
+
id: `conflict-${local.vectorId}-${Date.now()}`,
|
|
29
|
+
shardId: local.shardId,
|
|
30
|
+
vectorId: local.vectorId,
|
|
31
|
+
localChange: local,
|
|
32
|
+
remoteChange: remote,
|
|
33
|
+
detectedAt: Date.now() * 1000 // microseconds
|
|
34
|
+
};
|
|
35
|
+
// Apply strategy
|
|
36
|
+
let winner;
|
|
37
|
+
switch (this.strategy) {
|
|
38
|
+
case 'last-write-wins':
|
|
39
|
+
winner = this.lastWriteWins(local, remote);
|
|
40
|
+
break;
|
|
41
|
+
case 'first-write-wins':
|
|
42
|
+
winner = this.firstWriteWins(local, remote);
|
|
43
|
+
break;
|
|
44
|
+
case 'merge':
|
|
45
|
+
winner = this.merge(local, remote);
|
|
46
|
+
break;
|
|
47
|
+
case 'manual':
|
|
48
|
+
// Return conflict for manual resolution
|
|
49
|
+
return { winner: local, conflict };
|
|
50
|
+
default:
|
|
51
|
+
winner = this.lastWriteWins(local, remote);
|
|
52
|
+
}
|
|
53
|
+
return { winner, conflict };
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Check if two changes conflict
|
|
57
|
+
*/
|
|
58
|
+
isConflict(local, remote) {
|
|
59
|
+
// Same vector ID with concurrent version vectors = conflict
|
|
60
|
+
if (local.vectorId !== remote.vectorId) {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
const comparison = VersionVector.compare(local.versionVector, remote.versionVector);
|
|
64
|
+
// Concurrent changes = conflict
|
|
65
|
+
return comparison === 0;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Last-write-wins strategy
|
|
69
|
+
*/
|
|
70
|
+
lastWriteWins(local, remote) {
|
|
71
|
+
return local.timestamp >= remote.timestamp ? local : remote;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* First-write-wins strategy
|
|
75
|
+
*/
|
|
76
|
+
firstWriteWins(local, remote) {
|
|
77
|
+
return local.timestamp <= remote.timestamp ? local : remote;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Merge strategy (for metadata and vectors)
|
|
81
|
+
*/
|
|
82
|
+
merge(local, remote) {
|
|
83
|
+
// For deletes, prefer the delete operation
|
|
84
|
+
if (local.operation === 'delete' || remote.operation === 'delete') {
|
|
85
|
+
return local.operation === 'delete' ? local : remote;
|
|
86
|
+
}
|
|
87
|
+
// Merge metadata
|
|
88
|
+
const mergedMetadata = {
|
|
89
|
+
...remote.metadata,
|
|
90
|
+
...local.metadata // Local takes precedence
|
|
91
|
+
};
|
|
92
|
+
// For vectors, use element-wise average
|
|
93
|
+
let mergedVector;
|
|
94
|
+
if (local.vector && remote.vector) {
|
|
95
|
+
const len = Math.min(local.vector.length, remote.vector.length);
|
|
96
|
+
mergedVector = new Float32Array(len);
|
|
97
|
+
for (let i = 0; i < len; i++) {
|
|
98
|
+
mergedVector[i] = (local.vector[i] + remote.vector[i]) / 2;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
mergedVector = local.vector || remote.vector;
|
|
103
|
+
}
|
|
104
|
+
// Merge version vectors
|
|
105
|
+
const mergedVV = VersionVector.merge(local.versionVector, remote.versionVector);
|
|
106
|
+
return {
|
|
107
|
+
...local,
|
|
108
|
+
metadata: mergedMetadata,
|
|
109
|
+
vector: mergedVector,
|
|
110
|
+
versionVector: mergedVV,
|
|
111
|
+
timestamp: Math.max(local.timestamp, remote.timestamp)
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Batch resolve multiple conflicts
|
|
116
|
+
*/
|
|
117
|
+
resolveAll(localChanges, remoteChanges) {
|
|
118
|
+
const resolved = [];
|
|
119
|
+
const conflicts = [];
|
|
120
|
+
// Build maps for efficient lookup
|
|
121
|
+
const localMap = new Map(localChanges.map(c => [c.vectorId, c]));
|
|
122
|
+
const remoteMap = new Map(remoteChanges.map(c => [c.vectorId, c]));
|
|
123
|
+
// Get all vector IDs
|
|
124
|
+
const allVectorIds = new Set([
|
|
125
|
+
...localMap.keys(),
|
|
126
|
+
...remoteMap.keys()
|
|
127
|
+
]);
|
|
128
|
+
for (const vectorId of allVectorIds) {
|
|
129
|
+
const local = localMap.get(vectorId);
|
|
130
|
+
const remote = remoteMap.get(vectorId);
|
|
131
|
+
if (local && remote) {
|
|
132
|
+
// Potential conflict
|
|
133
|
+
const result = this.resolve(local, remote);
|
|
134
|
+
resolved.push(result.winner);
|
|
135
|
+
if (result.conflict) {
|
|
136
|
+
conflicts.push(result.conflict);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
// No conflict - add the change that exists
|
|
141
|
+
resolved.push((local || remote));
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return { resolved, conflicts };
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Change conflict resolution strategy
|
|
148
|
+
*/
|
|
149
|
+
setStrategy(strategy) {
|
|
150
|
+
this.strategy = strategy;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Conflict tracker for manual resolution
|
|
155
|
+
*/
|
|
156
|
+
export class ConflictTracker {
|
|
157
|
+
constructor() {
|
|
158
|
+
this.conflicts = new Map();
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Add conflict for manual resolution
|
|
162
|
+
*/
|
|
163
|
+
addConflict(conflict) {
|
|
164
|
+
this.conflicts.set(conflict.id, conflict);
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Get all unresolved conflicts
|
|
168
|
+
*/
|
|
169
|
+
getUnresolved() {
|
|
170
|
+
return Array.from(this.conflicts.values());
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Get conflicts for specific shard
|
|
174
|
+
*/
|
|
175
|
+
getForShard(shardId) {
|
|
176
|
+
return Array.from(this.conflicts.values()).filter(c => c.shardId === shardId);
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Resolve conflict manually
|
|
180
|
+
*/
|
|
181
|
+
resolveConflict(conflictId, winner) {
|
|
182
|
+
this.conflicts.delete(conflictId);
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Clear all conflicts
|
|
186
|
+
*/
|
|
187
|
+
clear() {
|
|
188
|
+
this.conflicts.clear();
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Get conflict count
|
|
192
|
+
*/
|
|
193
|
+
count() {
|
|
194
|
+
return this.conflicts.size;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SQLiteVector QUIC Sync - Multi-Shard Coordinator
|
|
3
|
+
*
|
|
4
|
+
* Orchestrates synchronization across multiple shards and peers
|
|
5
|
+
*/
|
|
6
|
+
import { VectorQuicSync } from './quic-sync';
|
|
7
|
+
import type { SyncResult, SyncConfig, ShardState, CoordinatorStats } from './types';
|
|
8
|
+
/**
|
|
9
|
+
* Peer connection info
|
|
10
|
+
*/
|
|
11
|
+
export interface PeerInfo {
|
|
12
|
+
/** Peer node ID */
|
|
13
|
+
nodeId: string;
|
|
14
|
+
/** Peer QUIC address */
|
|
15
|
+
address: string;
|
|
16
|
+
/** Shards available on peer */
|
|
17
|
+
shards: string[];
|
|
18
|
+
/** Last contact timestamp */
|
|
19
|
+
lastContact: number;
|
|
20
|
+
/** Connection status */
|
|
21
|
+
status: 'online' | 'offline' | 'unknown';
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Multi-shard synchronization coordinator
|
|
25
|
+
*/
|
|
26
|
+
export declare class ShardCoordinator {
|
|
27
|
+
private sync;
|
|
28
|
+
private peers;
|
|
29
|
+
private syncTasks;
|
|
30
|
+
private activeSyncs;
|
|
31
|
+
private stats;
|
|
32
|
+
private maxConcurrentSyncs;
|
|
33
|
+
constructor(sync: VectorQuicSync, maxConcurrentSyncs?: number);
|
|
34
|
+
/**
|
|
35
|
+
* Register peer for synchronization
|
|
36
|
+
*/
|
|
37
|
+
registerPeer(peer: PeerInfo): void;
|
|
38
|
+
/**
|
|
39
|
+
* Unregister peer
|
|
40
|
+
*/
|
|
41
|
+
unregisterPeer(nodeId: string): void;
|
|
42
|
+
/**
|
|
43
|
+
* Get registered peers
|
|
44
|
+
*/
|
|
45
|
+
getPeers(): PeerInfo[];
|
|
46
|
+
/**
|
|
47
|
+
* Schedule shard sync with peer
|
|
48
|
+
*/
|
|
49
|
+
scheduleSyncTask(shardId: string, peerAddress: string, priority?: number): void;
|
|
50
|
+
/**
|
|
51
|
+
* Execute scheduled sync tasks
|
|
52
|
+
*/
|
|
53
|
+
executeTasks(): Promise<SyncResult[]>;
|
|
54
|
+
/**
|
|
55
|
+
* Execute single sync task
|
|
56
|
+
*/
|
|
57
|
+
private executeTask;
|
|
58
|
+
/**
|
|
59
|
+
* Sync all shards with all peers
|
|
60
|
+
*/
|
|
61
|
+
syncAll(): Promise<SyncResult[]>;
|
|
62
|
+
/**
|
|
63
|
+
* Sync specific shard with all peers that have it
|
|
64
|
+
*/
|
|
65
|
+
syncShard(shardId: string): Promise<SyncResult[]>;
|
|
66
|
+
/**
|
|
67
|
+
* Sync all shards with specific peer
|
|
68
|
+
*/
|
|
69
|
+
syncWithPeer(peerAddress: string): Promise<SyncResult[]>;
|
|
70
|
+
/**
|
|
71
|
+
* Get state of all managed shards
|
|
72
|
+
*/
|
|
73
|
+
getAllShardStates(): Promise<ShardState[]>;
|
|
74
|
+
/**
|
|
75
|
+
* Start automatic sync for all shards
|
|
76
|
+
*/
|
|
77
|
+
startAutoSyncAll(intervalMs: number): void;
|
|
78
|
+
/**
|
|
79
|
+
* Stop all automatic syncs
|
|
80
|
+
*/
|
|
81
|
+
stopAutoSyncAll(): void;
|
|
82
|
+
/**
|
|
83
|
+
* Get coordinator statistics
|
|
84
|
+
*/
|
|
85
|
+
getStats(): CoordinatorStats;
|
|
86
|
+
/**
|
|
87
|
+
* Update statistics after sync
|
|
88
|
+
*/
|
|
89
|
+
private updateStats;
|
|
90
|
+
/**
|
|
91
|
+
* Get pending sync tasks count
|
|
92
|
+
*/
|
|
93
|
+
getPendingTasksCount(): number;
|
|
94
|
+
/**
|
|
95
|
+
* Get active syncs count
|
|
96
|
+
*/
|
|
97
|
+
getActiveSyncsCount(): number;
|
|
98
|
+
/**
|
|
99
|
+
* Clear all pending tasks
|
|
100
|
+
*/
|
|
101
|
+
clearPendingTasks(): void;
|
|
102
|
+
/**
|
|
103
|
+
* Health check for all peers
|
|
104
|
+
*/
|
|
105
|
+
healthCheck(): Promise<Map<string, boolean>>;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Create coordinator with convenience wrapper
|
|
109
|
+
*/
|
|
110
|
+
export declare function createShardCoordinator(db: any, nodeId: string, config?: Partial<SyncConfig>, maxConcurrentSyncs?: number): Promise<ShardCoordinator>;
|
|
111
|
+
//# sourceMappingURL=coordinator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"coordinator.d.ts","sourceRoot":"","sources":["../../src/sync/coordinator.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,KAAK,EACV,UAAU,EACV,UAAU,EACV,UAAU,EACV,gBAAgB,EACjB,MAAM,SAAS,CAAC;AAEjB;;GAEG;AACH,MAAM,WAAW,QAAQ;IACvB,mBAAmB;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,wBAAwB;IACxB,OAAO,EAAE,MAAM,CAAC;IAChB,+BAA+B;IAC/B,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,6BAA6B;IAC7B,WAAW,EAAE,MAAM,CAAC;IACpB,wBAAwB;IACxB,MAAM,EAAE,QAAQ,GAAG,SAAS,GAAG,SAAS,CAAC;CAC1C;AAaD;;GAEG;AACH,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,IAAI,CAAiB;IAC7B,OAAO,CAAC,KAAK,CAAoC;IACjD,OAAO,CAAC,SAAS,CAAkB;IACnC,OAAO,CAAC,WAAW,CAA0B;IAC7C,OAAO,CAAC,KAAK,CAAmB;IAChC,OAAO,CAAC,kBAAkB,CAAS;gBAGjC,IAAI,EAAE,cAAc,EACpB,kBAAkB,GAAE,MAAU;IAgBhC;;OAEG;IACH,YAAY,CAAC,IAAI,EAAE,QAAQ,GAAG,IAAI;IAKlC;;OAEG;IACH,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAQpC;;OAEG;IACH,QAAQ,IAAI,QAAQ,EAAE;IAItB;;OAEG;IACH,gBAAgB,CACd,OAAO,EAAE,MAAM,EACf,WAAW,EAAE,MAAM,EACnB,QAAQ,GAAE,MAAU,GACnB,IAAI;IAaP;;OAEG;IACG,YAAY,IAAI,OAAO,CAAC,UAAU,EAAE,CAAC;IAoC3C;;OAEG;YACW,WAAW;IAkBzB;;OAEG;IACG,OAAO,IAAI,OAAO,CAAC,UAAU,EAAE,CAAC;IAYtC;;OAEG;IACG,SAAS,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,EAAE,CAAC;IAYvD;;OAEG;IACG,YAAY,CAAC,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,EAAE,CAAC;IAgB9D;;OAEG;IACG,iBAAiB,IAAI,OAAO,CAAC,UAAU,EAAE,CAAC;IAoBhD;;OAEG;IACH,gBAAgB,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI;IAQ1C;;OAEG;IACH,eAAe,IAAI,IAAI;IAIvB;;OAEG;IACH,QAAQ,IAAI,gBAAgB;IAI5B;;OAEG;IACH,OAAO,CAAC,WAAW;IAUnB;;OAEG;IACH,oBAAoB,IAAI,MAAM;IAI9B;;OAEG;IACH,mBAAmB,IAAI,MAAM;IAI7B;;OAEG;IACH,iBAAiB,IAAI,IAAI;IAIzB;;OAEG;IACG,WAAW,IAAI,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAsBnD;AAED;;GAEG;AACH,wBAAsB,sBAAsB,CAC1C,EAAE,EAAE,GAAG,EACP,MAAM,EAAE,MAAM,EACd,MAAM,CAAC,EAAE,OAAO,CAAC,UAAU,CAAC,EAC5B,kBAAkB,CAAC,EAAE,MAAM,GAC1B,OAAO,CAAC,gBAAgB,CAAC,CAK3B"}
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* SQLiteVector QUIC Sync - Multi-Shard Coordinator
|
|
4
|
+
*
|
|
5
|
+
* Orchestrates synchronization across multiple shards and peers
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.ShardCoordinator = void 0;
|
|
9
|
+
exports.createShardCoordinator = createShardCoordinator;
|
|
10
|
+
const quic_sync_1 = require("./quic-sync");
|
|
11
|
+
/**
|
|
12
|
+
* Multi-shard synchronization coordinator
|
|
13
|
+
*/
|
|
14
|
+
class ShardCoordinator {
|
|
15
|
+
constructor(sync, maxConcurrentSyncs = 5) {
|
|
16
|
+
this.peers = new Map();
|
|
17
|
+
this.syncTasks = [];
|
|
18
|
+
this.activeSyncs = new Set();
|
|
19
|
+
this.sync = sync;
|
|
20
|
+
this.maxConcurrentSyncs = maxConcurrentSyncs;
|
|
21
|
+
this.stats = {
|
|
22
|
+
totalShards: 0,
|
|
23
|
+
activeSyncs: 0,
|
|
24
|
+
totalSyncs: 0,
|
|
25
|
+
totalConflicts: 0,
|
|
26
|
+
avgSyncDurationMs: 0,
|
|
27
|
+
bytesSent: 0,
|
|
28
|
+
bytesReceived: 0
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Register peer for synchronization
|
|
33
|
+
*/
|
|
34
|
+
registerPeer(peer) {
|
|
35
|
+
this.peers.set(peer.nodeId, peer);
|
|
36
|
+
this.stats.totalShards += peer.shards.length;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Unregister peer
|
|
40
|
+
*/
|
|
41
|
+
unregisterPeer(nodeId) {
|
|
42
|
+
const peer = this.peers.get(nodeId);
|
|
43
|
+
if (peer) {
|
|
44
|
+
this.stats.totalShards -= peer.shards.length;
|
|
45
|
+
this.peers.delete(nodeId);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Get registered peers
|
|
50
|
+
*/
|
|
51
|
+
getPeers() {
|
|
52
|
+
return Array.from(this.peers.values());
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Schedule shard sync with peer
|
|
56
|
+
*/
|
|
57
|
+
scheduleSyncTask(shardId, peerAddress, priority = 0) {
|
|
58
|
+
const task = {
|
|
59
|
+
shardId,
|
|
60
|
+
peerAddress,
|
|
61
|
+
priority,
|
|
62
|
+
scheduledAt: Date.now(),
|
|
63
|
+
retries: 0
|
|
64
|
+
};
|
|
65
|
+
this.syncTasks.push(task);
|
|
66
|
+
this.syncTasks.sort((a, b) => b.priority - a.priority);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Execute scheduled sync tasks
|
|
70
|
+
*/
|
|
71
|
+
async executeTasks() {
|
|
72
|
+
const results = [];
|
|
73
|
+
while (this.syncTasks.length > 0 && this.activeSyncs.size < this.maxConcurrentSyncs) {
|
|
74
|
+
const task = this.syncTasks.shift();
|
|
75
|
+
const taskKey = `${task.shardId}:${task.peerAddress}`;
|
|
76
|
+
// Skip if already syncing
|
|
77
|
+
if (this.activeSyncs.has(taskKey)) {
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
// Execute sync
|
|
81
|
+
this.activeSyncs.add(taskKey);
|
|
82
|
+
this.stats.activeSyncs = this.activeSyncs.size;
|
|
83
|
+
const result = await this.executeTask(task);
|
|
84
|
+
results.push(result);
|
|
85
|
+
this.activeSyncs.delete(taskKey);
|
|
86
|
+
this.stats.activeSyncs = this.activeSyncs.size;
|
|
87
|
+
// Update stats
|
|
88
|
+
this.updateStats(result);
|
|
89
|
+
// Retry on failure
|
|
90
|
+
if (!result.success && task.retries < 3) {
|
|
91
|
+
task.retries++;
|
|
92
|
+
task.priority -= 1;
|
|
93
|
+
this.syncTasks.push(task);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return results;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Execute single sync task
|
|
100
|
+
*/
|
|
101
|
+
async executeTask(task) {
|
|
102
|
+
try {
|
|
103
|
+
const result = await this.sync.sync(task.shardId, task.peerAddress);
|
|
104
|
+
return result;
|
|
105
|
+
}
|
|
106
|
+
catch (error) {
|
|
107
|
+
return {
|
|
108
|
+
shardId: task.shardId,
|
|
109
|
+
changesApplied: 0,
|
|
110
|
+
conflictsDetected: 0,
|
|
111
|
+
conflictsResolved: 0,
|
|
112
|
+
conflictsUnresolved: [],
|
|
113
|
+
durationMs: 0,
|
|
114
|
+
success: false,
|
|
115
|
+
error: error instanceof Error ? error.message : String(error)
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Sync all shards with all peers
|
|
121
|
+
*/
|
|
122
|
+
async syncAll() {
|
|
123
|
+
const results = [];
|
|
124
|
+
for (const peer of this.peers.values()) {
|
|
125
|
+
for (const shardId of peer.shards) {
|
|
126
|
+
this.scheduleSyncTask(shardId, peer.address, 1);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return this.executeTasks();
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Sync specific shard with all peers that have it
|
|
133
|
+
*/
|
|
134
|
+
async syncShard(shardId) {
|
|
135
|
+
const results = [];
|
|
136
|
+
for (const peer of this.peers.values()) {
|
|
137
|
+
if (peer.shards.includes(shardId)) {
|
|
138
|
+
this.scheduleSyncTask(shardId, peer.address, 5);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return this.executeTasks();
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Sync all shards with specific peer
|
|
145
|
+
*/
|
|
146
|
+
async syncWithPeer(peerAddress) {
|
|
147
|
+
const peer = Array.from(this.peers.values()).find(p => p.address === peerAddress);
|
|
148
|
+
if (!peer) {
|
|
149
|
+
throw new Error(`Peer not found: ${peerAddress}`);
|
|
150
|
+
}
|
|
151
|
+
for (const shardId of peer.shards) {
|
|
152
|
+
this.scheduleSyncTask(shardId, peer.address, 2);
|
|
153
|
+
}
|
|
154
|
+
return this.executeTasks();
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Get state of all managed shards
|
|
158
|
+
*/
|
|
159
|
+
async getAllShardStates() {
|
|
160
|
+
const states = [];
|
|
161
|
+
const shardIds = new Set();
|
|
162
|
+
// Collect all unique shard IDs
|
|
163
|
+
for (const peer of this.peers.values()) {
|
|
164
|
+
for (const shardId of peer.shards) {
|
|
165
|
+
shardIds.add(shardId);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
// Get state for each shard
|
|
169
|
+
for (const shardId of shardIds) {
|
|
170
|
+
const state = await this.sync.getShardState(shardId);
|
|
171
|
+
states.push(state);
|
|
172
|
+
}
|
|
173
|
+
return states;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Start automatic sync for all shards
|
|
177
|
+
*/
|
|
178
|
+
startAutoSyncAll(intervalMs) {
|
|
179
|
+
for (const peer of this.peers.values()) {
|
|
180
|
+
for (const shardId of peer.shards) {
|
|
181
|
+
this.sync.startAutoSync(shardId, peer.address);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Stop all automatic syncs
|
|
187
|
+
*/
|
|
188
|
+
stopAutoSyncAll() {
|
|
189
|
+
this.sync.stopAllAutoSyncs();
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Get coordinator statistics
|
|
193
|
+
*/
|
|
194
|
+
getStats() {
|
|
195
|
+
return { ...this.stats };
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Update statistics after sync
|
|
199
|
+
*/
|
|
200
|
+
updateStats(result) {
|
|
201
|
+
this.stats.totalSyncs++;
|
|
202
|
+
this.stats.totalConflicts += result.conflictsDetected;
|
|
203
|
+
// Update moving average for sync duration
|
|
204
|
+
const alpha = 0.3; // Smoothing factor
|
|
205
|
+
this.stats.avgSyncDurationMs =
|
|
206
|
+
this.stats.avgSyncDurationMs * (1 - alpha) + result.durationMs * alpha;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Get pending sync tasks count
|
|
210
|
+
*/
|
|
211
|
+
getPendingTasksCount() {
|
|
212
|
+
return this.syncTasks.length;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Get active syncs count
|
|
216
|
+
*/
|
|
217
|
+
getActiveSyncsCount() {
|
|
218
|
+
return this.activeSyncs.size;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Clear all pending tasks
|
|
222
|
+
*/
|
|
223
|
+
clearPendingTasks() {
|
|
224
|
+
this.syncTasks = [];
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Health check for all peers
|
|
228
|
+
*/
|
|
229
|
+
async healthCheck() {
|
|
230
|
+
const health = new Map();
|
|
231
|
+
for (const peer of this.peers.values()) {
|
|
232
|
+
try {
|
|
233
|
+
// Attempt sync with minimal data to check connectivity
|
|
234
|
+
const result = await this.sync.sync(peer.shards[0] || 'health-check', peer.address);
|
|
235
|
+
health.set(peer.nodeId, result.success);
|
|
236
|
+
peer.status = result.success ? 'online' : 'offline';
|
|
237
|
+
peer.lastContact = Date.now();
|
|
238
|
+
}
|
|
239
|
+
catch {
|
|
240
|
+
health.set(peer.nodeId, false);
|
|
241
|
+
peer.status = 'offline';
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
return health;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
exports.ShardCoordinator = ShardCoordinator;
|
|
248
|
+
/**
|
|
249
|
+
* Create coordinator with convenience wrapper
|
|
250
|
+
*/
|
|
251
|
+
async function createShardCoordinator(db, nodeId, config, maxConcurrentSyncs) {
|
|
252
|
+
const sync = new quic_sync_1.VectorQuicSync(db, nodeId, config);
|
|
253
|
+
await sync.initialize();
|
|
254
|
+
return new ShardCoordinator(sync, maxConcurrentSyncs);
|
|
255
|
+
}
|
|
256
|
+
//# sourceMappingURL=coordinator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"coordinator.js","sourceRoot":"","sources":["../../src/sync/coordinator.ts"],"names":[],"mappings":";AAAA;;;;GAIG;;;AA6UH,wDAUC;AArVD,2CAA6C;AAmC7C;;GAEG;AACH,MAAa,gBAAgB;IAQ3B,YACE,IAAoB,EACpB,qBAA6B,CAAC;QARxB,UAAK,GAA0B,IAAI,GAAG,EA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