bitcask

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2024 License: MIT Imports: 15 Imported by: 0

README

Translate to: 简体中文

⚠️ Most of the code was generated by Claude Sonnet 3.5 and GitHub Copilot, so there may be errors. Please use with caution.

go-bitcask

go-bitcask is a simple, fast, and efficient key-value store written in Go. It is inspired by the Bitcask storage model used in Riak(https://riak.com/assets/bitcask-intro.pdf).

Features
  • Simple API for storing and retrieving key-value pairs
  • Batch operations for efficient bulk writes and reads
  • Data compression support
  • Periodic data file merging
  • Snapshot creation for backups
  • Iterator for traversing keys
Installation

To install go-Bitcask, use go get:

go get github.com/yonwoo9/go-bitcask
Usage

Here is a simple example of how to use go-bitcask:

package main

import (
    "fmt"
    "github.com/yonwoo9/go-bitcask"
)

func main() {
    db, err := bitcask.Open("test")
    if err != nil {
    panic(err)
}
defer db.Close()

	// Put a key-value pair
	if err = db.Put("key1", []byte("value1")); err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println("Put key1 success")

	// Get the value for a key
	value, err := db.Get("key1")
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println("Get key1:", string(value))

	// Batch put key-value pairs
	batch := map[string][]byte{
		"key2": []byte("value2"),
		"key3": []byte("value3"),
	}
	if err = db.BatchPut(batch); err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println("Batch put success")

	// Batch get values for keys
	keys := []string{"key2", "key3"}
	values, err := db.BatchGet(keys)
	if err != nil {
		fmt.Println(err)
		return
	}
	for k, v := range values {
		fmt.Printf("Batch get key:%s, val:%s\n", k, string(v))
	}

	// Delete a key
	if err = db.Delete("key1"); err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println("Delete key1 success")

	// Iterate over keys
	iterator := db.Iterator()
	for iterator.Next() {
		key := iterator.Key()
		value, err := iterator.Value()
		if err != nil {
			fmt.Println(err)
			continue
		}
		fmt.Printf("Iterator key:%s, val:%s\n", key, string(value))
	}
}
API

Here is the API documentation for go-bitcask:

func Open(dir string, opts ...ConfOption) (*Bitcask, error)

Opens a Bitcask database in the specified directory with optional configuration options.

func (b *Bitcask) Put(key string, value []byte) error

Stores a key-value pair in the Bitcask database.

func (b *Bitcask) Get(key string) ([]byte, error)

Retrieves the value associated with the specified key from the Bitcask database.

func (b *Bitcask) BatchPut(batch map[string][]byte) error

Stores multiple key-value pairs in the Bitcask database in a batch operation.

func (b *Bitcask) BatchGet(keys []string) (map[string][]byte,
error)

Retrieves the values associated with multiple keys from the Bitcask database in a batch operation.

func (b *Bitcask) Delete(key string) error

Deletes the specified key from the Bitcask database.

func (b *Bitcask) Iterator() *Iterator

Creates an iterator over the keys in the Bitcask database.

func (b *Bitcask) Close() error

Closes the Bitcask database and releases any resources associated with it.

License

go-bitcask is licensed under the MIT License. See the LICENSE file for details.

Documentation

Index

Constants

View Source
const DefaultMaxDatafileSize = 1020 * 1024 * 10

DefaultMaxDatafileSize is the default maximum size of a datafile.

Variables

View Source
var (
	ErrKeyNotFound = errors.New("key not found")
	ErrIOFailure   = errors.New("I/O operation failed")
)

Functions

This section is empty.

Types

type Bitcask

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

func Open

func Open(dir string, opts ...ConfOption) (*Bitcask, error)

Open opens a Bitcask database instance.

func (*Bitcask) BatchGet

func (b *Bitcask) BatchGet(keys []string) (map[string][]byte, error)

BatchGet retrieves multiple key-value pairs from the Bitcask database.

func (*Bitcask) BatchPut

func (b *Bitcask) BatchPut(pairs map[string][]byte) error

BatchPut inserts multiple key-value pairs into the Bitcask database.

func (*Bitcask) Close

func (b *Bitcask) Close() error

Close closes the Bitcask database, ensuring all files are properly closed and memory maps are unmapped.

func (*Bitcask) Delete

func (b *Bitcask) Delete(key string) error

Delete removes a key-value pair from the Bitcask database.

func (*Bitcask) Get

func (b *Bitcask) Get(key string) ([]byte, error)

Get retrieves the value associated with a given key from the Bitcask database.

func (*Bitcask) Iterator

func (b *Bitcask) Iterator() *Iterator

Iterator creates an iterator over the key-value pairs in the Bitcask database.

func (*Bitcask) Put

func (b *Bitcask) Put(key string, value []byte) error

Put inserts a key-value pair into the Bitcask database.

func (*Bitcask) Snapshot

func (b *Bitcask) Snapshot(snapshotDir string) error

Snapshot creates a snapshot of the current state of the Bitcask database.

type ConfOption

type ConfOption func(*Config)

func CompressData

func CompressData(compress bool) ConfOption

CompressData sets whether to compress data.

func MaxDatafileSize

func MaxDatafileSize(size int64) ConfOption

MaxDatafileSize sets the maximum size of a datafile.

func MergeInterval

func MergeInterval(interval time.Duration) ConfOption

MergeInterval sets the interval for merging datafiles.

func MergeThreshold

func MergeThreshold(threshold int) ConfOption

MergeThreshold sets the threshold for merging datafiles.

func SyncWrites

func SyncWrites(sync bool) ConfOption

SyncWrites sets whether to sync writes to disk.

type Config

type Config struct {
	MaxFileSize    int64
	MergeThreshold int
	SyncWrites     bool
	CompressData   bool
	MergeInterval  time.Duration
}

Config is the configuration for a Bitcask instance.

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns the default configuration.

type Iterator

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

func (*Iterator) Key

func (it *Iterator) Key() string

Key returns the key of the current key-value pair.

func (*Iterator) Next

func (it *Iterator) Next() bool

Next advances the iterator to the next key-value pair.

func (*Iterator) Value

func (it *Iterator) Value() ([]byte, error)

Value returns the value of the current key-value pair.

type MmapedFile

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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