Golang with rocksdb

1,860 views
Skip to first unread message

others...@gmail.com

unread,
Jun 17, 2015, 7:13:55 PM6/17/15
to golan...@googlegroups.com
Hi,
  I am success to build rocksdb for golang windows.
  When i build project with import rocksdb, i got "gorocksdb(.text): rocksdb_backup_engine_close: not defined".
  I checked c.h/c.cc in rocksdb and fuction was there.


myEnv /cygdrive/c/mygo/src/gorocksdb
$ go install

myEnv /cygdrive/c/mygo/src/gorocksdb
$ go build
# _/C_/cygwin64/home/my/rocksdb/testRock
gorocksdb(.text): rocksdb_backup_engine_close: not defined
gorocksdb(.text): rocksdb_backup_engine_create_new_backup: not defined
gorocksdb(.text): rocksdb_backup_engine_get_backup_info: not defined
gorocksdb(.text): rocksdb_backup_engine_info_backup_id: not defined
gorocksdb(.text): rocksdb_backup_engine_info_count: not defined
gorocksdb(.text): rocksdb_backup_engine_info_destroy: not defined
gorocksdb(.text): rocksdb_backup_engine_info_number_files: not defined
gorocksdb(.text): rocksdb_backup_engine_info_size: not defined
gorocksdb(.text): rocksdb_backup_engine_info_timestamp: not defined
gorocksdb(.text): rocksdb_backup_engine_open: not defined
gorocksdb(.text): rocksdb_backup_engine_restore_db_from_latest_backup: not defined
gorocksdb(.text): rocksdb_restore_options_create: not defined
gorocksdb(.text): rocksdb_restore_options_destroy: not defined
gorocksdb(.text): rocksdb_restore_options_set_keep_log_files: not defined
gorocksdb(.text): rocksdb_cache_create_lru: not defined
gorocksdb(.text): rocksdb_cache_destroy: not defined
gorocksdb(.text): rocksdb_approximate_sizes: not defined
gorocksdb(.text): rocksdb_close: not defined
gorocksdb(.text): rocksdb_compact_range: not defined
gorocksdb(.text): rocksdb_create_iterator: not defined
gorocksdb(.text): rocksdb_create_snapshot: not defined
too many errors

Can you help me and how can i solve this?
Thanks,

Ian Lance Taylor

unread,
Jun 17, 2015, 7:45:19 PM6/17/15
to others...@gmail.com, golang-nuts
On Wed, Jun 17, 2015 at 12:52 PM, <others...@gmail.com> wrote:
>
> I am success to build rocksdb for golang windows.
> When i build project with import rocksdb, i got "gorocksdb(.text):
> rocksdb_backup_engine_close: not defined".
> I checked c.h/c.cc in rocksdb and fuction was there.
>
>
> myEnv /cygdrive/c/mygo/src/gorocksdb
> $ go install
>
> myEnv /cygdrive/c/mygo/src/gorocksdb
> $ go build
> # _/C_/cygwin64/home/my/rocksdb/testRock
> gorocksdb(.text): rocksdb_backup_engine_close: not defined

> Can you help me and how can i solve this?

I'm just guessing, but in the .go file that does import "C" you
probably need something like

#cgo LDFLAGS -lrocksdb

See http://golang.org/cmd/cgo for docs.

Ian

others...@gmail.com

unread,
Jun 17, 2015, 8:59:56 PM6/17/15
to golan...@googlegroups.com, others...@gmail.com
Hi Ian,
  Thanks for quick response.
  Yes, I set "// #cgo LDFLAGS: -lrocksdb -lstdc++ -lm -lz -lbz2 -lsnappy" to .go file.
  It is issue when I import {/"github.com/tecbot/gorocksdb"} to other .go program.

//--------------------------------
package main
// #cgo LDFLAGS: -lrocksdb -lstdc++ -lm -lz -lbz2 -lsnappy 
import "C"
import (
                "fmt"
                "github.com/tecbot/gorocksdb
)
//----------------------------------------------------

Thanks,
Dong

Dave Cheney

unread,
Jun 17, 2015, 9:12:50 PM6/17/15
to golan...@googlegroups.com, others...@gmail.com
Possibly related to your problem, you do not appear to have put your program inside $GOPATH/src. You can read about how to lay out our Go code here, http://golang.org/doc/code.html#Workspaces

others...@gmail.com

unread,
Jun 21, 2015, 5:33:02 AM6/21/15
to golan...@googlegroups.com, others...@gmail.com

I tried to build new c lib and using import "C" and it work fine.

Now, i am trying to link to compiled rocksdb shared_lib  in cygwin but is is not success.

khoadong@DDKHLAP2 /cygdrive/c/mygo/src/gorocksdbTest
$ go build
# gorocksdbTest
gorocksdbTest(.text): rocksdb_destroy_db: not defined
gorocksdbTest(.text): rocksdb_options_create: not defined
gorocksdbTest(.text): rocksdb_options_set_create_if_missing: not defined
gorocksdbTest(.text): rocksdb_options_set_target_file_size_base: not defined
gorocksdbTest(.text): undefined: rocksdb_destroy_db
gorocksdbTest.text): undefined: rocksdb_options_create
gorocksdbTest(.text): undefined: rocksdb_options_set_create_if_missing
gorocksdbTest(.text): undefined: rocksdb_options_set_target_file_size_base

//-----This is my test ---// 

package main 
// #cgo CFLAGS: -I C:/cygwin64/usr/local/include
// #cgo LDFLAGS: -L C:/cygwin64/usr/local/lib -lrocksdb -lstdc++ -lm -lz -lbz2  
// #include "rocksdb/c.h"
// #include <stdlib.h>
import "C"  

import (
     "unsafe"
 "errors"
     "fmt"
     "path/filepath" 
)


//types and funs got from goRocksdb

type Options struct {
c *C.rocksdb_options_t
 
cmp  *Comparator
mo   *MergeOperator
env  *Env
st   *SliceTransform
cf   *CompactionFilter
bbto *BlockBasedTableOptions
 
ccmp *C.rocksdb_comparator_t
cmo  *C.rocksdb_mergeoperator_t
cst  *C.rocksdb_slicetransform_t
ccf  *C.rocksdb_compactionfilter_t
 
type Comparator interface {
// Three-way comparison. Returns value:
//   < 0 iff "a" < "b",
//   == 0 iff "a" == "b",
//   > 0 iff "a" > "b"
Compare(a, b []byte) int

// The name of the comparator.
Name() string

type MergeOperator interface { 
FullMerge(key, existingValue []byte, operands [][]byte) ([]byte, bool) 
PartialMerge(key, leftOperand, rightOperand []byte) ([]byte, bool) 
Name() string
}



// Env is a system call environment used by a database.
type Env struct {
c *C.rocksdb_env_t
}


// A SliceTransform can be used as a prefix extractor.
type SliceTransform interface {
// Transform a src in domain to a dst in the range.
Transform(src []byte) []byte

// Determine whether this is a valid src upon the function applies.
InDomain(src []byte) bool

// Determine whether dst=Transform(src) for some src.
InRange(src []byte) bool

// Return the name of this transformation.
Name() string
}


type CompactionFilter interface { 
Name() string 
Filter(level int, key, val []byte) (remove bool, newVal []byte)
}

 
type BlockBasedTableOptions struct {
c *C.rocksdb_block_based_table_options_t

// hold references for GC
cache      *Cache
comp_cache *Cache
fp         *FilterPolicy

// Hold on so memory can be freed in Destroy.
cfp *C.rocksdb_filterpolicy_t
}

 
type FilterPolicy interface { 
CreateFilter(keys [][]byte) []byte 
KeyMayMatch(key []byte, filter []byte) bool

// Return the name of this policy.
Name() string
}

 
type Cache struct {
c *C.rocksdb_cache_t
}
  
type BackupEngineInfo struct {
    c *C.rocksdb_backup_engine_info_t
}
 
type Slice struct {
data  *C.char
size  C.size_t
freed bool
}

// DB is a reusable handle to a RocksDB database on disk, created by Open.
type DB struct {
c    *C.rocksdb_t
name string
opts *Options
}
 
// NewDefaultOptions creates the default Options.
func NewDefaultOptions() *Options {
return NewNativeOptions(C.rocksdb_options_create())
}

func NewNativeOptions(c *C.rocksdb_options_t) *Options {
return &Options{c: c}
}
 
// If true, the database will be created if it is missing.
// Default: false
func (self *Options) SetCreateIfMissing(value bool) {
C.rocksdb_options_set_create_if_missing(self.c, boolToChar(value))
}

 
func (self *Options) SetTargetFileSizeBase(value uint64) {
C.rocksdb_options_set_target_file_size_base(self.c, C.uint64_t(value))
}
 

func DestroyDb(name string, opts *Options) error {
var cErr *C.char
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
C.rocksdb_destroy_db(opts.c, cname, &cErr)
if cErr != nil {
defer C.free(unsafe.Pointer(cErr))

return errors.New(C.GoString(cErr))
}

return nil
}
 
/*func (self *BackupEngineInfo) Destroy() {
    C.rocksdb_backup_engine_info_destroy(self.c)
    self.c = nil
}
 
func (self *DB) Close() {
C.rocksdb_close(self.c)
}*/

//--utils
// boolToChar converts a bool value to C.uchar
func boolToChar(b bool) C.uchar {
if b {
return 1
}
return 0
}

//--slice
func (self *Slice) Free() {
if !self.freed {
C.free(unsafe.Pointer(self.data))
self.freed = true
}
}

var homePath string = "c:/"
func main() { 
                homePath := ""
                homePath = homePath + "/data/" 
                dbRocksDB_Name := "name" 
                dbMainName := dbDir(homePath, dbRocksDB_Name)
                options := NewDefaultOptions()
                options.SetTargetFileSizeBase(67108864) //64MB
                DestroyDb(dbMainName, options)  

}
 
func dbDir(home_path string, dbname string) string { 
                path := filepath.Join(home_path, dbname) 
                return path


//-------------------------------------
Regards,
Dong

others dong

unread,
Jun 24, 2015, 11:57:01 PM6/24/15
to golan...@googlegroups.com
SOLVED
Because cgo is using dynamics/share library with extension *.a but rocksdb build static as *.a. I copied librocksdb.so.3.11.0 to librocksdb.a and go identified library.

Thanks,
--

    O O 
  (>'.'<)
 ('`')_('`') Oops, it is free coupons, stuffs, promotions and etc...
Reply all
Reply to author
Forward
0 new messages