Goldb is a lightweight, efficient key-value database engine that leverages the power of a Log-Structured Merge (LSM) tree for high performance and reliability. The engine
package serves as the primary interface, abstracting complex internal mechanisms to provide an intuitive API for users.
- TODOs
- How It Works
- Setup Instructions
- Running the Server
- RESTful API
- How to Use The Engine
- Features
- Project Design
- Contributing
- Layer the DB engine with an HTTP server, like CouchDB.
- Manage periodic flushes.
- Implement a WAL (Write-Ahead Log).
- Make the WAL smarter by ignoring deleted set operations (Compaction).
- Write better documentation.
- Make logging conditional.
- Utilze go routines.
- Add locks to avoid concurrency issues.
- Use a compaction algorithm and perform compaction periodically.
- Add the use of bloom filters.
Goldb is built around the LSM tree, a modern data structure optimized for write-heavy workloads. Here’s how it operates:
-
In-Memory Data Handling (Memtable):
- Data is first written to an in-memory AVL tree, acting as the active layer of the LSM tree.
- The memtable ensures fast writes and lookups, maintaining sorted data for efficient flushing.
-
Persistent Storage (SSTables):
- When the memtable reaches a size limit, its contents are flushed to disk as Sorted String Tables (SSTables).
- SSTables are immutable and sorted, enabling efficient binary search operations during reads.
-
Storage Management:
- The engine manages the lifecycle of SSTables, including merging and compaction, ensuring that older tables are consolidated into fewer, larger ones to optimize performance.
-
High-Level API:
- The
engine
package provides an easy-to-use interface for storing, retrieving, and deleting key-value pairs, while abstracting the complexities of the LSM tree.
- The
- Go (Golang) version 1.20 or higher.
- A Unix-based or Windows operating system.
-
Clone the repository:
git clone https://github.com/hasssanezzz/goldb-engine.git cd goldb-engine
-
Install dependencies:
go mod tidy
-
Build the project:
go build -o goldb
By default, the server will:
- Run on
localhost
at port3011
. - Store data in the
~/.goldb
directory.
Run the server:
./goldb
You can customize the server's host, port, and data directory using flags:
./goldb -h <host> -p <port> -s <source_directory>
./goldb -h 0.0.0.0 -p 8080 -s /path/to/data
To view the available options:
./goldb --help
All operations use custom headers for key specification.
# PUT or POST to set/update a key
curl -X POST http://localhost:3011 \
-H "Key: mykey" \
-d "myvalue"
# GET a value by key
curl -X GET http://localhost:3011 \
-H "Key: mykey"
# DELETE a key
curl -X DELETE http://localhost:3011 \
-H "Key: mykey"
# Get all keys
curl -X GET http://localhost:3011 -H "prefix:"
# Get keys with a prefix
curl -X GET http://localhost:3011 -H "prefix:user"
Initialize the database engine with a directory for storing SSTables:
package main
import (
"log"
"github.com/hasssanezzz/goldb-engine/engine"
)
func main() {
db, err := engine.New("./.db")
if err != nil {
log.Fatalf("Failed to initialize engine: %v", err)
}
defer db.Close() // Ensure proper cleanup
}
Store key-value pairs using the Set
method:
db.Set("key-1", "value-1")
db.Set("key-2", "value-2")
Retrieve stored values using the Get
method:
value, err := db.Get("key-1")
keys, err := db.Scan("user") // returns all keys starting with "user"
keys, err := db.Scan("") // returns all keys
Flush the in-memory data to SSTables manually if needed:
err = db.Flush()
if err != nil {
log.Fatalf("Failed to flush data: %v", err)
}
Remove a key-value pair using the Delete
method:
if err := db.Delete("key-1"); err != nil {
log.Fatalf("Failed to delete data: %v", err)
}
-
Powered by LSM-Tree:
- Combines in-memory writes and immutable SSTables for fast and efficient storage.
- Supports write-heavy workloads while maintaining read efficiency.
-
Write Optimization:
- In-memory AVL tree ensures quick inserts and updates.
- Periodic flushing to SSTables ensures data persistence.
-
Read Optimization:
- Binary search through sorted SSTables for efficient lookups.
- Recently written data is quickly accessible in memory.
-
Persistence and Recovery:
- Write-Ahead Log (WAL): Ensures durability and enables crash recovery by logging all writes before they are applied to the in-memory structure.
- Durable storage with immutable SSTables.
- [Will do] Optimized disk usage through compaction of older SSTables.
-
API Layer:
- Implements HTTP endpoints using the
api
package. - Maps HTTP requests to database operations (e.g., GET, POST, DELETE).
- Implements HTTP endpoints using the
-
Engine Layer:
- Manages in-memory storage, SSTables, and compaction.
- Provides a clean interface for database operations.
-
Write-Ahead Log (WAL):
- Logs all write operations before applying them to in-memory structures.
- Ensures data durability and enables recovery from crashes.
-
Periodic Flushing:
- Ensures that in-memory data is frequently persisted to disk.
-
Server Initialization:
- Automatically creates a
.goldb
directory in the user’s home if no custom directory is provided.
- Automatically creates a
I welcome contributions! Here’s how you can help:
- Fork the repository.
- Create a feature branch.
- Make your changes and submit a pull request.