MCPcopy Create free account
hub / github.com/distributedio/titan / LIndex

Function LIndex

command/lists.go:167–193  ·  view source on GitHub ↗

LIndex get an element from a list by its index

(ctx *Context, txn *db.Transaction)

Source from the content-addressed store, hash-verified

165
166//LIndex get an element from a list by its index
167func LIndex(ctx *Context, txn *db.Transaction) (OnCommit, error) {
168 key := []byte(ctx.Args[0])
169 n, err := strconv.ParseInt(string(ctx.Args[1]), 10, 64)
170 if err != nil {
171 return nil, ErrInteger
172 }
173
174 lst, err := txn.List(key)
175 if err != nil {
176 if err == db.ErrTypeMismatch {
177 return nil, ErrTypeMismatch
178 }
179 return nil, errors.New("ERR " + err.Error())
180 }
181 if !lst.Exist() {
182 return NullBulkString(ctx.Out), nil
183 }
184 val, err := lst.Index(n)
185 if err != nil {
186 if err == db.ErrOutOfRange {
187 return NullBulkString(ctx.Out), nil
188 }
189 return nil, errors.New("ERR " + err.Error())
190
191 }
192 return BulkString(ctx.Out, string(val)), nil
193}
194
195//LLen get the length of a list
196func LLen(ctx *Context, txn *db.Transaction) (OnCommit, error) {

Callers

nothing calls this directly

Calls 6

NullBulkStringFunction · 0.85
BulkStringFunction · 0.85
ListMethod · 0.80
ErrorMethod · 0.65
ExistMethod · 0.65
IndexMethod · 0.65

Tested by

no test coverage detected