bolt icon indicating copy to clipboard operation
bolt copied to clipboard

C/C++ binding for key/value storage

Open topilski opened this issue 8 years ago • 3 comments

Hi, do you have some C/C++ binding for boltdb? I want to integrate your DB in FastoNoSQL.

topilski avatar Aug 25 '17 17:08 topilski

The possible solution is build go library that connect and manage boltdb and build this library with c-shared mode (see go help buildmode for more details). That is an idea.

wingyplus avatar Oct 04 '17 10:10 wingyplus

Hi @wingyplus, Do you want that FastoNoSQL support boltdb? I found that boltdb have many users, but i nothing know about it, this is something like investigation, need or not needed GUI for db?

topilski avatar Oct 04 '17 11:10 topilski

Hi all, i am wrote something like this in go:

package main
import (
	"C"
	"github.com/boltdb/bolt"
	"fmt"
)

//export open_boltdb
func open_boltdb(path string) (interface{}, error) {
	db, err := bolt.Open(path, 0600, nil)
	if err != nil {
		return nil, err
	}
	return db, nil
}

//export close_boltdb
func close_boltdb(db interface{}) {
	ldb := db.(*bolt.DB)
	ldb.Close()
}

//export get_value
func get_value(db interface{}, backet_name []byte, key_name []byte) ([]byte, error) {
	ldb := db.(*bolt.DB)
	var value []byte
	fn := func(tx *bolt.Tx) error {
		value = tx.Bucket(backet_name).Get(key_name)
		return nil
	}
	return value, ldb.View(fn)
}

//export set_value
func set_value(db interface{}, backet_name []byte, key_name []byte, value []byte) error {
	ldb := db.(*bolt.DB)
	fn := func(tx *bolt.Tx) error {
		tx.Bucket(backet_name).Put(key_name, value)
		return nil
	}
	return ldb.Update(fn)
}

//export boltdb_strerror
func boltdb_strerror(err error) *C.char {
	if err == nil {
		return nil
	}

	return C.CString(err.Error())
}

func main() {
	db, err := open_boltdb("my.db")
	if err != nil {
		boltdb_strerror(err)
		return
	}

	v11, _ := get_value(db, []byte("DB"), []byte("CONFIG"))
	fmt.Print(v11)
	set_value(db, []byte("DB"), []byte("CONFIG"), []byte("12"))
	v12, _ := get_value(db, []byte("DB"), []byte("CONFIG"))
	fmt.Print(v12)
	set_value(db, []byte("DB"), []byte("CONFIG"), []byte("11"))
	close_boltdb(db)
}

and next in C:

#include "test.h"

int main() {
  GoString db_path = {"my.db", 4};
  struct open_boltdb_return db_ptr = open_boltdb(db_path);
  /*if (db_ptr.r1) {
    boltdb_strerror(db_ptr.r1);
    return 1;
  }*/
  GoSlice backet = {"DB", 2, 2};
  GoSlice key = {"CONFIG", 6, 6};
  struct get_value_return val = get_value(db_ptr.r0, backet, key);
  close_boltdb(db_ptr.r0);
  return 0;
}

and compile all it via:

go build -buildmode=c-archive test.go
gcc -g -pthread main.c test.a -o main

I'am not super developer on Go, this is my first attempt to write something on it, what you think is it possible to use GO pointers in C code, now i have crash message:

panic: runtime error: cgo result has Go pointer

goroutine 17 [running, locked to thread]:
main._cgoexpwrap_e77067eccaa8_open_boltdb.func1(0xc420044e98, 0xc420044ea8)
	command-line-arguments/_obj/_cgo_gotypes.go:56 +0x39
main._cgoexpwrap_e77067eccaa8_open_boltdb(0x4af5a4, 0x4, 0x70aca0, 0xc420090000, 0x0, 0x0)
	command-line-arguments/_obj/_cgo_gotypes.go:59 +0xb8
Aborted

topilski avatar Oct 04 '17 15:10 topilski