github.com/grupokindynos/coins-explorer@v0.0.0-20210507172551-fa8983d19250/db/dboptions.go (about)

     1  package db
     2  
     3  // #include "rocksdb/c.h"
     4  import "C"
     5  
     6  import (
     7  	"reflect"
     8  	"unsafe"
     9  
    10  	"github.com/tecbot/gorocksdb"
    11  )
    12  
    13  /*
    14  	possible additional tuning, using options not accessible by gorocksdb
    15  
    16  // #include "rocksdb/c.h"
    17  import "C"
    18  
    19  	cNativeOpts := C.rocksdb_options_create()
    20  	opts := &gorocksdb.Options{}
    21  	cField := reflect.Indirect(reflect.ValueOf(opts)).FieldByName("c")
    22  	cPtr := (**C.rocksdb_options_t)(unsafe.Pointer(cField.UnsafeAddr()))
    23  	*cPtr = cNativeOpts
    24  
    25  	cNativeBlockOpts := C.rocksdb_block_based_options_create()
    26  	blockOpts := &gorocksdb.BlockBasedTableOptions{}
    27  	cBlockField := reflect.Indirect(reflect.ValueOf(blockOpts)).FieldByName("c")
    28  	cBlockPtr := (**C.rocksdb_block_based_table_options_t)(unsafe.Pointer(cBlockField.UnsafeAddr()))
    29  	*cBlockPtr = cNativeBlockOpts
    30  
    31  	// https://github.com/facebook/rocksdb/wiki/Partitioned-Index-Filters
    32  	blockOpts.SetIndexType(gorocksdb.KTwoLevelIndexSearchIndexType)
    33  	C.rocksdb_block_based_options_set_partition_filters(cNativeBlockOpts, boolToChar(true))
    34  	C.rocksdb_block_based_options_set_metadata_block_size(cNativeBlockOpts, C.uint64_t(4096))
    35  	C.rocksdb_block_based_options_set_cache_index_and_filter_blocks_with_high_priority(cNativeBlockOpts, boolToChar(true))
    36  	blockOpts.SetPinL0FilterAndIndexBlocksInCache(true)
    37  
    38  // boolToChar converts a bool value to C.uchar.
    39  func boolToChar(b bool) C.uchar {
    40  	if b {
    41  		return 1
    42  	}
    43  	return 0
    44  }
    45  */
    46  
    47  func createAndSetDBOptions(bloomBits int, c *gorocksdb.Cache, maxOpenFiles int) *gorocksdb.Options {
    48  	// blockOpts := gorocksdb.NewDefaultBlockBasedTableOptions()
    49  	cNativeBlockOpts := C.rocksdb_block_based_options_create()
    50  	blockOpts := &gorocksdb.BlockBasedTableOptions{}
    51  	cBlockField := reflect.Indirect(reflect.ValueOf(blockOpts)).FieldByName("c")
    52  	cBlockPtr := (**C.rocksdb_block_based_table_options_t)(unsafe.Pointer(cBlockField.UnsafeAddr()))
    53  	*cBlockPtr = cNativeBlockOpts
    54  	blockOpts.SetBlockSize(32 << 10) // 32kB
    55  	blockOpts.SetBlockCache(c)
    56  	if bloomBits > 0 {
    57  		blockOpts.SetFilterPolicy(gorocksdb.NewBloomFilter(bloomBits))
    58  	}
    59  	C.rocksdb_block_based_options_set_format_version(cNativeBlockOpts, 4)
    60  
    61  	opts := gorocksdb.NewDefaultOptions()
    62  	opts.SetBlockBasedTableFactory(blockOpts)
    63  	opts.SetCreateIfMissing(true)
    64  	opts.SetCreateIfMissingColumnFamilies(true)
    65  	opts.SetMaxBackgroundCompactions(6)
    66  	opts.SetMaxBackgroundFlushes(6)
    67  	opts.SetBytesPerSync(8 << 20)         // 8MB
    68  	opts.SetWriteBufferSize(1 << 27)      // 128MB
    69  	opts.SetMaxBytesForLevelBase(1 << 27) // 128MB
    70  	opts.SetMaxOpenFiles(maxOpenFiles)
    71  	opts.SetCompression(gorocksdb.LZ4HCCompression)
    72  	return opts
    73  }