Put summary in design.md

This commit is contained in:
2025-08-14 13:10:39 -04:00
parent 2c247fa75e
commit b3e504a6f9
2 changed files with 132 additions and 2 deletions

131
design.md
View File

@@ -1 +1,132 @@
# WeaselDB Design Overview
## Project Summary
WeaselDB is a high-performance write-side database component designed for systems where reading and writing are decoupled. The system focuses exclusively on handling transactional commits with optimistic concurrency control, while readers are expected to maintain their own queryable representations by subscribing to change streams.
## Architecture Overview
### Core Components
#### 1. **Arena Allocator** (`src/arena_allocator.hpp`)
- **Ultra-fast memory allocator** (~1ns per allocation vs ~20-270ns for malloc)
- **Lazy initialization** with geometric block growth (doubling strategy)
- **Intrusive linked list design** for minimal memory overhead
- **Memory-efficient reset** that keeps the first block and frees others
- **STL-compatible interface** via `ArenaStlAllocator`
Key features:
- O(1) amortized allocation
- Proper alignment handling for all types
- Move semantics for efficient transfers
- Requires trivially destructible types only
#### 2. **Commit Request Parser** (`src/commit_request.{hpp,cpp}`)
- **High-performance JSON parser** using `weaseljson` library
- **Streaming parser support** for incremental parsing of network data
- **Arena-based string storage** for zero-copy string handling
- **Base64 decoding** for binary key/value data
- **Comprehensive validation** of transaction structure
Parser capabilities:
- One-shot parsing for complete JSON
- Streaming parsing for network protocols
- Parse state management with error recovery
- Memory-efficient string views backed by arena storage
#### 3. **Configuration System** (`src/config.{hpp,cpp}`)
- **TOML-based configuration** using `toml11` library
- **Structured configuration** with server, commit, and subscription sections
- **Default fallback values** for all configuration options
- **Type-safe parsing** with validation
Configuration domains:
- **Server**: bind address, port, request size limits
- **Commit**: request ID validation, retention policies
- **Subscription**: buffer management, keepalive intervals
### Data Model
#### Transaction Structure
```
CommitRequest {
- request_id: Optional unique identifier
- leader_id: Expected leader for consistency
- read_version: Snapshot version for preconditions
- preconditions[]: Optimistic concurrency checks
- point_read: Single key existence/content validation
- range_read: Range-based consistency validation
- operations[]: Ordered mutation operations
- write: Set key-value pair
- delete: Remove single key
- range_delete: Remove key range
}
```
#### Memory Management
- **Arena-based allocation** ensures efficient bulk memory management
- **String views** eliminate unnecessary copying of JSON data
- **Zero-copy design** for binary data handling
- **Automatic memory cleanup** on transaction completion
### API Design
The system implements a RESTful API with three core endpoints:
1. **GET /v1/version**: Retrieve current committed version and leader
2. **POST /v1/commit**: Submit transactional operations
3. **GET /v1/subscribe**: Stream committed transactions (implied)
4. **GET /v1/status**: Check commit status by request_id (implied)
### Performance Characteristics
#### Memory Allocation
- **~1ns allocation time** vs standard allocators
- **Bulk deallocation** eliminates individual free() calls
- **Geometric growth** minimizes allocation frequency
- **Alignment-aware** allocation prevents performance penalties
#### JSON Parsing
- **Streaming parser** handles large payloads efficiently
- **Incremental processing** suitable for network protocols
- **Arena storage** eliminates string allocation overhead
- **Base64 decoding** integrated into parsing pipeline
### Design Principles
1. **Performance-first**: Every component optimized for high throughput
2. **Memory efficiency**: Arena allocation eliminates fragmentation
3. **Zero-copy**: Minimize data copying throughout pipeline
4. **Streaming-ready**: Support incremental processing
5. **Type safety**: Compile-time validation where possible
6. **Resource management**: RAII and move semantics throughout
### Testing & Benchmarking
The project includes comprehensive testing infrastructure:
- **Unit tests** using doctest framework
- **Performance benchmarks** using nanobench
- **Memory allocation benchmarks** for arena performance
- **JSON parsing validation** for correctness
Build targets:
- `test_arena_allocator`: Arena allocator functionality tests
- `test_commit_request`: JSON parsing and validation tests
- `bench_arena_allocator`: Performance benchmarking
### Dependencies
- **weaseljson**: High-performance streaming JSON parser
- **toml11**: TOML configuration file parsing
- **doctest**: Lightweight testing framework
- **nanobench**: Micro-benchmarking library
### Future Considerations
This write-side component is designed to integrate with:
- **Leader election** systems for distributed consensus
- **Replication** mechanisms for fault tolerance
- **Read-side systems** that consume the transaction stream
- **Monitoring** systems for operational visibility
The modular design allows each component to be optimized independently while maintaining clear interfaces for system integration.

View File

@@ -259,8 +259,7 @@ public:
// Reset parser state
if (json_parser_) {
WeaselJsonParser_destroy(json_parser_);
json_parser_ = nullptr;
WeaselJsonParser_reset(json_parser_);
}
parser_context_ = ParserContext(&arena_);
parser_context_.state_stack.push(ParseState::Root);