util.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. "encoding/hex"
  19. "math/rand"
  20. "os"
  21. "time"
  22. "github.com/pkg/errors"
  23. "github.com/dgraph-io/badger/v4/table"
  24. "github.com/dgraph-io/badger/v4/y"
  25. )
  26. func (s *levelsController) validate() error {
  27. for _, l := range s.levels {
  28. if err := l.validate(); err != nil {
  29. return y.Wrap(err, "Levels Controller")
  30. }
  31. }
  32. return nil
  33. }
  34. // Check does some sanity check on one level of data or in-memory index.
  35. func (s *levelHandler) validate() error {
  36. if s.level == 0 {
  37. return nil
  38. }
  39. s.RLock()
  40. defer s.RUnlock()
  41. numTables := len(s.tables)
  42. for j := 1; j < numTables; j++ {
  43. if j >= len(s.tables) {
  44. return errors.Errorf("Level %d, j=%d numTables=%d", s.level, j, numTables)
  45. }
  46. if y.CompareKeys(s.tables[j-1].Biggest(), s.tables[j].Smallest()) >= 0 {
  47. return errors.Errorf(
  48. "Inter: Biggest(j-1)[%d] \n%s\n vs Smallest(j)[%d]: \n%s\n: "+
  49. "level=%d j=%d numTables=%d",
  50. s.tables[j-1].ID(), hex.Dump(s.tables[j-1].Biggest()), s.tables[j].ID(),
  51. hex.Dump(s.tables[j].Smallest()), s.level, j, numTables)
  52. }
  53. if y.CompareKeys(s.tables[j].Smallest(), s.tables[j].Biggest()) > 0 {
  54. return errors.Errorf(
  55. "Intra: \n%s\n vs \n%s\n: level=%d j=%d numTables=%d",
  56. hex.Dump(s.tables[j].Smallest()), hex.Dump(s.tables[j].Biggest()), s.level, j, numTables)
  57. }
  58. }
  59. return nil
  60. }
  61. // func (s *KV) debugPrintMore() { s.lc.debugPrintMore() }
  62. // // debugPrintMore shows key ranges of each level.
  63. // func (s *levelsController) debugPrintMore() {
  64. // s.Lock()
  65. // defer s.Unlock()
  66. // for i := 0; i < s.kv.opt.MaxLevels; i++ {
  67. // s.levels[i].debugPrintMore()
  68. // }
  69. // }
  70. // func (s *levelHandler) debugPrintMore() {
  71. // s.RLock()
  72. // defer s.RUnlock()
  73. // s.elog.Printf("Level %d:", s.level)
  74. // for _, t := range s.tables {
  75. // y.Printf(" [%s, %s]", t.Smallest(), t.Biggest())
  76. // }
  77. // y.Printf("\n")
  78. // }
  79. // reserveFileID reserves a unique file id.
  80. func (s *levelsController) reserveFileID() uint64 {
  81. id := s.nextFileID.Add(1)
  82. return id - 1
  83. }
  84. func getIDMap(dir string) map[uint64]struct{} {
  85. fileInfos, err := os.ReadDir(dir)
  86. y.Check(err)
  87. idMap := make(map[uint64]struct{})
  88. for _, info := range fileInfos {
  89. if info.IsDir() {
  90. continue
  91. }
  92. fileID, ok := table.ParseFileID(info.Name())
  93. if !ok {
  94. continue
  95. }
  96. idMap[fileID] = struct{}{}
  97. }
  98. return idMap
  99. }
  100. func init() {
  101. rand.Seed(time.Now().UnixNano())
  102. }