func (db *LevelDB) Delete(key string) (err error) {
	opt := C.leveldb_writeoptions_create() // write option
	defer C.leveldb_free(unsafe.Pointer(opt))

	k := C.CString(key) // copy
	defer C.leveldb_free(unsafe.Pointer(k))

	var cerr *C.char
	C.leveldb_delete(db.CLevelDB, opt, k, C.size_t(len(key)), &cerr)

	if cerr != nil {
		defer C.leveldb_free(unsafe.Pointer(cerr))
		return errors.New(C.GoString(cerr))
	}

	return
}
示例#2
0
文件: db.go 项目: shafreeck/hermes
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
}
示例#3
0
文件: leveldb.go 项目: reusee/obox
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
}
示例#4
0
// NewWriteOptions allocates a new WriteOptions object.
//
// To prevent memory leaks, the *WriteOptions returned must have Close()
// called on it when it is no longer needed by the program.
func NewWriteOptions() *WriteOptions {
	opt := C.leveldb_writeoptions_create()
	return &WriteOptions{opt}
}
示例#5
0
func Create_write_options() *Writeoptions {
	option := C.leveldb_writeoptions_create()
	return &Writeoptions{option}
}