Example #1
0
// Open opens a database.
//
// Creating a new database is done by calling SetCreateIfMissing(true) on the
// *Options passed to Open.
//
// It is usually wise to set a Cache object on the *Options with SetCache() to
// keep recently used data from that database in memory.
func Open(dbname string, o *Options) (*DB, error) {
	var errStr *C.char
	ldbname := C.CString(dbname)
	defer C.free(unsafe.Pointer(ldbname))

	leveldb := C.leveldb_open(o.Opt, ldbname, &errStr)
	if errStr != nil {
		return nil, DatabaseError(C.GoString(errStr))
	}
	return &DB{leveldb}, nil
}
Example #2
0
func Open(name string, options *Options) (*Db, error) {
	var db_err *C.char

	db_name := C.CString(name)
	defer C.free(unsafe.Pointer(db_name))

	ldb := C.leveldb_open(options.options, db_name, &db_err)
	if ldb == nil {
		return nil, ldb_error(C.GoString(db_err))
	}

	return &Db{ldb}, nil
}
Example #3
0
func (db *DB) open() error {
	db.initOptions(db.cfg)

	var errStr *C.char
	ldbname := C.CString(db.path)
	defer C.leveldb_free(unsafe.Pointer(ldbname))

	db.db = C.leveldb_open(db.opts.Opt, ldbname, &errStr)
	if errStr != nil {
		db.db = nil
		return saveError(errStr)
	}
	return nil
}
Example #4
0
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
}
Example #5
0
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
}