linxGnu / grocksdb

RocksDB wrapper for Go. Support 9.x, 8.x, 7.x, 6.x, etc

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Incorrect binding for MultiGetWithCF

Gezort opened this issue · comments

How to reproduce:
package main

import (
	"fmt"

	"github.com/linxGnu/grocksdb"
)

const dbPath = "/tmp/rocksdb"

var allCFs = []string{"default", "cf1", "cf2"}

var keys = [][]byte{[]byte("k1"), []byte("k2")}
var values = [][]byte{[]byte("v1"), []byte("v2")}

func main() {
	opts := grocksdb.NewDefaultOptions()
	opts.SetCreateIfMissing(true)
	opts.SetCreateIfMissingColumnFamilies(true)

	dbOpts := grocksdb.NewDefaultTransactionDBOptions()
	perTableOpts := make([]*grocksdb.Options, len(allCFs))
	for i := range perTableOpts {
		perTableOpts[i] = grocksdb.NewDefaultOptions()
		perTableOpts[i].SetAtomicFlush(true)
	}

	db, cfs, err := grocksdb.OpenTransactionDbColumnFamilies(
		opts,
		dbOpts,
		dbPath,
		allCFs,
		perTableOpts,
	)

	if err != nil {
		panic(fmt.Sprintf("failed to open db : %s", err.Error()))
	}

	for i := range keys {
		err = db.PutCF(grocksdb.NewDefaultWriteOptions(), cfs[1], keys[i], values[i])
		if err != nil {
			panic(fmt.Sprintf("failed to insert key : %s", err.Error()))
		}
	}

	data, err := db.MultiGetWithCF(grocksdb.NewDefaultReadOptions(), cfs[1], keys...)
	if err != nil {
		panic(fmt.Sprintf("failed to run MultiGetWithCF : %s", err.Error()))
	}
	for i := range data {
		fmt.Printf("fetched data[%d] is : %s\n", i, data[i].Data())
	}
	// Moreover if you specify only one non-default CF call to MultiGetWithCF will cause segfault
	// This can be easily checked by removing `cf2` from `allCFs`
}

The reason of such behaviour is that here we have only one CF. However in rocksdb lib array of CFs is expected

Fix looks quite simple
	cfs := make(ColumnFamilyHandles, len(keys))
	for i := range keys {
		cfs[i] = cf
	}

	C.rocksdb_transactiondb_multi_get_cf(
		transaction.c,
		opts.c,
		cfs.toCSlice().c(),
		C.size_t(len(keys)),
		cKeys.c(),
		cKeySizes.c(),
		vals.c(),
		valSizes.c(),
		rocksErrs.c(),
	)

Do you accept PRs? :)

Note: there are several such places in the lib

@Gezort
Totally welcome PR. I am looking forward your PR and would review it asap.

@linxGnu
just mention in case you've missed the notification :)

Thank you for notifying me @Gezort
The PR LGTM. Thank you so much.

Thank you for the contribution. The PR is merged. I will release a new version accordingly.