MCPcopy Create free account
hub / github.com/ByteStorage/FlyDB / LIndex

Method LIndex

structure/list.go:412–434  ·  view source on GitHub ↗

LIndex returns the value of an element in a list associated with a key based on the index. If the key does not exist, an error is returned. If the list is empty, an error is returned. Negative indices can be used, where -1 represents the last element of the list, -2 represents the second last elemen

(key string, index int)

Source from the content-addressed store, hash-verified

410// Negative indices can be used, where -1 represents the last element of the list,
411// -2 represents the second last element, and so on.
412func (l *ListStructure) LIndex(key string, index int) (interface{}, error) {
413 // Get the list
414 lst, _, err := l.getListFromDB(key, false)
415 if err != nil {
416 return nil, err
417 }
418
419 // Return error if the list is empty
420 if lst.Length == 0 {
421 return nil, ErrListEmpty
422 }
423
424 // Calculate the correct index
425 index = (index%lst.Length + lst.Length) % lst.Length
426
427 nowNode := lst.Head
428
429 for i := 0; i < index; i++ {
430 nowNode = nowNode.Next
431 }
432
433 return nowNode.Value, nil
434}
435
436// Keys returns all the keys of the list structure.
437func (l *ListStructure) Keys(regx string) ([]string, error) {

Callers

nothing calls this directly

Calls 1

getListFromDBMethod · 0.95

Tested by

no test coverage detected