sdb

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2024 License: MIT Imports: 13 Imported by: 1

Documentation

Overview

Package sdb offers a simple key-value database that can be utilized with the go-shelve project.

It should be suitable for a wide range of applications, but the driver directory (go-shelve/driver) provides additional options for configuring the Shelf with other supported databases from the Go ecosystem.

DB Records

In sdb, each database record is represented by a distinct file stored in a bucket, which is a corresponding filesystem directory. The number of documents stored in each bucket is unlimited, and modern filesystems should be able to handle large buckets without significantly affecting performance.

Each file record's name is "base32hex" encoding of the key, which preserves lexical sort order [1]. Keys are limited to 128 characters. The record file is stored as binary data. With this design, Users do not need to worry about hitting the maximum filename length or storing keys with forbidden characters.

Cache

The sdb database uses a memory-based cache to speed up operations. By default, the cache size is unlimited, but it can be configured to a fixed size or disabled altogether.

The cache's design, albeit simple, can enhance the performance of "DB.Get" and "DB.Items" to more than 1 million reads per second on standard hardware.

Atomicity

New records are written atomically to the key-value store. With a file-per-record design, sdb achieves this by using atomic file writes, which consist of creating a temporary file and then renaming it [2].

This ensures that the database's methods are always performed with one atomic operation, significantly simplifying the recovery process.

Currently, the only data that can become inconsistent is the count of stored records, but if this happens, it is detected and corrected at the DB initialization.

As an optimization, records might be written directly without needing a temporary file if the data fits in a single sector since a single-sector write can be assumed to be atomic on some systems [3] [4].

Durability

By default, sdb leverages the filesystem cache to speed up the database writes. This is generally suitable for most applications for which sdb is intended, as modern hardware can offer sufficient protection against crashes and ensure durability.

For the highest level of durability, the WithSynchronousWrites option makes the database synchronize data to persistent storage on each write.

Notes

[1] https://datatracker.ietf.org/doc/html/rfc4648#section-7

[2] On Windows, additional configuration is involved.

[3] https://stackoverflow.com/questions/2009063/are-disk-sector-writes-atomic

[4] https://web.cs.ucla.edu/classes/spring07/cs111-2/scribe/lecture14.html

Example
package main

import (
	"bytes"
	"encoding/gob"
	"log"
	"os"
	"path/filepath"

	"github.com/lucmq/go-shelve/sdb"
)

func main() {
	path := filepath.Join(os.TempDir(), "sdb")

	// Open the database
	db, err := sdb.Open(path)
	if err != nil {
		log.Printf("open: %s", err)
		return
	}
	defer db.Close()

	item := struct {
		Name     string
		Price    float64
		Quantity int
		Active   bool
	}{
		Name:     "Apple",
		Price:    1.99,
		Quantity: 10,
		Active:   true,
	}

	// Encode data as gob
	data := new(bytes.Buffer)
	enc := gob.NewEncoder(data)
	if err = enc.Encode(item); err != nil {
		log.Printf("encode gob: %s", err)
		return
	}

	// Save the data
	err = db.Put([]byte("apple"), data.Bytes())
	if err != nil {
		log.Printf("put: %s", err)
		return
	}
}
Output:

Index

Examples

Constants

View Source
const (
	// DefaultCacheSize is the default size of the cache used to speed up the
	// database operations. A value of -1 represents an unlimited cache.
	DefaultCacheSize = -1

	// MaxKeyLength is the maximum size of a key.
	MaxKeyLength = 128
)

Variables

View Source
var (
	// ErrKeyTooLarge is returned when a key exceeds the maximum length.
	ErrKeyTooLarge = errors.New("key exceeds maximum length")
)

Functions

This section is empty.

Types

type DB

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

DB represents a database, which is created with the Open function.

Client applications must call DB.Close() when done with the database.

A DB is safe for concurrent use by multiple goroutines.

func Open

func Open(path string, options ...Option) (*DB, error)

Open opens the database at the given path. If the path does not exist, it is created.

Client applications must call DB.Close() when done with the database.

func (*DB) Close

func (db *DB) Close() error

Close synchronizes and closes the database.

func (*DB) Delete

func (db *DB) Delete(key []byte) error

Delete removes a key-value pair from the database.

func (*DB) Get

func (db *DB) Get(key []byte) ([]byte, error)

Get retrieves the value associated with a key from the database. If the key is not found, it returns nil.

func (*DB) Has

func (db *DB) Has(key []byte) (bool, error)

Has reports whether a key exists in the database.

func (*DB) Items

func (db *DB) Items(start []byte, order int, fn Yield) error

Items iterates over key-value pairs in the database, calling fn(k, v) for each pair in the sequence. The iteration stops early if the function fn returns false.

The start and order parameters exist for compatibility with the shelve.DB interface and are not currently used.

The operation will acquire a read lock everytime a database record is read and will hold for the duration of the fn callback. Implementations that need to quickly release the lock, should copy the key-value pair and return as soon as possible from the callback.

func (*DB) Len

func (db *DB) Len() int64

Len returns the number of items in the database. If an error occurs, it returns -1.

func (*DB) Put

func (db *DB) Put(key, value []byte) error

Put adds a key-value pair to the database. If the key already exists, it overwrites the existing value.

It returns an error if the key is greater than MaxKeyLength.

func (*DB) Sync

func (db *DB) Sync() error

Sync synchronizes the database to persistent storage.

type Option

type Option func(*DB)

Option is passed to the Open function to create a customized DB.

func WithCacheSize

func WithCacheSize(size int64) Option

WithCacheSize sets the size of the cache used by the database. A value of -1 represents an unlimited cache and a value of 0 disables the cache. The default cache size is -1.

func WithSynchronousWrites

func WithSynchronousWrites(sync bool) Option

WithSynchronousWrites enables synchronous writes to the database. By default, synchronous writes are disabled.

type Yield

type Yield = func(key, value []byte) (bool, error)

Yield is a function called when iterating over key-value pairs in the database. If Yield returns false or an error, the iteration stops.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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