| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.mvstore; |
| 7 |
|
| 8 |
/** |
| 9 |
* A position in a cursor. |
| 10 |
* Instance represents a node in the linked list, which traces path |
| 11 |
* from a specific (target) key within a leaf node all the way up to te root |
| 12 |
* (bottom up path). |
| 13 |
*/ |
| 14 |
public final class CursorPos<K,V> { |
| 15 |
|
| 16 |
/** |
| 17 |
* The page at the current level. |
| 18 |
*/ |
| 19 |
public Page<K,V> page; |
| 20 |
|
| 21 |
/** |
| 22 |
* Index of the key (within page above) used to go down to a lower level |
| 23 |
* in case of intermediate nodes, or index of the target key for leaf a node. |
| 24 |
* In a later case, it could be negative, if the key is not present. |
| 25 |
*/ |
| 26 |
public int index; |
| 27 |
|
| 28 |
/** |
| 29 |
* Next node in the linked list, representing the position within parent level, |
| 30 |
* or null, if we are at the root level already. |
| 31 |
*/ |
| 32 |
public CursorPos<K,V> parent; |
| 33 |
|
| 34 |
|
| 35 |
public CursorPos(Page<K,V> page, int index, CursorPos<K,V> parent) { |
| 36 |
this.page = page; |
| 37 |
this.index = index; |
| 38 |
this.parent = parent; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Searches for a given key and creates a breadcrumb trail through a B-tree |
| 43 |
* rooted at a given Page. Resulting path starts at "insertion point" for a |
| 44 |
* given key and goes back to the root. |
| 45 |
* |
| 46 |
* @param <K> key type |
| 47 |
* @param <V> value type |
| 48 |
* |
| 49 |
* @param page root of the tree |
| 50 |
* @param key the key to search for |
| 51 |
* @return head of the CursorPos chain (insertion point) |
| 52 |
*/ |
| 53 |
static <K,V> CursorPos<K,V> traverseDown(Page<K,V> page, K key) { |
| 54 |
CursorPos<K,V> cursorPos = null; |
| 55 |
while (!page.isLeaf()) { |
| 56 |
int index = page.binarySearch(key) + 1; |
| 57 |
if (index < 0) { |
| 58 |
index = -index; |
| 59 |
} |
| 60 |
cursorPos = new CursorPos<>(page, index, cursorPos); |
| 61 |
page = page.getChildPage(index); |
| 62 |
} |
| 63 |
return new CursorPos<>(page, page.binarySearch(key), cursorPos); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Calculate the memory used by changes that are not yet stored. |
| 68 |
* |
| 69 |
* @param version the version |
| 70 |
* @return the amount of memory |
| 71 |
*/ |
| 72 |
int processRemovalInfo(long version) { |
| 73 |
int unsavedMemory = 0; |
| 74 |
for (CursorPos<K,V> head = this; head != null; head = head.parent) { |
| 75 |
unsavedMemory += head.page.removePage(version); |
| 76 |
} |
| 77 |
return unsavedMemory; |
| 78 |
} |
| 79 |
|
| 80 |
@Override |
| 81 |
public String toString() { |
| 82 |
return "CursorPos{" + |
| 83 |
"page=" + page + |
| 84 |
", index=" + index + |
| 85 |
", parent=" + parent + |
| 86 |
'}'; |
| 87 |
} |
| 88 |
} |
| 89 |
|