MCPcopy Create free account
hub / github.com/nutsdb/nutsdb / GetRemoveIndexes

Method GetRemoveIndexes

internal/data/list.go:307–358  ·  view source on GitHub ↗

GetRemoveIndexes returns a slice of indices to be removed from the list based on the count

(key string, count int, cmp func(r *core.Record) (bool, error))

Source from the content-addressed store, hash-verified

305
306// GetRemoveIndexes returns a slice of indices to be removed from the list based on the count
307func (l *List) GetRemoveIndexes(key string, count int, cmp func(r *core.Record) (bool, error)) ([][]byte, error) {
308 if l.IsExpire(key) {
309 return nil, ErrListNotFound
310 }
311
312 list, ok := l.Items[key]
313
314 if !ok {
315 return nil, ErrListNotFound
316 }
317
318 var res [][]byte
319 var allItems []*core.Item[core.Record]
320 if count == 0 {
321 count = list.Count()
322 }
323
324 allItems = l.Items[key].AllItems()
325 if count > 0 {
326 for _, item := range allItems {
327 if count <= 0 {
328 break
329 }
330 r := item.Record
331 ok, err := cmp(r)
332 if err != nil {
333 return nil, err
334 }
335 if ok {
336 res = append(res, item.Key)
337 count--
338 }
339 }
340 } else {
341 for i := len(allItems) - 1; i >= 0; i-- {
342 if count >= 0 {
343 break
344 }
345 r := allItems[i].Record
346 ok, err := cmp(r)
347 if err != nil {
348 return nil, err
349 }
350 if ok {
351 res = append(res, allItems[i].Key)
352 count++
353 }
354 }
355 }
356
357 return res, nil
358}
359
360// LRem removes the first count occurrences of elements equal to value from the list stored at key.
361// The count argument influences the operation in the following ways:

Callers 2

LRemMethod · 0.95

Calls 4

IsExpireMethod · 0.95
appendFunction · 0.85
CountMethod · 0.65
AllItemsMethod · 0.65

Tested by

no test coverage detected