minidb

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

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

Go to latest
Published: Apr 3, 2023 License: GPL-2.0 Imports: 14 Imported by: 0

README

minidb

A storage engine similar to bitcask.

Getting Started

Installing

To start using minidb, install Go 1.19 or above. Minidb needs go modules. From your project, run the following command

$ go get github.com/yanghao888/minidb

This will retrieve the library.

Usage
// Open database
dir := filepath.Join(os.TempDir(), "minidb")
opts := minidb.DefaultOptions(dir)
db, err := minidb.Open(opts)
if err != nil {
    fmt.Printf("Error while opening minidb: %v\n", err)
    os.Exit(1)
}
defer db.Close()

// Put key-value
err = db.Put([]byte("name"), []byte("lion"))
if err != nil {
    fmt.Printf("Put value error: %v\n", err)
    return
}

// Get value
val, err := db.Get([]byte("name"))
if err != nil {
    fmt.Printf("Get value error: %v\n", err)
    return
}
fmt.Printf("Value is: %v\n", string(val))

// Delete key
err = db.Delete([]byte("name"))
if err != nil {
    fmt.Printf("Delete value error: %v\n", err)
    return
}

Design

Minidb’s design is based on a paper titled Bitcask: A Log-Structured Hash Table for Fast Key/Value Data.

Benchmarks

The benchmarking code, and the detailed logs for the benchmarks can be found in the benchmark package.

Run it by executing the following command in the root directory:

$ make bench

License

Minidb is licensed under the term of the GPLv2 License.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrLogFileSize is returned when "opt.LogFileSize" option is not within the valid range.
	ErrLogFileSize = errors.New("Invalid LogFileSize, must be between 1MB and 2GB")

	ErrDatabaseClosed = errors.New("Database already closed")

	ErrEmptyKey = errors.New("Key cannot be empty")

	ErrKeyNotFound = errors.New("Key not found")

	ErrFileNotFound = errors.New("File not found")

	ErrGcWorking = errors.New("Gc is working")
)

Functions

func OpenOrCreateFileWithZeroOffset

func OpenOrCreateFileWithZeroOffset(path string, flag int) (*os.File, uint32, error)

OpenOrCreateFileWithZeroOffset Opens or create file for path, and seek start.

func TruncateAndCloseFile

func TruncateAndCloseFile(fd *os.File, size uint32) error

Types

type DB

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

func Open

func Open(opt Options) (*DB, error)

Open return a new DB instance.

func (*DB) Close

func (db *DB) Close() (err error)

Close an opened DB instance.

func (*DB) Delete

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

Delete deletes a key. This is done by adding a deleted marker for the key.

func (*DB) Get

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

Get looks for key and returns corresponding Item. If key is not found, ErrKeyNotFound is returned.

func (*DB) Merge

func (db *DB) Merge() error

Merge cleans old log file and rewrite key-value pair index.

func (*DB) Put

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

Put adds a key-value pair to the database.

type Entry

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

Entry provides key size, value size, key, value.

func NewEntry

func NewEntry(key, val []byte, mark EntryMark) *Entry

func (*Entry) Size

func (e *Entry) Size() uint32

Size returns the size of the bytes occupied.

type EntryMark

type EntryMark byte
const (
	Normal EntryMark = iota
	Tombstone
)

type Index

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

Index is used in hint file.

func (*Index) Size

func (idx *Index) Size() uint32

Size returns the size of the bytes occupied.

type Options

type Options struct {

	// Directory to store the data in.
	Dir string

	// Size of single log file.
	LogFileSize int64
}

Options are params for creating DB object.

func DefaultOptions

func DefaultOptions(dir string) Options

DefaultOptions sets a list of recommended options for good performance. Feel free to modify these to suit your needs.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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