Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect binding for MultiGetWithCF #137

Closed
Gezort opened this issue Dec 13, 2023 · 4 comments
Closed

Incorrect binding for MultiGetWithCF #137

Gezort opened this issue Dec 13, 2023 · 4 comments

Comments

@Gezort
Copy link
Contributor

Gezort commented Dec 13, 2023

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

@linxGnu
Copy link
Owner

linxGnu commented Dec 13, 2023

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

@Gezort
Copy link
Contributor Author

Gezort commented Dec 14, 2023

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

@linxGnu
Copy link
Owner

linxGnu commented Dec 14, 2023

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

linxGnu pushed a commit that referenced this issue Dec 14, 2023
@linxGnu
Copy link
Owner

linxGnu commented Dec 14, 2023

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

@linxGnu linxGnu closed this as completed Jan 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants