prev moves the cursor to the previous item in the bucket and returns its key and value. If the cursor is at the beginning of the bucket then a nil key and value are returned.
()
| 249 | // prev moves the cursor to the previous item in the bucket and returns its key and value. |
| 250 | // If the cursor is at the beginning of the bucket then a nil key and value are returned. |
| 251 | func (c *Cursor) prev() (key []byte, value []byte, flags uint32) { |
| 252 | // Attempt to move back one element until we're successful. |
| 253 | // Move up the stack as we hit the beginning of each page in our stack. |
| 254 | for i := len(c.stack) - 1; i >= 0; i-- { |
| 255 | elem := &c.stack[i] |
| 256 | if elem.index > 0 { |
| 257 | elem.index-- |
| 258 | break |
| 259 | } |
| 260 | // If we've hit the beginning, we should stop moving the cursor, |
| 261 | // and stay at the first element, so that users can continue to |
| 262 | // iterate over the elements in reverse direction by calling `Next`. |
| 263 | // We should return nil in such case. |
| 264 | // Refer to https://github.com/etcd-io/bbolt/issues/733 |
| 265 | if len(c.stack) == 1 { |
| 266 | c.first() |
| 267 | return nil, nil, 0 |
| 268 | } |
| 269 | c.stack = c.stack[:i] |
| 270 | } |
| 271 | |
| 272 | // If we've hit the end then return nil. |
| 273 | if len(c.stack) == 0 { |
| 274 | return nil, nil, 0 |
| 275 | } |
| 276 | |
| 277 | // Move down the stack to find the last element of the last leaf under this branch. |
| 278 | c.last() |
| 279 | return c.keyValue() |
| 280 | } |
| 281 | |
| 282 | // search recursively performs a binary search against a given page/node until it finds a given key. |
| 283 | func (c *Cursor) search(key []byte, pgId common.Pgid) { |