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)
| 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. |
| 107 | func (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 |