github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/database/leveldb/options.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 // #include <leveldb/c.h> 8 import "C" 9 10 // CompressionOpt is a value for Options.SetCompression. 11 type CompressionOpt int 12 13 // Known compression arguments for Options.SetCompression. 14 const ( 15 NoCompression = CompressionOpt(C.leveldb_no_compression) 16 SnappyCompression = CompressionOpt(C.leveldb_snappy_compression) 17 ) 18 19 // Options represent all of the available options when opening a database with 20 // Open. Options should be created with NewOptions. 21 // 22 // It is usually with to call SetCache with a cache object. Otherwise, all 23 // data will be read off disk. 24 // 25 // To prevent memory leaks, Close must be called on an Options when the 26 // program no longer needs it. 27 type Options struct { 28 opt *C.leveldb_options_t 29 } 30 31 // ReadOptions represent all of the available options when reading from a 32 // database. 33 // 34 // To prevent memory leaks, Close must called on a ReadOptions when the 35 // program no longer needs it. 36 type ReadOptions struct { 37 opt *C.leveldb_readoptions_t 38 } 39 40 // WriteOptions represent all of the available options when writeing from a 41 // database. 42 // 43 // To prevent memory leaks, Close must called on a WriteOptions when the 44 // program no longer needs it. 45 type WriteOptions struct { 46 opt *C.leveldb_writeoptions_t 47 } 48 49 // NewOptions allocates a new Options object. 50 func NewOptions() *Options { 51 opt := C.leveldb_options_create() 52 return &Options{opt} 53 } 54 55 // NewReadOptions allocates a new ReadOptions object. 56 func NewReadOptions() *ReadOptions { 57 opt := C.leveldb_readoptions_create() 58 return &ReadOptions{opt} 59 } 60 61 // NewWriteOptions allocates a new WriteOptions object. 62 func NewWriteOptions() *WriteOptions { 63 opt := C.leveldb_writeoptions_create() 64 return &WriteOptions{opt} 65 } 66 67 // Close deallocates the Options, freeing its underlying C struct. 68 func (o *Options) Close() { 69 C.leveldb_options_destroy(o.opt) 70 } 71 72 // SetComparator sets the comparator to be used for all read and write 73 // operations. 74 // 75 // The comparator that created a database must be the same one (technically, 76 // one with the same name string) that is used to perform read and write 77 // operations. 78 // 79 // The default comparator is usually sufficient. 80 func (o *Options) SetComparator(cmp *C.leveldb_comparator_t) { 81 C.leveldb_options_set_comparator(o.opt, cmp) 82 } 83 84 // SetErrorIfExists, if passed true, will cause the opening of a database that 85 // already exists to throw an error. 86 func (o *Options) SetErrorIfExists(error_if_exists bool) { 87 eie := boolToUchar(error_if_exists) 88 C.leveldb_options_set_error_if_exists(o.opt, eie) 89 } 90 91 // SetCache places a cache object in the database when a database is opened. 92 // 93 // This is usually wise to use. See also ReadOptions.SetFillCache. 94 func (o *Options) SetCache(c *Cache) { 95 C.leveldb_options_set_cache(o.opt, c.cache) 96 } 97 98 // SetEnv sets the Env object for the new database handle. 99 func (o *Options) SetEnv(env *Env) { 100 C.leveldb_options_set_env(o.opt, env.env) 101 } 102 103 // SetInfoLog sets a *C.leveldb_logger_t object as the informational logger 104 // for the database. 105 func (o *Options) SetInfoLog(log *C.leveldb_logger_t) { 106 C.leveldb_options_set_info_log(o.opt, log) 107 } 108 109 // SetWriteBufferSize sets the number of bytes the database will build up in 110 // memory (backed by an unsorted log on disk) before converting to a sorted 111 // on-disk file. 112 func (o *Options) SetWriteBufferSize(s int) { 113 C.leveldb_options_set_write_buffer_size(o.opt, C.size_t(s)) 114 } 115 116 // SetParanoidChecks, when called with true, will cause the database to do 117 // aggressive checking of the data it is processing and will stop early if it 118 // detects errors. 119 // 120 // See the LevelDB documentation docs for details. 121 func (o *Options) SetParanoidChecks(pc bool) { 122 C.leveldb_options_set_paranoid_checks(o.opt, boolToUchar(pc)) 123 } 124 125 // SetMaxOpenFiles sets the number of files than can be used at once by the 126 // database. 127 // 128 // See the LevelDB documentation for details. 129 func (o *Options) SetMaxOpenFiles(n int) { 130 C.leveldb_options_set_max_open_files(o.opt, C.int(n)) 131 } 132 133 // SetBlockSize sets the approximate size of user data packed per block. 134 // 135 // The default is roughly 4096 uncompressed bytes. A better setting depends on 136 // your use case. See the LevelDB documentation for details. 137 func (o *Options) SetBlockSize(s int) { 138 C.leveldb_options_set_block_size(o.opt, C.size_t(s)) 139 } 140 141 // SetBlockRestartInterval is the number of keys between restarts points for 142 // delta encoding keys. 143 // 144 // Most clients should leave this parameter alone. See the LevelDB 145 // documentation for details. 146 func (o *Options) SetBlockRestartInterval(n int) { 147 C.leveldb_options_set_block_restart_interval(o.opt, C.int(n)) 148 } 149 150 // SetCompression sets whether to compress blocks using the specified 151 // compresssion algorithm. 152 // 153 // The default value is SnappyCompression and it is fast enough that it is 154 // unlikely you want to turn it off. The other option is NoCompression. 155 // 156 // If the LevelDB library was built without Snappy compression enabled, the 157 // SnappyCompression setting will be ignored. 158 func (o *Options) SetCompression(t CompressionOpt) { 159 C.leveldb_options_set_compression(o.opt, C.int(t)) 160 } 161 162 // SetCreateIfMissing causes Open to create a new database on disk if it does 163 // not already exist. 164 func (o *Options) SetCreateIfMissing(b bool) { 165 C.leveldb_options_set_create_if_missing(o.opt, boolToUchar(b)) 166 } 167 168 // SetFilterPolicy causes Open to create a new database that will uses filter 169 // created from the filter policy passed in. 170 func (o *Options) SetFilterPolicy(fp *FilterPolicy) { 171 var policy *C.leveldb_filterpolicy_t 172 if fp != nil { 173 policy = fp.policy 174 } 175 C.leveldb_options_set_filter_policy(o.opt, policy) 176 } 177 178 // Close deallocates the ReadOptions, freeing its underlying C struct. 179 func (ro *ReadOptions) Close() { 180 C.leveldb_readoptions_destroy(ro.opt) 181 } 182 183 // SetVerifyChecksums controls whether all data read with this ReadOptions 184 // will be verified against corresponding checksums. 185 // 186 // It defaults to false. See the LevelDB documentation for details. 187 func (ro *ReadOptions) SetVerifyChecksums(b bool) { 188 C.leveldb_readoptions_set_verify_checksums(ro.opt, boolToUchar(b)) 189 } 190 191 // SetFillCache controls whether reads performed with this ReadOptions will 192 // fill the Cache of the server. It defaults to true. 193 // 194 // It is useful to turn this off on ReadOptions for DB.Iterator (and DB.Get) 195 // calls used in offline threads to prevent bulk scans from flushing out live 196 // user data in the cache. 197 // 198 // See also Options.SetCache 199 func (ro *ReadOptions) SetFillCache(b bool) { 200 C.leveldb_readoptions_set_fill_cache(ro.opt, boolToUchar(b)) 201 } 202 203 // SetSnapshot causes reads to provided as they were when the passed in 204 // Snapshot was created by DB.NewSnapshot. This is useful for getting 205 // consistent reads during a bulk operation. 206 // 207 // See the LevelDB documentation for details. 208 func (ro *ReadOptions) SetSnapshot(snap *Snapshot) { 209 var s *C.leveldb_snapshot_t 210 if snap != nil { 211 s = snap.snap 212 } 213 C.leveldb_readoptions_set_snapshot(ro.opt, s) 214 } 215 216 // Close deallocates the WriteOptions, freeing its underlying C struct. 217 func (wo *WriteOptions) Close() { 218 C.leveldb_writeoptions_destroy(wo.opt) 219 } 220 221 // SetSync controls whether each write performed with this WriteOptions will 222 // be flushed from the operating system buffer cache before the write is 223 // considered complete. 224 // 225 // If called with true, this will signficantly slow down writes. If called 226 // with false, and the host machine crashes, some recent writes may be 227 // lost. The default is false. 228 // 229 // See the LevelDB documentation for details. 230 func (wo *WriteOptions) SetSync(b bool) { 231 C.leveldb_writeoptions_set_sync(wo.opt, boolToUchar(b)) 232 }