From 254f68ba8333423d240d2337354f043c70942a8e Mon Sep 17 00:00:00 2001 From: Linh Tran Tuan Date: Thu, 9 Nov 2023 15:38:15 +0900 Subject: [PATCH] CHORE Code refactoring (#130) --- array.go | 9 ++-- build.sh | 4 +- compaction_filter.go | 6 +-- comparator.go | 12 ++--- db.go | 116 +++++++++++++++++++++--------------------- iterator.go | 4 +- merge_operator.go | 10 ++-- options_compaction.go | 2 +- options_read.go | 8 +-- slice.go | 4 +- slice_transform.go | 6 +-- sst_file_writer.go | 28 +++++----- transaction.go | 38 +++++++------- transactiondb.go | 28 +++++----- util.go | 9 ++-- write_batch.go | 52 +++++++++---------- write_batch_wi.go | 44 ++++++++-------- 17 files changed, 192 insertions(+), 188 deletions(-) diff --git a/array.go b/array.go index 5a2c4eb..0c3668d 100644 --- a/array.go +++ b/array.go @@ -3,14 +3,17 @@ package grocksdb // #include "stdlib.h" // #include "rocksdb/c.h" import "C" + import ( "reflect" "unsafe" ) -type charsSlice []*C.char -type sizeTSlice []C.size_t -type columnFamilySlice []*C.rocksdb_column_family_handle_t +type ( + charsSlice []*C.char + sizeTSlice []C.size_t + columnFamilySlice []*C.rocksdb_column_family_handle_t +) func (s charsSlice) c() **C.char { sH := (*reflect.SliceHeader)(unsafe.Pointer(&s)) diff --git a/build.sh b/build.sh index 90127ab..f0a1163 100755 --- a/build.sh +++ b/build.sh @@ -8,7 +8,7 @@ mkdir -p $BUILD_PATH CMAKE_REQUIRED_PARAMS="-DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_INSTALL_PREFIX=${INSTALL_PREFIX}" -snappy_version="1.1.9" +snappy_version="1.1.10" cd $BUILD_PATH && wget https://github.com/google/snappy/archive/${snappy_version}.tar.gz && tar xzf ${snappy_version}.tar.gz && cd snappy-${snappy_version} && \ mkdir -p build_place && cd build_place && \ cmake $CMAKE_REQUIRED_PARAMS -DSNAPPY_BUILD_TESTS=OFF -DSNAPPY_BUILD_BENCHMARKS=OFF .. && \ @@ -18,7 +18,7 @@ cd $BUILD_PATH && wget https://github.com/google/snappy/archive/${snappy_version export CFLAGS='-fPIC -O3 -pipe' export CXXFLAGS='-fPIC -O3 -pipe -Wno-uninitialized' -zlib_version="1.2.13" +zlib_version="1.3" cd $BUILD_PATH && wget https://github.com/madler/zlib/archive/v${zlib_version}.tar.gz && tar xzf v${zlib_version}.tar.gz && cd zlib-${zlib_version} && \ ./configure --prefix=$INSTALL_PREFIX --static && make -j16 install && \ cd $BUILD_PATH && rm -rf * diff --git a/compaction_filter.go b/compaction_filter.go index 18ec3bb..ec31579 100644 --- a/compaction_filter.go +++ b/compaction_filter.go @@ -76,14 +76,14 @@ func registerCompactionFilter(filter CompactionFilter) int { //export gorocksdb_compactionfilter_filter func gorocksdb_compactionfilter_filter(idx int, cLevel C.int, cKey *C.char, cKeyLen C.size_t, cVal *C.char, cValLen C.size_t, cNewVal **C.char, cNewValLen *C.size_t, cValChanged *C.uchar) C.int { - key := charToByte(cKey, cKeyLen) - val := charToByte(cVal, cValLen) + key := refCBytes(cKey, cKeyLen) + val := refCBytes(cVal, cValLen) remove, newVal := compactionFilters.Get(idx).(compactionFilterWrapper).filter.Filter(int(cLevel), key, val) if remove { return C.int(1) } else if newVal != nil { - *cNewVal = byteToChar(newVal) + *cNewVal = refGoBytes(newVal) *cNewValLen = C.size_t(len(newVal)) *cValChanged = C.uchar(1) } diff --git a/comparator.go b/comparator.go index a681429..1dcde49 100644 --- a/comparator.go +++ b/comparator.go @@ -89,22 +89,22 @@ func registerComperator(cmp *Comparator) int { //export gorocksdb_comparator_compare func gorocksdb_comparator_compare(idx int, cKeyA *C.char, cKeyALen C.size_t, cKeyB *C.char, cKeyBLen C.size_t) C.int { - keyA := charToByte(cKeyA, cKeyALen) - keyB := charToByte(cKeyB, cKeyBLen) + keyA := refCBytes(cKeyA, cKeyALen) + keyB := refCBytes(cKeyB, cKeyBLen) return C.int(comperators.Get(idx).(comperatorWrapper).comparator.Compare(keyA, keyB)) } //export gorocksdb_comparator_compare_ts func gorocksdb_comparator_compare_ts(idx int, cTsA *C.char, cTsALen C.size_t, cTsB *C.char, cTsBLen C.size_t) C.int { - tsA := charToByte(cTsA, cTsALen) - tsB := charToByte(cTsB, cTsBLen) + tsA := refCBytes(cTsA, cTsALen) + tsB := refCBytes(cTsB, cTsBLen) return C.int(comperators.Get(idx).(comperatorWrapper).comparator.CompareTimestamp(tsA, tsB)) } //export gorocksdb_comparator_compare_without_ts func gorocksdb_comparator_compare_without_ts(idx int, cKeyA *C.char, cKeyALen C.size_t, cAHasTs C.uchar, cKeyB *C.char, cKeyBLen C.size_t, cBHasTs C.uchar) C.int { - keyA := charToByte(cKeyA, cKeyALen) - keyB := charToByte(cKeyB, cKeyBLen) + keyA := refCBytes(cKeyA, cKeyALen) + keyB := refCBytes(cKeyB, cKeyBLen) keyAHasTs := charToBool(cAHasTs) keyBHasTs := charToBool(cBHasTs) return C.int(comperators.Get(idx).(comperatorWrapper).comparator.CompareWithoutTimestamp(keyA, keyAHasTs, keyB, keyBHasTs)) diff --git a/db.go b/db.go index 28214bd..d5b98aa 100644 --- a/db.go +++ b/db.go @@ -403,7 +403,7 @@ func OpenDbAndTrimHistory(opts *Options, cHandles := make([]*C.rocksdb_column_family_handle_t, numColumnFamilies) - cTs := byteToChar(trimTimestamp) + cTs := refGoBytes(trimTimestamp) var cErr *C.char _db := C.rocksdb_open_and_trim_history( @@ -476,9 +476,9 @@ func (db *DB) KeyMayExists(opts *ReadOptions, key []byte, timestamp string) (sli var ( cValue *C.char cValLen C.size_t - cKey = byteToChar(key) + cKey = refGoBytes(key) cFound C.uchar = 0 - cTimestamp = byteToChar(t) + cTimestamp = refGoBytes(t) ) C.rocksdb_key_may_exist(db.c, opts.c, @@ -502,9 +502,9 @@ func (db *DB) KeyMayExistsCF(opts *ReadOptions, cf *ColumnFamilyHandle, key []by var ( cValue *C.char cValLen C.size_t - cKey = byteToChar(key) + cKey = refGoBytes(key) cFound C.uchar = 0 - cTimestamp = byteToChar(t) + cTimestamp = refGoBytes(t) ) C.rocksdb_key_may_exist_cf(db.c, opts.c, @@ -526,7 +526,7 @@ func (db *DB) Get(opts *ReadOptions, key []byte) (slice *Slice, err error) { var ( cErr *C.char cValLen C.size_t - cKey = byteToChar(key) + cKey = refGoBytes(key) ) cValue := C.rocksdb_get(db.c, opts.c, cKey, C.size_t(len(key)), &cValLen, &cErr) @@ -544,7 +544,7 @@ func (db *DB) GetWithTS(opts *ReadOptions, key []byte) (value, timestamp *Slice, cTs *C.char cValLen C.size_t cTsLen C.size_t - cKey = byteToChar(key) + cKey = refGoBytes(key) ) cValue := C.rocksdb_get_with_ts(db.c, opts.c, cKey, C.size_t(len(key)), &cValLen, &cTs, &cTsLen, &cErr) @@ -561,7 +561,7 @@ func (db *DB) GetBytes(opts *ReadOptions, key []byte) (data []byte, err error) { var ( cErr *C.char cValLen C.size_t - cKey = byteToChar(key) + cKey = refGoBytes(key) ) cValue := C.rocksdb_get(db.c, opts.c, cKey, C.size_t(len(key)), &cValLen, &cErr) @@ -584,7 +584,7 @@ func (db *DB) GetBytesWithTS(opts *ReadOptions, key []byte) (data, timestamp []b cTs *C.char cValLen C.size_t cTsLen C.size_t - cKey = byteToChar(key) + cKey = refGoBytes(key) ) cValue := C.rocksdb_get_with_ts(db.c, opts.c, cKey, C.size_t(len(key)), &cValLen, &cTs, &cTsLen, &cErr) @@ -607,7 +607,7 @@ func (db *DB) GetCF(opts *ReadOptions, cf *ColumnFamilyHandle, key []byte) (slic var ( cErr *C.char cValLen C.size_t - cKey = byteToChar(key) + cKey = refGoBytes(key) ) cValue := C.rocksdb_get_cf(db.c, opts.c, cf.c, cKey, C.size_t(len(key)), &cValLen, &cErr) @@ -625,7 +625,7 @@ func (db *DB) GetCFWithTS(opts *ReadOptions, cf *ColumnFamilyHandle, key []byte) cTs *C.char cValLen C.size_t cTsLen C.size_t - cKey = byteToChar(key) + cKey = refGoBytes(key) ) cValue := C.rocksdb_get_cf_with_ts(db.c, opts.c, cf.c, cKey, C.size_t(len(key)), &cValLen, &cTs, &cTsLen, &cErr) @@ -641,7 +641,7 @@ func (db *DB) GetCFWithTS(opts *ReadOptions, cf *ColumnFamilyHandle, key []byte) func (db *DB) GetPinned(opts *ReadOptions, key []byte) (handle *PinnableSliceHandle, err error) { var ( cErr *C.char - cKey = byteToChar(key) + cKey = refGoBytes(key) ) cHandle := C.rocksdb_get_pinned(db.c, opts.c, cKey, C.size_t(len(key)), &cErr) @@ -656,7 +656,7 @@ func (db *DB) GetPinned(opts *ReadOptions, key []byte) (handle *PinnableSliceHan func (db *DB) GetPinnedCF(opts *ReadOptions, cf *ColumnFamilyHandle, key []byte) (handle *PinnableSliceHandle, err error) { var ( cErr *C.char - cKey = byteToChar(key) + cKey = refGoBytes(key) ) cHandle := C.rocksdb_get_pinned_cf(db.c, opts.c, cf.c, cKey, C.size_t(len(key)), &cErr) @@ -880,8 +880,8 @@ func (db *DB) MultiGetMultiCFWithTS(opts *ReadOptions, cfs ColumnFamilyHandles, func (db *DB) Put(opts *WriteOptions, key, value []byte) (err error) { var ( cErr *C.char - cKey = byteToChar(key) - cValue = byteToChar(value) + cKey = refGoBytes(key) + cValue = refGoBytes(value) ) C.rocksdb_put(db.c, opts.c, cKey, C.size_t(len(key)), cValue, C.size_t(len(value)), &cErr) @@ -894,9 +894,9 @@ func (db *DB) Put(opts *WriteOptions, key, value []byte) (err error) { func (db *DB) PutWithTS(opts *WriteOptions, key, ts, value []byte) (err error) { var ( cErr *C.char - cKey = byteToChar(key) - cValue = byteToChar(value) - cTs = byteToChar(ts) + cKey = refGoBytes(key) + cValue = refGoBytes(value) + cTs = refGoBytes(ts) ) C.rocksdb_put_with_ts(db.c, opts.c, cKey, C.size_t(len(key)), cTs, C.size_t(len(ts)), cValue, C.size_t(len(value)), &cErr) @@ -909,8 +909,8 @@ func (db *DB) PutWithTS(opts *WriteOptions, key, ts, value []byte) (err error) { func (db *DB) PutCF(opts *WriteOptions, cf *ColumnFamilyHandle, key, value []byte) (err error) { var ( cErr *C.char - cKey = byteToChar(key) - cValue = byteToChar(value) + cKey = refGoBytes(key) + cValue = refGoBytes(value) ) C.rocksdb_put_cf(db.c, opts.c, cf.c, cKey, C.size_t(len(key)), cValue, C.size_t(len(value)), &cErr) @@ -923,9 +923,9 @@ func (db *DB) PutCF(opts *WriteOptions, cf *ColumnFamilyHandle, key, value []byt func (db *DB) PutCFWithTS(opts *WriteOptions, cf *ColumnFamilyHandle, key, ts, value []byte) (err error) { var ( cErr *C.char - cKey = byteToChar(key) - cValue = byteToChar(value) - cTs = byteToChar(ts) + cKey = refGoBytes(key) + cValue = refGoBytes(value) + cTs = refGoBytes(ts) ) C.rocksdb_put_cf_with_ts(db.c, opts.c, cf.c, cKey, C.size_t(len(key)), cTs, C.size_t(len(ts)), cValue, C.size_t(len(value)), &cErr) @@ -938,7 +938,7 @@ func (db *DB) PutCFWithTS(opts *WriteOptions, cf *ColumnFamilyHandle, key, ts, v func (db *DB) Delete(opts *WriteOptions, key []byte) (err error) { var ( cErr *C.char - cKey = byteToChar(key) + cKey = refGoBytes(key) ) C.rocksdb_delete(db.c, opts.c, cKey, C.size_t(len(key)), &cErr) @@ -951,7 +951,7 @@ func (db *DB) Delete(opts *WriteOptions, key []byte) (err error) { func (db *DB) DeleteCF(opts *WriteOptions, cf *ColumnFamilyHandle, key []byte) (err error) { var ( cErr *C.char - cKey = byteToChar(key) + cKey = refGoBytes(key) ) C.rocksdb_delete_cf(db.c, opts.c, cf.c, cKey, C.size_t(len(key)), &cErr) @@ -964,8 +964,8 @@ func (db *DB) DeleteCF(opts *WriteOptions, cf *ColumnFamilyHandle, key []byte) ( func (db *DB) DeleteWithTS(opts *WriteOptions, key, ts []byte) (err error) { var ( cErr *C.char - cKey = byteToChar(key) - cTs = byteToChar(ts) + cKey = refGoBytes(key) + cTs = refGoBytes(ts) ) C.rocksdb_delete_with_ts(db.c, opts.c, cKey, C.size_t(len(key)), cTs, C.size_t(len(ts)), &cErr) @@ -978,8 +978,8 @@ func (db *DB) DeleteWithTS(opts *WriteOptions, key, ts []byte) (err error) { func (db *DB) DeleteCFWithTS(opts *WriteOptions, cf *ColumnFamilyHandle, key, ts []byte) (err error) { var ( cErr *C.char - cKey = byteToChar(key) - cTs = byteToChar(ts) + cKey = refGoBytes(key) + cTs = refGoBytes(ts) ) C.rocksdb_delete_cf_with_ts(db.c, opts.c, cf.c, cKey, C.size_t(len(key)), cTs, C.size_t(len(ts)), &cErr) @@ -992,8 +992,8 @@ func (db *DB) DeleteCFWithTS(opts *WriteOptions, cf *ColumnFamilyHandle, key, ts func (db *DB) SingleDeleteWithTS(opts *WriteOptions, key, ts []byte) (err error) { var ( cErr *C.char - cKey = byteToChar(key) - cTs = byteToChar(ts) + cKey = refGoBytes(key) + cTs = refGoBytes(ts) ) C.rocksdb_singledelete_with_ts(db.c, opts.c, cKey, C.size_t(len(key)), cTs, C.size_t(len(ts)), &cErr) @@ -1006,8 +1006,8 @@ func (db *DB) SingleDeleteWithTS(opts *WriteOptions, key, ts []byte) (err error) func (db *DB) SingleDeleteCFWithTS(opts *WriteOptions, cf *ColumnFamilyHandle, key, ts []byte) (err error) { var ( cErr *C.char - cKey = byteToChar(key) - cTs = byteToChar(ts) + cKey = refGoBytes(key) + cTs = refGoBytes(ts) ) C.rocksdb_singledelete_cf_with_ts(db.c, opts.c, cf.c, cKey, C.size_t(len(key)), cTs, C.size_t(len(ts)), &cErr) @@ -1020,8 +1020,8 @@ func (db *DB) SingleDeleteCFWithTS(opts *WriteOptions, cf *ColumnFamilyHandle, k func (db *DB) DeleteRangeCF(opts *WriteOptions, cf *ColumnFamilyHandle, startKey []byte, endKey []byte) (err error) { var ( cErr *C.char - cStartKey = byteToChar(startKey) - cEndKey = byteToChar(endKey) + cStartKey = refGoBytes(startKey) + cEndKey = refGoBytes(endKey) ) C.rocksdb_delete_range_cf(db.c, opts.c, cf.c, cStartKey, C.size_t(len(startKey)), cEndKey, C.size_t(len(endKey)), &cErr) @@ -1049,7 +1049,7 @@ func (db *DB) DeleteRangeCF(opts *WriteOptions, cf *ColumnFamilyHandle, startKey func (db *DB) SingleDelete(opts *WriteOptions, key []byte) (err error) { var ( cErr *C.char - cKey = byteToChar(key) + cKey = refGoBytes(key) ) C.rocksdb_singledelete(db.c, opts.c, cKey, C.size_t(len(key)), &cErr) @@ -1077,7 +1077,7 @@ func (db *DB) SingleDelete(opts *WriteOptions, key []byte) (err error) { func (db *DB) SingleDeleteCF(opts *WriteOptions, cf *ColumnFamilyHandle, key []byte) (err error) { var ( cErr *C.char - cKey = byteToChar(key) + cKey = refGoBytes(key) ) C.rocksdb_singledelete_cf(db.c, opts.c, cf.c, cKey, C.size_t(len(key)), &cErr) @@ -1090,8 +1090,8 @@ func (db *DB) SingleDeleteCF(opts *WriteOptions, cf *ColumnFamilyHandle, key []b func (db *DB) Merge(opts *WriteOptions, key []byte, value []byte) (err error) { var ( cErr *C.char - cKey = byteToChar(key) - cValue = byteToChar(value) + cKey = refGoBytes(key) + cValue = refGoBytes(value) ) C.rocksdb_merge(db.c, opts.c, cKey, C.size_t(len(key)), cValue, C.size_t(len(value)), &cErr) @@ -1105,8 +1105,8 @@ func (db *DB) Merge(opts *WriteOptions, key []byte, value []byte) (err error) { func (db *DB) MergeCF(opts *WriteOptions, cf *ColumnFamilyHandle, key []byte, value []byte) (err error) { var ( cErr *C.char - cKey = byteToChar(key) - cValue = byteToChar(value) + cKey = refGoBytes(key) + cValue = refGoBytes(value) ) C.rocksdb_merge_cf(db.c, opts.c, cf.c, cKey, C.size_t(len(key)), cValue, C.size_t(len(value)), &cErr) @@ -1540,39 +1540,39 @@ func (db *DB) GetLiveFilesMetaData() []LiveFileMetadata { // CompactRange runs a manual compaction on the Range of keys given. This is // not likely to be needed for typical usage. func (db *DB) CompactRange(r Range) { - cStart := byteToChar(r.Start) - cLimit := byteToChar(r.Limit) + cStart := refGoBytes(r.Start) + cLimit := refGoBytes(r.Limit) C.rocksdb_compact_range(db.c, cStart, C.size_t(len(r.Start)), cLimit, C.size_t(len(r.Limit))) } // CompactRangeCF runs a manual compaction on the Range of keys given on the // given column family. This is not likely to be needed for typical usage. func (db *DB) CompactRangeCF(cf *ColumnFamilyHandle, r Range) { - cStart := byteToChar(r.Start) - cLimit := byteToChar(r.Limit) + cStart := refGoBytes(r.Start) + cLimit := refGoBytes(r.Limit) C.rocksdb_compact_range_cf(db.c, cf.c, cStart, C.size_t(len(r.Start)), cLimit, C.size_t(len(r.Limit))) } // CompactRangeOpt runs a manual compaction on the Range of keys given with provided options. This is // not likely to be needed for typical usage. func (db *DB) CompactRangeOpt(r Range, opt *CompactRangeOptions) { - cStart := byteToChar(r.Start) - cLimit := byteToChar(r.Limit) + cStart := refGoBytes(r.Start) + cLimit := refGoBytes(r.Limit) C.rocksdb_compact_range_opt(db.c, opt.c, cStart, C.size_t(len(r.Start)), cLimit, C.size_t(len(r.Limit))) } // CompactRangeCFOpt runs a manual compaction on the Range of keys given on the // given column family with provided options. This is not likely to be needed for typical usage. func (db *DB) CompactRangeCFOpt(cf *ColumnFamilyHandle, r Range, opt *CompactRangeOptions) { - cStart := byteToChar(r.Start) - cLimit := byteToChar(r.Limit) + cStart := refGoBytes(r.Start) + cLimit := refGoBytes(r.Limit) C.rocksdb_compact_range_cf_opt(db.c, cf.c, opt.c, cStart, C.size_t(len(r.Start)), cLimit, C.size_t(len(r.Limit))) } // SuggestCompactRange only for leveled compaction. func (db *DB) SuggestCompactRange(r Range) (err error) { - cStart := byteToChar(r.Start) - cLimit := byteToChar(r.Limit) + cStart := refGoBytes(r.Start) + cLimit := refGoBytes(r.Limit) var cErr *C.char C.rocksdb_suggest_compact_range(db.c, cStart, C.size_t(len(r.Start)), cLimit, C.size_t(len(r.Limit)), &cErr) @@ -1583,8 +1583,8 @@ func (db *DB) SuggestCompactRange(r Range) (err error) { // SuggestCompactRangeCF only for leveled compaction. func (db *DB) SuggestCompactRangeCF(cf *ColumnFamilyHandle, r Range) (err error) { - cStart := byteToChar(r.Start) - cLimit := byteToChar(r.Limit) + cStart := refGoBytes(r.Start) + cLimit := refGoBytes(r.Limit) var cErr *C.char C.rocksdb_suggest_compact_range_cf(db.c, cf.c, cStart, C.size_t(len(r.Start)), cLimit, C.size_t(len(r.Limit)), &cErr) @@ -1672,8 +1672,8 @@ func (db *DB) DeleteFile(name string) { // DeleteFileInRange deletes SST files that contain keys between the Range, [r.Start, r.Limit] func (db *DB) DeleteFileInRange(r Range) (err error) { - cStartKey := byteToChar(r.Start) - cLimitKey := byteToChar(r.Limit) + cStartKey := refGoBytes(r.Start) + cLimitKey := refGoBytes(r.Limit) var cErr *C.char @@ -1691,8 +1691,8 @@ func (db *DB) DeleteFileInRange(r Range) (err error) { // DeleteFileInRangeCF deletes SST files that contain keys between the Range, [r.Start, r.Limit], and // belong to a given column family func (db *DB) DeleteFileInRangeCF(cf *ColumnFamilyHandle, r Range) (err error) { - cStartKey := byteToChar(r.Start) - cLimitKey := byteToChar(r.Limit) + cStartKey := refGoBytes(r.Start) + cLimitKey := refGoBytes(r.Limit) var cErr *C.char @@ -1715,7 +1715,7 @@ func (db *DB) DeleteFileInRangeCF(cf *ColumnFamilyHandle, r Range) (err error) { func (db *DB) IncreaseFullHistoryTsLow(handle *ColumnFamilyHandle, ts []byte) (err error) { var cErr *C.char - cTs := byteToChar(ts) + cTs := refGoBytes(ts) C.rocksdb_increase_full_history_ts_low(db.c, handle.c, cTs, C.size_t(len(ts)), &cErr) err = fromCError(cErr) diff --git a/iterator.go b/iterator.go index f32276d..12d1bd5 100644 --- a/iterator.go +++ b/iterator.go @@ -104,14 +104,14 @@ func (iter *Iterator) SeekToLast() { // Seek moves the iterator to the position greater than or equal to the key. func (iter *Iterator) Seek(key []byte) { - cKey := byteToChar(key) + cKey := refGoBytes(key) C.rocksdb_iter_seek(iter.c, cKey, C.size_t(len(key))) } // SeekForPrev moves the iterator to the last key that less than or equal // to the target key, in contrast with Seek. func (iter *Iterator) SeekForPrev(key []byte) { - cKey := byteToChar(key) + cKey := refGoBytes(key) C.rocksdb_iter_seek_for_prev(iter.c, cKey, C.size_t(len(key))) } diff --git a/merge_operator.go b/merge_operator.go index da809f1..0b8d1f3 100644 --- a/merge_operator.go +++ b/merge_operator.go @@ -114,13 +114,13 @@ func registerMergeOperator(merger MergeOperator) int { //export gorocksdb_mergeoperator_full_merge func gorocksdb_mergeoperator_full_merge(idx int, cKey *C.char, cKeyLen C.size_t, cExistingValue *C.char, cExistingValueLen C.size_t, cOperands **C.char, cOperandsLen *C.size_t, cNumOperands C.int, cSuccess *C.uchar, cNewValueLen *C.size_t) *C.char { - key := charToByte(cKey, cKeyLen) + key := refCBytes(cKey, cKeyLen) rawOperands := charSlice(cOperands, cNumOperands) operandsLen := sizeSlice(cOperandsLen, cNumOperands) - existingValue := charToByte(cExistingValue, cExistingValueLen) + existingValue := refCBytes(cExistingValue, cExistingValueLen) operands := make([][]byte, int(cNumOperands)) for i, len := range operandsLen { - operands[i] = charToByte(rawOperands[i], len) + operands[i] = refCBytes(rawOperands[i], len) } newValue, success := mergeOperators.Get(idx).(mergeOperatorWrapper).mergeOperator.FullMerge(key, existingValue, operands) @@ -134,12 +134,12 @@ func gorocksdb_mergeoperator_full_merge(idx int, cKey *C.char, cKeyLen C.size_t, //export gorocksdb_mergeoperator_partial_merge_multi func gorocksdb_mergeoperator_partial_merge_multi(idx int, cKey *C.char, cKeyLen C.size_t, cOperands **C.char, cOperandsLen *C.size_t, cNumOperands C.int, cSuccess *C.uchar, cNewValueLen *C.size_t) *C.char { - key := charToByte(cKey, cKeyLen) + key := refCBytes(cKey, cKeyLen) rawOperands := charSlice(cOperands, cNumOperands) operandsLen := sizeSlice(cOperandsLen, cNumOperands) operands := make([][]byte, int(cNumOperands)) for i, len := range operandsLen { - operands[i] = charToByte(rawOperands[i], len) + operands[i] = refCBytes(rawOperands[i], len) } var newValue []byte diff --git a/options_compaction.go b/options_compaction.go index 2e79286..7f6433d 100644 --- a/options_compaction.go +++ b/options_compaction.go @@ -105,7 +105,7 @@ func (opts *CompactRangeOptions) TargetLevel() int32 { // low bound maybe GCed by compaction. // Default: nullptr func (opts *CompactRangeOptions) SetFullHistoryTsLow(ts []byte) { - cTs := byteToChar(ts) + cTs := refGoBytes(ts) C.rocksdb_compactoptions_set_full_history_ts_low(opts.c, cTs, C.size_t(len(ts))) } diff --git a/options_read.go b/options_read.go index e81a23b..11cf2da 100644 --- a/options_read.go +++ b/options_read.go @@ -90,7 +90,7 @@ func (opts *ReadOptions) SetSnapshot(snap *Snapshot) { // Default: nullptr func (opts *ReadOptions) SetIterateUpperBound(key []byte) { opts.iterUpperBound = key - cKey := byteToChar(key) + cKey := refGoBytes(key) cKeyLen := C.size_t(len(key)) C.rocksdb_readoptions_set_iterate_upper_bound(opts.c, cKey, cKeyLen) } @@ -105,7 +105,7 @@ func (opts *ReadOptions) SetIterateUpperBound(key []byte) { // Default: nullptr func (opts *ReadOptions) SetIterateLowerBound(key []byte) { opts.iterLowerBound = key - cKey := byteToChar(key) + cKey := refGoBytes(key) cKeyLen := C.size_t(len(key)) C.rocksdb_readoptions_set_iterate_lower_bound(opts.c, cKey, cKeyLen) } @@ -329,7 +329,7 @@ func (opts *ReadOptions) Destroy() { // Default: nullptr func (opts *ReadOptions) SetTimestamp(ts []byte) { opts.timestamp = ts - cTS := byteToChar(ts) + cTS := refGoBytes(ts) cTSLen := C.size_t(len(ts)) C.rocksdb_readoptions_set_timestamp(opts.c, cTS, cTSLen) } @@ -341,7 +341,7 @@ func (opts *ReadOptions) SetTimestamp(ts []byte) { // Default: nullptr func (opts *ReadOptions) SetIterStartTimestamp(ts []byte) { opts.timestampStart = ts - cTS := byteToChar(ts) + cTS := refGoBytes(ts) cTSLen := C.size_t(len(ts)) C.rocksdb_readoptions_set_iter_start_ts(opts.c, cTS, cTSLen) } diff --git a/slice.go b/slice.go index be1a6c6..1ec2d56 100644 --- a/slice.go +++ b/slice.go @@ -36,7 +36,7 @@ func (s *Slice) Exists() bool { // nil slice. func (s *Slice) Data() []byte { if s.Exists() { - return charToByte(s.data, s.size) + return refCBytes(s.data, s.size) } return nil @@ -76,7 +76,7 @@ func (h *PinnableSliceHandle) Data() []byte { if h.Exists() { var cValLen C.size_t cValue := C.rocksdb_pinnableslice_value(h.c, &cValLen) - return charToByte(cValue, cValLen) + return refCBytes(cValue, cValLen) } return nil diff --git a/slice_transform.go b/slice_transform.go index 5e83eb9..c65e494 100644 --- a/slice_transform.go +++ b/slice_transform.go @@ -64,7 +64,7 @@ func registerSliceTransform(st SliceTransform) int { //export gorocksdb_slicetransform_transform func gorocksdb_slicetransform_transform(idx int, cKey *C.char, cKeyLen C.size_t, cDstLen *C.size_t) *C.char { - key := charToByte(cKey, cKeyLen) + key := refCBytes(cKey, cKeyLen) dst := sliceTransforms.Get(idx).(sliceTransformWrapper).sliceTransform.Transform(key) *cDstLen = C.size_t(len(dst)) return cByteSlice(dst) @@ -72,14 +72,14 @@ func gorocksdb_slicetransform_transform(idx int, cKey *C.char, cKeyLen C.size_t, //export gorocksdb_slicetransform_in_domain func gorocksdb_slicetransform_in_domain(idx int, cKey *C.char, cKeyLen C.size_t) C.uchar { - key := charToByte(cKey, cKeyLen) + key := refCBytes(cKey, cKeyLen) inDomain := sliceTransforms.Get(idx).(sliceTransformWrapper).sliceTransform.InDomain(key) return boolToChar(inDomain) } //export gorocksdb_slicetransform_in_range func gorocksdb_slicetransform_in_range(idx int, cKey *C.char, cKeyLen C.size_t) C.uchar { - key := charToByte(cKey, cKeyLen) + key := refCBytes(cKey, cKeyLen) inRange := sliceTransforms.Get(idx).(sliceTransformWrapper).sliceTransform.InRange(key) return boolToChar(inRange) } diff --git a/sst_file_writer.go b/sst_file_writer.go index 7e492f7..96d6ecd 100644 --- a/sst_file_writer.go +++ b/sst_file_writer.go @@ -50,8 +50,8 @@ func (w *SSTFileWriter) Open(path string) (err error) { // Add adds key, value to currently opened file. // REQUIRES: key is after any previously added key according to comparator. func (w *SSTFileWriter) Add(key, value []byte) (err error) { - cKey := byteToChar(key) - cValue := byteToChar(value) + cKey := refGoBytes(key) + cValue := refGoBytes(value) var cErr *C.char C.rocksdb_sstfilewriter_add(w.c, cKey, C.size_t(len(key)), cValue, C.size_t(len(value)), &cErr) err = fromCError(cErr) @@ -60,8 +60,8 @@ func (w *SSTFileWriter) Add(key, value []byte) (err error) { // Put key, value to currently opened file. func (w *SSTFileWriter) Put(key, value []byte) (err error) { - cKey := byteToChar(key) - cValue := byteToChar(value) + cKey := refGoBytes(key) + cValue := refGoBytes(value) var cErr *C.char C.rocksdb_sstfilewriter_put(w.c, cKey, C.size_t(len(key)), cValue, C.size_t(len(value)), &cErr) err = fromCError(cErr) @@ -70,9 +70,9 @@ func (w *SSTFileWriter) Put(key, value []byte) (err error) { // Put key with timestamp, value to the currently opened file func (w *SSTFileWriter) PutWithTS(key, ts, value []byte) (err error) { - cKey := byteToChar(key) - cValue := byteToChar(value) - cTs := byteToChar(ts) + cKey := refGoBytes(key) + cValue := refGoBytes(value) + cTs := refGoBytes(ts) var cErr *C.char C.rocksdb_sstfilewriter_put_with_ts(w.c, cKey, C.size_t(len(key)), cTs, C.size_t(len(ts)), cValue, C.size_t(len(value)), &cErr) err = fromCError(cErr) @@ -81,8 +81,8 @@ func (w *SSTFileWriter) PutWithTS(key, ts, value []byte) (err error) { // Merge key, value to currently opened file. func (w *SSTFileWriter) Merge(key, value []byte) (err error) { - cKey := byteToChar(key) - cValue := byteToChar(value) + cKey := refGoBytes(key) + cValue := refGoBytes(value) var cErr *C.char C.rocksdb_sstfilewriter_merge(w.c, cKey, C.size_t(len(key)), cValue, C.size_t(len(value)), &cErr) err = fromCError(cErr) @@ -91,7 +91,7 @@ func (w *SSTFileWriter) Merge(key, value []byte) (err error) { // Delete key from currently opened file. func (w *SSTFileWriter) Delete(key []byte) (err error) { - cKey := byteToChar(key) + cKey := refGoBytes(key) var cErr *C.char C.rocksdb_sstfilewriter_delete(w.c, cKey, C.size_t(len(key)), &cErr) err = fromCError(cErr) @@ -100,8 +100,8 @@ func (w *SSTFileWriter) Delete(key []byte) (err error) { // DeleteWithTS deletes key with timestamp to the currently opened file func (w *SSTFileWriter) DeleteWithTS(key, ts []byte) (err error) { - cKey := byteToChar(key) - cTs := byteToChar(ts) + cKey := refGoBytes(key) + cTs := refGoBytes(ts) var cErr *C.char C.rocksdb_sstfilewriter_delete_with_ts(w.c, cKey, C.size_t(len(key)), cTs, C.size_t(len(ts)), &cErr) err = fromCError(cErr) @@ -110,8 +110,8 @@ func (w *SSTFileWriter) DeleteWithTS(key, ts []byte) (err error) { // DeleteRange deletes keys that are between [startKey, endKey) func (w *SSTFileWriter) DeleteRange(startKey, endKey []byte) (err error) { - cStartKey := byteToChar(startKey) - cEndKey := byteToChar(endKey) + cStartKey := refGoBytes(startKey) + cEndKey := refGoBytes(endKey) var cErr *C.char C.rocksdb_sstfilewriter_delete_range(w.c, cStartKey, C.size_t(len(startKey)), cEndKey, C.size_t(len(endKey)), &cErr) err = fromCError(cErr) diff --git a/transaction.go b/transaction.go index 936eb43..f08e568 100644 --- a/transaction.go +++ b/transaction.go @@ -22,7 +22,7 @@ func newNativeTransaction(c *C.rocksdb_transaction_t) *Transaction { func (transaction *Transaction) SetName(name string) (err error) { var ( cErr *C.char - name_ = byteToChar([]byte(name)) + name_ = refGoBytes([]byte(name)) ) C.rocksdb_transaction_set_name(transaction.c, name_, C.size_t(len(name)), &cErr) @@ -67,7 +67,7 @@ func (transaction *Transaction) Get(opts *ReadOptions, key []byte) (slice *Slice var ( cErr *C.char cValLen C.size_t - cKey = byteToChar(key) + cKey = refGoBytes(key) ) cValue := C.rocksdb_transaction_get( @@ -84,7 +84,7 @@ func (transaction *Transaction) Get(opts *ReadOptions, key []byte) (slice *Slice func (transaction *Transaction) GetPinned(opts *ReadOptions, key []byte) (handle *PinnableSliceHandle, err error) { var ( cErr *C.char - cKey = byteToChar(key) + cKey = refGoBytes(key) ) cHandle := C.rocksdb_transaction_get_pinned(transaction.c, opts.c, cKey, C.size_t(len(key)), &cErr) @@ -100,7 +100,7 @@ func (transaction *Transaction) GetWithCF(opts *ReadOptions, cf *ColumnFamilyHan var ( cErr *C.char cValLen C.size_t - cKey = byteToChar(key) + cKey = refGoBytes(key) ) cValue := C.rocksdb_transaction_get_cf( @@ -117,7 +117,7 @@ func (transaction *Transaction) GetWithCF(opts *ReadOptions, cf *ColumnFamilyHan func (transaction *Transaction) GetPinnedWithCF(opts *ReadOptions, cf *ColumnFamilyHandle, key []byte) (handle *PinnableSliceHandle, err error) { var ( cErr *C.char - cKey = byteToChar(key) + cKey = refGoBytes(key) ) cHandle := C.rocksdb_transaction_get_pinned_cf(transaction.c, opts.c, cf.c, cKey, C.size_t(len(key)), &cErr) @@ -134,7 +134,7 @@ func (transaction *Transaction) GetForUpdate(opts *ReadOptions, key []byte) (sli var ( cErr *C.char cValLen C.size_t - cKey = byteToChar(key) + cKey = refGoBytes(key) ) cValue := C.rocksdb_transaction_get_for_update( @@ -152,7 +152,7 @@ func (transaction *Transaction) GetForUpdate(opts *ReadOptions, key []byte) (sli func (transaction *Transaction) GetPinnedForUpdate(opts *ReadOptions, key []byte) (handle *PinnableSliceHandle, err error) { var ( cErr *C.char - cKey = byteToChar(key) + cKey = refGoBytes(key) ) cHandle := C.rocksdb_transaction_get_pinned_for_update( @@ -172,7 +172,7 @@ func (transaction *Transaction) GetForUpdateWithCF(opts *ReadOptions, cf *Column var ( cErr *C.char cValLen C.size_t - cKey = byteToChar(key) + cKey = refGoBytes(key) ) cValue := C.rocksdb_transaction_get_for_update_cf( @@ -190,7 +190,7 @@ func (transaction *Transaction) GetForUpdateWithCF(opts *ReadOptions, cf *Column func (transaction *Transaction) GetPinnedForUpdateWithCF(opts *ReadOptions, cf *ColumnFamilyHandle, key []byte) (handle *PinnableSliceHandle, err error) { var ( cErr *C.char - cKey = byteToChar(key) + cKey = refGoBytes(key) ) cHandle := C.rocksdb_transaction_get_pinned_for_update_cf( @@ -293,8 +293,8 @@ func (transaction *Transaction) MultiGetWithCF(opts *ReadOptions, cf *ColumnFami func (transaction *Transaction) Put(key, value []byte) (err error) { var ( cErr *C.char - cKey = byteToChar(key) - cValue = byteToChar(value) + cKey = refGoBytes(key) + cValue = refGoBytes(value) ) C.rocksdb_transaction_put( @@ -309,8 +309,8 @@ func (transaction *Transaction) Put(key, value []byte) (err error) { func (transaction *Transaction) PutCF(cf *ColumnFamilyHandle, key, value []byte) (err error) { var ( cErr *C.char - cKey = byteToChar(key) - cValue = byteToChar(value) + cKey = refGoBytes(key) + cValue = refGoBytes(value) ) C.rocksdb_transaction_put_cf( @@ -325,8 +325,8 @@ func (transaction *Transaction) PutCF(cf *ColumnFamilyHandle, key, value []byte) func (transaction *Transaction) Merge(key, value []byte) (err error) { var ( cErr *C.char - cKey = byteToChar(key) - cValue = byteToChar(value) + cKey = refGoBytes(key) + cValue = refGoBytes(value) ) C.rocksdb_transaction_merge( @@ -341,8 +341,8 @@ func (transaction *Transaction) Merge(key, value []byte) (err error) { func (transaction *Transaction) MergeCF(cf *ColumnFamilyHandle, key, value []byte) (err error) { var ( cErr *C.char - cKey = byteToChar(key) - cValue = byteToChar(value) + cKey = refGoBytes(key) + cValue = refGoBytes(value) ) C.rocksdb_transaction_merge_cf( @@ -357,7 +357,7 @@ func (transaction *Transaction) MergeCF(cf *ColumnFamilyHandle, key, value []byt func (transaction *Transaction) Delete(key []byte) (err error) { var ( cErr *C.char - cKey = byteToChar(key) + cKey = refGoBytes(key) ) C.rocksdb_transaction_delete(transaction.c, cKey, C.size_t(len(key)), &cErr) @@ -370,7 +370,7 @@ func (transaction *Transaction) Delete(key []byte) (err error) { func (transaction *Transaction) DeleteCF(cf *ColumnFamilyHandle, key []byte) (err error) { var ( cErr *C.char - cKey = byteToChar(key) + cKey = refGoBytes(key) ) C.rocksdb_transaction_delete_cf(transaction.c, cf.c, cKey, C.size_t(len(key)), &cErr) diff --git a/transactiondb.go b/transactiondb.go index ef6cace..cdfe73e 100644 --- a/transactiondb.go +++ b/transactiondb.go @@ -172,7 +172,7 @@ func (db *TransactionDB) Get(opts *ReadOptions, key []byte) (slice *Slice, err e var ( cErr *C.char cValLen C.size_t - cKey = byteToChar(key) + cKey = refGoBytes(key) ) cValue := C.rocksdb_transactiondb_get( @@ -189,7 +189,7 @@ func (db *TransactionDB) Get(opts *ReadOptions, key []byte) (slice *Slice, err e func (db *TransactionDB) GetPinned(opts *ReadOptions, key []byte) (handle *PinnableSliceHandle, err error) { var ( cErr *C.char - cKey = byteToChar(key) + cKey = refGoBytes(key) ) cHandle := C.rocksdb_transactiondb_get_pinned(db.c, opts.c, cKey, C.size_t(len(key)), &cErr) @@ -205,7 +205,7 @@ func (db *TransactionDB) GetCF(opts *ReadOptions, cf *ColumnFamilyHandle, key [] var ( cErr *C.char cValLen C.size_t - cKey = byteToChar(key) + cKey = refGoBytes(key) ) cValue := C.rocksdb_transactiondb_get_cf( @@ -222,7 +222,7 @@ func (db *TransactionDB) GetCF(opts *ReadOptions, cf *ColumnFamilyHandle, key [] func (db *TransactionDB) GetPinnedWithCF(opts *ReadOptions, cf *ColumnFamilyHandle, key []byte) (handle *PinnableSliceHandle, err error) { var ( cErr *C.char - cKey = byteToChar(key) + cKey = refGoBytes(key) ) cHandle := C.rocksdb_transactiondb_get_pinned_cf(db.c, opts.c, cf.c, cKey, C.size_t(len(key)), &cErr) @@ -322,8 +322,8 @@ func (db *TransactionDB) MultiGetWithCF(opts *ReadOptions, cf *ColumnFamilyHandl func (db *TransactionDB) Put(opts *WriteOptions, key, value []byte) (err error) { var ( cErr *C.char - cKey = byteToChar(key) - cValue = byteToChar(value) + cKey = refGoBytes(key) + cValue = refGoBytes(value) ) C.rocksdb_transactiondb_put( @@ -338,8 +338,8 @@ func (db *TransactionDB) Put(opts *WriteOptions, key, value []byte) (err error) func (db *TransactionDB) PutCF(opts *WriteOptions, cf *ColumnFamilyHandle, key, value []byte) (err error) { var ( cErr *C.char - cKey = byteToChar(key) - cValue = byteToChar(value) + cKey = refGoBytes(key) + cValue = refGoBytes(value) ) C.rocksdb_transactiondb_put_cf( @@ -354,8 +354,8 @@ func (db *TransactionDB) PutCF(opts *WriteOptions, cf *ColumnFamilyHandle, key, func (db *TransactionDB) Merge(opts *WriteOptions, key, value []byte) (err error) { var ( cErr *C.char - cKey = byteToChar(key) - cValue = byteToChar(value) + cKey = refGoBytes(key) + cValue = refGoBytes(value) ) C.rocksdb_transactiondb_merge( @@ -370,8 +370,8 @@ func (db *TransactionDB) Merge(opts *WriteOptions, key, value []byte) (err error func (db *TransactionDB) MergeCF(opts *WriteOptions, cf *ColumnFamilyHandle, key, value []byte) (err error) { var ( cErr *C.char - cKey = byteToChar(key) - cValue = byteToChar(value) + cKey = refGoBytes(key) + cValue = refGoBytes(value) ) C.rocksdb_transactiondb_merge_cf( @@ -386,7 +386,7 @@ func (db *TransactionDB) MergeCF(opts *WriteOptions, cf *ColumnFamilyHandle, key func (db *TransactionDB) Delete(opts *WriteOptions, key []byte) (err error) { var ( cErr *C.char - cKey = byteToChar(key) + cKey = refGoBytes(key) ) C.rocksdb_transactiondb_delete(db.c, opts.c, cKey, C.size_t(len(key)), &cErr) @@ -399,7 +399,7 @@ func (db *TransactionDB) Delete(opts *WriteOptions, key []byte) (err error) { func (db *TransactionDB) DeleteCF(opts *WriteOptions, cf *ColumnFamilyHandle, key []byte) (err error) { var ( cErr *C.char - cKey = byteToChar(key) + cKey = refGoBytes(key) ) C.rocksdb_transactiondb_delete_cf(db.c, opts.c, cf.c, cKey, C.size_t(len(key)), &cErr) diff --git a/util.go b/util.go index a61d4ac..b951aa8 100644 --- a/util.go +++ b/util.go @@ -3,6 +3,7 @@ package grocksdb // #include // #include "rocksdb/c.h" import "C" + import ( "errors" "unsafe" @@ -21,13 +22,13 @@ func charToBool(c C.uchar) bool { return c != 0 } -// charToByte converts a *C.char to a byte slice. -func charToByte(data *C.char, len C.size_t) []byte { +// refCBytes referencing *C.char. +func refCBytes(data *C.char, len C.size_t) []byte { return unsafe.Slice((*byte)(unsafe.Pointer(data)), int(len)) } -// byteToChar returns *C.char from byte slice. -func byteToChar(b []byte) *C.char { +// refGoBytes returns *C.char from byte slice. +func refGoBytes(b []byte) *C.char { var c *C.char if len(b) > 0 { c = (*C.char)(unsafe.Pointer(&b[0])) diff --git a/write_batch.go b/write_batch.go index 84a287e..7988628 100644 --- a/write_batch.go +++ b/write_batch.go @@ -27,56 +27,56 @@ func newNativeWriteBatch(c *C.rocksdb_writebatch_t) *WriteBatch { // WriteBatchFrom creates a write batch from a serialized WriteBatch. func WriteBatchFrom(data []byte) *WriteBatch { - cWB := C.rocksdb_writebatch_create_from(byteToChar(data), C.size_t(len(data))) + cWB := C.rocksdb_writebatch_create_from(refGoBytes(data), C.size_t(len(data))) return newNativeWriteBatch(cWB) } // Put queues a key-value pair. func (wb *WriteBatch) Put(key, value []byte) { - cKey := byteToChar(key) - cValue := byteToChar(value) + cKey := refGoBytes(key) + cValue := refGoBytes(value) C.rocksdb_writebatch_put(wb.c, cKey, C.size_t(len(key)), cValue, C.size_t(len(value))) } // PutCF queues a key-value pair in a column family. func (wb *WriteBatch) PutCF(cf *ColumnFamilyHandle, key, value []byte) { - cKey := byteToChar(key) - cValue := byteToChar(value) + cKey := refGoBytes(key) + cValue := refGoBytes(value) C.rocksdb_writebatch_put_cf(wb.c, cf.c, cKey, C.size_t(len(key)), cValue, C.size_t(len(value))) } // PutCFWithTS queues a key-value pair with given timestamp in a column family. func (wb *WriteBatch) PutCFWithTS(cf *ColumnFamilyHandle, key, ts, value []byte) { - cKey := byteToChar(key) - cValue := byteToChar(value) - cTs := byteToChar(ts) + cKey := refGoBytes(key) + cValue := refGoBytes(value) + cTs := refGoBytes(ts) C.rocksdb_writebatch_put_cf_with_ts(wb.c, cf.c, cKey, C.size_t(len(key)), cTs, C.size_t(len(ts)), cValue, C.size_t(len(value))) } // PutLogData appends a blob of arbitrary size to the records in this batch. func (wb *WriteBatch) PutLogData(blob []byte) { - cBlob := byteToChar(blob) + cBlob := refGoBytes(blob) C.rocksdb_writebatch_put_log_data(wb.c, cBlob, C.size_t(len(blob))) } // Merge queues a merge of "value" with the existing value of "key". func (wb *WriteBatch) Merge(key, value []byte) { - cKey := byteToChar(key) - cValue := byteToChar(value) + cKey := refGoBytes(key) + cValue := refGoBytes(value) C.rocksdb_writebatch_merge(wb.c, cKey, C.size_t(len(key)), cValue, C.size_t(len(value))) } // MergeCF queues a merge of "value" with the existing value of "key" in a // column family. func (wb *WriteBatch) MergeCF(cf *ColumnFamilyHandle, key, value []byte) { - cKey := byteToChar(key) - cValue := byteToChar(value) + cKey := refGoBytes(key) + cValue := refGoBytes(value) C.rocksdb_writebatch_merge_cf(wb.c, cf.c, cKey, C.size_t(len(key)), cValue, C.size_t(len(value))) } // Delete queues a deletion of the data at key. func (wb *WriteBatch) Delete(key []byte) { - cKey := byteToChar(key) + cKey := refGoBytes(key) C.rocksdb_writebatch_delete(wb.c, cKey, C.size_t(len(key))) } @@ -97,48 +97,48 @@ func (wb *WriteBatch) Delete(key []byte) { // // Note: consider setting options.sync = true. func (wb *WriteBatch) SingleDelete(key []byte) { - cKey := byteToChar(key) + cKey := refGoBytes(key) C.rocksdb_writebatch_singledelete(wb.c, cKey, C.size_t(len(key))) } // DeleteCF queues a deletion of the data at key in a column family. func (wb *WriteBatch) DeleteCF(cf *ColumnFamilyHandle, key []byte) { - cKey := byteToChar(key) + cKey := refGoBytes(key) C.rocksdb_writebatch_delete_cf(wb.c, cf.c, cKey, C.size_t(len(key))) } // DeleteCF queues a deletion of the data at key with given timestamp in a column family. func (wb *WriteBatch) DeleteCFWithTS(cf *ColumnFamilyHandle, key, ts []byte) { - cKey := byteToChar(key) - cTs := byteToChar(ts) + cKey := refGoBytes(key) + cTs := refGoBytes(ts) C.rocksdb_writebatch_delete_cf_with_ts(wb.c, cf.c, cKey, C.size_t(len(key)), cTs, C.size_t(len(ts))) } // SingleDeleteCF same as SingleDelete but specific column family func (wb *WriteBatch) SingleDeleteCF(cf *ColumnFamilyHandle, key []byte) { - cKey := byteToChar(key) + cKey := refGoBytes(key) C.rocksdb_writebatch_singledelete_cf(wb.c, cf.c, cKey, C.size_t(len(key))) } // SingleDeleteCFWithTS same as SingleDelete but with timestamp for specific column family func (wb *WriteBatch) SingleDeleteCFWithTS(cf *ColumnFamilyHandle, key, ts []byte) { - cKey := byteToChar(key) - cTs := byteToChar(ts) + cKey := refGoBytes(key) + cTs := refGoBytes(ts) C.rocksdb_writebatch_singledelete_cf_with_ts(wb.c, cf.c, cKey, C.size_t(len(key)), cTs, C.size_t(len(ts))) } // DeleteRange deletes keys that are between [startKey, endKey) func (wb *WriteBatch) DeleteRange(startKey []byte, endKey []byte) { - cStartKey := byteToChar(startKey) - cEndKey := byteToChar(endKey) + cStartKey := refGoBytes(startKey) + cEndKey := refGoBytes(endKey) C.rocksdb_writebatch_delete_range(wb.c, cStartKey, C.size_t(len(startKey)), cEndKey, C.size_t(len(endKey))) } // DeleteRangeCF deletes keys that are between [startKey, endKey) and // belong to a given column family func (wb *WriteBatch) DeleteRangeCF(cf *ColumnFamilyHandle, startKey []byte, endKey []byte) { - cStartKey := byteToChar(startKey) - cEndKey := byteToChar(endKey) + cStartKey := refGoBytes(startKey) + cEndKey := refGoBytes(endKey) C.rocksdb_writebatch_delete_range_cf(wb.c, cf.c, cStartKey, C.size_t(len(startKey)), cEndKey, C.size_t(len(endKey))) } @@ -146,7 +146,7 @@ func (wb *WriteBatch) DeleteRangeCF(cf *ColumnFamilyHandle, startKey []byte, end func (wb *WriteBatch) Data() []byte { var cSize C.size_t cValue := C.rocksdb_writebatch_data(wb.c, &cSize) - return charToByte(cValue, cSize) + return refCBytes(cValue, cSize) } // Count returns the number of updates in the batch. diff --git a/write_batch_wi.go b/write_batch_wi.go index 832e365..5dbef6b 100644 --- a/write_batch_wi.go +++ b/write_batch_wi.go @@ -26,42 +26,42 @@ func newNativeWriteBatchWI(c *C.rocksdb_writebatch_wi_t) *WriteBatchWI { // Put queues a key-value pair. func (wb *WriteBatchWI) Put(key, value []byte) { - cKey := byteToChar(key) - cValue := byteToChar(value) + cKey := refGoBytes(key) + cValue := refGoBytes(value) C.rocksdb_writebatch_wi_put(wb.c, cKey, C.size_t(len(key)), cValue, C.size_t(len(value))) } // PutCF queues a key-value pair in a column family. func (wb *WriteBatchWI) PutCF(cf *ColumnFamilyHandle, key, value []byte) { - cKey := byteToChar(key) - cValue := byteToChar(value) + cKey := refGoBytes(key) + cValue := refGoBytes(value) C.rocksdb_writebatch_wi_put_cf(wb.c, cf.c, cKey, C.size_t(len(key)), cValue, C.size_t(len(value))) } // PutLogData appends a blob of arbitrary size to the records in this batch. func (wb *WriteBatchWI) PutLogData(blob []byte) { - cBlob := byteToChar(blob) + cBlob := refGoBytes(blob) C.rocksdb_writebatch_wi_put_log_data(wb.c, cBlob, C.size_t(len(blob))) } // Merge queues a merge of "value" with the existing value of "key". func (wb *WriteBatchWI) Merge(key, value []byte) { - cKey := byteToChar(key) - cValue := byteToChar(value) + cKey := refGoBytes(key) + cValue := refGoBytes(value) C.rocksdb_writebatch_wi_merge(wb.c, cKey, C.size_t(len(key)), cValue, C.size_t(len(value))) } // MergeCF queues a merge of "value" with the existing value of "key" in a // column family. func (wb *WriteBatchWI) MergeCF(cf *ColumnFamilyHandle, key, value []byte) { - cKey := byteToChar(key) - cValue := byteToChar(value) + cKey := refGoBytes(key) + cValue := refGoBytes(value) C.rocksdb_writebatch_wi_merge_cf(wb.c, cf.c, cKey, C.size_t(len(key)), cValue, C.size_t(len(value))) } // Delete queues a deletion of the data at key. func (wb *WriteBatchWI) Delete(key []byte) { - cKey := byteToChar(key) + cKey := refGoBytes(key) C.rocksdb_writebatch_wi_delete(wb.c, cKey, C.size_t(len(key))) } @@ -82,34 +82,34 @@ func (wb *WriteBatchWI) Delete(key []byte) { // // Note: consider setting options.sync = true. func (wb *WriteBatchWI) SingleDelete(key []byte) { - cKey := byteToChar(key) + cKey := refGoBytes(key) C.rocksdb_writebatch_wi_singledelete(wb.c, cKey, C.size_t(len(key))) } // DeleteCF queues a deletion of the data at key in a column family. func (wb *WriteBatchWI) DeleteCF(cf *ColumnFamilyHandle, key []byte) { - cKey := byteToChar(key) + cKey := refGoBytes(key) C.rocksdb_writebatch_wi_delete_cf(wb.c, cf.c, cKey, C.size_t(len(key))) } // SingleDeleteCF same as SingleDelete but specific column family func (wb *WriteBatchWI) SingleDeleteCF(cf *ColumnFamilyHandle, key []byte) { - cKey := byteToChar(key) + cKey := refGoBytes(key) C.rocksdb_writebatch_wi_singledelete_cf(wb.c, cf.c, cKey, C.size_t(len(key))) } // DeleteRange deletes keys that are between [startKey, endKey) func (wb *WriteBatchWI) DeleteRange(startKey []byte, endKey []byte) { - cStartKey := byteToChar(startKey) - cEndKey := byteToChar(endKey) + cStartKey := refGoBytes(startKey) + cEndKey := refGoBytes(endKey) C.rocksdb_writebatch_wi_delete_range(wb.c, cStartKey, C.size_t(len(startKey)), cEndKey, C.size_t(len(endKey))) } // DeleteRangeCF deletes keys that are between [startKey, endKey) and // belong to a given column family func (wb *WriteBatchWI) DeleteRangeCF(cf *ColumnFamilyHandle, startKey []byte, endKey []byte) { - cStartKey := byteToChar(startKey) - cEndKey := byteToChar(endKey) + cStartKey := refGoBytes(startKey) + cEndKey := refGoBytes(endKey) C.rocksdb_writebatch_wi_delete_range_cf(wb.c, cf.c, cStartKey, C.size_t(len(startKey)), cEndKey, C.size_t(len(endKey))) } @@ -117,7 +117,7 @@ func (wb *WriteBatchWI) DeleteRangeCF(cf *ColumnFamilyHandle, startKey []byte, e func (wb *WriteBatchWI) Data() []byte { var cSize C.size_t cValue := C.rocksdb_writebatch_wi_data(wb.c, &cSize) - return charToByte(cValue, cSize) + return refCBytes(cValue, cSize) } // Count returns the number of updates in the batch. @@ -154,7 +154,7 @@ func (wb *WriteBatchWI) Get(opts *Options, key []byte) (slice *Slice, err error) var ( cErr *C.char cValLen C.size_t - cKey = byteToChar(key) + cKey = refGoBytes(key) ) cValue := C.rocksdb_writebatch_wi_get_from_batch(wb.c, opts.c, cKey, C.size_t(len(key)), &cValLen, &cErr) @@ -171,7 +171,7 @@ func (wb *WriteBatchWI) GetWithCF(opts *Options, cf *ColumnFamilyHandle, key []b var ( cErr *C.char cValLen C.size_t - cKey = byteToChar(key) + cKey = refGoBytes(key) ) cValue := C.rocksdb_writebatch_wi_get_from_batch_cf(wb.c, opts.c, cf.c, cKey, C.size_t(len(key)), &cValLen, &cErr) @@ -187,7 +187,7 @@ func (wb *WriteBatchWI) GetFromDB(db *DB, opts *ReadOptions, key []byte) (slice var ( cErr *C.char cValLen C.size_t - cKey = byteToChar(key) + cKey = refGoBytes(key) ) cValue := C.rocksdb_writebatch_wi_get_from_batch_and_db(wb.c, db.c, opts.c, cKey, C.size_t(len(key)), &cValLen, &cErr) @@ -204,7 +204,7 @@ func (wb *WriteBatchWI) GetFromDBWithCF(db *DB, opts *ReadOptions, cf *ColumnFam var ( cErr *C.char cValLen C.size_t - cKey = byteToChar(key) + cKey = refGoBytes(key) ) cValue := C.rocksdb_writebatch_wi_get_from_batch_and_db_cf(wb.c, db.c, opts.c, cf.c, cKey, C.size_t(len(key)), &cValLen, &cErr)