Skip to content

A lightweight key-value store database implemented in Go, featuring dynamic data storage, basic CRUD operations, and query functionality. Currently uses JSON file storage.

Notifications You must be signed in to change notification settings

Mahopanda/golang-database

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Key-Value Database

This project is a simple key-value store database implemented in Go, inspired by NoSQL databases such as MongoDB. It allows for dynamic data storage using map[string]interface{} and supports basic CRUD (Create, Read, Update, Delete) operations, along with query capabilities. Currently, the data is stored only in JSON files, as a key-value storage engine, like Bitcask, has not yet been implemented.

Features

  • Dynamic Schema: Stores data without requiring predefined models or structures.
  • CRUD Operations: Supports Create, Read, Update, Delete operations for collections.
  • Query Functionality: Query collections using a custom filter function to search for records.
  • File-based Storage: Data is stored in JSON format in the local file system.
  • Concurrency Safe: Uses sync.Mutex to manage concurrent access to collections.

Getting Started

Prerequisites

  • Go 1.16 or later

Installation

  1. Clone the repository:

    git clone https://github.com/Mahopanda/golang-database.git
  2. Navigate to the project directory:

cd golang-database
  1. Install dependencies (if any):
    go mod tidy
    

Usage

To use the key-value store in your project, follow these steps:

  1. Initialize the Logger, Storage, and Driver:
logger := database.NewConsoleLogger()
serializer := &database.JSONSerializer{}
store := database.NewFileStore("./storage", serializer)
db := database.NewDriver(store, lockManager, logger)
  1. Insert Data:
user := map[string]interface{}{
    "Name":    "John",
    "Age":     30,
    "Contact": "[email protected]",
}

err := db.Write("users", "john", user)
if err != nil {
    logger.Error("Failed to write user:", err)
}
  1. Read Data:
var result map[string]interface{}
err := db.Read("users", "john", &result)
if err != nil {
    logger.Error("Failed to read user:", err)
}
  1. Query Data:
results, err := db.Query("users", func(data map[string]interface{}) bool {
    return data["Age"] == 30
})
  1. Delete Data:
err := db.Delete("users", "john")
if err != nil {
    logger.Error("Failed to delete user:", err)
}

Running Tests

To run the unit tests for the project, execute the following command:

go test ./...

Folder Structure

├── database/           # Core logic for the key-value database
│   ├── driver.go       # Handles high-level operations and concurrency control
│   ├── storage.go      # File-based storage implementation
│   ├── lock_manager.go # Contains logic for Lock Manager
│   └── logger.go       # Logger implementation
│   └── serializer.go   # Contains logic for Serializer
├── test/               # Test cases for the database
└── main.go             # Example usage of the key-value database

About

A lightweight key-value store database implemented in Go, featuring dynamic data storage, basic CRUD operations, and query functionality. Currently uses JSON file storage.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages