filedb

package module
v0.0.0-...-bd42065 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 3, 2024 License: BSD-3-Clause Imports: 9 Imported by: 0

README

FileDB

FileDB is a simple file-based database written in Go. It provides basic CRUD operations on documents, which are stored as JSON files in a directory structure. The library also handles concurrent modifications and provides a simple key-based access to the documents.

Installation

To use FileDB in your Go project, you can install it by running:

go get github.com/oxplot/filedb

Usage

Here is a basic example of how to use FileDB:

package main

import (
	"fmt"
	"github.com/yourusername/filedb"
)

func main() {
	db, err := filedb.Open("/path/to/db")
	if err != nil {
		panic(err)
	}

	err = db.Set("key", "value")
	if err != nil {
		panic(err)
	}

	value, err := db.Get("key")
	if err != nil {
		panic(err)
	}

	fmt.Println(value) // Output: value
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrConcurrentMod = errors.New("concurrent modification")

ErrConcurrentMod is returned by if the key is concurrently modified.

Functions

This section is empty.

Types

type DB

type DB struct {
	// contains filtered or unexported fields
}

DB is a file-based database.

func Open

func Open(root string) (*DB, error)

Open opens/initializes a database at the given path.

func (*DB) Delete

func (db *DB) Delete(key string) error

Delete deletes the document for the given key.

func (*DB) Get

func (db *DB) Get(key string) (any, error)

Get returns the document for the given key, or nil if it does not exist.

func (*DB) List

func (db *DB) List(prefix string) ([]string, error)

List returns the keys in the database that have the given prefix.

func (*DB) Set

func (db *DB) Set(key string, doc any) error

Set sets the document for the given key.

func (*DB) SetWithRetry

func (db *DB) SetWithRetry(key string, doc any, retries int) error

SetWithRetry sets the document for the given key, retrying the given number of times if the key is concurrently modified.

func (*DB) Update

func (db *DB) Update(key string, apply func(existing any) (any, error), retries int) (any, error)

Update updates the document for the given key, using the given function to apply the update. The function will be called with the current document for the key, or nil if the key does not exist. If the function returns nil, the key will be deleted.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL