func Open(path string) (*LevelDB, error) { var err *C.char db := &LevelDB{} db.dbOpts = &DBOptions{C.leveldb_options_create()} db.readOpts = &ReadOptions{C.leveldb_readoptions_create()} db.writeOpts = &WriteOptions{C.leveldb_writeoptions_create()} C.leveldb_options_set_create_if_missing(db.dbOpts.Opts, 1) db.db = C.leveldb_open(db.dbOpts.Opts, C.CString(path), &err) if db.db == nil { e := &DBError{} e.Err = "Open db failed" return nil, e } if err != nil { e := &DBError{} e.Err = C.GoString(err) return db, e } return db, nil }
func OpenLeveldb(dir string) (*Leveldb, error) { options := C.leveldb_options_create() C.leveldb_options_set_create_if_missing(options, C.uchar(1)) cache := C.leveldb_cache_create_lru(levelCacheCap) C.leveldb_options_set_cache(options, cache) cDir := C.CString(dir) defer C.free(unsafe.Pointer(cDir)) var err *C.char db := C.leveldb_open(options, cDir, &err) if err != nil { return nil, errors.New(fmt.Sprintf("%s: %s", err, dir)) } C.leveldb_free(unsafe.Pointer(err)) err = nil leveldb := &Leveldb{ cdb: db, read_options: C.leveldb_readoptions_create(), write_options: C.leveldb_writeoptions_create(), cache: cache, } return leveldb, nil }
func (db *LevelDB) Get(key string) (value string, err error) { opt := C.leveldb_readoptions_create() // write option defer C.leveldb_free(unsafe.Pointer(opt)) k := C.CString(key) // copy defer C.leveldb_free(unsafe.Pointer(k)) var vallen C.size_t var cerr *C.char cvalue := C.leveldb_get(db.CLevelDB, opt, k, C.size_t(len(key)), &vallen, &cerr) if cerr != nil { defer C.leveldb_free(unsafe.Pointer(cerr)) return "", errors.New(C.GoString(cerr)) } if cvalue == nil { return "", nil } defer C.leveldb_free(unsafe.Pointer(cvalue)) return C.GoString(cvalue), nil }
// NewReadOptions allocates a new ReadOptions object. // // To prevent memory leaks, the *ReadOptions returned must have Close() called // on it when it is no longer needed by the program. func NewReadOptions() *ReadOptions { opt := C.leveldb_readoptions_create() return &ReadOptions{opt} }
func Create_read_options() *Readoptions { option := C.leveldb_readoptions_create() return &Readoptions{option} }