qbolt/main.go
2017-05-16 19:34:54 +12:00

136 lines
2.4 KiB
Go

package main
import "C"
import (
"errors"
"os"
"github.com/boltdb/bolt"
)
//export Bolt_Options_New
func Bolt_Options_New() ObjectReference {
b := *bolt.DefaultOptions
return gms.Put(&b)
}
//export Bolt_Options_New_Readonly
func Bolt_Options_New_Readonly() ObjectReference {
b := *bolt.DefaultOptions
b.ReadOnly = true
return gms.Put(&b)
}
//export Recall
func Recall(f func(a int64) int64) int64 {
return f(3)
}
//export Bolt_Open
func Bolt_Open(path string, mode uint32, opts ObjectReference) (ObjectReference, error) {
optsIFC, ok := gms.Get(opts)
if !ok {
return 0, NullObjectReference
}
ptrBoltOps, ok := optsIFC.(*bolt.Options)
if !ok {
return 0, NullObjectReference
}
ptrDB, err := bolt.Open(path, os.FileMode(mode), ptrBoltOps)
if err != nil {
return 0, err
}
dbRef := gms.Put(ptrDB)
return dbRef, nil
}
func withBoltDBReference(b ObjectReference, fn func(db *bolt.DB) error) error {
dbIFC, ok := gms.Get(b)
if !ok {
return NullObjectReference
}
ptrDB, ok := dbIFC.(*bolt.DB)
if !ok {
return NullObjectReference
}
return fn(ptrDB)
}
//export Error_Error
func Error_Error(e error) string {
return e.Error()
}
//export Bolt_ListBuckets
func Bolt_ListBuckets(b ObjectReference, browse []string, withEach func(string)) error {
if len(browse) == 0 {
return Bolt_ListBucketsAtRoot(b, withEach)
}
err := withBoltDBReference(b, func(db *bolt.DB) error {
return db.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte(browse[0]))
if bucket == nil {
return errors.New("Unknown bucket")
}
for i := 1; i < len(browse); i += 1 {
bucket = bucket.Bucket([]byte(browse[i]))
if bucket == nil {
return errors.New("Unknown bucket")
}
}
return bucket.ForEach(func(k, v []byte) error {
withEach(string(k))
return nil
})
})
})
return err
}
//export Bolt_ListBucketsAtRoot
func Bolt_ListBucketsAtRoot(b ObjectReference, withEach func(string)) error {
err := withBoltDBReference(b, func(db *bolt.DB) error {
return db.View(func(tx *bolt.Tx) error {
return tx.ForEach(func(n []byte, bucket *bolt.Bucket) error {
withEach(string(n))
return nil
})
})
})
return err
}
//export Bolt_Close
func Bolt_Close(b ObjectReference) error {
err := withBoltDBReference(b, func(db *bolt.DB) error {
return db.Close()
})
if err != nil {
return err
}
gms.Delete(b)
return nil
}
func main() {
// virtual
}