...
1
2
3
4
5
6
7
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 DefaultWriteBuffer = 4 * MiB
41 DefaultWriteL0PauseTrigger = 12
42 DefaultWriteL0SlowdownTrigger = 8
43 DefaultFilterBaseLg = 11
44 DefaultMaxManifestFileSize = int64(64 * MiB)
45 )
46
47
48 type Cacher interface {
49 New(capacity int) cache.Cacher
50 }
51
52 type cacherFunc struct {
53 NewFunc func(capacity int) cache.Cacher
54 }
55
56 func (f *cacherFunc) New(capacity int) cache.Cacher {
57 if f != nil && f.NewFunc != nil {
58 return f.NewFunc(capacity)
59 }
60 return nil
61 }
62
63 func CacherFunc(f func(capacity int) cache.Cacher) Cacher {
64 return &cacherFunc{f}
65 }
66
67 type passthroughCacher struct {
68 Cacher cache.Cacher
69 }
70
71 func (p *passthroughCacher) New(capacity int) cache.Cacher {
72 return p.Cacher
73 }
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91 func PassthroughCacher(x cache.Cacher) Cacher {
92 return &passthroughCacher{x}
93 }
94
95
96 func NewLRU(capacity int) Cacher {
97 return PassthroughCacher(cache.NewLRU(capacity))
98 }
99
100 var (
101
102 LRUCacher = CacherFunc(cache.NewLRU)
103
104
105 NoCacher = CacherFunc(nil)
106 )
107
108
109 type Compression uint
110
111 func (c Compression) String() string {
112 switch c {
113 case DefaultCompression:
114 return "default"
115 case NoCompression:
116 return "none"
117 case SnappyCompression:
118 return "snappy"
119 }
120 return "invalid"
121 }
122
123 const (
124 DefaultCompression Compression = iota
125 NoCompression
126 SnappyCompression
127 nCompression
128 )
129
130
131 type Strict uint
132
133 const (
134
135
136
137 StrictManifest Strict = 1 << iota
138
139
140 StrictJournalChecksum
141
142
143
144
145 StrictJournal
146
147
148
149 StrictBlockChecksum
150
151
152
153 StrictCompaction
154
155
156 StrictReader
157
158
159 StrictRecovery
160
161
162
163 StrictOverride
164
165
166 StrictAll = StrictManifest | StrictJournalChecksum | StrictJournal | StrictBlockChecksum | StrictCompaction | StrictReader | StrictRecovery
167
168
169
170 DefaultStrict = StrictJournalChecksum | StrictBlockChecksum | StrictCompaction | StrictReader
171
172
173 NoStrict = ^StrictAll
174 )
175
176
177 type Options struct {
178
179
180
181
182
183 AltFilters []filter.Filter
184
185
186
187
188
189 BlockCacher Cacher
190
191
192
193
194
195 BlockCacheCapacity int
196
197
198
199
200
201 BlockCacheEvictRemoved bool
202
203
204
205
206
207 BlockRestartInterval int
208
209
210
211
212
213 BlockSize int
214
215
216
217
218
219 CompactionExpandLimitFactor int
220
221
222
223
224
225
226 CompactionGPOverlapsFactor int
227
228
229
230
231
232 CompactionL0Trigger int
233
234
235
236
237
238
239 CompactionSourceLimitFactor int
240
241
242
243
244
245
246
247 CompactionTableSize int
248
249
250
251
252 CompactionTableSizeMultiplier float64
253
254
255
256
257
258
259 CompactionTableSizeMultiplierPerLevel []float64
260
261
262
263
264
265
266
267
268 CompactionTotalSize int
269
270
271
272
273 CompactionTotalSizeMultiplier float64
274
275
276
277
278
279
280 CompactionTotalSizeMultiplierPerLevel []float64
281
282
283
284
285
286
287 Comparer comparer.Comparer
288
289
290
291
292 Compression Compression
293
294
295
296
297 DisableBufferPool bool
298
299
300
301
302
303 DisableBlockCache bool
304
305
306
307
308 DisableCompactionBackoff bool
309
310
311
312
313
314
315 DisableLargeBatchTransaction bool
316
317
318
319
320
321
322
323 DisableSeeksCompaction bool
324
325
326
327
328
329 ErrorIfExist bool
330
331
332
333
334
335
336 ErrorIfMissing bool
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351 Filter filter.Filter
352
353
354
355
356
357
358
359
360 IteratorSamplingRate int
361
362
363
364
365 NoSync bool
366
367
368
369
370 NoWriteMerge bool
371
372
373
374
375
376 OpenFilesCacher Cacher
377
378
379
380
381
382 OpenFilesCacheCapacity int
383
384
385
386
387 ReadOnly bool
388
389
390 Strict Strict
391
392
393
394
395
396
397
398
399 WriteBuffer int
400
401
402
403
404
405 WriteL0PauseTrigger int
406
407
408
409
410
411 WriteL0SlowdownTrigger int
412
413
414
415
416 FilterBaseLg int
417
418
419
420
421
422
423 MaxManifestFileSize int64
424 }
425
426 func (o *Options) GetAltFilters() []filter.Filter {
427 if o == nil {
428 return nil
429 }
430 return o.AltFilters
431 }
432
433 func (o *Options) GetBlockCacher() Cacher {
434 if o == nil || o.BlockCacher == nil {
435 return DefaultBlockCacher
436 }
437 return o.BlockCacher
438 }
439
440 func (o *Options) GetBlockCacheCapacity() int {
441 if o == nil || o.BlockCacheCapacity == 0 {
442 return DefaultBlockCacheCapacity
443 } else if o.BlockCacheCapacity < 0 {
444 return 0
445 }
446 return o.BlockCacheCapacity
447 }
448
449 func (o *Options) GetBlockCacheEvictRemoved() bool {
450 if o == nil {
451 return false
452 }
453 return o.BlockCacheEvictRemoved
454 }
455
456 func (o *Options) GetBlockRestartInterval() int {
457 if o == nil || o.BlockRestartInterval <= 0 {
458 return DefaultBlockRestartInterval
459 }
460 return o.BlockRestartInterval
461 }
462
463 func (o *Options) GetBlockSize() int {
464 if o == nil || o.BlockSize <= 0 {
465 return DefaultBlockSize
466 }
467 return o.BlockSize
468 }
469
470 func (o *Options) GetCompactionExpandLimit(level int) int {
471 factor := DefaultCompactionExpandLimitFactor
472 if o != nil && o.CompactionExpandLimitFactor > 0 {
473 factor = o.CompactionExpandLimitFactor
474 }
475 return o.GetCompactionTableSize(level+1) * factor
476 }
477
478 func (o *Options) GetCompactionGPOverlaps(level int) int {
479 factor := DefaultCompactionGPOverlapsFactor
480 if o != nil && o.CompactionGPOverlapsFactor > 0 {
481 factor = o.CompactionGPOverlapsFactor
482 }
483 return o.GetCompactionTableSize(level+2) * factor
484 }
485
486 func (o *Options) GetCompactionL0Trigger() int {
487 if o == nil || o.CompactionL0Trigger == 0 {
488 return DefaultCompactionL0Trigger
489 }
490 return o.CompactionL0Trigger
491 }
492
493 func (o *Options) GetCompactionSourceLimit(level int) int {
494 factor := DefaultCompactionSourceLimitFactor
495 if o != nil && o.CompactionSourceLimitFactor > 0 {
496 factor = o.CompactionSourceLimitFactor
497 }
498 return o.GetCompactionTableSize(level+1) * factor
499 }
500
501 func (o *Options) GetCompactionTableSize(level int) int {
502 var (
503 base = DefaultCompactionTableSize
504 mult float64
505 )
506 if o != nil {
507 if o.CompactionTableSize > 0 {
508 base = o.CompactionTableSize
509 }
510 if level < len(o.CompactionTableSizeMultiplierPerLevel) && o.CompactionTableSizeMultiplierPerLevel[level] > 0 {
511 mult = o.CompactionTableSizeMultiplierPerLevel[level]
512 } else if o.CompactionTableSizeMultiplier > 0 {
513 mult = math.Pow(o.CompactionTableSizeMultiplier, float64(level))
514 }
515 }
516 if mult == 0 {
517 mult = math.Pow(DefaultCompactionTableSizeMultiplier, float64(level))
518 }
519 return int(float64(base) * mult)
520 }
521
522 func (o *Options) GetCompactionTotalSize(level int) int64 {
523 var (
524 base = DefaultCompactionTotalSize
525 mult float64
526 )
527 if o != nil {
528 if o.CompactionTotalSize > 0 {
529 base = o.CompactionTotalSize
530 }
531 if level < len(o.CompactionTotalSizeMultiplierPerLevel) && o.CompactionTotalSizeMultiplierPerLevel[level] > 0 {
532 mult = o.CompactionTotalSizeMultiplierPerLevel[level]
533 } else if o.CompactionTotalSizeMultiplier > 0 {
534 mult = math.Pow(o.CompactionTotalSizeMultiplier, float64(level))
535 }
536 }
537 if mult == 0 {
538 mult = math.Pow(DefaultCompactionTotalSizeMultiplier, float64(level))
539 }
540 return int64(float64(base) * mult)
541 }
542
543 func (o *Options) GetComparer() comparer.Comparer {
544 if o == nil || o.Comparer == nil {
545 return comparer.DefaultComparer
546 }
547 return o.Comparer
548 }
549
550 func (o *Options) GetCompression() Compression {
551 if o == nil || o.Compression <= DefaultCompression || o.Compression >= nCompression {
552 return DefaultCompressionType
553 }
554 return o.Compression
555 }
556
557 func (o *Options) GetDisableBufferPool() bool {
558 if o == nil {
559 return false
560 }
561 return o.DisableBufferPool
562 }
563
564 func (o *Options) GetDisableBlockCache() bool {
565 if o == nil {
566 return false
567 }
568 return o.DisableBlockCache
569 }
570
571 func (o *Options) GetDisableCompactionBackoff() bool {
572 if o == nil {
573 return false
574 }
575 return o.DisableCompactionBackoff
576 }
577
578 func (o *Options) GetDisableLargeBatchTransaction() bool {
579 if o == nil {
580 return false
581 }
582 return o.DisableLargeBatchTransaction
583 }
584
585 func (o *Options) GetDisableSeeksCompaction() bool {
586 if o == nil {
587 return false
588 }
589 return o.DisableSeeksCompaction
590 }
591
592 func (o *Options) GetErrorIfExist() bool {
593 if o == nil {
594 return false
595 }
596 return o.ErrorIfExist
597 }
598
599 func (o *Options) GetErrorIfMissing() bool {
600 if o == nil {
601 return false
602 }
603 return o.ErrorIfMissing
604 }
605
606 func (o *Options) GetFilter() filter.Filter {
607 if o == nil {
608 return nil
609 }
610 return o.Filter
611 }
612
613 func (o *Options) GetIteratorSamplingRate() int {
614 if o == nil || o.IteratorSamplingRate == 0 {
615 return DefaultIteratorSamplingRate
616 } else if o.IteratorSamplingRate < 0 {
617 return 0
618 }
619 return o.IteratorSamplingRate
620 }
621
622 func (o *Options) GetNoSync() bool {
623 if o == nil {
624 return false
625 }
626 return o.NoSync
627 }
628
629 func (o *Options) GetNoWriteMerge() bool {
630 if o == nil {
631 return false
632 }
633 return o.NoWriteMerge
634 }
635
636 func (o *Options) GetOpenFilesCacher() Cacher {
637 if o == nil || o.OpenFilesCacher == nil {
638 return DefaultOpenFilesCacher
639 }
640 return o.OpenFilesCacher
641 }
642
643 func (o *Options) GetOpenFilesCacheCapacity() int {
644 if o == nil || o.OpenFilesCacheCapacity == 0 {
645 return DefaultOpenFilesCacheCapacity
646 } else if o.OpenFilesCacheCapacity < 0 {
647 return 0
648 }
649 return o.OpenFilesCacheCapacity
650 }
651
652 func (o *Options) GetReadOnly() bool {
653 if o == nil {
654 return false
655 }
656 return o.ReadOnly
657 }
658
659 func (o *Options) GetStrict(strict Strict) bool {
660 if o == nil || o.Strict == 0 {
661 return DefaultStrict&strict != 0
662 }
663 return o.Strict&strict != 0
664 }
665
666 func (o *Options) GetWriteBuffer() int {
667 if o == nil || o.WriteBuffer <= 0 {
668 return DefaultWriteBuffer
669 }
670 return o.WriteBuffer
671 }
672
673 func (o *Options) GetWriteL0PauseTrigger() int {
674 if o == nil || o.WriteL0PauseTrigger == 0 {
675 return DefaultWriteL0PauseTrigger
676 }
677 return o.WriteL0PauseTrigger
678 }
679
680 func (o *Options) GetWriteL0SlowdownTrigger() int {
681 if o == nil || o.WriteL0SlowdownTrigger == 0 {
682 return DefaultWriteL0SlowdownTrigger
683 }
684 return o.WriteL0SlowdownTrigger
685 }
686
687 func (o *Options) GetFilterBaseLg() int {
688 if o == nil || o.FilterBaseLg <= 0 {
689 return DefaultFilterBaseLg
690 }
691 return o.FilterBaseLg
692 }
693
694
695
696 type ReadOptions struct {
697
698
699
700
701
702 DontFillCache bool
703
704
705
706 Strict Strict
707 }
708
709 func (ro *ReadOptions) GetDontFillCache() bool {
710 if ro == nil {
711 return false
712 }
713 return ro.DontFillCache
714 }
715
716 func (ro *ReadOptions) GetStrict(strict Strict) bool {
717 if ro == nil {
718 return false
719 }
720 return ro.Strict&strict != 0
721 }
722
723
724
725 type WriteOptions struct {
726
727
728
729 NoWriteMerge bool
730
731
732
733
734
735
736
737
738
739
740
741
742
743 Sync bool
744 }
745
746 func (wo *WriteOptions) GetNoWriteMerge() bool {
747 if wo == nil {
748 return false
749 }
750 return wo.NoWriteMerge
751 }
752
753 func (wo *WriteOptions) GetSync() bool {
754 if wo == nil {
755 return false
756 }
757 return wo.Sync
758 }
759
760 func GetStrict(o *Options, ro *ReadOptions, strict Strict) bool {
761 if ro.GetStrict(StrictOverride) {
762 return ro.GetStrict(strict)
763 }
764 return o.GetStrict(strict) || ro.GetStrict(strict)
765 }
766
767 func (o *Options) GetMaxManifestFileSize() int64 {
768 if o == nil || o.MaxManifestFileSize <= 0 {
769 return DefaultMaxManifestFileSize
770 }
771 return o.MaxManifestFileSize
772 }
773
View as plain text