Skip to content

Commit

Permalink
Change NewNative funcs to accept unsafe.Pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
muthukrishnan24 committed Sep 21, 2022
1 parent 2aa505c commit 4b89983
Show file tree
Hide file tree
Showing 31 changed files with 129 additions and 111 deletions.
10 changes: 6 additions & 4 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@ type Cache struct {

// NewLRUCache creates a new LRU Cache object with the capacity given.
func NewLRUCache(capacity uint64) *Cache {
return NewNativeCache(C.rocksdb_cache_create_lru(C.size_t(capacity)))
cCache := C.rocksdb_cache_create_lru(C.size_t(capacity))
return newNativeCache(cCache)
}

// NewLRUCacheWithOptions creates a new LRU Cache from options.
func NewLRUCacheWithOptions(opt *LRUCacheOptions) *Cache {
return NewNativeCache(C.rocksdb_cache_create_lru_opts(opt.c))
cCache := C.rocksdb_cache_create_lru_opts(opt.c)
return newNativeCache(cCache)
}

// NewNativeCache creates a Cache object.
func NewNativeCache(c *C.rocksdb_cache_t) *Cache {
return &Cache{c}
func newNativeCache(c *C.rocksdb_cache_t) *Cache {
return &Cache{c: c}
}

// GetUsage returns the Cache memory usage.
Expand Down
4 changes: 2 additions & 2 deletions cf_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ type ColumnFamilyHandle struct {
}

// NewNativeColumnFamilyHandle creates a ColumnFamilyHandle object.
func NewNativeColumnFamilyHandle(c *C.rocksdb_column_family_handle_t) *ColumnFamilyHandle {
return &ColumnFamilyHandle{c}
func newNativeColumnFamilyHandle(c *C.rocksdb_column_family_handle_t) *ColumnFamilyHandle {
return &ColumnFamilyHandle{c: c}
}

// Destroy calls the destructor of the underlying column family handle.
Expand Down
4 changes: 2 additions & 2 deletions checkpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ type Checkpoint struct {
}

// NewNativeCheckpoint creates a new checkpoint.
func NewNativeCheckpoint(c *C.rocksdb_checkpoint_t) *Checkpoint {
return &Checkpoint{c}
func newNativeCheckpoint(c *C.rocksdb_checkpoint_t) *Checkpoint {
return &Checkpoint{c: c}
}

// CreateCheckpoint builds an openable snapshot of RocksDB on the same disk, which
Expand Down
5 changes: 3 additions & 2 deletions compaction_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package grocksdb

// #include "rocksdb/c.h"
import "C"
import "unsafe"

// A CompactionFilter can be used to filter keys during compaction time.
type CompactionFilter interface {
Expand Down Expand Up @@ -39,8 +40,8 @@ type CompactionFilter interface {
}

// NewNativeCompactionFilter creates a CompactionFilter object.
func NewNativeCompactionFilter(c *C.rocksdb_compactionfilter_t) CompactionFilter {
return &nativeCompactionFilter{c}
func NewNativeCompactionFilter(c unsafe.Pointer) CompactionFilter {
return &nativeCompactionFilter{c: (*C.rocksdb_compactionfilter_t)(c)}
}

type nativeCompactionFilter struct {
Expand Down
22 changes: 11 additions & 11 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func OpenDbColumnFamilies(
}
cfHandles = make([]*ColumnFamilyHandle, numColumnFamilies)
for i, c := range cHandles {
cfHandles[i] = NewNativeColumnFamilyHandle(c)
cfHandles[i] = newNativeColumnFamilyHandle(c)
}
}

Expand Down Expand Up @@ -234,7 +234,7 @@ func OpenDbColumnFamiliesWithTTL(
}
cfHandles = make([]*ColumnFamilyHandle, numColumnFamilies)
for i, c := range cHandles {
cfHandles[i] = NewNativeColumnFamilyHandle(c)
cfHandles[i] = newNativeColumnFamilyHandle(c)
}
}

Expand Down Expand Up @@ -292,7 +292,7 @@ func OpenDbForReadOnlyColumnFamilies(
}
cfHandles = make([]*ColumnFamilyHandle, numColumnFamilies)
for i, c := range cHandles {
cfHandles[i] = NewNativeColumnFamilyHandle(c)
cfHandles[i] = newNativeColumnFamilyHandle(c)
}
}

Expand Down Expand Up @@ -360,7 +360,7 @@ func OpenDbAsSecondaryColumnFamilies(
}
cfHandles = make([]*ColumnFamilyHandle, numColumnFamilies)
for i, c := range cHandles {
cfHandles[i] = NewNativeColumnFamilyHandle(c)
cfHandles[i] = newNativeColumnFamilyHandle(c)
}
}

Expand Down Expand Up @@ -425,7 +425,7 @@ func OpenDbAndTrimHistory(opts *Options,
}
cfHandles = make([]*ColumnFamilyHandle, numColumnFamilies)
for i, c := range cHandles {
cfHandles[i] = NewNativeColumnFamilyHandle(c)
cfHandles[i] = newNativeColumnFamilyHandle(c)
}
}

Expand Down Expand Up @@ -646,7 +646,7 @@ func (db *DB) GetPinned(opts *ReadOptions, key []byte) (handle *PinnableSliceHan

cHandle := C.rocksdb_get_pinned(db.c, opts.c, cKey, C.size_t(len(key)), &cErr)
if err = fromCError(cErr); err == nil {
handle = NewNativePinnableSliceHandle(cHandle)
handle = newNativePinnableSliceHandle(cHandle)
}

return
Expand All @@ -661,7 +661,7 @@ func (db *DB) GetPinnedCF(opts *ReadOptions, cf *ColumnFamilyHandle, key []byte)

cHandle := C.rocksdb_get_pinned_cf(db.c, opts.c, cf.c, cKey, C.size_t(len(key)), &cErr)
if err = fromCError(cErr); err == nil {
handle = NewNativePinnableSliceHandle(cHandle)
handle = newNativePinnableSliceHandle(cHandle)
}

return
Expand Down Expand Up @@ -1202,7 +1202,7 @@ func (db *DB) GetLatestSequenceNumber() uint64 {
// NewSnapshot creates a new snapshot of the database.
func (db *DB) NewSnapshot() *Snapshot {
cSnap := C.rocksdb_create_snapshot(db.c)
return NewNativeSnapshot(cSnap)
return newNativeSnapshot(cSnap)
}

// ReleaseSnapshot releases the snapshot and its resources.
Expand Down Expand Up @@ -1262,7 +1262,7 @@ func (db *DB) CreateColumnFamily(opts *Options, name string) (handle *ColumnFami

cHandle := C.rocksdb_create_column_family(db.c, opts.c, cName, &cErr)
if err = fromCError(cErr); err == nil {
handle = NewNativeColumnFamilyHandle(cHandle)
handle = newNativeColumnFamilyHandle(cHandle)
}

C.free(unsafe.Pointer(cName))
Expand Down Expand Up @@ -1293,7 +1293,7 @@ func (db *DB) CreateColumnFamilyWithTTL(opts *Options, name string, ttl int) (ha

cHandle := C.rocksdb_create_column_family_with_ttl(db.c, opts.c, cName, C.int(ttl), &cErr)
if err = fromCError(cErr); err == nil {
handle = NewNativeColumnFamilyHandle(cHandle)
handle = newNativeColumnFamilyHandle(cHandle)
}

C.free(unsafe.Pointer(cName))
Expand Down Expand Up @@ -1750,7 +1750,7 @@ func (db *DB) NewCheckpoint() (cp *Checkpoint, err error) {
db.c, &cErr,
)
if err = fromCError(cErr); err == nil {
cp = NewNativeCheckpoint(cCheckpoint)
cp = newNativeCheckpoint(cCheckpoint)
}
return
}
Expand Down
7 changes: 4 additions & 3 deletions dbpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ type DBPath struct {
// with the given path and target_size.
func NewDBPath(path string, targetSize uint64) (dbPath *DBPath) {
cpath := C.CString(path)
dbPath = NewNativeDBPath(C.rocksdb_dbpath_create(cpath, C.uint64_t(targetSize)))
cDBPath := C.rocksdb_dbpath_create(cpath, C.uint64_t(targetSize))
dbPath = newNativeDBPath(cDBPath)
C.free(unsafe.Pointer(cpath))
return
}

// NewNativeDBPath creates a DBPath object.
func NewNativeDBPath(c *C.rocksdb_dbpath_t) *DBPath {
return &DBPath{c}
func newNativeDBPath(c *C.rocksdb_dbpath_t) *DBPath {
return &DBPath{c: c}
}

// Destroy deallocates the DBPath object.
Expand Down
8 changes: 4 additions & 4 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ type Env struct {

// NewDefaultEnv creates a default environment.
func NewDefaultEnv() *Env {
return NewNativeEnv(C.rocksdb_create_default_env())
return newNativeEnv(C.rocksdb_create_default_env())
}

// NewMemEnv returns a new environment that stores its data in memory and delegates
// all non-file-storage tasks to base_env.
func NewMemEnv() *Env {
return NewNativeEnv(C.rocksdb_create_mem_env())
return newNativeEnv(C.rocksdb_create_mem_env())
}

// NewNativeEnv creates a Environment object.
func NewNativeEnv(c *C.rocksdb_env_t) *Env {
return &Env{c}
func newNativeEnv(c *C.rocksdb_env_t) *Env {
return &Env{c: c}
}

// SetBackgroundThreads sets the number of background worker threads
Expand Down
14 changes: 9 additions & 5 deletions filter_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (fp *NativeFilterPolicy) Destroy() {

// creates a FilterPolicy object.
func newNativeFilterPolicy(c *C.rocksdb_filterpolicy_t) *NativeFilterPolicy {
return &NativeFilterPolicy{c}
return &NativeFilterPolicy{c: c}
}

// NewBloomFilter returns a new filter policy that uses a bloom filter with approximately
Expand All @@ -30,7 +30,8 @@ func newNativeFilterPolicy(c *C.rocksdb_filterpolicy_t) *NativeFilterPolicy {
// FilterPolicy (like NewBloomFilterPolicy) that does not ignore
// trailing spaces in keys.
func NewBloomFilter(bitsPerKey float64) *NativeFilterPolicy {
return newNativeFilterPolicy(C.rocksdb_filterpolicy_create_bloom(C.double(bitsPerKey)))
cFilter := C.rocksdb_filterpolicy_create_bloom(C.double(bitsPerKey))
return newNativeFilterPolicy(cFilter)
}

// NewBloomFilterFull returns a new filter policy that uses a full bloom filter
Expand All @@ -45,7 +46,8 @@ func NewBloomFilter(bitsPerKey float64) *NativeFilterPolicy {
// FilterPolicy (like NewBloomFilterPolicy) that does not ignore
// trailing spaces in keys.
func NewBloomFilterFull(bitsPerKey float64) *NativeFilterPolicy {
return newNativeFilterPolicy(C.rocksdb_filterpolicy_create_bloom_full(C.double(bitsPerKey)))
cFilter := C.rocksdb_filterpolicy_create_bloom_full(C.double(bitsPerKey))
return newNativeFilterPolicy(cFilter)
}

// NewRibbonFilterPolicy creates a new Bloom alternative that saves about
Expand Down Expand Up @@ -76,7 +78,8 @@ func NewBloomFilterFull(bitsPerKey float64) *NativeFilterPolicy {
// Also consider using optimize_filters_for_memory to save filter
// memory.
func NewRibbonFilterPolicy(bloomEquivalentBitsPerKey float64) *NativeFilterPolicy {
return newNativeFilterPolicy(C.rocksdb_filterpolicy_create_ribbon(C.double(bloomEquivalentBitsPerKey)))
cFilter := C.rocksdb_filterpolicy_create_ribbon(C.double(bloomEquivalentBitsPerKey))
return newNativeFilterPolicy(cFilter)
}

// NewRibbonHybridFilterPolicy similar to Ribbon.
Expand All @@ -92,5 +95,6 @@ func NewRibbonFilterPolicy(bloomEquivalentBitsPerKey float64) *NativeFilterPolic
// bloom_before_level=-1 -> Always generate Ribbon filters (except in
// some extreme or exceptional cases).
func NewRibbonHybridFilterPolicy(bloomEquivalentBitsPerKey float64, bloomBeforeLevel int) *NativeFilterPolicy {
return newNativeFilterPolicy(C.rocksdb_filterpolicy_create_ribbon_hybrid(C.double(bloomEquivalentBitsPerKey), C.int(bloomBeforeLevel)))
cFilter := C.rocksdb_filterpolicy_create_ribbon_hybrid(C.double(bloomEquivalentBitsPerKey), C.int(bloomBeforeLevel))
return newNativeFilterPolicy(cFilter)
}
2 changes: 1 addition & 1 deletion iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type Iterator struct {

// NewNativeIterator creates a Iterator object.
func NewNativeIterator(c unsafe.Pointer) *Iterator {
return &Iterator{(*C.rocksdb_iterator_t)(c)}
return &Iterator{c: (*C.rocksdb_iterator_t)(c)}
}

// Valid returns false only when an Iterator has iterated past either the
Expand Down
6 changes: 4 additions & 2 deletions merge_operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package grocksdb

// #include "rocksdb/c.h"
import "C"
import "unsafe"

// A MergeOperator specifies the SEMANTICS of a merge, which only
// client knows. It could be numeric addition, list append, string
Expand Down Expand Up @@ -78,8 +79,8 @@ type MultiMerger interface {
}

// NewNativeMergeOperator creates a MergeOperator object.
func NewNativeMergeOperator(c *C.rocksdb_mergeoperator_t) MergeOperator {
return &nativeMergeOperator{c}
func NewNativeMergeOperator(c unsafe.Pointer) MergeOperator {
return &nativeMergeOperator{c: (*C.rocksdb_mergeoperator_t)(c)}
}

type nativeMergeOperator struct {
Expand All @@ -89,6 +90,7 @@ type nativeMergeOperator struct {
func (mo *nativeMergeOperator) FullMerge(key, existingValue []byte, operands [][]byte) ([]byte, bool) {
return nil, false
}

func (mo *nativeMergeOperator) PartialMerge(key, leftOperand, rightOperand []byte) ([]byte, bool) {
return nil, false
}
Expand Down
14 changes: 8 additions & 6 deletions optimistic_transaction_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package grocksdb
// #include <stdlib.h>
// #include "rocksdb/c.h"
import "C"

import (
"unsafe"
)
Expand Down Expand Up @@ -82,7 +83,7 @@ func OpenOptimisticTransactionDbColumnFamilies(
}
cfHandles = make([]*ColumnFamilyHandle, numColumnFamilies)
for i, c := range cHandles {
cfHandles[i] = NewNativeColumnFamilyHandle(c)
cfHandles[i] = newNativeColumnFamilyHandle(c)
}
}

Expand All @@ -101,16 +102,17 @@ func (db *OptimisticTransactionDB) TransactionBegin(
oldTransaction *Transaction,
) *Transaction {
if oldTransaction != nil {
return NewNativeTransaction(C.rocksdb_optimistictransaction_begin(
cTx := C.rocksdb_optimistictransaction_begin(
db.c,
opts.c,
transactionOpts.c,
oldTransaction.c,
))
)
return newNativeTransaction(cTx)
}

return NewNativeTransaction(C.rocksdb_optimistictransaction_begin(
db.c, opts.c, transactionOpts.c, nil))
cTx := C.rocksdb_optimistictransaction_begin(db.c, opts.c, transactionOpts.c, nil)
return newNativeTransaction(cTx)
}

// NewCheckpoint creates a new Checkpoint for this db.
Expand All @@ -121,7 +123,7 @@ func (db *OptimisticTransactionDB) NewCheckpoint() (cp *Checkpoint, err error) {
db.c, &cErr,
)
if err = fromCError(cErr); err == nil {
cp = NewNativeCheckpoint(cCheckpoint)
cp = newNativeCheckpoint(cCheckpoint)
}

return
Expand Down
4 changes: 2 additions & 2 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ type Options struct {

// NewDefaultOptions creates the default Options.
func NewDefaultOptions() *Options {
return NewNativeOptions(C.rocksdb_options_create())
return newNativeOptions(C.rocksdb_options_create())
}

// NewNativeOptions creates a Options object.
func NewNativeOptions(c *C.rocksdb_options_t) *Options {
func newNativeOptions(c *C.rocksdb_options_t) *Options {
return &Options{c: c}
}

Expand Down
4 changes: 2 additions & 2 deletions options_block_based_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ type BlockBasedTableOptions struct {

// NewDefaultBlockBasedTableOptions creates a default BlockBasedTableOptions object.
func NewDefaultBlockBasedTableOptions() *BlockBasedTableOptions {
return NewNativeBlockBasedTableOptions(C.rocksdb_block_based_options_create())
return newNativeBlockBasedTableOptions(C.rocksdb_block_based_options_create())
}

// NewNativeBlockBasedTableOptions creates a BlockBasedTableOptions object.
func NewNativeBlockBasedTableOptions(c *C.rocksdb_block_based_table_options_t) *BlockBasedTableOptions {
func newNativeBlockBasedTableOptions(c *C.rocksdb_block_based_table_options_t) *BlockBasedTableOptions {
return &BlockBasedTableOptions{c: c}
}

Expand Down
12 changes: 6 additions & 6 deletions options_compaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ type FIFOCompactionOptions struct {

// NewDefaultFIFOCompactionOptions creates a default FIFOCompactionOptions object.
func NewDefaultFIFOCompactionOptions() *FIFOCompactionOptions {
return NewNativeFIFOCompactionOptions(C.rocksdb_fifo_compaction_options_create())
return newNativeFIFOCompactionOptions(C.rocksdb_fifo_compaction_options_create())
}

// NewNativeFIFOCompactionOptions creates a native FIFOCompactionOptions object.
func NewNativeFIFOCompactionOptions(c *C.rocksdb_fifo_compaction_options_t) *FIFOCompactionOptions {
return &FIFOCompactionOptions{c}
func newNativeFIFOCompactionOptions(c *C.rocksdb_fifo_compaction_options_t) *FIFOCompactionOptions {
return &FIFOCompactionOptions{c: c}
}

// SetMaxTableFilesSize sets the max table file size.
Expand Down Expand Up @@ -156,13 +156,13 @@ type UniversalCompactionOptions struct {
// NewDefaultUniversalCompactionOptions creates a default UniversalCompactionOptions
// object.
func NewDefaultUniversalCompactionOptions() *UniversalCompactionOptions {
return NewNativeUniversalCompactionOptions(C.rocksdb_universal_compaction_options_create())
return newNativeUniversalCompactionOptions(C.rocksdb_universal_compaction_options_create())
}

// NewNativeUniversalCompactionOptions creates a UniversalCompactionOptions
// object.
func NewNativeUniversalCompactionOptions(c *C.rocksdb_universal_compaction_options_t) *UniversalCompactionOptions {
return &UniversalCompactionOptions{c}
func newNativeUniversalCompactionOptions(c *C.rocksdb_universal_compaction_options_t) *UniversalCompactionOptions {
return &UniversalCompactionOptions{c: c}
}

// SetSizeRatio sets the percentage flexibility while comparing file size.
Expand Down
Loading

0 comments on commit 4b89983

Please sign in to comment.