agentic-flow 2.0.2 → 2.0.4
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/.claude/agents/test-neural.md +14 -0
- package/.claude/settings.json +9 -133
- package/README.md +622 -1862
- package/dist/.tsbuildinfo +1 -1
- package/dist/billing/mcp/tools.js +0 -1
- package/dist/billing/mcp/tools.js.map +1 -1
- package/dist/cli/commands/hooks.d.ts.map +1 -1
- package/dist/cli/commands/hooks.js +4 -79
- package/dist/cli/commands/hooks.js.map +1 -1
- package/dist/cli-proxy.js +1 -89
- package/dist/cli-proxy.js.map +1 -1
- package/dist/core/agentdb-fast.js +3 -3
- package/dist/core/agentdb-fast.js.map +1 -1
- package/dist/core/agentdb-wrapper-enhanced.d.ts.map +1 -1
- package/dist/core/agentdb-wrapper-enhanced.js +4 -20
- package/dist/core/agentdb-wrapper-enhanced.js.map +1 -1
- package/dist/core/agentdb-wrapper.d.ts +2 -3
- package/dist/core/agentdb-wrapper.d.ts.map +1 -1
- package/dist/core/agentdb-wrapper.js +1 -15
- package/dist/core/agentdb-wrapper.js.map +1 -1
- package/dist/core/attention-native.d.ts +0 -4
- package/dist/core/attention-native.d.ts.map +1 -1
- package/dist/core/attention-native.js +2 -14
- package/dist/core/attention-native.js.map +1 -1
- package/dist/federation/SecurityManager.d.ts +2 -11
- package/dist/federation/SecurityManager.d.ts.map +1 -1
- package/dist/federation/SecurityManager.js +17 -50
- package/dist/federation/SecurityManager.js.map +1 -1
- package/dist/federation/integrations/supabase-adapter-debug.js +3 -3
- package/dist/federation/integrations/supabase-adapter-debug.js.map +1 -1
- package/dist/hooks/swarm-learning-optimizer.js +5 -5
- package/dist/hooks/swarm-learning-optimizer.js.map +1 -1
- package/dist/intelligence/IntelligenceStore.d.ts +26 -35
- package/dist/intelligence/IntelligenceStore.d.ts.map +1 -1
- package/dist/intelligence/IntelligenceStore.js +123 -308
- package/dist/intelligence/IntelligenceStore.js.map +1 -1
- package/dist/intelligence/RuVectorIntelligence.d.ts +1 -26
- package/dist/intelligence/RuVectorIntelligence.d.ts.map +1 -1
- package/dist/intelligence/RuVectorIntelligence.js +10 -49
- package/dist/intelligence/RuVectorIntelligence.js.map +1 -1
- package/dist/intelligence/agent-booster-enhanced.d.ts +0 -1
- package/dist/intelligence/agent-booster-enhanced.d.ts.map +1 -1
- package/dist/intelligence/agent-booster-enhanced.js +3 -24
- package/dist/intelligence/agent-booster-enhanced.js.map +1 -1
- package/dist/intelligence/index.d.ts +3 -29
- package/dist/intelligence/index.d.ts.map +1 -1
- package/dist/intelligence/index.js +3 -13
- package/dist/intelligence/index.js.map +1 -1
- package/dist/mcp/claudeFlowSdkServer.d.ts.map +1 -1
- package/dist/mcp/claudeFlowSdkServer.js +3 -9
- package/dist/mcp/claudeFlowSdkServer.js.map +1 -1
- package/dist/mcp/fastmcp/tools/hooks/intelligence-bridge.js +5 -5
- package/dist/mcp/fastmcp/tools/hooks/intelligence-bridge.js.map +1 -1
- package/dist/mcp/fastmcp/tools/swarm/init.d.ts.map +1 -1
- package/dist/mcp/fastmcp/tools/swarm/init.js +7 -36
- package/dist/mcp/fastmcp/tools/swarm/init.js.map +1 -1
- package/dist/mcp/fastmcp/tools/swarm/spawn.d.ts.map +1 -1
- package/dist/mcp/fastmcp/tools/swarm/spawn.js +8 -47
- package/dist/mcp/fastmcp/tools/swarm/spawn.js.map +1 -1
- package/dist/mcp/tools/agent-booster-tools.d.ts +1 -1
- package/dist/mcp/tools/agent-booster-tools.d.ts.map +1 -1
- package/dist/mcp/tools/agent-booster-tools.js +4 -10
- package/dist/mcp/tools/agent-booster-tools.js.map +1 -1
- package/dist/mcp/tools/sona-tools.d.ts.map +1 -1
- package/dist/mcp/tools/sona-tools.js +0 -6
- package/dist/mcp/tools/sona-tools.js.map +1 -1
- package/dist/optimizations/agent-booster-migration.d.ts.map +1 -1
- package/dist/optimizations/agent-booster-migration.js +2 -5
- package/dist/optimizations/agent-booster-migration.js.map +1 -1
- package/dist/proxy/anthropic-to-openrouter.js.map +1 -1
- package/dist/proxy/anthropic-to-requesty.js.map +1 -1
- package/dist/proxy/quic-proxy.d.ts +1 -0
- package/dist/proxy/quic-proxy.d.ts.map +1 -1
- package/dist/proxy/quic-proxy.js +2 -2
- package/dist/proxy/quic-proxy.js.map +1 -1
- package/dist/reasoningbank/AdvancedMemory.js +1 -1
- package/dist/reasoningbank/AdvancedMemory.js.map +1 -1
- package/dist/reasoningbank/HybridBackend.d.ts.map +1 -1
- package/dist/reasoningbank/HybridBackend.js +5 -2
- package/dist/reasoningbank/HybridBackend.js.map +1 -1
- package/dist/reasoningbank/backend-selector.d.ts +1 -11
- package/dist/reasoningbank/backend-selector.d.ts.map +1 -1
- package/dist/reasoningbank/backend-selector.js +5 -45
- package/dist/reasoningbank/backend-selector.js.map +1 -1
- package/dist/reasoningbank/core/consolidate.d.ts.map +1 -1
- package/dist/reasoningbank/core/consolidate.js +45 -113
- package/dist/reasoningbank/core/consolidate.js.map +1 -1
- package/dist/reasoningbank/index-new.d.ts +6 -1
- package/dist/reasoningbank/index-new.d.ts.map +1 -1
- package/dist/reasoningbank/index-new.js +6 -1
- package/dist/reasoningbank/index-new.js.map +1 -1
- package/dist/reasoningbank/index.d.ts +6 -2
- package/dist/reasoningbank/index.d.ts.map +1 -1
- package/dist/reasoningbank/index.js +6 -2
- package/dist/reasoningbank/index.js.map +1 -1
- package/dist/reasoningbank/utils/embeddings.d.ts +0 -1
- package/dist/reasoningbank/utils/embeddings.d.ts.map +1 -1
- package/dist/reasoningbank/utils/embeddings.js +26 -53
- package/dist/reasoningbank/utils/embeddings.js.map +1 -1
- package/dist/router/index.d.ts +17 -0
- package/dist/router/index.d.ts.map +1 -0
- package/dist/router/index.js +19 -0
- package/dist/router/index.js.map +1 -0
- package/dist/router/providers/onnx-local-optimized.d.ts +0 -2
- package/dist/router/providers/onnx-local-optimized.d.ts.map +1 -1
- package/dist/router/providers/onnx-local-optimized.js +0 -10
- package/dist/router/providers/onnx-local-optimized.js.map +1 -1
- package/dist/router/providers/onnx-local.d.ts +0 -1
- package/dist/router/providers/onnx-local.d.ts.map +1 -1
- package/dist/router/providers/onnx-local.js +5 -22
- package/dist/router/providers/onnx-local.js.map +1 -1
- package/dist/services/embedding-service.js.map +1 -1
- package/dist/services/sona-agent-training.d.ts +0 -1
- package/dist/services/sona-agent-training.d.ts.map +1 -1
- package/dist/services/sona-agent-training.js.map +1 -1
- package/dist/services/sona-agentdb-integration.d.ts.map +1 -1
- package/dist/services/sona-agentdb-integration.js +6 -9
- package/dist/services/sona-agentdb-integration.js.map +1 -1
- package/dist/services/sona-service.d.ts.map +1 -1
- package/dist/services/sona-service.js +5 -6
- package/dist/services/sona-service.js.map +1 -1
- package/dist/utils/cli.d.ts +1 -1
- package/dist/utils/cli.d.ts.map +1 -1
- package/dist/utils/cli.js +0 -21
- package/dist/utils/cli.js.map +1 -1
- package/package.json +7 -19
- package/scripts/postinstall.js +4 -45
- package/wasm/reasoningbank/reasoningbank_wasm_bg.js +2 -2
- package/wasm/reasoningbank/reasoningbank_wasm_bg.wasm +0 -0
- package/.claude/agents/.claude-flow/metrics/agent-metrics.json +0 -1
- package/.claude/agents/.claude-flow/metrics/performance.json +0 -87
- package/.claude/agents/.claude-flow/metrics/task-metrics.json +0 -10
- package/.claude/skills/.claude-flow/metrics/agent-metrics.json +0 -1
- package/.claude/skills/.claude-flow/metrics/performance.json +0 -87
- package/.claude/skills/.claude-flow/metrics/task-metrics.json +0 -10
- package/.claude/skills/agentic-flow-quickstart/skill.md +0 -69
- package/.claude/skills/hooks-automation/skill.md +0 -155
- package/.claude/skills/memory-patterns/skill.md +0 -110
- package/.claude/skills/skill-builder/.claude-flow/metrics/agent-metrics.json +0 -1
- package/.claude/skills/skill-builder/.claude-flow/metrics/performance.json +0 -87
- package/.claude/skills/skill-builder/.claude-flow/metrics/task-metrics.json +0 -10
- package/.claude/skills/sparc-methodology/skill.md +0 -137
- package/.claude/skills/swarm-coordination/skill.md +0 -94
- package/.claude/skills/worker-benchmarks/skill.md +0 -135
- package/.claude/skills/worker-integration/skill.md +0 -154
- package/.claude/statusline.mjs +0 -109
- package/.claude/statusline.sh +0 -71
- package/dist/agentdb/benchmarks/comprehensive-benchmark.js +0 -664
- package/dist/agentdb/benchmarks/frontier-benchmark.js +0 -419
- package/dist/agentdb/benchmarks/reflexion-benchmark.js +0 -370
- package/dist/agentdb/cli/agentdb-cli.js +0 -717
- package/dist/agentdb/controllers/CausalMemoryGraph.js +0 -322
- package/dist/agentdb/controllers/CausalRecall.js +0 -281
- package/dist/agentdb/controllers/EmbeddingService.d.ts +0 -37
- package/dist/agentdb/controllers/EmbeddingService.d.ts.map +0 -1
- package/dist/agentdb/controllers/EmbeddingService.js +0 -119
- package/dist/agentdb/controllers/EmbeddingService.js.map +0 -1
- package/dist/agentdb/controllers/ExplainableRecall.js +0 -387
- package/dist/agentdb/controllers/NightlyLearner.js +0 -382
- package/dist/agentdb/controllers/ReflexionMemory.js +0 -239
- package/dist/agentdb/controllers/SkillLibrary.js +0 -276
- package/dist/agentdb/controllers/frontier-index.js +0 -9
- package/dist/agentdb/controllers/index.js +0 -8
- package/dist/agentdb/optimizations/BatchOperations.js +0 -198
- package/dist/agentdb/optimizations/QueryOptimizer.js +0 -225
- package/dist/agentdb/optimizations/index.js +0 -7
- package/dist/agentdb/tests/frontier-features.test.js +0 -665
- package/dist/benchmarks/embeddings-benchmark.d.ts +0 -38
- package/dist/benchmarks/embeddings-benchmark.d.ts.map +0 -1
- package/dist/benchmarks/embeddings-benchmark.js +0 -282
- package/dist/benchmarks/embeddings-benchmark.js.map +0 -1
- package/dist/cli/commands/embeddings.d.ts +0 -12
- package/dist/cli/commands/embeddings.d.ts.map +0 -1
- package/dist/cli/commands/embeddings.js +0 -386
- package/dist/cli/commands/embeddings.js.map +0 -1
- package/dist/cli/commands/init.d.ts +0 -8
- package/dist/cli/commands/init.d.ts.map +0 -1
- package/dist/cli/commands/init.js +0 -514
- package/dist/cli/commands/init.js.map +0 -1
- package/dist/cli/commands/workers.d.ts +0 -9
- package/dist/cli/commands/workers.d.ts.map +0 -1
- package/dist/cli/commands/workers.js +0 -991
- package/dist/cli/commands/workers.js.map +0 -1
- package/dist/cli/skills-manager.js +0 -1297
- package/dist/cli/update-message.js +0 -175
- package/dist/embeddings/index.d.ts +0 -17
- package/dist/embeddings/index.d.ts.map +0 -1
- package/dist/embeddings/index.js +0 -17
- package/dist/embeddings/index.js.map +0 -1
- package/dist/embeddings/neural-substrate.d.ts +0 -206
- package/dist/embeddings/neural-substrate.d.ts.map +0 -1
- package/dist/embeddings/neural-substrate.js +0 -629
- package/dist/embeddings/neural-substrate.js.map +0 -1
- package/dist/embeddings/optimized-embedder.d.ts +0 -103
- package/dist/embeddings/optimized-embedder.d.ts.map +0 -1
- package/dist/embeddings/optimized-embedder.js +0 -730
- package/dist/embeddings/optimized-embedder.js.map +0 -1
- package/dist/examples/embedding-geometry.d.ts +0 -105
- package/dist/examples/embedding-geometry.d.ts.map +0 -1
- package/dist/examples/embedding-geometry.js +0 -528
- package/dist/examples/embedding-geometry.js.map +0 -1
- package/dist/memory/SharedMemoryPool.d.ts +0 -129
- package/dist/memory/SharedMemoryPool.d.ts.map +0 -1
- package/dist/memory/SharedMemoryPool.js +0 -243
- package/dist/memory/SharedMemoryPool.js.map +0 -1
- package/dist/memory/index.d.ts +0 -8
- package/dist/memory/index.d.ts.map +0 -1
- package/dist/memory/index.js +0 -7
- package/dist/memory/index.js.map +0 -1
- package/dist/proxy/http3-proxy-old.js +0 -331
- package/dist/proxy/proxy/anthropic-to-gemini.js +0 -439
- package/dist/proxy/utils/logger.js +0 -59
- package/dist/reasoningbank/agentdb-adapter.js +0 -125
- package/dist/reasoningbank/core/database.js +0 -250
- package/dist/reasoningbank/core/memory-engine.js +0 -335
- package/dist/swarm/ipfs-swarm.d.ts +0 -265
- package/dist/swarm/ipfs-swarm.d.ts.map +0 -1
- package/dist/swarm/ipfs-swarm.js +0 -508
- package/dist/swarm/ipfs-swarm.js.map +0 -1
- package/dist/swarm/p2p-free-swarm.d.ts +0 -344
- package/dist/swarm/p2p-free-swarm.d.ts.map +0 -1
- package/dist/swarm/p2p-free-swarm.js +0 -603
- package/dist/swarm/p2p-free-swarm.js.map +0 -1
- package/dist/swarm/real-p2p-swarm.d.ts +0 -183
- package/dist/swarm/real-p2p-swarm.d.ts.map +0 -1
- package/dist/swarm/real-p2p-swarm.js +0 -469
- package/dist/swarm/real-p2p-swarm.js.map +0 -1
- package/dist/utils/adaptive-pool-sizing.js +0 -414
- package/dist/utils/agentdbCommands.js +0 -175
- package/dist/utils/circular-rate-limiter.js +0 -391
- package/dist/utils/dynamic-compression.js +0 -298
- package/dist/utils/http2-multiplexing.js +0 -319
- package/dist/utils/index.d.ts +0 -6
- package/dist/utils/index.d.ts.map +0 -1
- package/dist/utils/index.js +0 -6
- package/dist/utils/index.js.map +0 -1
- package/dist/utils/lazy-auth.js +0 -311
- package/dist/utils/model-cache.d.ts +0 -61
- package/dist/utils/model-cache.d.ts.map +0 -1
- package/dist/utils/model-cache.js +0 -176
- package/dist/utils/model-cache.js.map +0 -1
- package/dist/utils/server-push.js +0 -251
- package/dist/utils/suppress-warnings.d.ts +0 -19
- package/dist/utils/suppress-warnings.d.ts.map +0 -1
- package/dist/utils/suppress-warnings.js +0 -59
- package/dist/utils/suppress-warnings.js.map +0 -1
- package/dist/utils/zero-copy-buffer.js +0 -286
- package/dist/workers/consolidated-phases.d.ts +0 -40
- package/dist/workers/consolidated-phases.d.ts.map +0 -1
- package/dist/workers/consolidated-phases.js +0 -497
- package/dist/workers/consolidated-phases.js.map +0 -1
- package/dist/workers/custom-worker-config.d.ts +0 -133
- package/dist/workers/custom-worker-config.d.ts.map +0 -1
- package/dist/workers/custom-worker-config.js +0 -215
- package/dist/workers/custom-worker-config.js.map +0 -1
- package/dist/workers/custom-worker-factory.d.ts +0 -89
- package/dist/workers/custom-worker-factory.d.ts.map +0 -1
- package/dist/workers/custom-worker-factory.js +0 -404
- package/dist/workers/custom-worker-factory.js.map +0 -1
- package/dist/workers/dispatch-service.d.ts +0 -123
- package/dist/workers/dispatch-service.d.ts.map +0 -1
- package/dist/workers/dispatch-service.js +0 -1024
- package/dist/workers/dispatch-service.js.map +0 -1
- package/dist/workers/hooks-integration.d.ts +0 -79
- package/dist/workers/hooks-integration.d.ts.map +0 -1
- package/dist/workers/hooks-integration.js +0 -286
- package/dist/workers/hooks-integration.js.map +0 -1
- package/dist/workers/index.d.ts +0 -42
- package/dist/workers/index.d.ts.map +0 -1
- package/dist/workers/index.js +0 -52
- package/dist/workers/index.js.map +0 -1
- package/dist/workers/mcp-tools.d.ts +0 -56
- package/dist/workers/mcp-tools.d.ts.map +0 -1
- package/dist/workers/mcp-tools.js +0 -359
- package/dist/workers/mcp-tools.js.map +0 -1
- package/dist/workers/phase-executors.d.ts +0 -22
- package/dist/workers/phase-executors.d.ts.map +0 -1
- package/dist/workers/phase-executors.js +0 -445
- package/dist/workers/phase-executors.js.map +0 -1
- package/dist/workers/resource-governor.d.ts +0 -75
- package/dist/workers/resource-governor.d.ts.map +0 -1
- package/dist/workers/resource-governor.js +0 -187
- package/dist/workers/resource-governor.js.map +0 -1
- package/dist/workers/ruvector-integration.d.ts +0 -163
- package/dist/workers/ruvector-integration.d.ts.map +0 -1
- package/dist/workers/ruvector-integration.js +0 -543
- package/dist/workers/ruvector-integration.js.map +0 -1
- package/dist/workers/ruvector-native-integration.d.ts +0 -91
- package/dist/workers/ruvector-native-integration.d.ts.map +0 -1
- package/dist/workers/ruvector-native-integration.js +0 -254
- package/dist/workers/ruvector-native-integration.js.map +0 -1
- package/dist/workers/trigger-detector.d.ts +0 -68
- package/dist/workers/trigger-detector.d.ts.map +0 -1
- package/dist/workers/trigger-detector.js +0 -281
- package/dist/workers/trigger-detector.js.map +0 -1
- package/dist/workers/types.d.ts +0 -145
- package/dist/workers/types.d.ts.map +0 -1
- package/dist/workers/types.js +0 -6
- package/dist/workers/types.js.map +0 -1
- package/dist/workers/worker-agent-integration.d.ts +0 -140
- package/dist/workers/worker-agent-integration.d.ts.map +0 -1
- package/dist/workers/worker-agent-integration.js +0 -471
- package/dist/workers/worker-agent-integration.js.map +0 -1
- package/dist/workers/worker-benchmarks.d.ts +0 -88
- package/dist/workers/worker-benchmarks.d.ts.map +0 -1
- package/dist/workers/worker-benchmarks.js +0 -452
- package/dist/workers/worker-benchmarks.js.map +0 -1
- package/dist/workers/worker-registry.d.ts +0 -85
- package/dist/workers/worker-registry.d.ts.map +0 -1
- package/dist/workers/worker-registry.js +0 -547
- package/dist/workers/worker-registry.js.map +0 -1
- package/docs/.claude-flow/metrics/agent-metrics.json +0 -1
- package/docs/.claude-flow/metrics/performance.json +0 -87
- package/docs/.claude-flow/metrics/task-metrics.json +0 -10
- package/docs/embeddings/EMBEDDING_GEOMETRY.md +0 -935
- /package/.claude/agents/analysis/{analyze-code-quality.md → code-review/analyze-code-quality.md} +0 -0
- /package/.claude/agents/architecture/{arch-system-design.md → system-design/arch-system-design.md} +0 -0
- /package/.claude/agents/data/{data-ml-model.md → ml/data-ml-model.md} +0 -0
- /package/.claude/agents/development/{dev-backend-api.md → backend/dev-backend-api.md} +0 -0
- /package/.claude/agents/devops/{ops-cicd-github.md → ci-cd/ops-cicd-github.md} +0 -0
- /package/.claude/agents/documentation/{docs-api-openapi.md → api-docs/docs-api-openapi.md} +0 -0
- /package/.claude/agents/specialized/{spec-mobile-react-native.md → mobile/spec-mobile-react-native.md} +0 -0
- /package/.claude/agents/testing/{tdd-london-swarm.md → unit/tdd-london-swarm.md} +0 -0
- /package/.claude/agents/testing/{production-validator.md → validation/production-validator.md} +0 -0
|
@@ -1,225 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* QueryOptimizer - Advanced Query Optimization for AgentDB
|
|
3
|
-
*
|
|
4
|
-
* Implements:
|
|
5
|
-
* - Query result caching with TTL
|
|
6
|
-
* - Prepared statement pooling
|
|
7
|
-
* - Batch operation optimization
|
|
8
|
-
* - Index usage analysis
|
|
9
|
-
* - Query plan analysis
|
|
10
|
-
*/
|
|
11
|
-
export class QueryOptimizer {
|
|
12
|
-
db;
|
|
13
|
-
cache;
|
|
14
|
-
stats;
|
|
15
|
-
config;
|
|
16
|
-
constructor(db, config) {
|
|
17
|
-
this.db = db;
|
|
18
|
-
this.cache = new Map();
|
|
19
|
-
this.stats = new Map();
|
|
20
|
-
this.config = {
|
|
21
|
-
maxSize: 1000,
|
|
22
|
-
ttl: 60000, // 1 minute default
|
|
23
|
-
enabled: true,
|
|
24
|
-
...config
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Execute query with caching
|
|
29
|
-
*/
|
|
30
|
-
query(sql, params = [], cacheKey) {
|
|
31
|
-
const key = cacheKey || this.generateCacheKey(sql, params);
|
|
32
|
-
const startTime = Date.now();
|
|
33
|
-
// Check cache
|
|
34
|
-
if (this.config.enabled && this.cache.has(key)) {
|
|
35
|
-
const cached = this.cache.get(key);
|
|
36
|
-
if (Date.now() - cached.timestamp < this.config.ttl) {
|
|
37
|
-
this.recordStats(sql, Date.now() - startTime, true);
|
|
38
|
-
return cached.result;
|
|
39
|
-
}
|
|
40
|
-
else {
|
|
41
|
-
this.cache.delete(key);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
// Execute query
|
|
45
|
-
const stmt = this.db.prepare(sql);
|
|
46
|
-
const result = params.length > 0 ? stmt.all(...params) : stmt.all();
|
|
47
|
-
const executionTime = Date.now() - startTime;
|
|
48
|
-
this.recordStats(sql, executionTime, false);
|
|
49
|
-
// Cache result
|
|
50
|
-
if (this.config.enabled) {
|
|
51
|
-
this.cacheResult(key, result);
|
|
52
|
-
}
|
|
53
|
-
return result;
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Execute query that returns single row
|
|
57
|
-
*/
|
|
58
|
-
queryOne(sql, params = [], cacheKey) {
|
|
59
|
-
const results = this.query(sql, params, cacheKey);
|
|
60
|
-
return results[0];
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* Execute write operation (no caching)
|
|
64
|
-
*/
|
|
65
|
-
execute(sql, params = []) {
|
|
66
|
-
const startTime = Date.now();
|
|
67
|
-
const stmt = this.db.prepare(sql);
|
|
68
|
-
const result = params.length > 0 ? stmt.run(...params) : stmt.run();
|
|
69
|
-
this.recordStats(sql, Date.now() - startTime, false);
|
|
70
|
-
// Invalidate relevant cache entries
|
|
71
|
-
this.invalidateCache(sql);
|
|
72
|
-
return result;
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Batch insert optimization
|
|
76
|
-
*/
|
|
77
|
-
batchInsert(table, columns, rows) {
|
|
78
|
-
const placeholders = columns.map(() => '?').join(', ');
|
|
79
|
-
const sql = `INSERT INTO ${table} (${columns.join(', ')}) VALUES (${placeholders})`;
|
|
80
|
-
const transaction = this.db.transaction((rows) => {
|
|
81
|
-
const stmt = this.db.prepare(sql);
|
|
82
|
-
for (const row of rows) {
|
|
83
|
-
stmt.run(...row);
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
const startTime = Date.now();
|
|
87
|
-
transaction(rows);
|
|
88
|
-
this.recordStats(`BATCH INSERT ${table}`, Date.now() - startTime, false);
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* Analyze query plan
|
|
92
|
-
*/
|
|
93
|
-
analyzeQuery(sql) {
|
|
94
|
-
const plan = this.db.prepare(`EXPLAIN QUERY PLAN ${sql}`).all();
|
|
95
|
-
const planText = plan.map((row) => row.detail).join(' ');
|
|
96
|
-
const usesIndex = planText.toLowerCase().includes('index');
|
|
97
|
-
const hasFullScan = planText.toLowerCase().includes('scan');
|
|
98
|
-
// Simple cost estimation
|
|
99
|
-
let estimatedCost = 1;
|
|
100
|
-
if (hasFullScan)
|
|
101
|
-
estimatedCost *= 10;
|
|
102
|
-
if (!usesIndex)
|
|
103
|
-
estimatedCost *= 5;
|
|
104
|
-
return {
|
|
105
|
-
plan: planText,
|
|
106
|
-
usesIndex,
|
|
107
|
-
estimatedCost
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* Get optimization suggestions
|
|
112
|
-
*/
|
|
113
|
-
getSuggestions() {
|
|
114
|
-
const suggestions = [];
|
|
115
|
-
// Analyze frequently run queries
|
|
116
|
-
const frequentQueries = Array.from(this.stats.values())
|
|
117
|
-
.filter(s => s.executionCount > 100)
|
|
118
|
-
.sort((a, b) => b.totalTime - a.totalTime)
|
|
119
|
-
.slice(0, 10);
|
|
120
|
-
for (const stat of frequentQueries) {
|
|
121
|
-
if (stat.avgTime > 50) {
|
|
122
|
-
const analysis = this.analyzeQuery(stat.query);
|
|
123
|
-
if (!analysis.usesIndex) {
|
|
124
|
-
suggestions.push(`Slow query (${stat.avgTime.toFixed(1)}ms avg): Consider adding index for:\n${stat.query}`);
|
|
125
|
-
}
|
|
126
|
-
if (stat.cacheHits === 0 && stat.executionCount > 50) {
|
|
127
|
-
suggestions.push(`Frequently run query without cache hits: ${stat.query.substring(0, 50)}...`);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
// Check cache efficiency
|
|
132
|
-
const totalHits = Array.from(this.stats.values()).reduce((sum, s) => sum + s.cacheHits, 0);
|
|
133
|
-
const totalMisses = Array.from(this.stats.values()).reduce((sum, s) => sum + s.cacheMisses, 0);
|
|
134
|
-
const hitRate = totalHits / (totalHits + totalMisses) || 0;
|
|
135
|
-
if (hitRate < 0.3 && totalHits + totalMisses > 1000) {
|
|
136
|
-
suggestions.push(`Low cache hit rate (${(hitRate * 100).toFixed(1)}%). Consider increasing cache size or TTL.`);
|
|
137
|
-
}
|
|
138
|
-
return suggestions;
|
|
139
|
-
}
|
|
140
|
-
/**
|
|
141
|
-
* Get query statistics
|
|
142
|
-
*/
|
|
143
|
-
getStats() {
|
|
144
|
-
return Array.from(this.stats.values())
|
|
145
|
-
.sort((a, b) => b.totalTime - a.totalTime);
|
|
146
|
-
}
|
|
147
|
-
/**
|
|
148
|
-
* Clear cache
|
|
149
|
-
*/
|
|
150
|
-
clearCache() {
|
|
151
|
-
this.cache.clear();
|
|
152
|
-
}
|
|
153
|
-
/**
|
|
154
|
-
* Get cache statistics
|
|
155
|
-
*/
|
|
156
|
-
getCacheStats() {
|
|
157
|
-
const totalHits = Array.from(this.stats.values()).reduce((sum, s) => sum + s.cacheHits, 0);
|
|
158
|
-
const totalMisses = Array.from(this.stats.values()).reduce((sum, s) => sum + s.cacheMisses, 0);
|
|
159
|
-
return {
|
|
160
|
-
size: this.cache.size,
|
|
161
|
-
hitRate: totalHits / (totalHits + totalMisses) || 0,
|
|
162
|
-
totalHits,
|
|
163
|
-
totalMisses
|
|
164
|
-
};
|
|
165
|
-
}
|
|
166
|
-
// ========================================================================
|
|
167
|
-
// Private Methods
|
|
168
|
-
// ========================================================================
|
|
169
|
-
generateCacheKey(sql, params) {
|
|
170
|
-
return `${sql}:${JSON.stringify(params)}`;
|
|
171
|
-
}
|
|
172
|
-
cacheResult(key, result) {
|
|
173
|
-
if (this.cache.size >= this.config.maxSize) {
|
|
174
|
-
// Simple LRU: remove oldest entry
|
|
175
|
-
const oldestKey = this.cache.keys().next().value;
|
|
176
|
-
this.cache.delete(oldestKey);
|
|
177
|
-
}
|
|
178
|
-
this.cache.set(key, {
|
|
179
|
-
result,
|
|
180
|
-
timestamp: Date.now()
|
|
181
|
-
});
|
|
182
|
-
}
|
|
183
|
-
invalidateCache(sql) {
|
|
184
|
-
// Invalidate cache entries related to modified tables
|
|
185
|
-
const tables = this.extractTables(sql);
|
|
186
|
-
for (const [key] of this.cache) {
|
|
187
|
-
for (const table of tables) {
|
|
188
|
-
if (key.toLowerCase().includes(table.toLowerCase())) {
|
|
189
|
-
this.cache.delete(key);
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
extractTables(sql) {
|
|
195
|
-
const matches = sql.match(/(?:FROM|INTO|UPDATE|JOIN)\s+(\w+)/gi);
|
|
196
|
-
if (!matches)
|
|
197
|
-
return [];
|
|
198
|
-
return matches
|
|
199
|
-
.map(m => m.split(/\s+/)[1])
|
|
200
|
-
.filter((v, i, a) => a.indexOf(v) === i); // unique
|
|
201
|
-
}
|
|
202
|
-
recordStats(sql, time, cacheHit) {
|
|
203
|
-
const key = sql.substring(0, 100); // Use first 100 chars as key
|
|
204
|
-
if (!this.stats.has(key)) {
|
|
205
|
-
this.stats.set(key, {
|
|
206
|
-
query: sql,
|
|
207
|
-
executionCount: 0,
|
|
208
|
-
totalTime: 0,
|
|
209
|
-
avgTime: 0,
|
|
210
|
-
cacheHits: 0,
|
|
211
|
-
cacheMisses: 0
|
|
212
|
-
});
|
|
213
|
-
}
|
|
214
|
-
const stat = this.stats.get(key);
|
|
215
|
-
stat.executionCount++;
|
|
216
|
-
stat.totalTime += time;
|
|
217
|
-
stat.avgTime = stat.totalTime / stat.executionCount;
|
|
218
|
-
if (cacheHit) {
|
|
219
|
-
stat.cacheHits++;
|
|
220
|
-
}
|
|
221
|
-
else {
|
|
222
|
-
stat.cacheMisses++;
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
}
|