qbolt/main.go

251 lines
5.1 KiB
Go

package main
import "C"
import (
"encoding/json"
"errors"
"os"
"github.com/boltdb/bolt"
)
const (
ERROR_AND_STOP_CALLING int64 = 100
ERROR_AND_KEEP_CALLING = 101
FINISHED_OK = 102
REAL_MESSAGE = 103
)
const Magic int64 = 0x10203040
//export GetMagic
func GetMagic() int64 {
return Magic
}
//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 Bolt_Open
func Bolt_Open(path string, mode uint32, opts ObjectReference) (ObjectReference, *C.char, int) {
optsIFC, ok := gms.Get(opts)
if !ok {
errMsg := NullObjectReference.Error()
return 0, C.CString(errMsg), len(errMsg)
}
ptrBoltOps, ok := optsIFC.(*bolt.Options)
if !ok {
errMsg := NullObjectReference.Error()
return 0, C.CString(errMsg), len(errMsg)
}
ptrDB, err := bolt.Open(path, os.FileMode(mode), ptrBoltOps)
if err != nil {
errMsg := err.Error()
return 0, C.CString(errMsg), len(errMsg)
}
dbRef := gms.Put(ptrDB)
return dbRef, nil, 0
}
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)
}
func withBrowse_ReadOnly(b_ref ObjectReference, browse []string, fn func(db *bolt.DB, tx *bolt.Tx, bucket *bolt.Bucket) error) error {
if len(browse) == 0 {
// not a bucket
return errors.New("No bucket selected")
}
return withBoltDBReference(b_ref, 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")
}
}
// Walked the bucket chain, now run the user callback
return fn(db, tx, bucket)
})
})
}
type CallResponse struct {
s string
e error
}
//export Bolt_DBStats
func Bolt_DBStats(b ObjectReference) (int64, *C.char, int) {
var stats bolt.Stats
err := withBoltDBReference(b, func(db *bolt.DB) error {
stats = db.Stats()
return nil
})
jBytes, err := json.Marshal(stats)
if err != nil {
return ERROR_AND_STOP_CALLING, C.CString(err.Error()), len(err.Error())
}
return REAL_MESSAGE, C.CString(string(jBytes)), len(jBytes)
}
//export Bolt_BucketStats
func Bolt_BucketStats(b ObjectReference, browse []string) (int64, *C.char, int) {
var stats bolt.BucketStats
err := withBrowse_ReadOnly(b, browse, func(db *bolt.DB, tx *bolt.Tx, bucket *bolt.Bucket) error {
stats = bucket.Stats()
return nil
})
if err != nil {
return ERROR_AND_STOP_CALLING, C.CString(err.Error()), len(err.Error())
}
jBytes, err := json.Marshal(stats)
if err != nil {
return ERROR_AND_STOP_CALLING, C.CString(err.Error()), len(err.Error())
}
return REAL_MESSAGE, C.CString(string(jBytes)), len(jBytes)
}
type NextCall struct {
content chan CallResponse
}
//export Bolt_ListBuckets
func Bolt_ListBuckets(b ObjectReference, browse []string) ObjectReference {
pNC := &NextCall{
content: make(chan CallResponse, 0),
}
pNC_Ref := gms.Put(pNC)
go func() {
var err error
if len(browse) == 0 {
// root mode
err = withBoltDBReference(b, func(db *bolt.DB) error {
return db.View(func(tx *bolt.Tx) error {
return tx.ForEach(func(k []byte, _ *bolt.Bucket) error {
pNC.content <- CallResponse{s: string(k)}
return nil
})
})
})
} else {
// Nested-mode
err = withBrowse_ReadOnly(b, browse, func(db *bolt.DB, tx *bolt.Tx, bucket *bolt.Bucket) error {
return bucket.ForEach(func(k, v []byte) error {
// non-nil v means it's a data item
if v == nil {
pNC.content <- CallResponse{s: string(k)}
}
return nil
})
})
}
if err != nil {
pNC.content <- CallResponse{e: err}
}
close(pNC.content)
}()
return pNC_Ref
}
//export GetNext
func GetNext(oRef ObjectReference) (int64, *C.char, int) {
pNC_Iface, ok := gms.Get(oRef)
if !ok {
msg := NullObjectReference.Error()
return ERROR_AND_STOP_CALLING, C.CString(msg), len(msg)
}
pNC, ok := pNC_Iface.(*NextCall)
if !ok {
msg := NullObjectReference.Error()
return ERROR_AND_STOP_CALLING, C.CString(msg), len(msg)
}
cr, ok := <-pNC.content
if !ok {
gms.Delete(oRef)
return FINISHED_OK, nil, 0
}
if cr.e != nil {
msg := cr.e.Error()
return ERROR_AND_KEEP_CALLING, C.CString(msg), len(msg)
}
return REAL_MESSAGE, C.CString(cr.s), len(cr.s)
}
//export Bolt_ListBucketsAtRoot
func Bolt_ListBucketsAtRoot(b ObjectReference) ObjectReference {
return Bolt_ListBuckets(b, nil)
}
//export Bolt_Close
func Bolt_Close(b ObjectReference) (*C.char, int) {
err := withBoltDBReference(b, func(db *bolt.DB) error {
return db.Close()
})
if err != nil {
msg := err.Error()
return C.CString(msg), len(msg)
}
gms.Delete(b)
return nil, 0
}
func main() {
// virtual
}