pudge: github.com/recoilme/pudge Index | Examples | Files | Directories

package pudge

import "github.com/recoilme/pudge"

Package pudge implements a low-level key/value store in pure Go. Keys stored in memory, Value stored on disk

Usage

package main

import (
	"log"

	"github.com/recoilme/pudge"
)

func main() {
	cfg := pudge.DefaultConfig()
	cfg.SyncInterval = 0 //disable every second fsync
	db, err := pudge.Open("../test/db", cfg)
	if err != nil {
		log.Panic(err)
	}
	defer db.DeleteFile()
	type Point struct {
		X int
		Y int
	}
	for i := 100; i >= 0; i-- {
		p := &Point{X: i, Y: i}
		db.Set(i, p)
	}
	var point Point
	db.Get(8, &point)
	log.Println(point)
	// Output: {8 8}
}

Index

Examples

Package Files

api.go doc.go pudge.go

Variables

var DefaultConfig = &Config{
    FileMode:     0644,
    DirMode:      0755,
    SyncInterval: 0,
    StoreMode:    0}

DefaultConfig is default config

var (

    // ErrKeyNotFound - key not found
    ErrKeyNotFound = errors.New("Error: key not found")
)

func BackupAll Uses

func BackupAll(dir string) (err error)

BackupAll - backup all opened Db if dir not set it will be backup delete old backup file before run ignore all errors

func Close Uses

func Close(f string) error

Close - sync & close files. Return error if any.

func CloseAll Uses

func CloseAll() (err error)

CloseAll - close all opened Db

func Count Uses

func Count(f string) (int, error)

Count returns the number of items in the Db.

func Counter Uses

func Counter(f string, key interface{}, incr int) (int64, error)

Counter return int64 incremented on incr with lazy open

func Delete Uses

func Delete(f string, key interface{}) error

Delete remove key Returns error if key not found

func DeleteFile Uses

func DeleteFile(file string) error

DeleteFile close db and delete file

func Get Uses

func Get(f string, key, value interface{}) error

Get return value by key with opening if needed Return error if any.

Code:

Set("test/test", "Hello", "World")
output := ""
Get("test/test", "Hello", &output)
defer CloseAll()
fmt.Println(output)

Output:

World

func Gets Uses

func Gets(file string, keys []interface{}) (result [][]byte)

Gets return key/value pairs in random order result contains key and value Gets not return error if key not found If no keys found return empty result

func Has Uses

func Has(f string, key interface{}) (bool, error)

Has return true if key exists. Return error if any.

func KeyToBinary Uses

func KeyToBinary(v interface{}) ([]byte, error)

KeyToBinary return key in bytes

func Keys Uses

func Keys(f string, from interface{}, limit, offset int, asc bool) ([][]byte, error)

Keys return keys in ascending or descending order (false - descending,true - ascending) if limit == 0 return all keys if offset > 0 - skip offset records If from not nil - return keys after from (from not included)

func Set Uses

func Set(f string, key, value interface{}) error

Set store any key value to db with opening if needed

Code:

Set("test/test", "Hello", "World")
defer CloseAll()

func Sets Uses

func Sets(file string, pairs []interface{}) (err error)

Sets store vals and keys Use it for mass insertion every pair must contain key and value

func ValToBinary Uses

func ValToBinary(v interface{}) ([]byte, error)

ValToBinary return value in bytes

type Cmd Uses

type Cmd struct {
    Seek    uint32
    Size    uint32
    KeySeek uint32
    Val     []byte
}

Cmd represent keys and vals addresses

type Config Uses

type Config struct {
    FileMode     int // 0644
    DirMode      int // 0755
    SyncInterval int // in seconds
    StoreMode    int // 0 - file first, 2 - memory first(with persist on close), 2 - with empty file - memory without persist
}

Config fo db Default FileMode = 0644 Default DirMode = 0755 Default SyncInterval = 0 sec, 0 - disable sync (os will sync, typically 30 sec or so) If StroreMode==2 && file == "" - pure inmemory mode

type Db Uses

type Db struct {
    sync.RWMutex
    // contains filtered or unexported fields
}

Db represent database

func Open Uses

func Open(f string, cfg *Config) (*Db, error)

Open return db object if it opened. Create new db if not exist. Read db to obj if exist. Or error if any. Default Config (if nil): &Config{FileMode: 0644, DirMode: 0755, SyncInterval: 0}

Code:

cfg := &Config{
    SyncInterval: 0} //disable every second fsync
db, err := Open("test/db", cfg)
if err != nil {
    log.Panic(err)
}
defer db.DeleteFile()
type Point struct {
    X   int
    Y   int
}
for i := 100; i >= 0; i-- {
    p := &Point{X: i, Y: i}
    db.Set(i, p)
}
var point Point
db.Get(8, &point)
fmt.Println(point)

Output:

{8 8}

func (*Db) Close Uses

func (db *Db) Close() error

Close - sync & close files. Return error if any.

func (*Db) Count Uses

func (db *Db) Count() (int, error)

Count returns the number of items in the Db.

func (*Db) Counter Uses

func (db *Db) Counter(key interface{}, incr int) (int64, error)

Counter return int64 incremented on incr

func (*Db) Delete Uses

func (db *Db) Delete(key interface{}) error

Delete remove key Returns error if key not found

func (*Db) DeleteFile Uses

func (db *Db) DeleteFile() error

DeleteFile close and delete file

func (*Db) FileSize Uses

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

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

func (*Db) Get Uses

func (db *Db) Get(key, value interface{}) error

Get return value by key Return error if any.

func (*Db) Has Uses

func (db *Db) Has(key interface{}) (bool, error)

Has return true if key exists. Return error if any.

func (*Db) Keys Uses

func (db *Db) Keys(from interface{}, limit, offset int, asc bool) ([][]byte, error)

Keys return keys in ascending or descending order (false - descending,true - ascending) if limit == 0 return all keys if offset > 0 - skip offset records If from not nil - return keys after from (from not included)

func (*Db) KeysByPrefix Uses

func (db *Db) KeysByPrefix(prefix []byte, limit, offset int, asc bool) ([][]byte, error)

KeysByPrefix return keys with prefix in ascending or descending order (false - descending,true - ascending) if limit == 0 return all keys if offset > 0 - skip offset records If from not nil - return keys after from (from not included)

func (*Db) Set Uses

func (db *Db) Set(key, value interface{}) error

Set store any key value to db

Directories

PathSynopsis
examples

Package pudge imports 11 packages (graph) and is imported by 6 packages. Updated 2020-04-15. Refresh now. Tools for package owners.