github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/database/leveldb.chai2010/capi_helper.go (about)

     1  // Copyright 2013 <chaishushan{AT}gmail.com>. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package leveldb
     6  
     7  import "runtime"
     8  
     9  // ----------------------------------------------------------------------------
    10  // Value
    11  // ----------------------------------------------------------------------------
    12  
    13  // Value represents an c memory data.
    14  //
    15  // The caller should not modify the contents of the returned slice.
    16  type Value struct {
    17  	*valueHandler
    18  }
    19  type valueHandler struct {
    20  	value *leveldb_value_t
    21  }
    22  
    23  func newValueHandler(p *leveldb_value_t) *Value {
    24  	if p == nil {
    25  		return nil
    26  	}
    27  	h := &valueHandler{p}
    28  	runtime.SetFinalizer(h, (*valueHandler).Release)
    29  	return &Value{h}
    30  }
    31  
    32  func (p *valueHandler) Slice() []byte {
    33  	if p.value != nil {
    34  		return leveldb_value_data(p.value)
    35  	}
    36  	return nil
    37  }
    38  
    39  func (p *valueHandler) Release() {
    40  	if p.value != nil {
    41  		leveldb_value_destroy(p.value)
    42  	}
    43  	runtime.SetFinalizer(p, nil)
    44  }
    45  
    46  // ----------------------------------------------------------------------------
    47  // Options
    48  // ----------------------------------------------------------------------------
    49  
    50  // Avoid GC
    51  type optStateContainer struct {
    52  	cmp             *leveldb_comparator_t
    53  	filter          *leveldb_filterpolicy_t
    54  	cache           *Cache
    55  	stateComparator stateComparator
    56  	stateFilter     stateFilter
    57  }
    58  
    59  func (p *optStateContainer) Release() {
    60  	if p.cmp != nil {
    61  		leveldb_comparator_destroy(p.cmp)
    62  	}
    63  	if p.filter != nil {
    64  		leveldb_filterpolicy_destroy(p.filter)
    65  	}
    66  	*p = optStateContainer{}
    67  }
    68  
    69  func leveldb_options_create_copy(state *optStateContainer, opt *Options) *leveldb_options_t {
    70  	p := leveldb_options_create()
    71  	if opt != nil {
    72  		state.Release()
    73  		if opt.GetComparator() != nil {
    74  			state.stateComparator.Comparator = opt.GetComparator()
    75  			state.cmp = leveldb_comparator_create(&state.stateComparator)
    76  			leveldb_options_set_comparator(p, state.cmp)
    77  		}
    78  
    79  		leveldb_options_set_create_if_missing(p, opt.HasFlag(OFCreateIfMissing))
    80  		leveldb_options_set_error_if_exists(p, opt.HasFlag(OFErrorIfExist))
    81  		leveldb_options_set_paranoid_checks(p, opt.HasFlag(OFStrict))
    82  
    83  		if v := opt.GetWriteBufferSize(); v > 0 {
    84  			leveldb_options_set_write_buffer_size(p, v)
    85  		}
    86  
    87  		if v := opt.GetMaxOpenFiles(); v > 0 {
    88  			leveldb_options_set_max_open_files(p, v)
    89  		}
    90  
    91  		if v := opt.GetBlockCache(); v != nil {
    92  			state.cache = v
    93  			leveldb_options_set_cache(p, state.cache.cacheHandler.cache)
    94  		}
    95  
    96  		if v := opt.GetBlockSize(); v > 0 {
    97  			leveldb_options_set_block_size(p, v)
    98  		}
    99  
   100  		if v := opt.GetBlockRestartInterval(); v > 0 {
   101  			leveldb_options_set_block_restart_interval(p, v)
   102  		}
   103  
   104  		switch opt.GetCompression() {
   105  		case NoCompression:
   106  			leveldb_options_set_compression(p, leveldb_compression_nil)
   107  		case SnappyCompression:
   108  			leveldb_options_set_compression(p, leveldb_compression_snappy)
   109  		}
   110  
   111  		if opt.GetFilter() != nil {
   112  			state.stateFilter.Filter = opt.GetFilter()
   113  			state.filter = leveldb_filterpolicy_create(&state.stateFilter)
   114  			leveldb_options_set_filter_policy(p, state.filter)
   115  		}
   116  	}
   117  	return p
   118  }
   119  
   120  func leveldb_readoptions_create_copy(opt *ReadOptions) *leveldb_readoptions_t {
   121  	p := leveldb_readoptions_create()
   122  	if opt != nil {
   123  		leveldb_readoptions_set_verify_checksums(p, opt.HasFlag(RFVerifyChecksums))
   124  		leveldb_readoptions_set_fill_cache(p, !opt.HasFlag(RFDontFillCache))
   125  		if opt.Snapshot != nil && opt.Snapshot.snap != nil {
   126  			leveldb_readoptions_set_snapshot(p, opt.Snapshot.snap)
   127  		}
   128  	}
   129  	return p
   130  }
   131  
   132  func leveldb_writeoptions_create_copy(opt *WriteOptions) *leveldb_writeoptions_t {
   133  	p := leveldb_writeoptions_create()
   134  	if opt != nil {
   135  		leveldb_writeoptions_set_sync(p, opt.HasFlag(WFSync))
   136  	}
   137  	return p
   138  }
   139  
   140  // ----------------------------------------------------------------------------
   141  // Callback
   142  // ----------------------------------------------------------------------------
   143  
   144  type stateWriteBatchIterater struct {
   145  	WriteBatchIterater
   146  }
   147  
   148  type stateComparator struct {
   149  	Comparator
   150  	cname []byte
   151  }
   152  
   153  type stateFilter struct {
   154  	Filter
   155  	cname []byte
   156  }
   157  
   158  // ----------------------------------------------------------------------------
   159  // END
   160  // ----------------------------------------------------------------------------