-
Notifications
You must be signed in to change notification settings - Fork 67
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
Custom Comparator in Go #66
Comments
@muthukrishnan24 please forgive me that I did not fully understand your question? Could you please describe it in simpler way?
|
I'm trying to create a Custom Comparator for a CF
Since Comparator needs to implement method Above code throws following errors
Directly referencing the type, also not possible since it is always unexported
|
@muthukrishnan24 I will reproduce at my side and tell you. |
package main
// #cgo LDFLAGS: -lrocksdb -pthread -lstdc++ -ldl -lm -lzstd -llz4 -lz -lsnappy
// #include "rocksdb/c.h"
// #include "a.h"
import "C"
import (
"fmt"
"os"
"unsafe"
"github.com/linxGnu/grocksdb"
)
func main() {
opts := grocksdb.NewDefaultOptions()
opts.SetCreateIfMissing(true)
// work:
cmp := C.comparator_create(0)
opts.SetNativeComparator(unsafe.Pointer(cmp))
// Also work:
// opts.SetComparator(grocksdb.NewComparator("abc", func(a, b []byte) int {
// return bytes.Compare(b, a)
// }))
db, err := grocksdb.OpenDb(opts, "testdb")
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
defer db.Close()
// insert keys
givenKeys := [][]byte{[]byte("key1"), []byte("key2"), []byte("key3")}
wo := grocksdb.NewDefaultWriteOptions()
for _, k := range givenKeys {
if err := db.Put(wo, k, []byte("val")); err != nil {
panic(err)
}
}
// create a iterator to collect the keys
ro := grocksdb.NewDefaultReadOptions()
iter := db.NewIterator(ro)
defer iter.Close()
// we seek to the last key and iterate in reverse order
// to match given keys
for iter.SeekToFirst(); iter.Valid(); iter.Next() {
key := make([]byte, 4)
copy(key, iter.Key().Data())
fmt.Println(string(key))
}
}
#include <stdlib.h>
#include "rocksdb/c.h"
// This API provides convenient C wrapper functions for rocksdb client.
/* Base */
static void gorocksdb_destruct_handler(void* state) {
}
static const char* cmp_name(void*) {
return "abc";
}
static int compare_custom(void* state, const char* a, size_t alen, const char* b, size_t blen) {
for (size_t i = 0; i < alen; ++i)
if (a[i] > b[i]) return -1;
return 1;
}
/* Comparator */
rocksdb_comparator_t* comparator_create(uintptr_t idx) {
return rocksdb_comparator_create(
(void*)idx,
gorocksdb_destruct_handler,
compare_custom,
cmp_name);
} |
@muthukrishnan24 I have updated the API to make your code built-able, as well as easier to create custom comparator:
The patch is included in the release: https://github.com/linxGnu/grocksdb/releases/tag/v1.6.47 I also gave an example above to test. PTAL |
Thanks @linxGnu |
Comparator interface needs Native method
Native() *C.rocksdb_comparator_t
Because of this, it is not possible to implement this interface.
The text was updated successfully, but these errors were encountered: