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.
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
}