FileDB
golang build a file DB
Install / Use
/learn @DanielZhui/FileDBREADME
FileDB 📁
FileDB is a simple key-value store implementation in Go that persists data to disk. It provides basic CRUD operations and is designed for lightweight storage needs. 🚀
✨ Features
- 💾 Persistent storage: Data is stored on disk and can be retrieved across program restarts.
- 🔄 Basic CRUD operations: Set, Get, Update, and Delete operations are supported.
- 🗂️ Key directory: Maintains an in-memory index of keys for fast lookups.
- ➕ Append-only write: New data is appended to the file, improving write performance.
🛠️ Installation
To use FileDB in your Go project, you can install it using go get:
go get github.com/DanielZhui/fileDB@v0.0.1
🚀 Usage
Here's a basic example of how to use FileDB:
package main
import (
"fmt"
"log"
"github.com/DanielZhui/fileDB"
)
func main() {
filePath := "./test.db"
ds, err := fileDB.InitDiskStore(filePath)
if err != nil {
log.Fatalf("Failed to initialize disk store: %v", err)
}
// Set a key-value pair
err = ds.Set("hello", "world")
if err != nil {
log.Printf("Failed to set 'hello': %v", err)
}
// Get a value
value, err := ds.Get("hello")
if err != nil {
log.Printf("Failed to get 'hello': %v", err)
} else {
fmt.Println(value) // Output: world
}
// Update a value
err = ds.Update("hello", "new world")
if err != nil {
log.Printf("Failed to update 'hello': %v", err)
}
// Delete a key
err = ds.Delete("hello")
if err != nil {
log.Printf("Failed to delete 'hello': %v", err)
}
// List all keys (reloads the key directory from the file)
ds.List(filePath)
}
📚 API
- 🆕
InitDiskStore(fileName string) (*DiskStore, error): Initialize a new DiskStore instance. - ✍️
Set(key string, value string) error: Set a key-value pair. - 🔍
Get(key string) (string, error): Retrieve the value for a given key. - 🔄
Update(key string, value string) error: Update the value for an existing key. - 🗑️
Delete(key string) error: Delete a key-value pair. - 📋
List(filePath string): Reload the key directory from the file.
🏗️ Data Structure
FileDB uses a simple file format to store data:
- 📊 Header (12 bytes): timestamp (4 bytes), key size (4 bytes), value size (4 bytes)
- 🔑 Key: Variable length string
- 📄 Value: Variable length string
⚠️ Limitations
- 🔒 FileDB is not designed for concurrent access. It's suitable for single-threaded applications or scenarios where external synchronization is applied.
- 💾 The entire key directory is kept in memory, which may not be suitable for very large datasets.
- 🗑️ Deleted keys are not removed from the file, potentially leading to file size growth over time.
🤝 Contributing
Contributions to FileDB are welcome! Please feel free to submit a Pull Request. 😊
📜 License
This project is licensed under the MIT License - see the LICENSE file for details.
Related Skills
node-connect
350.8kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
110.4kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
350.8kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
350.8kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
