Help us shape the future of Go! Take the 2026 Go Developer Survey

pogreb

package module
v0.10.2 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2023 License: Apache-2.0 Imports: 23 Imported by: 66

README

Pogreb

Docs Build Status Go Report Card Codecov

Pogreb is an embedded key-value store for read-heavy workloads written in Go.

Key characteristics

  • 100% Go.
  • Optimized for fast random lookups and infrequent bulk inserts.
  • Can store larger-than-memory data sets.
  • Low memory usage.
  • All DB methods are safe for concurrent use by multiple goroutines.

Installation

$ go get -u github.com/akrylysov/pogreb

Usage

Opening a database

To open or create a new database, use the pogreb.Open() function:

package main

import (
	"log"

	"github.com/akrylysov/pogreb"
)

func main() {
    db, err := pogreb.Open("pogreb.test", nil)
    if err != nil {
        log.Fatal(err)
        return
    }	
    defer db.Close()
}
Writing to a database

Use the DB.Put() function to insert a new key-value pair:

err := db.Put([]byte("testKey"), []byte("testValue"))
if err != nil {
	log.Fatal(err)
}
Reading from a database

To retrieve the inserted value, use the DB.Get() function:

val, err := db.Get([]byte("testKey"))
if err != nil {
	log.Fatal(err)
}
log.Printf("%s", val)
Deleting from a database

Use the DB.Delete() function to delete a key-value pair:

err := db.Delete([]byte("testKey"))
if err != nil {
	log.Fatal(err)
}
Iterating over items

To iterate over items, use ItemIterator returned by DB.Items():

it := db.Items()
for {
    key, val, err := it.Next()
    if err == pogreb.ErrIterationDone {
    	break
    }
    if err != nil { 
        log.Fatal(err)
    }
    log.Printf("%s %s", key, val)
}

Performance

The benchmarking code can be found in the pogreb-bench repository.

Results of read performance benchmark of pogreb, goleveldb, bolt and badgerdb on DigitalOcean 8 CPUs / 16 GB RAM / 160 GB SSD + Ubuntu 16.04.3 (higher is better):

Internals

Design document.

Documentation

Overview

Package pogreb implements an embedded key-value store for read-heavy workloads.

Example
package main

import (
	"log"

	"github.com/akrylysov/pogreb"
)

func main() {
	db, err := pogreb.Open("pogreb.test", nil)
	if err != nil {
		log.Fatal(err)
		return
	}
	defer db.Close()

	// Insert a new key-value pair.
	if err := db.Put([]byte("testKey"), []byte("testValue")); err != nil {
		log.Fatal(err)
	}

	// Retrieve the inserted value.
	val, err := db.Get([]byte("testKey"))
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("%s", val)

	// Iterate over items.
	it := db.Items()
	for {
		key, val, err := it.Next()
		if err == pogreb.ErrIterationDone {
			break
		}
		if err != nil {
			log.Fatal(err)
		}
		log.Printf("%s %s", key, val)
	}
}

Index

Examples

Constants

View Source
const (
	// MaxKeyLength is the maximum size of a key in bytes.
	MaxKeyLength = math.MaxUint16

	// MaxValueLength is the maximum size of a value in bytes.
	MaxValueLength = 512 << 20 // 512 MiB

	// MaxKeys is the maximum numbers of keys in the DB.
	MaxKeys = math.MaxUint32
)

Variables

View Source
var ErrIterationDone = errors.New("no more items in iterator")

ErrIterationDone is returned by ItemIterator.Next calls when there are no more items to return.

Functions

func SetLogger

func SetLogger(l *log.Logger)

SetLogger sets the global logger.

Types

type CompactionResult added in v0.9.0

type CompactionResult struct {
	CompactedSegments int
	ReclaimedRecords  int
	ReclaimedBytes    int
}

CompactionResult holds the compaction result.

type DB

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

DB represents the key-value storage. All DB methods are safe for concurrent use by multiple goroutines.

func Open

func Open(path string, opts *Options) (*DB, error)

Open opens or creates a new DB. The DB must be closed after use, by calling Close method.

func (*DB) Close

func (db *DB) Close() error

Close closes the DB.

func (*DB) Compact added in v0.9.0

func (db *DB) Compact() (CompactionResult, error)

Compact compacts the DB. Deleted and overwritten items are discarded. Returns an error if compaction is already in progress.

func (*DB) Count

func (db *DB) Count() uint32

Count returns the number of keys in the DB.

func (*DB) Delete

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

Delete deletes the given key from the DB.

func (*DB) FileSize

func (db *DB) FileSize() (int64, error)

FileSize returns the total size of the disk storage used by the DB.

func (*DB) Get

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

Get returns the value for the given key stored in the DB or nil if the key doesn't exist.

func (*DB) Has

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

Has returns true if the DB contains the given key.

func (*DB) Items

func (db *DB) Items() *ItemIterator

Items returns a new ItemIterator.

func (*DB) Metrics

func (db *DB) Metrics() *Metrics

Metrics returns the DB metrics.

func (*DB) Put

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

Put sets the value for the given key. It updates the value for the existing key.

func (*DB) Sync

func (db *DB) Sync() error

Sync commits the contents of the database to the backing FileSystem.

type ItemIterator

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

ItemIterator is an iterator over DB key-value pairs. It iterates the items in an unspecified order.

func (*ItemIterator) Next

func (it *ItemIterator) Next() ([]byte, []byte, error)

Next returns the next key-value pair if available, otherwise it returns ErrIterationDone error.

type Metrics

type Metrics struct {
	Puts           expvar.Int
	Dels           expvar.Int
	Gets           expvar.Int
	HashCollisions expvar.Int
}

Metrics holds the DB metrics.

type Options

type Options struct {
	// BackgroundSyncInterval sets the amount of time between background Sync() calls.
	//
	// Setting the value to 0 disables the automatic background synchronization.
	// Setting the value to -1 makes the DB call Sync() after every write operation.
	BackgroundSyncInterval time.Duration

	// BackgroundCompactionInterval sets the amount of time between background Compact() calls.
	//
	// Setting the value to 0 disables the automatic background compaction.
	BackgroundCompactionInterval time.Duration

	// FileSystem sets the file system implementation.
	//
	// Default: fs.OSMMap.
	FileSystem fs.FileSystem
	// contains filtered or unexported fields
}

Options holds the optional DB parameters.

Directories

Path Synopsis
Package fs provides a file system interface.
Package fs provides a file system interface.
internal

Jump to

Keyboard shortcuts

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