How curiouslei/daredis works
Project type and purpose
• This is a Java‑based in‑memory key‑value database server, modeled on Redis (open‑source database tool).
• It accepts client connections, parses Redis‑style commands, executes them against in‑memory data structures, and optionally appends commands to an AOF persistence file.
File structure
• Top‑level: pom.xml, README.md, src/main/java, src/main/resources, src/test/java.
• Packages under src/main/java/cn/buptleida:
• conf – command definitions and mapping factory
• database – high‑level RedisServer/RedisDB, RedisClient proxy
• persistence – AOF persistence handler
• structure – Redis data types (String, List, Set, ZSet, Hash) and underlying data structures (Dict, SkipList, ZipList, SDS)
• util – helper classes (CloseUtil, ConvertUtil, IoThreadFactory, etc.)
• src/main/resources holds initial AOF file and command metadata (cmdTable.txt).
README for additional insights
• Confirms it is a Redis‑inspired key‑value store.
• Shows performance tuning steps (switching to single‑thread model, dynamic buffers).
• No built‑in clustering or replication—focus is a standalone server.
Main components
a. Clients
• Test clients in src/test: NIOConnector, Netty clients, pressure tests.
• External real‑world Redis clients.
b. Network I/O layer
• IOService / IOServiceSingle
• Connector, IOClient
• ioProvider / SingleIOSelectorProvider
• Core dispatchers: ReceiveDispatcher, SendDispatcher, Sender, Receiver
• Packet formats: StringReceivePacket, StringSendPacket, StringArrayReceivePacket, etc.
c. Command layer
• CommandMapFactory loads Command definitions (cmdTable.txt) into a map
• Toast for formatting responses
• CmdExecutor interface implemented by each structure (RedisString, RedisList, etc.)
d. Data storage layer
• In‑memory structures under structure: RedisString, RedisList, RedisHash, RedisSet, RedisZSet
• Underlying collections: Dict (hash table), SkipList, ZipList, SDS (simple dynamic string), IntSet
e. Persistence layer
• AOF file writer / AofHandler
• AOF class that serializes commands to appendonly.aof
f. Server bootstrap
• SvrFrame / SvrFrameLinux initializes network, loads configuration, instantiates RedisServer
• RedisServer ties network, command dispatcher, and persistence together
Relationships and interactions
• Client → TCP socket → IOService → Connector → ReceiveDispatcher
• ReceiveDispatcher → parse into ReceivePacket → CommandMapFactory → find CmdExecutor
• CmdExecutor → read/write in‑memory structures → if write, hand command to AofHandler → append to AOF
• CmdExecutor → Toast → SendPacket → SendDispatcher → back to client
• RedisServer orchestrates lifecycle: startup, load AOF, handle shutdown, background AOF rewriting (if implemented)
Architectural patterns and design principles
• Reactor pattern for non‑blocking I/O (ioProvider, selectors, dispatchers)
• Command pattern for mapping commands to executors
• Proxy pattern for RedisClient (database/ProxyInvocationHandler)
• Single‑thread vs multi‑thread model options for both client and server sides (toggled in code)
• Separation of concerns: I/O, command parsing/execution, data storage, persistence
Final checklist
• Clear component labels (include package names or representative class names).
• Sub‑groupings for packages/modules.
• Resource and configuration files shown feeding into startup or command loading.