splitIndex finds the position where a page will fill a given threshold. It returns the index as well as the size of the first page. This is only be called from split().
(threshold int)
| 269 | // It returns the index as well as the size of the first page. |
| 270 | // This is only be called from split(). |
| 271 | func (n *node) splitIndex(threshold int) (index, sz uintptr) { |
| 272 | sz = common.PageHeaderSize |
| 273 | |
| 274 | // Loop until we only have the minimum number of keys required for the second page. |
| 275 | for i := 0; i < len(n.inodes)-common.MinKeysPerPage; i++ { |
| 276 | index = uintptr(i) |
| 277 | inode := n.inodes[i] |
| 278 | elsize := n.pageElementSize() + uintptr(len(inode.Key())) + uintptr(len(inode.Value())) |
| 279 | |
| 280 | // If we have at least the minimum number of keys and adding another |
| 281 | // node would put us over the threshold then exit and return. |
| 282 | if index >= common.MinKeysPerPage && sz+elsize > uintptr(threshold) { |
| 283 | break |
| 284 | } |
| 285 | |
| 286 | // Add the element size to the total size. |
| 287 | sz += elsize |
| 288 | } |
| 289 | |
| 290 | return |
| 291 | } |
| 292 | |
| 293 | // spill writes the nodes to dirty pages and splits nodes as it goes. |
| 294 | // Returns an error if dirty pages cannot be allocated. |
no test coverage detected