Note
This README was translated by ChatGPT 4o
JsonDB is a high-performance JSON database system that combines Redis-like key-value operations with MongoDB-like document query capabilities.
It integrates a multi-layer memory caching system to deliver optimal read and write performance.
Initially developed in Go for architecture validation, a Rust version will be provided later.This is an extension project of go-redis-fallback, focusing on a JSON-centric database.
It explores database construction using standard libraries and serves as a foundation for learning Rust syntax.
- Architecture: Multi-layer memory cache + disk persistence
- Style: Redis-like key-value operations + MongoDB-like document queries
- Caching: LRU / memory cache structure + cache warming mechanism
- Storage: AOF logs for data safety + three-layer directory structure for JSON file storage
- TTL: Lazy deletion + automatic expiration cleanup + cache eviction policies
go/
├── cmd/
│ ├── cli/main.go # CLI client entry point
│ └── server/main.go # Server entry point
├── internal/
│ ├── command/ # Command parsing and types
│ │ ├── parser.go # Command parser
│ │ └── types.go # Command type definitions
│ ├── server/ # Server core
│ │ ├── server.go # Server main logic
│ │ ├── client.go # Client handling
│ │ ├── clientKV.go # KV operations implementation
│ │ ├── clientDoc.go # Document operations implementation
│ │ └── clientTTL.go # TTL operations implementation
│ ├── storage/ # Storage layer
│ │ ├── config.go # Configuration and path management
│ │ ├── aofReader.go # AOF reader
│ │ └── aofWriter.go # AOF writer
│ └── util/
│ └── util.go # Utility functions
└── data/ # Data storage directory
├── aof/ # AOF log files
│ ├── db_0.aof
│ └── db_1.aof
└── 0/ # Database 0 JSON files
└── 09/8f/6b/ # Three-layer directory structure
└── hash.json
- Multi-database support (0-15)
- Three-layer directory structure for file storage (MD5 hash-based)
- AOF persistence mechanism (append-only log files)
- Automatic expiration cleanup (runs every minute)
- CLI client interface
- Support single-action commands using
-c "SET <key>" - Implement LRU caching mechanism
- Cache warming functionality
- Connection pool management
-
SELECT <db:int>- Select database (0-15) -
GET <key>- Retrieve the value of a specified key -
SET <key> <value> [ttl_second|expire_time]- Set a key-value pair with optional expiration -
DEL <key1> [key2] ...- Delete one or more keys -
EXISTS <key>- Check if a key exists -
KEYS <pattern>- Search for keys matching a pattern -
TYPE <key>- Get the data type of a key
-
FIND <key> <filters> [page:int] [offset:int]- Query documents matching conditions{filters:[]}, supports pagination -
ADD <key> <value>- Add a document to a collection -
SORT <key> <filters> <sort_by> [page:int] [offset:int]- Sort query results using{sort:[]} -
UPDATE <key> <filters> <set>- Update documents matching conditions using{set:{}} -
REMOVE <key> <filters>- Delete documents matching conditions
-
TTL <key> [filters]- View the remaining time for a key -
EXPIRE <key> <ttl_second|expire_time> [filters]- Set the expiration time for a key -
PERSIST <key> [filters]- Remove the expiration setting for a key
-
PING- Test connection -
HELP- Display help information - Performance testing
- Error handling
- Unit testing
# Basic query
FIND users {"name":"John"}
# Compound query
FIND products {"category":"electronics","price":{"$lt": 1000}}
# Regex query
FIND users {"email":{"$regex":".*@gmail.com"}}
# Range query
FIND orders {"date":{"$gte": "2024-01-01","$lte": "2024-12-31"}}# Paginated query
FIND users {"status": "active"} 0 10
# Sorted query
SORT users {"name": "John"} {"age": 1, "name": -1}
# Sorted + paginated query
SORT users {"name": "John"} {"age": 1, "name": -1} 0 10# Single update
UPDATE users {"name": "John"} {"$set": {"age": 30}}
# Batch update
UPDATE products {"category": "electronics"} {"$inc": {"stock": -1}}
# Conditional update
UPDATE orders {"status": "pending"} {"$set": {"status": "processing"}}# Conditional delete
REMOVE users {"status": "inactive"}
# Batch delete
REMOVE logs {"date": {"$lt": "2024-01-01"}}This project is licensed under the MIT license.
©️ 2025 邱敬幃 Pardn Chiu