예제 #1
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
}
예제 #2
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
}
// Open LevelDB with given name
func OpenLevelDB(path string) (leveldb *LevelDB, err error) {

	cpath := C.CString(path) // convert path to c string
	defer C.leveldb_free(unsafe.Pointer(cpath))

	// allocate LevelDB Option struct to open
	opt := C.leveldb_options_create()
	defer C.leveldb_free(unsafe.Pointer(opt))

	// set open option
	C.leveldb_options_set_create_if_missing(opt, C.uchar(1))

	// open leveldb
	var cerr *C.char
	cleveldb := C.leveldb_open(opt, cpath, &cerr)

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

	return &LevelDB{cleveldb, path}, nil
}
예제 #4
0
// NewOptions allocates a new Options object.
//
// To prevent memory leaks, the *Options returned must have DestroyOptions()
// called on it when it is no longer needed by the program.
func NewOptions() *Options {
	opt := C.leveldb_options_create()
	return &Options{opt}
}
예제 #5
0
func Create_options() *Options {
	option := C.leveldb_options_create()
	return &Options{option}
}