github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/database/leveldb.chai2010/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  const (
     8  	DefaultWriteBufferSize      = 4 << 20
     9  	DefaultMaxOpenFiles         = 1000
    10  	DefaultBlockCacheSize       = 8 << 20
    11  	DefaultBlockSize            = 4096
    12  	DefaultBlockRestartInterval = 16
    13  	DefaultCompressionType      = SnappyCompression
    14  )
    15  
    16  type OptionsFlag uint
    17  
    18  const (
    19  	// If set, the database will be created if it is missing.
    20  	OFCreateIfMissing OptionsFlag = 1 << iota
    21  
    22  	// If set, an error is raised if the database already exists.
    23  	OFErrorIfExist
    24  
    25  	// If set, the implementation will do aggressive checking of the
    26  	// data it is processing and will stop early if it detects any
    27  	// errors.  This may have unforeseen ramifications: for example, a
    28  	// corruption of one DB entry may cause a large number of entries to
    29  	// become unreadable or for the entire DB to become unopenable.
    30  	OFStrict
    31  )
    32  
    33  // Database compression type
    34  type Compression int
    35  
    36  func (c Compression) String() string {
    37  	switch c {
    38  	case DefaultCompression:
    39  		return "leveldb.Compression: default(snappy)"
    40  	case NoCompression:
    41  		return "leveldb.Compression: none"
    42  	case SnappyCompression:
    43  		return "leveldb.Compression: snappy"
    44  	}
    45  	return "leveldb.Compression: unknown"
    46  }
    47  
    48  const (
    49  	DefaultCompression Compression = iota
    50  	NoCompression
    51  	SnappyCompression
    52  	nCompression
    53  )
    54  
    55  // Options to control the behavior of a database (passed to DB::Open)
    56  type Options struct {
    57  	// Comparator used to define the order of keys in the table.
    58  	// Default: a comparator that uses lexicographic byte-wise ordering
    59  	//
    60  	// REQUIRES: The client must ensure that the comparator supplied
    61  	// here has the same name and orders keys *exactly* the same as the
    62  	// comparator provided to previous open calls on the same DB.
    63  	Comparator Comparator
    64  
    65  	// Specify the database flag.
    66  	Flag OptionsFlag
    67  
    68  	// Amount of data to build up in memory (backed by an unsorted log
    69  	// on disk) before converting to a sorted on-disk file.
    70  	//
    71  	// Larger values increase performance, especially during bulk loads.
    72  	// Up to two write buffers may be held in memory at the same time,
    73  	// so you may wish to adjust this parameter to control memory usage.
    74  	// Also, a larger write buffer will result in a longer recovery time
    75  	// the next time the database is opened.
    76  	//
    77  	// Default: 4MB
    78  	WriteBufferSize int
    79  
    80  	// Number of open files that can be used by the DB.  You may need to
    81  	// increase this if your database has a large working set (budget
    82  	// one open file per 2MB of working set).
    83  	//
    84  	// Default: 1000
    85  	MaxOpenFiles int
    86  
    87  	// Control over blocks (user data is stored in a set of blocks, and
    88  	// a block is the unit of reading from disk).
    89  	//
    90  	// If non-nil, use the specified cache for blocks.
    91  	// If nil, leveldb will automatically create and use an 8MB internal cache.
    92  	//
    93  	// Default: nil
    94  	BlockCache *Cache
    95  
    96  	// Approximate size of user data packed per block.  Note that the
    97  	// block size specified here corresponds to uncompressed data.  The
    98  	// actual size of the unit read from disk may be smaller if
    99  	// compression is enabled.  This parameter can be changed dynamically.
   100  	//
   101  	// Default: 4K
   102  	BlockSize int
   103  
   104  	// Number of keys between restart points for delta encoding of keys.
   105  	// This parameter can be changed dynamically.  Most clients should
   106  	// leave this parameter alone.
   107  	//
   108  	// Default: 16
   109  	BlockRestartInterval int
   110  
   111  	// Compress blocks using the specified compression algorithm.  This
   112  	// parameter can be changed dynamically.
   113  	//
   114  	// Default: kSnappyCompression, which gives lightweight but fast
   115  	// compression.
   116  	//
   117  	// Typical speeds of kSnappyCompression on an Intel(R) Core(TM)2 2.4GHz:
   118  	//    ~200-500MB/s compression
   119  	//    ~400-800MB/s decompression
   120  	// Note that these speeds are significantly faster than most
   121  	// persistent storage speeds, and therefore it is typically never
   122  	// worth switching to kNoCompression.  Even if the input data is
   123  	// incompressible, the kSnappyCompression implementation will
   124  	// efficiently detect that and will switch to uncompressed mode.
   125  	Compression Compression
   126  
   127  	// If non-NULL, use the specified filter policy to reduce disk reads.
   128  	// Many applications will benefit from passing the result of
   129  	// NewBloomFilterPolicy() here.
   130  	//
   131  	// Default: NULL
   132  	Filter Filter
   133  }
   134  
   135  func (o *Options) GetComparator() Comparator {
   136  	return o.Comparator
   137  }
   138  func (o *Options) SetComparator(cmp Comparator) {
   139  	o.Comparator = cmp
   140  }
   141  
   142  func (o *Options) HasFlag(flag OptionsFlag) bool {
   143  	return (o.Flag & flag) != 0
   144  }
   145  func (o *Options) SetFlag(flag OptionsFlag) {
   146  	o.Flag |= flag
   147  }
   148  func (o *Options) ClearFlag(flag OptionsFlag) {
   149  	o.Flag &= ^flag
   150  }
   151  
   152  func (o *Options) GetWriteBufferSize() int {
   153  	if o.WriteBufferSize <= 0 {
   154  		return DefaultWriteBufferSize
   155  	}
   156  	return o.WriteBufferSize
   157  }
   158  func (o *Options) SetWriteBufferSize(size int) {
   159  	o.WriteBufferSize = size
   160  }
   161  
   162  func (o *Options) GetMaxOpenFiles() int {
   163  	if o.MaxOpenFiles <= 0 {
   164  		return DefaultMaxOpenFiles
   165  	}
   166  	return o.MaxOpenFiles
   167  }
   168  func (o *Options) SetMaxOpenFiles(max int) {
   169  	o.MaxOpenFiles = max
   170  }
   171  
   172  func (o *Options) GetBlockCache() *Cache {
   173  	return o.BlockCache
   174  }
   175  func (o *Options) SetBlockCache(cache *Cache) {
   176  	o.BlockCache = cache
   177  }
   178  func (o *Options) SetBlockCacheCapacity(capacity int64) {
   179  	o.BlockCache = NewCache(capacity)
   180  }
   181  
   182  func (o *Options) GetBlockSize() int {
   183  	if o.BlockSize <= 0 {
   184  		return DefaultBlockSize
   185  	}
   186  	return o.BlockSize
   187  }
   188  func (o *Options) SetBlockSize(size int) {
   189  	o.BlockSize = size
   190  }
   191  
   192  func (o *Options) GetBlockRestartInterval() int {
   193  	if o.BlockRestartInterval <= 0 {
   194  		return DefaultBlockRestartInterval
   195  	}
   196  	return o.BlockRestartInterval
   197  }
   198  func (o *Options) SetBlockRestartInterval(interval int) {
   199  	o.BlockRestartInterval = interval
   200  }
   201  
   202  func (o *Options) GetCompression() Compression {
   203  	if o.Compression <= DefaultCompression || o.Compression >= nCompression {
   204  		return DefaultCompressionType
   205  	}
   206  	return o.Compression
   207  }
   208  func (o *Options) SetCompression(compression Compression) {
   209  	o.Compression = compression
   210  }
   211  
   212  func (o *Options) GetFilter() Filter {
   213  	return o.Filter
   214  }
   215  func (o *Options) SetFilter(p Filter) {
   216  	o.Filter = p
   217  }
   218  
   219  type ReadOptionsFlag uint
   220  
   221  const (
   222  	// If true, all data read from underlying storage will be
   223  	// verified against corresponding checksums.
   224  	RFVerifyChecksums ReadOptionsFlag = 1 << iota
   225  
   226  	// Should the data read for this iteration be cached in memory?
   227  	// If set iteration chaching will be disabled.
   228  	// Callers may wish to set this flag for bulk scans.
   229  	RFDontFillCache
   230  )
   231  
   232  // ReadOptions represent sets of options used by LevelDB during read
   233  // operations.
   234  type ReadOptions struct {
   235  	// Specify the read flag.
   236  	Flag ReadOptionsFlag
   237  
   238  	// If "snapshot" is non-NULL, read as of the supplied snapshot
   239  	// (which must belong to the DB that is being read and which must
   240  	// not have been released).  If "snapshot" is NULL, use an impliicit
   241  	// snapshot of the state at the beginning of this read operation.
   242  	Snapshot *Snapshot
   243  }
   244  
   245  func (o *ReadOptions) HasFlag(flag ReadOptionsFlag) bool {
   246  	return (o.Flag & flag) != 0
   247  }
   248  func (o *ReadOptions) SetFlag(flag ReadOptionsFlag) {
   249  	o.Flag |= flag
   250  }
   251  func (o *ReadOptions) ClearFlag(flag ReadOptionsFlag) {
   252  	o.Flag &= ^flag
   253  }
   254  
   255  func (o *ReadOptions) GetSnapshot() *Snapshot {
   256  	return o.Snapshot
   257  }
   258  func (o *ReadOptions) SetSnapshot(snap *Snapshot) {
   259  	o.Snapshot = snap
   260  }
   261  
   262  type WriteOptionsFlag uint
   263  
   264  const (
   265  	// If set, the write will be flushed from the operating system
   266  	// buffer cache (by calling WritableFile::Sync()) before the write
   267  	// is considered complete.  If this flag is true, writes will be
   268  	// slower.
   269  	//
   270  	// If this flag is false, and the machine crashes, some recent
   271  	// writes may be lost.  Note that if it is just the process that
   272  	// crashes (i.e., the machine does not reboot), no writes will be
   273  	// lost even if sync==false.
   274  	//
   275  	// In other words, a DB write with sync==false has similar
   276  	// crash semantics as the "write()" system call.  A DB write
   277  	// with sync==true has similar crash semantics to a "write()"
   278  	// system call followed by "fsync()".
   279  	WFSync WriteOptionsFlag = 1 << iota
   280  )
   281  
   282  // WriteOptions represent sets of options used by LevelDB during write
   283  // operations.
   284  type WriteOptions struct {
   285  	// Specify the write flag.
   286  	Flag WriteOptionsFlag
   287  }
   288  
   289  func (o *WriteOptions) HasFlag(flag WriteOptionsFlag) bool {
   290  	return (o.Flag & flag) != 0
   291  }
   292  func (o *WriteOptions) SetFlag(flag WriteOptionsFlag) {
   293  	o.Flag |= flag
   294  }
   295  func (o *WriteOptions) ClearFlag(flag WriteOptionsFlag) {
   296  	o.Flag &= ^flag
   297  }