MCPcopy Create free account
hub / github.com/apache/kvrocks-controller / AddSlotToSlotRanges

Function AddSlotToSlotRanges

store/slot.go:259–282  ·  view source on GitHub ↗

Implemented following leetcode solution: https://leetcode.com/problems/merge-intervals/solutions/1805268/go-clean-code-with-explanation-and-visual-10ms-100

(source SlotRanges, slot SlotRange)

Source from the content-addressed store, hash-verified

257// Implemented following leetcode solution:
258// https://leetcode.com/problems/merge-intervals/solutions/1805268/go-clean-code-with-explanation-and-visual-10ms-100
259func AddSlotToSlotRanges(source SlotRanges, slot SlotRange) SlotRanges {
260 if len(source) == 0 {
261 return append(source, slot)
262 }
263 source = append(source, slot)
264 sort.Slice(source, func(i, j int) bool {
265 return source[i].Start < source[j].Start
266 })
267
268 mergedSlotRanges := make([]SlotRange, 0, len(source))
269 mergedSlotRanges = append(mergedSlotRanges, source[0])
270
271 for _, interval := range source[1:] {
272 lastIntervalPos := len(mergedSlotRanges) - 1
273 lastInterval := mergedSlotRanges[lastIntervalPos]
274 if CanMerge(lastInterval, interval) {
275 mergedSlotRanges[lastIntervalPos] = MergeSlotRanges(interval, lastInterval)
276 } else {
277 mergedSlotRanges = append(mergedSlotRanges, interval)
278 }
279 }
280
281 return mergedSlotRanges
282}
283
284func RemoveSlotFromSlotRanges(source SlotRanges, slot SlotRange) SlotRanges {
285 sort.Slice(source, func(i, j int) bool {

Callers 4

TestClusterMigrateDataFunction · 0.92
TestAddSlotToSlotRangesFunction · 0.85
MigrateSlotMethod · 0.85

Calls 2

CanMergeFunction · 0.85
MergeSlotRangesFunction · 0.85

Tested by 2

TestClusterMigrateDataFunction · 0.74
TestAddSlotToSlotRangesFunction · 0.68