diff --git a/compaction_filter.go b/compaction_filter.go index 43e9617b..bda27e20 100644 --- a/compaction_filter.go +++ b/compaction_filter.go @@ -42,8 +42,13 @@ func (c nativeCompactionFilter) Name() string { return "" } // Hold references to compaction filters. var compactionFilters = NewCOWList() +type compactionFilterWrapper struct { + name *C.char + filter CompactionFilter +} + func registerCompactionFilter(filter CompactionFilter) int { - return compactionFilters.Append(filter) + return compactionFilters.Append(compactionFilterWrapper{C.CString(filter.Name()), filter}) } //export gorocksdb_compactionfilter_filter @@ -51,7 +56,7 @@ func gorocksdb_compactionfilter_filter(idx int, cLevel C.int, cKey *C.char, cKey key := charToByte(cKey, cKeyLen) val := charToByte(cVal, cValLen) - remove, newVal := compactionFilters.Get(idx).(CompactionFilter).Filter(int(cLevel), key, val) + remove, newVal := compactionFilters.Get(idx).(compactionFilterWrapper).filter.Filter(int(cLevel), key, val) if remove { return C.int(1) } else if newVal != nil { @@ -64,5 +69,5 @@ func gorocksdb_compactionfilter_filter(idx int, cLevel C.int, cKey *C.char, cKey //export gorocksdb_compactionfilter_name func gorocksdb_compactionfilter_name(idx int) *C.char { - return stringToChar(compactionFilters.Get(idx).(CompactionFilter).Name()) + return compactionFilters.Get(idx).(compactionFilterWrapper).name } diff --git a/comparator.go b/comparator.go index 9e30c1fb..242771e3 100644 --- a/comparator.go +++ b/comparator.go @@ -31,18 +31,23 @@ func (c nativeComparator) Name() string { return "" } // Hold references to comperators. var comperators = NewCOWList() +type comperatorWrapper struct { + name *C.char + comparator Comparator +} + func registerComperator(cmp Comparator) int { - return comperators.Append(cmp) + return comperators.Append(comperatorWrapper{C.CString(cmp.Name()), cmp}) } //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) - return C.int(comperators.Get(idx).(Comparator).Compare(keyA, keyB)) + return C.int(comperators.Get(idx).(comperatorWrapper).comparator.Compare(keyA, keyB)) } //export gorocksdb_comparator_name func gorocksdb_comparator_name(idx int) *C.char { - return stringToChar(comperators.Get(idx).(Comparator).Name()) + return comperators.Get(idx).(comperatorWrapper).name } diff --git a/filter_policy.go b/filter_policy.go index 71e74e89..ac57fd99 100644 --- a/filter_policy.go +++ b/filter_policy.go @@ -52,8 +52,13 @@ func NewBloomFilter(bitsPerKey int) FilterPolicy { // Hold references to filter policies. var filterPolicies = NewCOWList() +type filterPolicyWrapper struct { + name *C.char + filterPolicy FilterPolicy +} + func registerFilterPolicy(fp FilterPolicy) int { - return filterPolicies.Append(fp) + return filterPolicies.Append(filterPolicyWrapper{C.CString(fp.Name()), fp}) } //export gorocksdb_filterpolicy_create_filter @@ -65,7 +70,7 @@ func gorocksdb_filterpolicy_create_filter(idx int, cKeys **C.char, cKeysLen *C.s keys[i] = charToByte(rawKeys[i], len) } - dst := filterPolicies.Get(idx).(FilterPolicy).CreateFilter(keys) + dst := filterPolicies.Get(idx).(filterPolicyWrapper).filterPolicy.CreateFilter(keys) *cDstLen = C.size_t(len(dst)) return cByteSlice(dst) } @@ -74,10 +79,10 @@ func gorocksdb_filterpolicy_create_filter(idx int, cKeys **C.char, cKeysLen *C.s func gorocksdb_filterpolicy_key_may_match(idx int, cKey *C.char, cKeyLen C.size_t, cFilter *C.char, cFilterLen C.size_t) C.uchar { key := charToByte(cKey, cKeyLen) filter := charToByte(cFilter, cFilterLen) - return boolToChar(filterPolicies.Get(idx).(FilterPolicy).KeyMayMatch(key, filter)) + return boolToChar(filterPolicies.Get(idx).(filterPolicyWrapper).filterPolicy.KeyMayMatch(key, filter)) } //export gorocksdb_filterpolicy_name func gorocksdb_filterpolicy_name(idx int) *C.char { - return stringToChar(filterPolicies.Get(idx).(FilterPolicy).Name()) + return filterPolicies.Get(idx).(filterPolicyWrapper).name } diff --git a/merge_operator.go b/merge_operator.go index cbf66da5..33f83948 100644 --- a/merge_operator.go +++ b/merge_operator.go @@ -67,8 +67,13 @@ func (mo nativeMergeOperator) Name() string { return "" } // Hold references to merge operators. var mergeOperators = NewCOWList() +type mergeOperatorWrapper struct { + name *C.char + mergeOperator MergeOperator +} + func registerMergeOperator(merger MergeOperator) int { - return mergeOperators.Append(merger) + return mergeOperators.Append(mergeOperatorWrapper{C.CString(merger.Name()), merger}) } //export gorocksdb_mergeoperator_full_merge @@ -82,7 +87,7 @@ func gorocksdb_mergeoperator_full_merge(idx int, cKey *C.char, cKeyLen C.size_t, operands[i] = charToByte(rawOperands[i], len) } - newValue, success := mergeOperators.Get(idx).(MergeOperator).FullMerge(key, existingValue, operands) + newValue, success := mergeOperators.Get(idx).(mergeOperatorWrapper).mergeOperator.FullMerge(key, existingValue, operands) newValueLen := len(newValue) *cNewValueLen = C.size_t(newValueLen) @@ -104,7 +109,7 @@ func gorocksdb_mergeoperator_partial_merge_multi(idx int, cKey *C.char, cKeyLen var newValue []byte success := true - merger := mergeOperators.Get(idx).(MergeOperator) + merger := mergeOperators.Get(idx).(mergeOperatorWrapper).mergeOperator leftOperand := operands[0] for i := 1; i < int(cNumOperands); i++ { newValue, success = merger.PartialMerge(key, leftOperand, operands[i]) @@ -123,5 +128,5 @@ func gorocksdb_mergeoperator_partial_merge_multi(idx int, cKey *C.char, cKeyLen //export gorocksdb_mergeoperator_name func gorocksdb_mergeoperator_name(idx int) *C.char { - return stringToChar(mergeOperators.Get(idx).(MergeOperator).Name()) + return mergeOperators.Get(idx).(mergeOperatorWrapper).name } diff --git a/slice_transform.go b/slice_transform.go index 0505df2a..e66e4d84 100644 --- a/slice_transform.go +++ b/slice_transform.go @@ -40,14 +40,19 @@ func (st nativeSliceTransform) Name() string { return "" } // Hold references to slice transforms. var sliceTransforms = NewCOWList() +type sliceTransformWrapper struct { + name *C.char + sliceTransform SliceTransform +} + func registerSliceTransform(st SliceTransform) int { - return sliceTransforms.Append(st) + return sliceTransforms.Append(sliceTransformWrapper{C.CString(st.Name()), st}) } //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) - dst := sliceTransforms.Get(idx).(SliceTransform).Transform(key) + dst := sliceTransforms.Get(idx).(sliceTransformWrapper).sliceTransform.Transform(key) *cDstLen = C.size_t(len(dst)) return cByteSlice(dst) } @@ -55,18 +60,18 @@ 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) - inDomain := sliceTransforms.Get(idx).(SliceTransform).InDomain(key) + 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) - inRange := sliceTransforms.Get(idx).(SliceTransform).InRange(key) + inRange := sliceTransforms.Get(idx).(sliceTransformWrapper).sliceTransform.InRange(key) return boolToChar(inRange) } //export gorocksdb_slicetransform_name func gorocksdb_slicetransform_name(idx int) *C.char { - return stringToChar(sliceTransforms.Get(idx).(SliceTransform).Name()) + return sliceTransforms.Get(idx).(sliceTransformWrapper).name } diff --git a/util.go b/util.go index 9c373306..236f3fea 100644 --- a/util.go +++ b/util.go @@ -51,12 +51,6 @@ func cByteSlice(b []byte) *C.char { return c } -// stringToChar returns *C.char from string. -func stringToChar(s string) *C.char { - ptrStr := (*reflect.StringHeader)(unsafe.Pointer(&s)) - return (*C.char)(unsafe.Pointer(ptrStr.Data)) -} - // charSlice converts a C array of *char to a []*C.char. func charSlice(data **C.char, len C.int) []*C.char { var value []*C.char