示例#1
0
文件: db.go 项目: C0rWin/fabric
// OpenDbColumnFamilies opens a database with the specified column families.
func OpenDbColumnFamilies(
	opts *Options,
	name string,
	cfNames []string,
	cfOpts []*Options,
) (*DB, []*ColumnFamilyHandle, error) {
	numColumnFamilies := len(cfNames)
	if numColumnFamilies != len(cfOpts) {
		return nil, nil, errors.New("must provide the same number of column family names and options")
	}

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

	cNames := make([]*C.char, numColumnFamilies)
	for i, s := range cfNames {
		cNames[i] = C.CString(s)
	}
	defer func() {
		for _, s := range cNames {
			C.free(unsafe.Pointer(s))
		}
	}()

	cOpts := make([]*C.rocksdb_options_t, numColumnFamilies)
	for i, o := range cfOpts {
		cOpts[i] = o.c
	}

	cHandles := make([]*C.rocksdb_column_family_handle_t, numColumnFamilies)

	var cErr *C.char
	db := C.rocksdb_open_column_families(
		opts.c,
		cName,
		C.int(numColumnFamilies),
		&cNames[0],
		&cOpts[0],
		&cHandles[0],
		&cErr,
	)
	if cErr != nil {
		defer C.free(unsafe.Pointer(cErr))
		return nil, nil, errors.New(C.GoString(cErr))
	}

	cfHandles := make([]*ColumnFamilyHandle, numColumnFamilies)
	for i, c := range cHandles {
		cfHandles[i] = NewNativeColumnFamilyHandle(c)
	}

	return &DB{
		name: name,
		c:    db,
		opts: opts,
	}, cfHandles, nil
}
示例#2
0
// OpenWithColumnFamilies is similar to OpenDb but all column families defined must be
// specified in this call.
func OpenDbWithColumnFamilies(dbOpts *Options, name string, cfds []ColumnFamilyDescriptor) (*DB, []*ColumnFamily, error) {
	var cErr *C.char
	len := len(cfds)
	cfNames, cfOpts := cfdsToNameOpts(cfds)
	handles := make([]*C.rocksdb_column_family_handle_t, len)

	cname := C.CString(name)
	defer C.free(unsafe.Pointer(cname))
	db := C.rocksdb_open_column_families(dbOpts.c, cname,
		C.int(len), &cfNames[0], &cfOpts[0], &handles[0], &cErr)
	if cErr != nil {
		defer C.free(unsafe.Pointer(cErr))
		return nil, nil, errors.New(C.GoString(cErr))
	}

	cfs := make([]*ColumnFamily, len)
	for i, h := range handles {
		cfs[i] = &ColumnFamily{c: h}
	}

	return &DB{name: name, c: db, opts: dbOpts}, cfs, nil
}