| 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 |
import java.util.Iterator; |
| 9 |
import java.util.NoSuchElementException; |
| 10 |
|
| 11 |
/** |
| 12 |
* A cursor to iterate over elements in ascending or descending order. |
| 13 |
* |
| 14 |
* @param <K> the key type |
| 15 |
* @param <V> the value type |
| 16 |
*/ |
| 17 |
public final class Cursor<K,V> implements Iterator<K> { |
| 18 |
private final boolean reverse; |
| 19 |
private final K to; |
| 20 |
private CursorPos<K,V> cursorPos; |
| 21 |
private CursorPos<K,V> keeper; |
| 22 |
private K current; |
| 23 |
private K last; |
| 24 |
private V lastValue; |
| 25 |
private Page<K,V> lastPage; |
| 26 |
|
| 27 |
|
| 28 |
public Cursor(RootReference<K,V> rootReference, K from, K to) { |
| 29 |
this(rootReference, from, to, false); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* @param rootReference of the tree |
| 34 |
* @param from starting key (inclusive), if null start from the first / last key |
| 35 |
* @param to ending key (inclusive), if null there is no boundary |
| 36 |
* @param reverse true if tree should be iterated in key's descending order |
| 37 |
*/ |
| 38 |
public Cursor(RootReference<K,V> rootReference, K from, K to, boolean reverse) { |
| 39 |
this.lastPage = rootReference.root; |
| 40 |
this.cursorPos = traverseDown(lastPage, from, reverse); |
| 41 |
this.to = to; |
| 42 |
this.reverse = reverse; |
| 43 |
} |
| 44 |
|
| 45 |
@Override |
| 46 |
public boolean hasNext() { |
| 47 |
if (cursorPos != null) { |
| 48 |
int increment = reverse ? -1 : 1; |
| 49 |
while (current == null) { |
| 50 |
Page<K,V> page = cursorPos.page; |
| 51 |
int index = cursorPos.index; |
| 52 |
if (reverse ? index < 0 : index >= upperBound(page)) { |
| 53 |
// traversal of this page is over, going up a level or stop if at the root already |
| 54 |
CursorPos<K,V> tmp = cursorPos; |
| 55 |
cursorPos = cursorPos.parent; |
| 56 |
if (cursorPos == null) { |
| 57 |
return false; |
| 58 |
} |
| 59 |
tmp.parent = keeper; |
| 60 |
keeper = tmp; |
| 61 |
} else { |
| 62 |
// traverse down to the leaf taking the leftmost path |
| 63 |
while (!page.isLeaf()) { |
| 64 |
page = page.getChildPage(index); |
| 65 |
index = reverse ? upperBound(page) - 1 : 0; |
| 66 |
if (keeper == null) { |
| 67 |
cursorPos = new CursorPos<>(page, index, cursorPos); |
| 68 |
} else { |
| 69 |
CursorPos<K,V> tmp = keeper; |
| 70 |
keeper = keeper.parent; |
| 71 |
tmp.parent = cursorPos; |
| 72 |
tmp.page = page; |
| 73 |
tmp.index = index; |
| 74 |
cursorPos = tmp; |
| 75 |
} |
| 76 |
} |
| 77 |
if (reverse ? index >= 0 : index < page.getKeyCount()) { |
| 78 |
K key = page.getKey(index); |
| 79 |
if (to != null && Integer.signum(page.map.getKeyType().compare(key, to)) == increment) { |
| 80 |
return false; |
| 81 |
} |
| 82 |
current = last = key; |
| 83 |
lastValue = page.getValue(index); |
| 84 |
lastPage = page; |
| 85 |
} |
| 86 |
} |
| 87 |
cursorPos.index += increment; |
| 88 |
} |
| 89 |
} |
| 90 |
return current != null; |
| 91 |
} |
| 92 |
|
| 93 |
@Override |
| 94 |
public K next() { |
| 95 |
if(!hasNext()) { |
| 96 |
throw new NoSuchElementException(); |
| 97 |
} |
| 98 |
current = null; |
| 99 |
return last; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Get the last read key if there was one. |
| 104 |
* |
| 105 |
* @return the key or null |
| 106 |
*/ |
| 107 |
public K getKey() { |
| 108 |
return last; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Get the last read value if there was one. |
| 113 |
* |
| 114 |
* @return the value or null |
| 115 |
*/ |
| 116 |
public V getValue() { |
| 117 |
return lastValue; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Get the page where last retrieved key is located. |
| 122 |
* |
| 123 |
* @return the page |
| 124 |
*/ |
| 125 |
@SuppressWarnings("unused") |
| 126 |
Page<K,V> getPage() { |
| 127 |
return lastPage; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Skip over that many entries. This method is relatively fast (for this map |
| 132 |
* implementation) even if many entries need to be skipped. |
| 133 |
* |
| 134 |
* @param n the number of entries to skip |
| 135 |
*/ |
| 136 |
public void skip(long n) { |
| 137 |
if (n < 10) { |
| 138 |
while (n-- > 0 && hasNext()) { |
| 139 |
next(); |
| 140 |
} |
| 141 |
} else if(hasNext()) { |
| 142 |
assert cursorPos != null; |
| 143 |
CursorPos<K,V> cp = cursorPos; |
| 144 |
CursorPos<K,V> parent; |
| 145 |
while ((parent = cp.parent) != null) cp = parent; |
| 146 |
Page<K,V> root = cp.page; |
| 147 |
MVMap<K,V> map = root.map; |
| 148 |
long index = map.getKeyIndex(next()); |
| 149 |
last = map.getKey(index + (reverse ? -n : n)); |
| 150 |
this.cursorPos = traverseDown(root, last, reverse); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Fetch the next entry that is equal or larger than the given key, starting |
| 156 |
* from the given page. This method returns the path. |
| 157 |
* |
| 158 |
* @param <K> key type |
| 159 |
* @param <V> value type |
| 160 |
* |
| 161 |
* @param page to start from as a root |
| 162 |
* @param key to search for, null means search for the first available key |
| 163 |
* @param reverse true if traversal is in reverse direction, false otherwise |
| 164 |
* @return CursorPos representing path from the entry found, |
| 165 |
* or from insertion point if not, |
| 166 |
* all the way up to to the root page provided |
| 167 |
*/ |
| 168 |
static <K,V> CursorPos<K,V> traverseDown(Page<K,V> page, K key, boolean reverse) { |
| 169 |
CursorPos<K,V> cursorPos = key != null ? CursorPos.traverseDown(page, key) : |
| 170 |
reverse ? page.getAppendCursorPos(null) : page.getPrependCursorPos(null); |
| 171 |
int index = cursorPos.index; |
| 172 |
if (index < 0) { |
| 173 |
index = ~index; |
| 174 |
if (reverse) { |
| 175 |
--index; |
| 176 |
} |
| 177 |
cursorPos.index = index; |
| 178 |
} |
| 179 |
return cursorPos; |
| 180 |
} |
| 181 |
|
| 182 |
private static <K,V> int upperBound(Page<K,V> page) { |
| 183 |
return page.isLeaf() ? page.getKeyCount() : page.map.getChildPageCount(page); |
| 184 |
} |
| 185 |
} |