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

Method RPop

structure/list.go:195–222  ·  view source on GitHub ↗

RPop returns and removes the rightmost value of a list associated with a key. If the key does not exist, an error is returned. If the list is empty, an error is returned.

(key string)

Source from the content-addressed store, hash-verified

193// If the key does not exist, an error is returned.
194// If the list is empty, an error is returned.
195func (l *ListStructure) RPop(key string) (interface{}, error) {
196 // Get the list
197 lst, _, err := l.getListFromDB(key, false)
198 if err != nil {
199 return nil, err
200 }
201 // Return error if the list is empty
202 if lst.Length == 0 {
203 return nil, ErrListEmpty
204 } else if lst.Length == 1 {
205 popValue := lst.Head.Value
206 lst.Head = nil
207 lst.Length = 0
208 return popValue, l.setListToDB(key, lst, 0)
209 }
210
211 // Find the new tail
212 newTail := lst.Head
213 for i := 0; i < lst.Length-2; i++ {
214 newTail = newTail.Next
215 }
216 popValue := newTail.Next.Value
217 newTail.Next = nil
218 lst.Length--
219
220 // Store in the database
221 return popValue, l.setListToDB(key, lst, 0)
222}
223
224// LRange returns a range of elements from a list associated with a key.
225// The range is inclusive, including both the start and stop indices.

Callers

nothing calls this directly

Calls 2

getListFromDBMethod · 0.95
setListToDBMethod · 0.95

Tested by

no test coverage detected