Goldb is a simple key-value database engine built in Go, inspired by Log-Structured Merge-Trees (LSM-Trees). It ensures durability through a Write-Ahead Log (WAL), uses an in-memory AVL tree (memtable) for temporary storage, and employs SSTables for persistent storage. The project includes a REST API for interaction and can be embedded in other Go applications.
Goldb is designed for learning and lightweight use cases, implementing core LSM-tree principles such as memtables, SSTables, WAL, and compaction, while keeping the architecture simple and easy to understand.
cmd/
: Contains the main package to run the server.internal/
: Core components:index_manager/
: Manages indexes, including SSTables and levels.memtable/
: In-memory AVL tree for temporary key-value storage.storage_manager/
: Handles data writing and reading to persistent storage.wal/
: Write-Ahead Log for durability.
api/
: Provides RESTful API endpoints.
Goldb is inspired by Log-Structured Merge-Trees (LSM-Trees), a popular design for key-value storage systems optimized for write-heavy workloads. Here's how Goldb implements LSM-tree-like behavior:
-
Memtable (In-Memory AVL Tree):
- Stores key-value pairs temporarily in memory.
- Provides fast writes and reads for recently inserted data.
-
Write-Ahead Log (WAL):
- Ensures durability by logging all writes before they are applied to the memtable.
- Allows recovery of data in case of a crash.
-
SSTables (Sorted String Tables):
- Immutable, sorted files on disk that store key-value pairs.
- When the memtable is full, it is flushed to disk as an SSTable.
-
Compaction:
- Merges multiple SSTables into a single larger SSTable (a "level") when the number of SSTables exceeds a threshold.
- Improves read performance and reduces disk usage by removing redundant or deleted keys.
-
Index Manager:
- Manages the organization of SSTables and levels.
- Handles compaction and ensures efficient key lookups across the memtable, SSTables, and levels.
-
Prerequisites:
- Go installed on your system.
-
Setup:
- Clone the repository:
git clone https://github.com/hasssanezzz/goldb.git
- Build the project:
go build -o goldb-engine
- Run the server:
./goldb-engine
- The server runs on
http://localhost:3011
.
- Clone the repository:
-
POST /: Set a key-value pair.
- Headers:
key
- Data: Value as a byte array.
- Example:
curl -X POST -H "key: testKey" -d "testValue" http://localhost:3011
- Headers:
-
GET /: Get the value by key.
- Headers:
key
- Example:
curl -X GET -H "key: testKey" http://localhost:3011
- Headers:
-
DELETE /: Delete a key.
- Headers:
key
- Example:
curl -X DELETE -H "key: testKey" http://localhost:3011
- Headers:
-
GET / with prefix header: Scan keys with a prefix.
- Headers:
prefix
- Example:
curl -X GET -H "prefix: test" http://localhost:3011
- Headers:
-
Import the Package:
import "github.com/hasssanezzz/goldb"
-
Create and Use the Engine:
package main import ( "log" "github.com/hasssanezzz/goldb" ) func main() { db, err := goldb.New("path/to/home") if err != nil { log.Fatal(err) } defer db.Close() // Set a key-value pair err = db.Set("testKey", []byte("testValue")) if err != nil { log.Fatal(err) } // Get a value by key value, err := db.Get("testKey") if err != nil { log.Fatal(err) } log.Println(string(value)) keys, err := db.Scan("user") // returns all keys starting with "user" keys, err := db.Scan("") // returns all keys // Delete a key err = db.Delete("testKey") if err != nil { log.Fatal(err) } }
Project is not finished yet.
- 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).
- Use a compaction algorithm and perform compaction periodically.
- Utilze go routines.
- Add the use of bloom filters.
- Write better documentation.
- Make logging conditional.