Ejemplo n.º 1
0
Archivo: db.go Proyecto: tradia/gotable
func (db *DB) Open(name string, createIfMissing bool, maxOpenFiles int,
	writeBufSize int, cacheSize int64, compression int) error {
	db.opt = C.rocksdb_options_create()
	C.rocksdb_options_set_create_if_missing(db.opt, boolToUchar(createIfMissing))
	C.rocksdb_options_set_write_buffer_size(db.opt, C.size_t(writeBufSize))
	C.rocksdb_options_set_max_open_files(db.opt, C.int(maxOpenFiles))
	C.rocksdb_options_set_compression(db.opt, C.int(compression))

	var block_options = C.rocksdb_block_based_options_create()
	if cacheSize > 0 {
		db.cache = C.rocksdb_cache_create_lru(C.size_t(cacheSize))
		C.rocksdb_block_based_options_set_block_cache(block_options, db.cache)
	} else {
		C.rocksdb_block_based_options_set_no_block_cache(block_options, 1)
	}
	db.fp = C.rocksdb_filterpolicy_create_bloom(10)
	C.rocksdb_block_based_options_set_filter_policy(block_options, db.fp)

	C.rocksdb_options_set_block_based_table_factory(db.opt, block_options)

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

	var errStr *C.char
	db.db = C.rocksdb_open(db.opt, cname, &errStr)
	if errStr != nil {
		defer C.free(unsafe.Pointer(errStr))
		return errors.New(C.GoString(errStr))
	}

	db.rOpt = C.rocksdb_readoptions_create()
	db.wOpt = C.rocksdb_writeoptions_create()

	return nil
}
Ejemplo n.º 2
0
// OpenDB opens a database with the specified options.
func OpenDB(opts *Options, name string) (*DB, error) {
	cName := C.CString(name)
	defer C.free(unsafe.Pointer(cName))
	var cErr *C.char
	db := C.rocksdb_open(opts.c, cName, &cErr)
	if cErr != nil {
		return nil, convertErr(cErr)
	}
	return &DB{c: db}, nil
}
Ejemplo n.º 3
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))

	rocksdb := C.rocksdb_open(o.Opt, ldbname, &errStr)
	if errStr != nil {
		gs := C.GoString(errStr)
		C.free(unsafe.Pointer(errStr))
		return nil, DatabaseError(gs)
	}
	return &DB{rocksdb}, nil
}
Ejemplo n.º 4
0
func (db *DB) open() error {
	db.initOptions(db.cfg)

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

	db.db = C.rocksdb_open(db.opts.Opt, ldbname, &errStr)
	if errStr != nil {
		db.db = nil
		return saveError(errStr)
	}
	return nil
}
Ejemplo n.º 5
0
// OpenDb opens a database with the specified options.
func OpenDb(opts *Options, name string) (*DB, error) {
	var cErr *C.char
	cname := C.CString(name)
	defer C.free(unsafe.Pointer(cname))
	db := C.rocksdb_open(opts.c, cname, &cErr)
	if cErr != nil {
		defer C.free(unsafe.Pointer(cErr))

		return nil, errors.New(C.GoString(cErr))
	}

	return &DB{
		name: name,
		c:    db,
		opts: opts,
	}, nil
}
Ejemplo n.º 6
0
// Start creates options and opens the database. If the database
// doesn't yet exist at the specified directory, one is initialized
// from scratch.
func (r *RocksDB) Start() error {
	r.createOptions()

	cDir := C.CString(r.dir)
	defer C.free(unsafe.Pointer(cDir))

	var cErr *C.char
	if r.rdb = C.rocksdb_open(r.opts, cDir, &cErr); cErr != nil {
		r.rdb = nil
		r.destroyOptions()
		return charToErr(cErr)
	}
	if _, err := r.Capacity(); err != nil {
		if err := r.Destroy(); err != nil {
			log.Warningf("could not destroy db at %s", r.dir)
		}
		return err
	}
	return nil
}
Ejemplo n.º 7
0
// NewRocksDB allocates and returns a new RocksDB object.
func NewRocksDB(attrs Attributes, dir string) (*RocksDB, error) {
	r := &RocksDB{attrs: attrs, dir: dir}
	r.createOptions()

	cDir := C.CString(dir)
	defer C.free(unsafe.Pointer(cDir))

	var cErr *C.char
	if r.rdb = C.rocksdb_open(r.opts, cDir, &cErr); cErr != nil {
		r.rdb = nil
		r.destroyOptions()
		return nil, charToErr(cErr)
	}
	if _, err := r.Capacity(); err != nil {
		if err := r.destroy(); err != nil {
			glog.Warningf("could not destroy db at %s", dir)
		}
		return nil, err
	}
	return r, nil
}
Ejemplo n.º 8
0
// NewRocksDB allocates and returns a new RocksDB object.
func NewRocksDB(typ DiskType, dir string) (*RocksDB, error) {
	r := &RocksDB{typ: typ, dir: dir}
	r.createOptions()

	cDir := C.CString(dir)
	defer C.free(unsafe.Pointer(cDir))

	var cErr *C.char
	if r.rdb = C.rocksdb_open(r.opts, cDir, &cErr); cErr != nil {
		r.rdb = nil
		r.destroyOptions()
		s := C.GoString(cErr)
		C.free(unsafe.Pointer(cErr))
		// TODO(andybons): use util.Error.
		return nil, errors.New(s)
	}
	if _, err := r.capacity(); err != nil {
		if err := r.destroy(); err != nil {
			glog.Warningf("could not destroy db at %s", dir)
		}
		return nil, err
	}
	return r, nil
}