MCPcopy
hub / github.com/dgraph-io/badger / replaceTables

Method replaceTables

level_handler.go:107–141  ·  view source on GitHub ↗

replaceTables will replace tables[left:right] with newTables. Note this EXCLUDES tables[right]. You must call decr() to delete the old tables _after_ writing the update to the manifest.

(toDel, toAdd []*table.Table)

Source from the content-addressed store, hash-verified

105// replaceTables will replace tables[left:right] with newTables. Note this EXCLUDES tables[right].
106// You must call decr() to delete the old tables _after_ writing the update to the manifest.
107func (s *levelHandler) replaceTables(toDel, toAdd []*table.Table) error {
108 // Need to re-search the range of tables in this level to be replaced as other goroutines might
109 // be changing it as well. (They can't touch our tables, but if they add/remove other tables,
110 // the indices get shifted around.)
111 s.Lock() // We s.Unlock() below.
112
113 toDelMap := make(map[uint64]struct{})
114 for _, t := range toDel {
115 toDelMap[t.ID()] = struct{}{}
116 }
117 var newTables []*table.Table
118 for _, t := range s.tables {
119 _, found := toDelMap[t.ID()]
120 if !found {
121 newTables = append(newTables, t)
122 continue
123 }
124 s.totalSize -= t.Size()
125 }
126
127 // Increase totalSize first.
128 for _, t := range toAdd {
129 s.totalSize += t.Size()
130 t.IncrRef()
131 newTables = append(newTables, t)
132 }
133
134 // Assign tables.
135 s.tables = newTables
136 sort.Slice(s.tables, func(i, j int) bool {
137 return y.CompareKeys(s.tables[i].Smallest(), s.tables[j].Smallest()) < 0
138 })
139 s.Unlock() // s.Unlock before we DecrRef tables -- that can be slow.
140 return decrRefs(toDel)
141}
142
143// addTable adds toAdd table to levelHandler. Normally when we add tables to levelHandler, we sort
144// tables based on table.Smallest. This is required for correctness of the system. But in case of

Callers 2

runCompactDefMethod · 0.80

Calls 6

CompareKeysFunction · 0.92
decrRefsFunction · 0.85
IDMethod · 0.80
SmallestMethod · 0.65
SizeMethod · 0.45
IncrRefMethod · 0.45

Tested by 1