Last moves the cursor to the last item in the bucket and returns its key and value. If the bucket is empty then a nil key and value are returned. The returned key and value are only valid for the life of the transaction.
()
| 64 | // If the bucket is empty then a nil key and value are returned. |
| 65 | // The returned key and value are only valid for the life of the transaction. |
| 66 | func (c *Cursor) Last() (key []byte, value []byte) { |
| 67 | common.Assert(c.bucket.tx.db != nil, "tx closed") |
| 68 | c.stack = c.stack[:0] |
| 69 | p, n := c.bucket.pageNode(c.bucket.RootPage()) |
| 70 | ref := elemRef{page: p, node: n} |
| 71 | ref.index = ref.count() - 1 |
| 72 | c.stack = append(c.stack, ref) |
| 73 | c.last() |
| 74 | |
| 75 | // If this is an empty page (calling Delete may result in empty pages) |
| 76 | // we call prev to find the last page that is not empty |
| 77 | for len(c.stack) > 1 && c.stack[len(c.stack)-1].count() == 0 { |
| 78 | c.prev() |
| 79 | } |
| 80 | |
| 81 | if len(c.stack) == 0 { |
| 82 | return nil, nil |
| 83 | } |
| 84 | |
| 85 | k, v, flags := c.keyValue() |
| 86 | if (flags & uint32(common.BucketLeafFlag)) != 0 { |
| 87 | return k, nil |
| 88 | } |
| 89 | return k, v |
| 90 | } |
| 91 | |
| 92 | // Next moves the cursor to the next item in the bucket and returns its key and value. |
| 93 | // If the cursor is at the end of the bucket then a nil key and value are returned. |