ALWAYS follow these essential guidelines:
-
Model Separation: Each model MUST be in its own separate file
-
AppContext-First Pattern: Use
Arc<AppContext>parameter instead of individual fields -
Performance & Memory Optimization: Focus on lightweight memory usage and high concurrency
- Use
Arc<T>for zero-copy sharing (no cloning data) - DashMap for lock-free concurrent access
- Memoize expensive computations (e.g., Arrow schema construction)
- Separate large structs from hot-path metadata (LRU timestamps in separate map)
- Cache singleton instances (e.g., UserTableShared per table, not per user)
- Use
-
instead than doing user_id.map(kalamdb_commons::models::UserId::from); always add it to the head of the file:
use kalamdb_commons::models::UserId;-
Well-organized code with minimal duplication
-
Leveraging DataFusion for query processing, version resolution, and deletion filtering
-
WHEN FINIDNG MULTIPLE ERRORS dont kleep running cargo check or cargo build run one time and output to a file and fix all of them at once!
-
Instead of passing both Namespaceid and TableName pass TableId which contains both
-
Don't import use inside methods always add them to the top of the rust file instead
-
EntityStore is used instead of using the EntityStorev2 alias
-
Always use type-safe enums and types instead of raw strings (e.g.,
JobStatus,Role,TableType,NamespaceId,TableId) -
Filesystem vs RocksDB separation of concerns
- Filesystem/file storage logic (cold storage, Parquet files, directory management, file size accounting) must live in
backend/crates/kalamdb-filestore - Key-value storage engines and partition/column family logic must live in
backend/crates/kalamdb-store - Orchestration layers in
kalamdb-core(DDL/DML handlers, job executors) should delegate to these crates and avoid embedding filesystem or RocksDB specifics directly - When adding cleanup/compaction/file lifecycle functionality, implement it in
kalamdb-filestoreand call it fromkalamdb-core
- Smoke Tests Priority: Always ensure smoke tests are passing before committing changes. If smoke tests fail, fix them or the underlying backend issue immediately. Run
cargo test --test smokein theclidirectory to verify. - Tracing Table Field Convention: In spans/events, log
table_id(formatnamespace.table) instead of separatetable_namespaceandtable_namefields. - No SQL Rewrite in Hot Paths: Do not add SQL/DML/SELECT rewrite passes in execution hot paths. Prefer type-safe coercion at typed boundaries (parameter binding, scalar coercion, provider write path, DataFusion-native casts/UDFs explicitly invoked by query authors) to avoid extra parse/transform overhead.
- SDK Changes Must Update Docs: Any change under
link/sdks/**or SDK bridge crates (for examplelink/kalam-link-dart/**) must also update the corresponding SDK docs in theKalamSiterepo (typically../KalamSite/content/sdk/**) and include appropriate test coverage. - Performance-First Execution: Prefer approaches that reduce runtime, allocations, binary size, and compile time; avoid adding abstractions or dependencies that materially slow hot paths or build/test feedback loops without a clear benefit.
- Performance Test Timing: Whenever you run performance tests, benchmarks, or perf-focused e2e cases, record and report how long each relevant test took in seconds.
⚠️ IMPORTANT: Smoke tests require a running KalamDB server! Start the server first withcargo runin thebackenddirectory before running smoke tests. The tests will fail if no server is running.
When adding a new dependency:
- Add it to
Cargo.toml(root) under[workspace.dependencies]with version - Reference it in individual crates using
{ workspace = true } - Add crate-specific features if needed:
{ workspace = true, features = ["..."] } - Enable only the features that are actually required; prefer
default-features = falsewhen defaults pull in unused code or slow compilation.
To update a dependency version:
- Only edit the version in root
Cargo.toml - All crates will automatically use the new version
- Rust 1.92+ (stable toolchain, edition 2021)
- RocksDB 0.24, Apache Arrow 52.0, Apache Parquet 52.0, DataFusion 40.0, Actix-Web 4.4
- RocksDB for write path (<1ms), Parquet for flushed storage (compressed columnar format)
- TypeScript/JavaScript ES2020+ (frontend SDKs)
- WASM for browser-based client library
Job Management System (Phase 9 + Phase 8.5 Complete):
- UnifiedJobManager: Typed JobIds (FL/CL/RT/SE/UC/CO/BK/RS), idempotency, retry logic (3× default with exponential backoff)
- 8 Job Executors: FlushExecutor (complete), CleanupExecutor (✅), RetentionExecutor (✅), StreamEvictionExecutor (✅), UserCleanupExecutor (✅), CompactExecutor (placeholder), BackupExecutor (placeholder), RestoreExecutor (placeholder)
- Status Transitions: New → Queued → Running → Completed/Failed/Retrying/Cancelled
- Crash Recovery: Marks Running jobs as Failed on server restart
- Idempotency Keys: Format "{job_type}:{namespace}:{table}:{date}" prevents duplicate jobs
Authentication & Authorization:
- bcrypt 0.15: Password hashing (cost factor 12, min 8 chars, max 72 chars)
- jsonwebtoken 9.2: JWT token generation and validation (HS256 algorithm)
- HTTP Basic Auth: Base64-encoded username:password (Authorization: Basic header)
- JWT Bearer Tokens: Stateless authentication (Authorization: Bearer header)
- OAuth 2.0 Integration: Google Workspace, GitHub, Microsoft Azure AD
- RBAC (Role-Based Access Control): Four roles (user, service, dba, system)
- Actix-Web Middleware: Custom authentication extractors and guards
- StorageBackend Abstraction:
Arc<dyn StorageBackend>isolates RocksDB dependencies
backend/: Main Rust server workspace; most database engine work starts here.backend/crates/: Core server crates grouped by responsibility; prefer editing the owning crate instead of cross-cutting changes.cli/: Kalam CLI, smoke tests, and CLI-facing integration flows.link/: SDK bridge workspace and shared link infrastructure.link/sdks/typescript/: TypeScript SDK.link/sdks/dart/: Dart/Flutter SDK.link/sdks/dart/lib/src/generatedis generated; regenerate and prepare the SDK withlink/sdks/dart/build.sh.link/kalam-link-dart/: Rust bridge/native layer used by the Dart SDK.pg/: PostgreSQL extension workspace forpg_kalam; seepg/pg_kalam.control,pg/src/,pg/crates/, andpg/tests/.benchv2/: Benchmark harness, scenarios, templates, and results for performance work.ui/: Frontend/admin UI.docs/: Architecture, API, security, and operational documentation.specs/: Historical and active design specs by feature/phase.docker/: Container builds and local deployment layouts.
backend/crates/
- kalamdb-api: HTTP/REST + WebSocket server surface, routes, UI asset serving.
- kalamdb-auth: Authentication/authorization (bcrypt, JWT, RBAC, guards).
- kalamdb-commons: Shared types, IDs, constants, utilities.
- kalamdb-configs: Server configuration structs and loaders.
- kalamdb-core: Core orchestration (DDL/DML handlers, jobs, live queries, schema registry).
- kalamdb-filestore: Filesystem + object-store (S3/GCS/Azure/local) Parquet segment lifecycle.
- kalamdb-observability: Metrics/telemetry helpers and system stats.
- kalamdb-publisher: Durable topic publishing (route matching, offset allocation, payload extraction), synchronous write-path integration.
- kalamdb-raft: Raft consensus, replication, and cluster coordination.
- kalamdb-session: Session context + permission-aware table provider abstraction.
- kalamdb-sharding: Shard models and routing logic.
- kalamdb-dialect: SQL dialect, parsing, classification, compatibility helpers, and reusable DDL ASTs.
- kalamdb-store: RocksDB backend and storage abstractions; provides
EntityStoreandIndexedEntityStore(indexed store) with automatic secondary indexes. - kalamdb-streams: Stream storage and commit log utilities.
- kalamdb-system: System tables + metadata providers (EntityStore/IndexedEntityStore-based),
TopicPublishertrait. - kalamdb-tables: User/shared/stream table providers built on
EntityStore/IndexedEntityStore.
- Rust 2021 edition: Follow standard Rust conventions
- Type-safe wrappers: Use
NamespaceId,TableName,UserId,StorageId,TableTypeenum,UserRoleenum,TableAccessLevelenum instead of raw strings - Error handling: Use
Result<T, KalamDbError>for all fallible operations - Async: Use
tokioruntime, preferasync/awaitover raw futures - Logging: Use
logmacros (info!,debug!,warn!,error!) - Serialization: Use
serdewith#[derive(Serialize, Deserialize)]
- Prefer batching compilation feedback to avoid slow edit-run loops.
- When iterating on multi-file changes, run a single workspace-wide check and capture output to a file, fix all issues, then re-check:
- Example:
cargo check > batch_compile_output.txt 2>&1 - Parse and address all errors/warnings in one pass; avoid running
cargo checkrepeatedly after each tiny edit.
- Example:
- If a task requires multiple related code changes, finish the full edit batch first and only then run
cargo checkorcargo buildwhen validation is actually needed. - Re-run
cargo checkonly after a meaningful batch of fixes. This keeps feedback fast and focused, and prevents thrashing CI and local builds.
- Use
cargo nextest runfor all test executions unless explicitly told otherwise. - For CLI e2e tests: run
cargo nextest run --features e2e-testswithout--no-fail-fast, capture output to a file, then fix failures one-by-one by running only the failing test(s). Re-run the full suite after fixes. - For e2e test runs, do NOT pass
--no-fail-fast. Run normally, fix the first failure, re-run until it passes, then move to the next failing issue. - To verify the full core repo test matrix, start the KalamDB server first and then run
./scripts/test-all.shfrom the repo root. This script runs the Rust workspace tests, the feature-gated FDW import test, PostgreSQL extension e2e tests, the TypeScript SDK tests, the admin UI tests, and the Dart SDK tests. - For performance-focused tests, benchmarks, and perf e2e cases, capture and report the runtime for each relevant test in seconds in the final update.
- Always add
#[ntest::timeout(time)]to every async test wheretimeis the actual observed runtime × 1.5 (to cover slower machines).- Example: if a test took 40s, set
#[ntest::timeout(60000)]. - Recalculate and update timeouts after significant changes to test behavior or data size.
- Example: if a test took 40s, set
- Timeouts are guardrails, not the fix: do not increase a test timeout just because a test started failing. Fix the hang, race, or slow path first, then set the timeout from the measured healthy runtime × 1.5.
- Backend build:
cd backend && cargo build - Backend run (default config):
cd backend && cargo run(server onhttp://127.0.0.1:8080) - Backend run (explicit binary):
cd backend && cargo run --bin kalamdb-server - Full test sweep:
cd /path/to/KalamDB && ./scripts/test-all.sh(start the backend server first) - Backend config bootstrap:
cd backend && cp server.example.toml server.toml - Create API key user:
cd backend && cargo run --bin kalamdb-server -- create-user --name "demo-user" --role "user" - Backend config via env vars:
KALAMDB_SERVER_PORT=9000 KALAMDB_LOG_LEVEL=debug cargo run - CLI build:
cd cli && cargo build --release(binary atcli/target/release/kalam) - CLI smoke tests with env vars:
KALAMDB_SERVER_URL="http://localhost:3000" KALAMDB_ROOT_PASSWORD="mypass" cargo test --test smoke -- --nocapture - Docker build and run:
cd docker/build && docker build -f Dockerfile -t jamals86/kalamdb:latest ../..thencd ../run/single && docker-compose up -d
Authentication Patterns:
- Password Security: ALWAYS use
bcrypt::hash()for password storage, NEVER store plaintext - Timing-Safe Comparisons: Use
bcrypt::verify()for constant-time password verification - JWT Claims: Include
user_id,role,exp(expiration) in token payload - Role Hierarchy: system > dba > service > user (enforced in authorization middleware)
- Generic Error Messages: Use "Invalid username or password" (NEVER "user not found" vs "wrong password")
- Soft Deletes: Set
deleted_attimestamp, return same error as invalid credentials - Authorization Checks: Verify role permissions BEFORE executing database operations
- Storage Abstraction: Use
Arc<dyn StorageBackend>instead ofArc<rocksdb::DB>(except in kalamdb-store)
Always check these before shipping changes that touch APIs, auth, SQL, or storage:
- SQL injection: ensure any internal SQL built from user input is parsed/parameterized and not string-concatenated into privileged queries.
- Auth bypass: confirm every protected endpoint uses
AuthSessionExtractoror verified JWT, not just header presence. - Role escalation: verify role claims are validated against DB and
AS USER/impersonation paths are gated. - Flooding/bruteforce: ensure pre-auth rate limits are enabled and login/refresh endpoints are throttled.
- System tables: confirm non-admin roles cannot read or mutate
system.*tables (including via views). - Nested queries: confirm subqueries/UNION/VIEW cannot bypass system table guards.
- Anonymous access: enumerate public endpoints and validate the data they return is safe.
- File upload/download: validate paths, size limits, storage access checks, and cleanup on failure.
Suggested extra checks:
- Token handling: rotation, expiry, and refresh scope for access vs refresh tokens.
- Secrets in logs: ensure SQL redaction and auth events never log plaintext secrets.
- CORS/Origin: verify WS/HTTP origin checks align with deployment model.
- Health endpoints must be localhost-only unless explicitly authenticated and authorized.
- Never treat
Authorizationheader presence as authentication. Always validate tokens. - Auth endpoints must be IP rate-limited in addition to account lockout.
- JWT secrets must be non-default and at least 32 chars; refuse startup on non-localhost if not.
- Cookies carrying auth tokens must be
HttpOnlyandSameSite=Strict;Securein production. - WebSocket origins must be validated against config or rejected when strict mode is enabled.
- Admin or root password by default should be set or is set to kalamdb123 for testing or writting in tests, and the user should be admin
the folder: link/sdks/dart/lib/src/generated is generated dont modify anything in it, to regenerate this run the link/sdks/dart/build.sh script which also prepares the SDK artefacts