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/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 ¶
- Variables
- func BackupAll(dir string) (err error)
- func Close(f string) error
- func CloseAll() (err error)
- func Count(f string) (int, error)
- func Counter(f string, key interface{}, incr int) (int64, error)
- func Delete(f string, key interface{}) error
- func DeleteFile(file string) error
- func Get(f string, key, value interface{}) error
- func Gets(file string, keys []interface{}) (result [][]byte)
- func Has(f string, key interface{}) (bool, error)
- func KeyToBinary(v interface{}) ([]byte, error)
- func Keys(f string, from interface{}, limit, offset int, asc bool) ([][]byte, int, error)
- func Set(f string, key, value interface{}) error
- func Sets(file string, pairs []interface{}) (err error)
- func ValToBinary(v interface{}) ([]byte, error)
- type Cmd
- type Config
- type Db
- func (db *Db) Close() error
- func (db *Db) Count() (int, error)
- func (db *Db) Counter(key interface{}, incr int) (int64, error)
- func (db *Db) Delete(key interface{}) error
- func (db *Db) DeleteFile() error
- func (db *Db) FileSize() (int64, error)
- func (db *Db) Get(key, value interface{}) error
- func (db *Db) Has(key interface{}) (bool, error)
- func (db *Db) Keys(from interface{}, limit, offset int, asc bool) ([][]byte, int, error)
- func (db *Db) KeysByPrefix(prefix []byte, limit, offset int, asc bool) ([][]byte, int, error)
- func (db *Db) ModTime() (*time.Time, error)
- func (db *Db) Set(key, value interface{}) error
- func (db *Db) Values(offset int, limit int, asc bool) ([][]byte, int, 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")
output := ""
Get("test/test", "Hello", &output)
defer CloseAll()
fmt.Println(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()
func Sets ¶
Sets store vals and keys Use it for mass insertion every pair must contain key and value
func ValToBinary ¶
ValToBinary return value in bytes
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)
}
var point Point
db.Get(8, &point)
fmt.Println(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)