Skip to content

Instantly share code, notes, and snippets.

@pyanfield
Created March 26, 2014 06:20
Show Gist options
  • Save pyanfield/9777719 to your computer and use it in GitHub Desktop.
Save pyanfield/9777719 to your computer and use it in GitHub Desktop.
test cznic/kv
package main
import (
"fmt"
"github.com/cznic/kv"
"io"
"os"
)
func getBuf(n int) []byte {
return make([]byte, n)
}
func openDB(dbFile string, opts *kv.Options) (*kv.DB, error) {
createOpen := kv.Open
status := "opening"
if _, err := os.Stat(dbFile); os.IsNotExist(err) {
createOpen = kv.Create
status = "creating"
}
if opts == nil {
opts = &kv.Options{}
}
db, err := createOpen(dbFile, opts)
if err != nil {
return nil, fmt.Errorf("error %s %s: %v \n", status, dbFile, err)
}
return db, nil
}
func main() {
dbFile := "test.kv"
db, err := openDB(dbFile, nil)
defer db.Close()
if err != nil {
fmt.Printf("Couldn't create/open kv db at %s, error is %v \n", dbFile, err)
}
key := []byte("hello")
err = db.Set(key, []byte("world"))
if err != nil {
fmt.Printf("Couldn't set value to db: %v \n", err)
}
val, err := db.Get(getBuf(200), key)
if err != nil {
fmt.Printf("Could't get value from db: %v \n", err)
}
fmt.Printf(">> %s = %s \n", key, val)
fmt.Printf("=========== all data from begin =========== \n")
for i := 0; i < 100; i++ {
db.Set([]byte(string(i)), []byte(string(100-i)))
}
k, v, _ := db.First()
fmt.Printf(">>First %s = %s \n", k, v)
k, v, _ = db.Last()
fmt.Printf(">>Last %s = %s \n", k, v)
enum, _ := db.SeekFirst()
k, v, _ = enum.Next()
fmt.Printf(">> %s = %s \n", k, v)
fmt.Printf("=========== end ========== \n")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment