Пример #1
0
// destroyOptions destroys the options used for creating, reading, and writing
// from the db. It is meant to be used in conjunction with createOptions.
func (r *RocksDB) destroyOptions() {
	C.rocksdb_options_destroy(r.opts)
	C.rocksdb_readoptions_destroy(r.rOpts)
	C.rocksdb_writeoptions_destroy(r.wOpts)
	r.opts = nil
	r.rOpts = nil
	r.wOpts = nil
}
Пример #2
0
// GetSnapshot returns the value for the given key from the given
// snapshotID, nil otherwise.
func (r *RocksDB) GetSnapshot(key Key, snapshotID string) ([]byte, error) {
	snapshotHandle, ok := r.snapshots[snapshotID]
	if !ok {
		return nil, util.Errorf("snapshotID %s does not exist", snapshotID)
	}

	opts := C.rocksdb_readoptions_create()
	C.rocksdb_readoptions_set_snapshot(opts, snapshotHandle)
	defer C.rocksdb_readoptions_destroy(opts)
	return r.getInternal(key, opts)
}
Пример #3
0
// ScanSnapshot returns up to max key/value objects starting from
// start (inclusive) and ending at end (non-inclusive) from the
// given snapshotID.
// Specify max=0 for unbounded scans.
func (r *RocksDB) ScanSnapshot(start, end Key, max int64, snapshotID string) ([]proto.RawKeyValue, error) {
	snapshotHandle, ok := r.snapshots[snapshotID]
	if !ok {
		return nil, util.Errorf("snapshotID %s does not exist", snapshotID)
	}

	opts := C.rocksdb_readoptions_create()
	C.rocksdb_readoptions_set_fill_cache(opts, 0)
	C.rocksdb_readoptions_set_snapshot(opts, snapshotHandle)
	defer C.rocksdb_readoptions_destroy(opts)
	return r.scanInternal(start, end, max, opts)
}
Пример #4
0
func (opt *ReadOptions) Destroy() {
	if opt.rOpt != nil {
		C.rocksdb_readoptions_destroy(opt.rOpt)
		opt.rOpt = nil
	}

	if opt.snap != nil {
		C.rocksdb_release_snapshot(opt.db, opt.snap)
		opt.snap = nil
		opt.db = nil
	}
}
Пример #5
0
// Scan returns up to max key/value objects starting from
// start (inclusive) and ending at end (non-inclusive).
// If max is zero then the number of key/values returned is unbounded.
func (r *RocksDB) Scan(start, end Key, max int64) ([]RawKeyValue, error) {
	var keyVals []RawKeyValue
	if bytes.Compare(start, end) >= 0 {
		return keyVals, nil
	}
	// In order to prevent content displacement, caching is disabled
	// when performing scans. Any options set within the shared read
	// options field that should be carried over needs to be set here
	// as well.
	opts := C.rocksdb_readoptions_create()
	C.rocksdb_readoptions_set_fill_cache(opts, 0)
	defer C.rocksdb_readoptions_destroy(opts)
	it := C.rocksdb_create_iterator(r.rdb, opts)
	defer C.rocksdb_iter_destroy(it)

	byteCount := len(start)
	if byteCount == 0 {
		// start=Key("") needs special treatment since we need
		// to access start[0] in an explicit seek.
		C.rocksdb_iter_seek_to_first(it)
	} else {
		C.rocksdb_iter_seek(it, bytesPointer(start), C.size_t(byteCount))
	}
	for i := int64(1); C.rocksdb_iter_valid(it) == 1; C.rocksdb_iter_next(it) {
		if max > 0 && i > max {
			break
		}
		var l C.size_t
		// The data returned by rocksdb_iter_{key,value} is not meant to be
		// freed by the client. It is a direct reference to the data managed
		// by the iterator, so it is copied instead of freed.
		data := C.rocksdb_iter_key(it, &l)
		k := C.GoBytes(unsafe.Pointer(data), C.int(l))
		if bytes.Compare(k, end) >= 0 {
			break
		}
		data = C.rocksdb_iter_value(it, &l)
		v := C.GoBytes(unsafe.Pointer(data), C.int(l))
		keyVals = append(keyVals, RawKeyValue{
			Key:   k,
			Value: v,
		})
		i++
	}
	// Check for any errors during iteration.
	var cErr *C.char
	C.rocksdb_iter_get_error(it, &cErr)
	if cErr != nil {
		return nil, charToErr(cErr)
	}
	return keyVals, nil
}
Пример #6
0
// destroyOptions destroys the options used for creating, reading, and writing
// from the db. It is meant to be used in conjunction with createOptions.
func (r *RocksDB) destroyOptions() {
	// The merge operator is stored inside of r.opts as a std::shared_ptr,
	// so it will actually be freed automatically.
	// Calling rocksdb_mergeoperator_destroy will ignore that shared_ptr,
	// and a subsequent rocksdb_options_destroy would segfault.
	// The following line zeroes the shared_ptr instead, effectively
	// deallocating the merge operator if one is set.
	C.rocksdb_options_set_merge_operator(r.opts, nil)
	C.rocksdb_options_destroy(r.opts)
	C.rocksdb_readoptions_destroy(r.rOpts)
	C.rocksdb_writeoptions_destroy(r.wOpts)
	r.mergeOperator = nil
	r.opts = nil
	r.rOpts = nil
	r.wOpts = nil
}
Пример #7
0
// destroyOptions destroys the options used for creating, reading, and writing
// from the db. It is meant to be used in conjunction with createOptions.
func (r *RocksDB) destroyOptions() {
	// The merge operator and compaction filter are stored inside of
	// r.opts using std::shared_ptrs, so they'll be freed
	// automatically. Calling the *_destroy methods directly would
	// ignore the shared_ptrs, and a subsequent rocksdb_options_destroy
	// would segfault. The following lines zero the shared_ptrs instead,
	// which deletes the underlying values.
	C.rocksdb_options_set_merge_operator(r.opts, nil)
	C.rocksdb_options_set_compaction_filter_factory(r.opts, nil)
	C.rocksdb_options_destroy(r.opts)
	C.rocksdb_readoptions_destroy(r.rOpts)
	C.rocksdb_writeoptions_destroy(r.wOpts)
	r.mergeOperator = nil
	r.compactionFilterFactory = nil
	r.opts = nil
	r.rOpts = nil
	r.wOpts = nil
}
Пример #8
0
// scan returns up to max key/value objects starting from
// start (inclusive) and ending at end (non-inclusive).
// If max is zero then the number of key/values returned is unbounded.
func (r *RocksDB) scan(start, end Key, max int64) ([]KeyValue, error) {
	// In order to prevent content displacement, caching is disabled
	// when performing scans. Any options set within the shared read
	// options field that should be carried over needs to be set here
	// as well.
	opts := C.rocksdb_readoptions_create()
	C.rocksdb_readoptions_set_fill_cache(opts, 0)
	defer C.rocksdb_readoptions_destroy(opts)
	it := C.rocksdb_create_iterator(r.rdb, opts)
	defer C.rocksdb_iter_destroy(it)

	keyVals := []KeyValue{}
	C.rocksdb_iter_seek(it, (*C.char)(unsafe.Pointer(&start[0])), C.size_t(len(start)))
	for i := int64(1); C.rocksdb_iter_valid(it) == 1; C.rocksdb_iter_next(it) {
		if max > 0 && i > max {
			break
		}
		var l C.size_t
		// The data returned by rocksdb_iter_{key,value} is not meant to be
		// freed by the client. It is a direct reference to the data managed
		// by the iterator, so it is copied instead of freed.
		data := C.rocksdb_iter_key(it, &l)
		k := C.GoBytes(unsafe.Pointer(data), C.int(l))
		if bytes.Equal(k, end) {
			break
		}
		data = C.rocksdb_iter_value(it, &l)
		v := C.GoBytes(unsafe.Pointer(data), C.int(l))
		keyVals = append(keyVals, KeyValue{
			Key:   k,
			Value: Value{Bytes: v},
		})
		i++
	}
	// Check for any errors during iteration.
	var cErr *C.char
	C.rocksdb_iter_get_error(it, &cErr)
	if cErr != nil {
		return nil, charToErr(cErr)
	}
	return keyVals, nil
}
Пример #9
0
func (db *DB) Close() {
	if db.db != nil {
		C.rocksdb_close(db.db)
		db.db = nil

		if db.opt != nil {
			C.rocksdb_options_destroy(db.opt)
		}
		if db.rOpt != nil {
			C.rocksdb_readoptions_destroy(db.rOpt)
		}
		if db.wOpt != nil {
			C.rocksdb_writeoptions_destroy(db.wOpt)
		}
		if db.cache != nil {
			C.rocksdb_cache_destroy(db.cache)
		}
		if db.fp != nil {
			C.rocksdb_filterpolicy_destroy(db.fp)
		}
	}
}
Пример #10
0
// Close deallocates the ReadOptions, freeing its underlying C struct.
func (ro *ReadOptions) Close() {
	C.rocksdb_readoptions_destroy(ro.Opt)
}
Пример #11
0
// Destroy deallocates the ReadOptions object.
func (self *ReadOptions) Destroy() {
	C.rocksdb_readoptions_destroy(self.c)
	self.c = nil
}
Пример #12
0
// Destroy deallocates the ReadOptions object.
func (opts *ReadOptions) Destroy() {
	C.rocksdb_readoptions_destroy(opts.c)
	opts.c = nil
}
Пример #13
0
// Scan returns up to max key/value objects starting from
// start (inclusive) and ending at end (non-inclusive).
// If max is zero then the number of key/values returned is unbounded.
func (r *RocksDB) Scan(start, end Key, max int64) ([]proto.RawKeyValue, error) {
	opts := C.rocksdb_readoptions_create()
	C.rocksdb_readoptions_set_fill_cache(opts, 0)
	defer C.rocksdb_readoptions_destroy(opts)
	return r.scanInternal(start, end, max, opts)
}
Пример #14
0
// Release deallocates the ReadOptions object.
func (o *ReadOptions) Release() {
	C.rocksdb_readoptions_destroy(o.c)
	o.c = nil
}