Exemple #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
}
Exemple #2
0
// NewLRUCache creates a new Cache object with the capacity given.
//
// To prevent memory leaks, Close should be called on the Cache when the
// program no longer needs it. Note: if the process is shutting down, this may
// not be necessary and could be avoided to shorten shutdown time.
func NewLRUCache(capacity int) *Cache {
	return &Cache{C.leveldb_cache_create_lru(C.size_t(capacity))}
}