Giter Site home page Giter Site logo

Comments (3)

wingyplus avatar wingyplus commented on June 13, 2024

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.

from bolt.

topilski avatar topilski commented on June 13, 2024

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?

from bolt.

topilski avatar topilski commented on June 13, 2024

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

from bolt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.