smallindex.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright (C) 2018 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package olddb
  7. import (
  8. "encoding/binary"
  9. "slices"
  10. "sync"
  11. "github.com/syncthing/syncthing/internal/db/olddb/backend"
  12. )
  13. // A smallIndex is an in memory bidirectional []byte to uint32 map. It gives
  14. // fast lookups in both directions and persists to the database. Don't use for
  15. // storing more items than fit comfortably in RAM.
  16. type smallIndex struct {
  17. db backend.Backend
  18. prefix []byte
  19. id2val map[uint32]string
  20. val2id map[string]uint32
  21. nextID uint32
  22. mut sync.Mutex
  23. }
  24. func newSmallIndex(db backend.Backend, prefix []byte) *smallIndex {
  25. idx := &smallIndex{
  26. db: db,
  27. prefix: prefix,
  28. id2val: make(map[uint32]string),
  29. val2id: make(map[string]uint32),
  30. }
  31. idx.load()
  32. return idx
  33. }
  34. // load iterates over the prefix space in the database and populates the in
  35. // memory maps.
  36. func (i *smallIndex) load() {
  37. it, err := i.db.NewPrefixIterator(i.prefix)
  38. if err != nil {
  39. panic("loading small index: " + err.Error())
  40. }
  41. defer it.Release()
  42. for it.Next() {
  43. val := string(it.Value())
  44. id := binary.BigEndian.Uint32(it.Key()[len(i.prefix):])
  45. if val != "" {
  46. // Empty value means the entry has been deleted.
  47. i.id2val[id] = val
  48. i.val2id[val] = id
  49. }
  50. if id >= i.nextID {
  51. i.nextID = id + 1
  52. }
  53. }
  54. }
  55. // ID returns the index number for the given byte slice, allocating a new one
  56. // and persisting this to the database if necessary.
  57. func (i *smallIndex) ID(val []byte) (uint32, error) {
  58. i.mut.Lock()
  59. // intentionally avoiding defer here as we want this call to be as fast as
  60. // possible in the general case (folder ID already exists). The map lookup
  61. // with the conversion of []byte to string is compiler optimized to not
  62. // copy the []byte, which is why we don't assign it to a temp variable
  63. // here.
  64. if id, ok := i.val2id[string(val)]; ok {
  65. i.mut.Unlock()
  66. return id, nil
  67. }
  68. panic("missing ID")
  69. }
  70. // Val returns the value for the given index number, or (nil, false) if there
  71. // is no such index number.
  72. func (i *smallIndex) Val(id uint32) ([]byte, bool) {
  73. i.mut.Lock()
  74. val, ok := i.id2val[id]
  75. i.mut.Unlock()
  76. if !ok {
  77. return nil, false
  78. }
  79. return []byte(val), true
  80. }
  81. // Values returns the set of values in the index
  82. func (i *smallIndex) Values() []string {
  83. // In principle this method should return [][]byte because all the other
  84. // methods deal in []byte keys. However, in practice, where it's used
  85. // wants a []string and it's easier to just create that here rather than
  86. // having to convert both here and there...
  87. i.mut.Lock()
  88. vals := make([]string, 0, len(i.val2id))
  89. for val := range i.val2id {
  90. vals = append(vals, val)
  91. }
  92. i.mut.Unlock()
  93. slices.Sort(vals)
  94. return vals
  95. }