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.constant.SysProperties;
11: import org.h2.engine.Session;
12: import org.h2.message.Message;
13: import org.h2.result.Row;
14: import org.h2.result.SearchRow;
15:
16: /**
17: * The cursor implementation for the b tree index.
18: */
19: public class BtreeCursor implements Cursor {
20: private BtreeIndex index;
21: private BtreePosition top;
22: private SearchRow currentSearchRow;
23: private Row currentRow;
24: private boolean first;
25: private SearchRow last;
26: private Session session;
27:
28: BtreeCursor(Session session, BtreeIndex index, SearchRow last) {
29: this .session = session;
30: this .index = index;
31: this .last = last;
32: first = true;
33: }
34:
35: Session getSession() {
36: return session;
37: }
38:
39: void setStackPosition(int position) {
40: top.position = position;
41: }
42:
43: void push(BtreePage page, int position) {
44: if (SysProperties.CHECK && (top != null && top.page == page)) {
45: throw Message.getInternalError();
46: }
47: top = new BtreePosition(page, position, top);
48: }
49:
50: BtreePosition pop() {
51: BtreePosition t = top;
52: if (t == null) {
53: return null;
54: }
55: top = top.next;
56: return t;
57: }
58:
59: void setCurrentRow(SearchRow searchRow) throws SQLException {
60: this .currentSearchRow = searchRow;
61: currentRow = null;
62: }
63:
64: public Row get() throws SQLException {
65: if (currentRow == null && currentSearchRow != null) {
66: currentRow = index.getRow(session, currentSearchRow
67: .getPos());
68: }
69: return currentRow;
70: }
71:
72: public SearchRow getSearchRow() throws SQLException {
73: return currentSearchRow;
74: }
75:
76: public int getPos() {
77: return currentSearchRow.getPos();
78: }
79:
80: public boolean next() throws SQLException {
81: if (first) {
82: first = false;
83: } else {
84: top.page.next(this , top.position);
85: if (currentSearchRow != null && last != null) {
86: if (index.compareRows(currentSearchRow, last) > 0) {
87: currentSearchRow = null;
88: currentRow = null;
89: }
90: }
91: }
92: return currentSearchRow != null;
93: }
94:
95: }
|