本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
2.9KB

  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. package leveldb
  7. import (
  8. "github.com/syndtr/goleveldb/leveldb/filter"
  9. "github.com/syndtr/goleveldb/leveldb/opt"
  10. )
  11. func dupOptions(o *opt.Options) *opt.Options {
  12. newo := &opt.Options{}
  13. if o != nil {
  14. *newo = *o
  15. }
  16. if newo.Strict == 0 {
  17. newo.Strict = opt.DefaultStrict
  18. }
  19. return newo
  20. }
  21. func (s *session) setOptions(o *opt.Options) {
  22. no := dupOptions(o)
  23. // Alternative filters.
  24. if filters := o.GetAltFilters(); len(filters) > 0 {
  25. no.AltFilters = make([]filter.Filter, len(filters))
  26. for i, filter := range filters {
  27. no.AltFilters[i] = &iFilter{filter}
  28. }
  29. }
  30. // Comparer.
  31. s.icmp = &iComparer{o.GetComparer()}
  32. no.Comparer = s.icmp
  33. // Filter.
  34. if filter := o.GetFilter(); filter != nil {
  35. no.Filter = &iFilter{filter}
  36. }
  37. s.o = &cachedOptions{Options: no}
  38. s.o.cache()
  39. }
  40. const optCachedLevel = 7
  41. type cachedOptions struct {
  42. *opt.Options
  43. compactionExpandLimit []int
  44. compactionGPOverlaps []int
  45. compactionSourceLimit []int
  46. compactionTableSize []int
  47. compactionTotalSize []int64
  48. }
  49. func (co *cachedOptions) cache() {
  50. co.compactionExpandLimit = make([]int, optCachedLevel)
  51. co.compactionGPOverlaps = make([]int, optCachedLevel)
  52. co.compactionSourceLimit = make([]int, optCachedLevel)
  53. co.compactionTableSize = make([]int, optCachedLevel)
  54. co.compactionTotalSize = make([]int64, optCachedLevel)
  55. for level := 0; level < optCachedLevel; level++ {
  56. co.compactionExpandLimit[level] = co.Options.GetCompactionExpandLimit(level)
  57. co.compactionGPOverlaps[level] = co.Options.GetCompactionGPOverlaps(level)
  58. co.compactionSourceLimit[level] = co.Options.GetCompactionSourceLimit(level)
  59. co.compactionTableSize[level] = co.Options.GetCompactionTableSize(level)
  60. co.compactionTotalSize[level] = co.Options.GetCompactionTotalSize(level)
  61. }
  62. }
  63. func (co *cachedOptions) GetCompactionExpandLimit(level int) int {
  64. if level < optCachedLevel {
  65. return co.compactionExpandLimit[level]
  66. }
  67. return co.Options.GetCompactionExpandLimit(level)
  68. }
  69. func (co *cachedOptions) GetCompactionGPOverlaps(level int) int {
  70. if level < optCachedLevel {
  71. return co.compactionGPOverlaps[level]
  72. }
  73. return co.Options.GetCompactionGPOverlaps(level)
  74. }
  75. func (co *cachedOptions) GetCompactionSourceLimit(level int) int {
  76. if level < optCachedLevel {
  77. return co.compactionSourceLimit[level]
  78. }
  79. return co.Options.GetCompactionSourceLimit(level)
  80. }
  81. func (co *cachedOptions) GetCompactionTableSize(level int) int {
  82. if level < optCachedLevel {
  83. return co.compactionTableSize[level]
  84. }
  85. return co.Options.GetCompactionTableSize(level)
  86. }
  87. func (co *cachedOptions) GetCompactionTotalSize(level int) int64 {
  88. if level < optCachedLevel {
  89. return co.compactionTotalSize[level]
  90. }
  91. return co.Options.GetCompactionTotalSize(level)
  92. }
上海开阖软件有限公司 沪ICP备12045867号-1