Skip to content
This repository has been archived by the owner on Mar 9, 2019. It is now read-only.

C/C++ binding for key/value storage #723

Open
topilski opened this issue Aug 25, 2017 · 3 comments
Open

C/C++ binding for key/value storage #723

topilski opened this issue Aug 25, 2017 · 3 comments

Comments

@topilski
Copy link

topilski commented Aug 25, 2017

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

@wingyplus
Copy link

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.

@topilski
Copy link
Author

topilski commented Oct 4, 2017

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
Copy link
Author

topilski commented Oct 4, 2017

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants