Awesome
Sparkey in Go
sparkey-go
is cgo bindings around the sparkey
library.
sparkey
is a disk-based hash table, optimized for bulk writes and fast reads. It keeps two files on disk: a log file (e.g. "blargh.spl"), and an index file (e.g. "blargh.spi").
Setup
First install the sparkey
library:
- in OSX:
brew install sparkey
- in Unix: build from source
Install manually
go get github.com/tiegz/sparkey-go
Install with go dep
dep ensure -add github.com/tiegz/sparkey-go
Usage
Importing it
import "github.com/tiegz/sparkey-go"
Creating the store
s := sparkey.New("sparkey_db", sparkey.COMPRESSION_NONE, 1024)
Setting values
s.Put("first", "Hello")
s.Put("second", "Worlb")
s.Put("third", "Goodbye")
s.Put("fourth", "EOM")
s.Flush()
Deleting values
s.Delete("third")
s.Flush()
Getting values
fmt.Printf("First value is %s", s.Get("first"))
// Hello
Iterating over values
s.ForEachHash(func(k, v string) {
fmt.Printf("%s: %s\n", k, v)
})
// first: Hello
// second: Worlb
// fourth: EOM
Pretty-printing the store
s.PrettyPrintHash();
// {
// first => Hello
// second => Worlb
// fourth => EOM
// }
Inspecting the store
fmt.Printf("Sparkey info:\n\n")
fmt.Printf(" Basename:\t\t%s\n", s.Basename)
fmt.Printf(" CompressionType:\t\t%d\n", s.CompressionType)
fmt.Printf(" Size:\t\t%d\n", s.Size())
fmt.Printf(" LogWriter.CompressionType:\t\t%d\n", s.CompressionType)
fmt.Printf(" LogWriter.BlockSize:\t\t%d\n", s.BlockSize)
fmt.Printf(" MaxKeyLen:\t\t%d\n", s.MaxKeyLen())
fmt.Printf(" MaxValueLen:\t\t%d\n", s.MaxValueLen())
s.Close()
// Sparkey info:
//
// Basename: sparkey_db
// CompressionType: 0
// Size: 3
// LogWriter.CompressionType: 0
// LogWriter.BlockSize: 1024
// MaxKeyLen: 6
// MaxValueLen: 7
Running Tests
cd sparkey
go test -v .
Running Benches
cd sparkey
go test -bench=. -v