Example #1
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
}
Example #2
0
// SetCache places a cache object in the database when a database is opened.
//
// This is usually wise to use.
func (o *Options) SetCache(cache *Cache) {
	C.leveldb_options_set_cache(o.Opt, cache.Cache)
}