util.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright 2017 Dgraph Labs, Inc. and Contributors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package badger
  17. import (
  18. "io/ioutil"
  19. "math/rand"
  20. "sync/atomic"
  21. "time"
  22. "github.com/dgraph-io/badger/table"
  23. "github.com/dgraph-io/badger/y"
  24. "github.com/pkg/errors"
  25. )
  26. // summary is produced when DB is closed. Currently it is used only for testing.
  27. type summary struct {
  28. fileIDs map[uint64]bool
  29. }
  30. func (s *levelsController) getSummary() *summary {
  31. out := &summary{
  32. fileIDs: make(map[uint64]bool),
  33. }
  34. for _, l := range s.levels {
  35. l.getSummary(out)
  36. }
  37. return out
  38. }
  39. func (s *levelHandler) getSummary(sum *summary) {
  40. s.RLock()
  41. defer s.RUnlock()
  42. for _, t := range s.tables {
  43. sum.fileIDs[t.ID()] = true
  44. }
  45. }
  46. func (s *DB) validate() error { return s.lc.validate() }
  47. func (s *levelsController) validate() error {
  48. for _, l := range s.levels {
  49. if err := l.validate(); err != nil {
  50. return errors.Wrap(err, "Levels Controller")
  51. }
  52. }
  53. return nil
  54. }
  55. // Check does some sanity check on one level of data or in-memory index.
  56. func (s *levelHandler) validate() error {
  57. if s.level == 0 {
  58. return nil
  59. }
  60. s.RLock()
  61. defer s.RUnlock()
  62. numTables := len(s.tables)
  63. for j := 1; j < numTables; j++ {
  64. if j >= len(s.tables) {
  65. return errors.Errorf("Level %d, j=%d numTables=%d", s.level, j, numTables)
  66. }
  67. if y.CompareKeys(s.tables[j-1].Biggest(), s.tables[j].Smallest()) >= 0 {
  68. return errors.Errorf(
  69. "Inter: %q vs %q: level=%d j=%d numTables=%d",
  70. string(s.tables[j-1].Biggest()), string(s.tables[j].Smallest()), s.level, j, numTables)
  71. }
  72. if y.CompareKeys(s.tables[j].Smallest(), s.tables[j].Biggest()) > 0 {
  73. return errors.Errorf(
  74. "Intra: %q vs %q: level=%d j=%d numTables=%d",
  75. s.tables[j].Smallest(), s.tables[j].Biggest(), s.level, j, numTables)
  76. }
  77. }
  78. return nil
  79. }
  80. // func (s *KV) debugPrintMore() { s.lc.debugPrintMore() }
  81. // // debugPrintMore shows key ranges of each level.
  82. // func (s *levelsController) debugPrintMore() {
  83. // s.Lock()
  84. // defer s.Unlock()
  85. // for i := 0; i < s.kv.opt.MaxLevels; i++ {
  86. // s.levels[i].debugPrintMore()
  87. // }
  88. // }
  89. // func (s *levelHandler) debugPrintMore() {
  90. // s.RLock()
  91. // defer s.RUnlock()
  92. // s.elog.Printf("Level %d:", s.level)
  93. // for _, t := range s.tables {
  94. // y.Printf(" [%s, %s]", t.Smallest(), t.Biggest())
  95. // }
  96. // y.Printf("\n")
  97. // }
  98. // reserveFileID reserves a unique file id.
  99. func (s *levelsController) reserveFileID() uint64 {
  100. id := atomic.AddUint64(&s.nextFileID, 1)
  101. return id - 1
  102. }
  103. func getIDMap(dir string) map[uint64]struct{} {
  104. fileInfos, err := ioutil.ReadDir(dir)
  105. y.Check(err)
  106. idMap := make(map[uint64]struct{})
  107. for _, info := range fileInfos {
  108. if info.IsDir() {
  109. continue
  110. }
  111. fileID, ok := table.ParseFileID(info.Name())
  112. if !ok {
  113. continue
  114. }
  115. idMap[fileID] = struct{}{}
  116. }
  117. return idMap
  118. }
  119. func init() {
  120. rand.Seed(time.Now().UnixNano())
  121. }