Documentation
¶
Overview ¶
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/gnuos/fudge"
)
func main() {
cfg := fudge.DefaultConfig()
cfg.SyncInterval = 0 //disable every second fsync
db, err := fudge.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)
}
point := new(Point)
db.Get(8, point)
log.Println(point)
// Output: {8 8}
}
Index ¶
- Variables
- func BackupAll(dir string) (err error)
- func Close(f string) error
- func CloseAll() (err error)
- func Count(f string) (int, error)
- func Delete(f string, key any) error
- func DeleteFile(file string) error
- func Get(f string, key any, value any) error
- func Gets(file string, keys []any) (result [][]byte)
- func Has(f string, key any) (bool, error)
- func KeyToBinary(v any) ([]byte, error)
- func Keys(f string, from any, limit, offset int, asc bool) ([][]byte, error)
- func Set(f string, key any, value any) error
- func Sets(file string, pairs []any) (err error)
- func ValToBinary(v any) ([]byte, error)
- type Cmd
- type Config
- type DB
- func (db *DB) Close() error
- func (db *DB) Count() (int, error)
- func (db *DB) Delete(key any) error
- func (db *DB) DeleteFile() error
- func (db *DB) FileSize() (int64, error)
- func (db *DB) Get(key any, value any) error
- func (db *DB) Has(key any) (bool, error)
- func (db *DB) Keys(from any, limit, offset int, asc bool) ([][]byte, error)
- func (db *DB) KeysByPrefix(prefix []byte, limit, offset int, asc bool) ([][]byte, error)
- func (db *DB) Set(key any, value any) error
Examples ¶
Constants ¶
This section is empty.
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") )
Functions ¶
func BackupAll ¶
BackupAll - backup all opened Db if dir not set it will be backup delete old backup file before run ignore all errors
func Get ¶
Get return value by key with opening if needed Return error if any.
Example ¶
Set("test/test", "Hello", "World")
var output string
Get("test/test", "Hello", &output)
defer CloseAll()
fmt.Printf("%v\n", output)
Output: World
func Gets ¶
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 Keys ¶
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 ¶
Set store any key value to db with opening if needed
Example ¶
Set("test/test", "Hello", "World")
defer CloseAll()
Types ¶
type Config ¶
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 ¶
DB represent database
func Open ¶
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}
Example ¶
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)
}
point := new(Point)
db.Get(8, point)
fmt.Printf("%v\n", *point)
Output: {8 8}
func (*DB) Keys ¶
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 ¶
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)