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))
| 305 | |
| 306 | // GetRemoveIndexes returns a slice of indices to be removed from the list based on the count |
| 307 | func (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: |