OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / syndtr / goleveldb / leveldb / opt / options.go
1 // Copyright (c) 2012, Suryandaru Triandana <syndtr@gmail.com>
2 // All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style license that can be
5 // found in the LICENSE file.
6
7 // Package opt provides sets of options used by LevelDB.
8 package opt
9
10 import (
11         "math"
12
13         "github.com/syndtr/goleveldb/leveldb/cache"
14         "github.com/syndtr/goleveldb/leveldb/comparer"
15         "github.com/syndtr/goleveldb/leveldb/filter"
16 )
17
18 const (
19         KiB = 1024
20         MiB = KiB * 1024
21         GiB = MiB * 1024
22 )
23
24 var (
25         DefaultBlockCacher                   = LRUCacher
26         DefaultBlockCacheCapacity            = 8 * MiB
27         DefaultBlockRestartInterval          = 16
28         DefaultBlockSize                     = 4 * KiB
29         DefaultCompactionExpandLimitFactor   = 25
30         DefaultCompactionGPOverlapsFactor    = 10
31         DefaultCompactionL0Trigger           = 4
32         DefaultCompactionSourceLimitFactor   = 1
33         DefaultCompactionTableSize           = 2 * MiB
34         DefaultCompactionTableSizeMultiplier = 1.0
35         DefaultCompactionTotalSize           = 10 * MiB
36         DefaultCompactionTotalSizeMultiplier = 10.0
37         DefaultCompressionType               = SnappyCompression
38         DefaultIteratorSamplingRate          = 1 * MiB
39         DefaultOpenFilesCacher               = LRUCacher
40         DefaultOpenFilesCacheCapacity        = 500
41         DefaultWriteBuffer                   = 4 * MiB
42         DefaultWriteL0PauseTrigger           = 12
43         DefaultWriteL0SlowdownTrigger        = 8
44 )
45
46 // Cacher is a caching algorithm.
47 type Cacher interface {
48         New(capacity int) cache.Cacher
49 }
50
51 type CacherFunc struct {
52         NewFunc func(capacity int) cache.Cacher
53 }
54
55 func (f *CacherFunc) New(capacity int) cache.Cacher {
56         if f.NewFunc != nil {
57                 return f.NewFunc(capacity)
58         }
59         return nil
60 }
61
62 func noCacher(int) cache.Cacher { return nil }
63
64 var (
65         // LRUCacher is the LRU-cache algorithm.
66         LRUCacher = &CacherFunc{cache.NewLRU}
67
68         // NoCacher is the value to disable caching algorithm.
69         NoCacher = &CacherFunc{}
70 )
71
72 // Compression is the 'sorted table' block compression algorithm to use.
73 type Compression uint
74
75 func (c Compression) String() string {
76         switch c {
77         case DefaultCompression:
78                 return "default"
79         case NoCompression:
80                 return "none"
81         case SnappyCompression:
82                 return "snappy"
83         }
84         return "invalid"
85 }
86
87 const (
88         DefaultCompression Compression = iota
89         NoCompression
90         SnappyCompression
91         nCompression
92 )
93
94 // Strict is the DB 'strict level'.
95 type Strict uint
96
97 const (
98         // If present then a corrupted or invalid chunk or block in manifest
99         // journal will cause an error instead of being dropped.
100         // This will prevent database with corrupted manifest to be opened.
101         StrictManifest Strict = 1 << iota
102
103         // If present then journal chunk checksum will be verified.
104         StrictJournalChecksum
105
106         // If present then a corrupted or invalid chunk or block in journal
107         // will cause an error instead of being dropped.
108         // This will prevent database with corrupted journal to be opened.
109         StrictJournal
110
111         // If present then 'sorted table' block checksum will be verified.
112         // This has effect on both 'read operation' and compaction.
113         StrictBlockChecksum
114
115         // If present then a corrupted 'sorted table' will fails compaction.
116         // The database will enter read-only mode.
117         StrictCompaction
118
119         // If present then a corrupted 'sorted table' will halts 'read operation'.
120         StrictReader
121
122         // If present then leveldb.Recover will drop corrupted 'sorted table'.
123         StrictRecovery
124
125         // This only applicable for ReadOptions, if present then this ReadOptions
126         // 'strict level' will override global ones.
127         StrictOverride
128
129         // StrictAll enables all strict flags.
130         StrictAll = StrictManifest | StrictJournalChecksum | StrictJournal | StrictBlockChecksum | StrictCompaction | StrictReader | StrictRecovery
131
132         // DefaultStrict is the default strict flags. Specify any strict flags
133         // will override default strict flags as whole (i.e. not OR'ed).
134         DefaultStrict = StrictJournalChecksum | StrictBlockChecksum | StrictCompaction | StrictReader
135
136         // NoStrict disables all strict flags. Override default strict flags.
137         NoStrict = ^StrictAll
138 )
139
140 // Options holds the optional parameters for the DB at large.
141 type Options struct {
142         // AltFilters defines one or more 'alternative filters'.
143         // 'alternative filters' will be used during reads if a filter block
144         // does not match with the 'effective filter'.
145         //
146         // The default value is nil
147         AltFilters []filter.Filter
148
149         // BlockCacher provides cache algorithm for LevelDB 'sorted table' block caching.
150         // Specify NoCacher to disable caching algorithm.
151         //
152         // The default value is LRUCacher.
153         BlockCacher Cacher
154
155         // BlockCacheCapacity defines the capacity of the 'sorted table' block caching.
156         // Use -1 for zero, this has same effect as specifying NoCacher to BlockCacher.
157         //
158         // The default value is 8MiB.
159         BlockCacheCapacity int
160
161         // BlockRestartInterval is the number of keys between restart points for
162         // delta encoding of keys.
163         //
164         // The default value is 16.
165         BlockRestartInterval int
166
167         // BlockSize is the minimum uncompressed size in bytes of each 'sorted table'
168         // block.
169         //
170         // The default value is 4KiB.
171         BlockSize int
172
173         // CompactionExpandLimitFactor limits compaction size after expanded.
174         // This will be multiplied by table size limit at compaction target level.
175         //
176         // The default value is 25.
177         CompactionExpandLimitFactor int
178
179         // CompactionGPOverlapsFactor limits overlaps in grandparent (Level + 2) that a
180         // single 'sorted table' generates.
181         // This will be multiplied by table size limit at grandparent level.
182         //
183         // The default value is 10.
184         CompactionGPOverlapsFactor int
185
186         // CompactionL0Trigger defines number of 'sorted table' at level-0 that will
187         // trigger compaction.
188         //
189         // The default value is 4.
190         CompactionL0Trigger int
191
192         // CompactionSourceLimitFactor limits compaction source size. This doesn't apply to
193         // level-0.
194         // This will be multiplied by table size limit at compaction target level.
195         //
196         // The default value is 1.
197         CompactionSourceLimitFactor int
198
199         // CompactionTableSize limits size of 'sorted table' that compaction generates.
200         // The limits for each level will be calculated as:
201         //   CompactionTableSize * (CompactionTableSizeMultiplier ^ Level)
202         // The multiplier for each level can also fine-tuned using CompactionTableSizeMultiplierPerLevel.
203         //
204         // The default value is 2MiB.
205         CompactionTableSize int
206
207         // CompactionTableSizeMultiplier defines multiplier for CompactionTableSize.
208         //
209         // The default value is 1.
210         CompactionTableSizeMultiplier float64
211
212         // CompactionTableSizeMultiplierPerLevel defines per-level multiplier for
213         // CompactionTableSize.
214         // Use zero to skip a level.
215         //
216         // The default value is nil.
217         CompactionTableSizeMultiplierPerLevel []float64
218
219         // CompactionTotalSize limits total size of 'sorted table' for each level.
220         // The limits for each level will be calculated as:
221         //   CompactionTotalSize * (CompactionTotalSizeMultiplier ^ Level)
222         // The multiplier for each level can also fine-tuned using
223         // CompactionTotalSizeMultiplierPerLevel.
224         //
225         // The default value is 10MiB.
226         CompactionTotalSize int
227
228         // CompactionTotalSizeMultiplier defines multiplier for CompactionTotalSize.
229         //
230         // The default value is 10.
231         CompactionTotalSizeMultiplier float64
232
233         // CompactionTotalSizeMultiplierPerLevel defines per-level multiplier for
234         // CompactionTotalSize.
235         // Use zero to skip a level.
236         //
237         // The default value is nil.
238         CompactionTotalSizeMultiplierPerLevel []float64
239
240         // Comparer defines a total ordering over the space of []byte keys: a 'less
241         // than' relationship. The same comparison algorithm must be used for reads
242         // and writes over the lifetime of the DB.
243         //
244         // The default value uses the same ordering as bytes.Compare.
245         Comparer comparer.Comparer
246
247         // Compression defines the 'sorted table' block compression to use.
248         //
249         // The default value (DefaultCompression) uses snappy compression.
250         Compression Compression
251
252         // DisableBufferPool allows disable use of util.BufferPool functionality.
253         //
254         // The default value is false.
255         DisableBufferPool bool
256
257         // DisableBlockCache allows disable use of cache.Cache functionality on
258         // 'sorted table' block.
259         //
260         // The default value is false.
261         DisableBlockCache bool
262
263         // DisableCompactionBackoff allows disable compaction retry backoff.
264         //
265         // The default value is false.
266         DisableCompactionBackoff bool
267
268         // DisableLargeBatchTransaction allows disabling switch-to-transaction mode
269         // on large batch write. If enable batch writes large than WriteBuffer will
270         // use transaction.
271         //
272         // The default is false.
273         DisableLargeBatchTransaction bool
274
275         // ErrorIfExist defines whether an error should returned if the DB already
276         // exist.
277         //
278         // The default value is false.
279         ErrorIfExist bool
280
281         // ErrorIfMissing defines whether an error should returned if the DB is
282         // missing. If false then the database will be created if missing, otherwise
283         // an error will be returned.
284         //
285         // The default value is false.
286         ErrorIfMissing bool
287
288         // Filter defines an 'effective filter' to use. An 'effective filter'
289         // if defined will be used to generate per-table filter block.
290         // The filter name will be stored on disk.
291         // During reads LevelDB will try to find matching filter from
292         // 'effective filter' and 'alternative filters'.
293         //
294         // Filter can be changed after a DB has been created. It is recommended
295         // to put old filter to the 'alternative filters' to mitigate lack of
296         // filter during transition period.
297         //
298         // A filter is used to reduce disk reads when looking for a specific key.
299         //
300         // The default value is nil.
301         Filter filter.Filter
302
303         // IteratorSamplingRate defines approximate gap (in bytes) between read
304         // sampling of an iterator. The samples will be used to determine when
305         // compaction should be triggered.
306         //
307         // The default is 1MiB.
308         IteratorSamplingRate int
309
310         // NoSync allows completely disable fsync.
311         //
312         // The default is false.
313         NoSync bool
314
315         // NoWriteMerge allows disabling write merge.
316         //
317         // The default is false.
318         NoWriteMerge bool
319
320         // OpenFilesCacher provides cache algorithm for open files caching.
321         // Specify NoCacher to disable caching algorithm.
322         //
323         // The default value is LRUCacher.
324         OpenFilesCacher Cacher
325
326         // OpenFilesCacheCapacity defines the capacity of the open files caching.
327         // Use -1 for zero, this has same effect as specifying NoCacher to OpenFilesCacher.
328         //
329         // The default value is 500.
330         OpenFilesCacheCapacity int
331
332         // If true then opens DB in read-only mode.
333         //
334         // The default value is false.
335         ReadOnly bool
336
337         // Strict defines the DB strict level.
338         Strict Strict
339
340         // WriteBuffer defines maximum size of a 'memdb' before flushed to
341         // 'sorted table'. 'memdb' is an in-memory DB backed by an on-disk
342         // unsorted journal.
343         //
344         // LevelDB may held up to two 'memdb' at the same time.
345         //
346         // The default value is 4MiB.
347         WriteBuffer int
348
349         // WriteL0StopTrigger defines number of 'sorted table' at level-0 that will
350         // pause write.
351         //
352         // The default value is 12.
353         WriteL0PauseTrigger int
354
355         // WriteL0SlowdownTrigger defines number of 'sorted table' at level-0 that
356         // will trigger write slowdown.
357         //
358         // The default value is 8.
359         WriteL0SlowdownTrigger int
360 }
361
362 func (o *Options) GetAltFilters() []filter.Filter {
363         if o == nil {
364                 return nil
365         }
366         return o.AltFilters
367 }
368
369 func (o *Options) GetBlockCacher() Cacher {
370         if o == nil || o.BlockCacher == nil {
371                 return DefaultBlockCacher
372         } else if o.BlockCacher == NoCacher {
373                 return nil
374         }
375         return o.BlockCacher
376 }
377
378 func (o *Options) GetBlockCacheCapacity() int {
379         if o == nil || o.BlockCacheCapacity == 0 {
380                 return DefaultBlockCacheCapacity
381         } else if o.BlockCacheCapacity < 0 {
382                 return 0
383         }
384         return o.BlockCacheCapacity
385 }
386
387 func (o *Options) GetBlockRestartInterval() int {
388         if o == nil || o.BlockRestartInterval <= 0 {
389                 return DefaultBlockRestartInterval
390         }
391         return o.BlockRestartInterval
392 }
393
394 func (o *Options) GetBlockSize() int {
395         if o == nil || o.BlockSize <= 0 {
396                 return DefaultBlockSize
397         }
398         return o.BlockSize
399 }
400
401 func (o *Options) GetCompactionExpandLimit(level int) int {
402         factor := DefaultCompactionExpandLimitFactor
403         if o != nil && o.CompactionExpandLimitFactor > 0 {
404                 factor = o.CompactionExpandLimitFactor
405         }
406         return o.GetCompactionTableSize(level+1) * factor
407 }
408
409 func (o *Options) GetCompactionGPOverlaps(level int) int {
410         factor := DefaultCompactionGPOverlapsFactor
411         if o != nil && o.CompactionGPOverlapsFactor > 0 {
412                 factor = o.CompactionGPOverlapsFactor
413         }
414         return o.GetCompactionTableSize(level+2) * factor
415 }
416
417 func (o *Options) GetCompactionL0Trigger() int {
418         if o == nil || o.CompactionL0Trigger == 0 {
419                 return DefaultCompactionL0Trigger
420         }
421         return o.CompactionL0Trigger
422 }
423
424 func (o *Options) GetCompactionSourceLimit(level int) int {
425         factor := DefaultCompactionSourceLimitFactor
426         if o != nil && o.CompactionSourceLimitFactor > 0 {
427                 factor = o.CompactionSourceLimitFactor
428         }
429         return o.GetCompactionTableSize(level+1) * factor
430 }
431
432 func (o *Options) GetCompactionTableSize(level int) int {
433         var (
434                 base = DefaultCompactionTableSize
435                 mult float64
436         )
437         if o != nil {
438                 if o.CompactionTableSize > 0 {
439                         base = o.CompactionTableSize
440                 }
441                 if level < len(o.CompactionTableSizeMultiplierPerLevel) && o.CompactionTableSizeMultiplierPerLevel[level] > 0 {
442                         mult = o.CompactionTableSizeMultiplierPerLevel[level]
443                 } else if o.CompactionTableSizeMultiplier > 0 {
444                         mult = math.Pow(o.CompactionTableSizeMultiplier, float64(level))
445                 }
446         }
447         if mult == 0 {
448                 mult = math.Pow(DefaultCompactionTableSizeMultiplier, float64(level))
449         }
450         return int(float64(base) * mult)
451 }
452
453 func (o *Options) GetCompactionTotalSize(level int) int64 {
454         var (
455                 base = DefaultCompactionTotalSize
456                 mult float64
457         )
458         if o != nil {
459                 if o.CompactionTotalSize > 0 {
460                         base = o.CompactionTotalSize
461                 }
462                 if level < len(o.CompactionTotalSizeMultiplierPerLevel) && o.CompactionTotalSizeMultiplierPerLevel[level] > 0 {
463                         mult = o.CompactionTotalSizeMultiplierPerLevel[level]
464                 } else if o.CompactionTotalSizeMultiplier > 0 {
465                         mult = math.Pow(o.CompactionTotalSizeMultiplier, float64(level))
466                 }
467         }
468         if mult == 0 {
469                 mult = math.Pow(DefaultCompactionTotalSizeMultiplier, float64(level))
470         }
471         return int64(float64(base) * mult)
472 }
473
474 func (o *Options) GetComparer() comparer.Comparer {
475         if o == nil || o.Comparer == nil {
476                 return comparer.DefaultComparer
477         }
478         return o.Comparer
479 }
480
481 func (o *Options) GetCompression() Compression {
482         if o == nil || o.Compression <= DefaultCompression || o.Compression >= nCompression {
483                 return DefaultCompressionType
484         }
485         return o.Compression
486 }
487
488 func (o *Options) GetDisableBufferPool() bool {
489         if o == nil {
490                 return false
491         }
492         return o.DisableBufferPool
493 }
494
495 func (o *Options) GetDisableBlockCache() bool {
496         if o == nil {
497                 return false
498         }
499         return o.DisableBlockCache
500 }
501
502 func (o *Options) GetDisableCompactionBackoff() bool {
503         if o == nil {
504                 return false
505         }
506         return o.DisableCompactionBackoff
507 }
508
509 func (o *Options) GetDisableLargeBatchTransaction() bool {
510         if o == nil {
511                 return false
512         }
513         return o.DisableLargeBatchTransaction
514 }
515
516 func (o *Options) GetErrorIfExist() bool {
517         if o == nil {
518                 return false
519         }
520         return o.ErrorIfExist
521 }
522
523 func (o *Options) GetErrorIfMissing() bool {
524         if o == nil {
525                 return false
526         }
527         return o.ErrorIfMissing
528 }
529
530 func (o *Options) GetFilter() filter.Filter {
531         if o == nil {
532                 return nil
533         }
534         return o.Filter
535 }
536
537 func (o *Options) GetIteratorSamplingRate() int {
538         if o == nil || o.IteratorSamplingRate <= 0 {
539                 return DefaultIteratorSamplingRate
540         }
541         return o.IteratorSamplingRate
542 }
543
544 func (o *Options) GetNoSync() bool {
545         if o == nil {
546                 return false
547         }
548         return o.NoSync
549 }
550
551 func (o *Options) GetNoWriteMerge() bool {
552         if o == nil {
553                 return false
554         }
555         return o.NoWriteMerge
556 }
557
558 func (o *Options) GetOpenFilesCacher() Cacher {
559         if o == nil || o.OpenFilesCacher == nil {
560                 return DefaultOpenFilesCacher
561         }
562         if o.OpenFilesCacher == NoCacher {
563                 return nil
564         }
565         return o.OpenFilesCacher
566 }
567
568 func (o *Options) GetOpenFilesCacheCapacity() int {
569         if o == nil || o.OpenFilesCacheCapacity == 0 {
570                 return DefaultOpenFilesCacheCapacity
571         } else if o.OpenFilesCacheCapacity < 0 {
572                 return 0
573         }
574         return o.OpenFilesCacheCapacity
575 }
576
577 func (o *Options) GetReadOnly() bool {
578         if o == nil {
579                 return false
580         }
581         return o.ReadOnly
582 }
583
584 func (o *Options) GetStrict(strict Strict) bool {
585         if o == nil || o.Strict == 0 {
586                 return DefaultStrict&strict != 0
587         }
588         return o.Strict&strict != 0
589 }
590
591 func (o *Options) GetWriteBuffer() int {
592         if o == nil || o.WriteBuffer <= 0 {
593                 return DefaultWriteBuffer
594         }
595         return o.WriteBuffer
596 }
597
598 func (o *Options) GetWriteL0PauseTrigger() int {
599         if o == nil || o.WriteL0PauseTrigger == 0 {
600                 return DefaultWriteL0PauseTrigger
601         }
602         return o.WriteL0PauseTrigger
603 }
604
605 func (o *Options) GetWriteL0SlowdownTrigger() int {
606         if o == nil || o.WriteL0SlowdownTrigger == 0 {
607                 return DefaultWriteL0SlowdownTrigger
608         }
609         return o.WriteL0SlowdownTrigger
610 }
611
612 // ReadOptions holds the optional parameters for 'read operation'. The
613 // 'read operation' includes Get, Find and NewIterator.
614 type ReadOptions struct {
615         // DontFillCache defines whether block reads for this 'read operation'
616         // should be cached. If false then the block will be cached. This does
617         // not affects already cached block.
618         //
619         // The default value is false.
620         DontFillCache bool
621
622         // Strict will be OR'ed with global DB 'strict level' unless StrictOverride
623         // is present. Currently only StrictReader that has effect here.
624         Strict Strict
625 }
626
627 func (ro *ReadOptions) GetDontFillCache() bool {
628         if ro == nil {
629                 return false
630         }
631         return ro.DontFillCache
632 }
633
634 func (ro *ReadOptions) GetStrict(strict Strict) bool {
635         if ro == nil {
636                 return false
637         }
638         return ro.Strict&strict != 0
639 }
640
641 // WriteOptions holds the optional parameters for 'write operation'. The
642 // 'write operation' includes Write, Put and Delete.
643 type WriteOptions struct {
644         // NoWriteMerge allows disabling write merge.
645         //
646         // The default is false.
647         NoWriteMerge bool
648
649         // Sync is whether to sync underlying writes from the OS buffer cache
650         // through to actual disk, if applicable. Setting Sync can result in
651         // slower writes.
652         //
653         // If false, and the machine crashes, then some recent writes may be lost.
654         // Note that if it is just the process that crashes (and the machine does
655         // not) then no writes will be lost.
656         //
657         // In other words, Sync being false has the same semantics as a write
658         // system call. Sync being true means write followed by fsync.
659         //
660         // The default value is false.
661         Sync bool
662 }
663
664 func (wo *WriteOptions) GetNoWriteMerge() bool {
665         if wo == nil {
666                 return false
667         }
668         return wo.NoWriteMerge
669 }
670
671 func (wo *WriteOptions) GetSync() bool {
672         if wo == nil {
673                 return false
674         }
675         return wo.Sync
676 }
677
678 func GetStrict(o *Options, ro *ReadOptions, strict Strict) bool {
679         if ro.GetStrict(StrictOverride) {
680                 return ro.GetStrict(strict)
681         } else {
682                 return o.GetStrict(strict) || ro.GetStrict(strict)
683         }
684 }