01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (http://h2database.com/html/license.html).
04: * Initial Developer: H2 Group
05: */
06: package org.h2.index;
07:
08: import java.sql.SQLException;
09:
10: import org.h2.result.Row;
11: import org.h2.result.SearchRow;
12:
13: /**
14: * The cursor implementation for a tree index.
15: */
16: public class TreeCursor implements Cursor {
17: private TreeIndex tree;
18: private TreeNode node;
19: private boolean beforeFirst;
20: private SearchRow first, last;
21:
22: TreeCursor(TreeIndex tree, TreeNode node, SearchRow first,
23: SearchRow last) {
24: this .tree = tree;
25: this .node = node;
26: this .first = first;
27: this .last = last;
28: beforeFirst = true;
29: }
30:
31: public Row get() {
32: return node == null ? null : node.row;
33: }
34:
35: public SearchRow getSearchRow() {
36: return get();
37: }
38:
39: public int getPos() {
40: return node.row.getPos();
41: }
42:
43: public boolean next() throws SQLException {
44: if (beforeFirst) {
45: beforeFirst = false;
46: if (node == null) {
47: return false;
48: }
49: if (first != null && tree.compareRows(node.row, first) < 0) {
50: node = tree.next(node);
51: }
52: } else {
53: node = tree.next(node);
54: }
55: if (node != null && last != null) {
56: if (tree.compareRows(node.row, last) > 0) {
57: node = null;
58: }
59: }
60: return node != null;
61: }
62:
63: }
|