1 | /* |
2 | * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, |
3 | * and the EPL 1.0 (http://h2database.com/html/license.html). |
4 | * Initial Developer: H2 Group |
5 | */ |
6 | package org.h2.index; |
7 | |
8 | import org.h2.engine.Session; |
9 | import org.h2.result.Row; |
10 | import org.h2.result.SearchRow; |
11 | |
12 | /** |
13 | * The cursor implementation for the page b-tree index. |
14 | */ |
15 | public class PageBtreeCursor implements Cursor { |
16 | |
17 | private final Session session; |
18 | private final PageBtreeIndex index; |
19 | private final SearchRow last; |
20 | private PageBtreeLeaf current; |
21 | private int i; |
22 | private SearchRow currentSearchRow; |
23 | private Row currentRow; |
24 | |
25 | PageBtreeCursor(Session session, PageBtreeIndex index, SearchRow last) { |
26 | this.session = session; |
27 | this.index = index; |
28 | this.last = last; |
29 | } |
30 | |
31 | /** |
32 | * Set the position of the current row. |
33 | * |
34 | * @param current the leaf page |
35 | * @param i the index within the page |
36 | */ |
37 | void setCurrent(PageBtreeLeaf current, int i) { |
38 | this.current = current; |
39 | this.i = i; |
40 | } |
41 | |
42 | @Override |
43 | public Row get() { |
44 | if (currentRow == null && currentSearchRow != null) { |
45 | currentRow = index.getRow(session, currentSearchRow.getKey()); |
46 | } |
47 | return currentRow; |
48 | } |
49 | |
50 | @Override |
51 | public SearchRow getSearchRow() { |
52 | return currentSearchRow; |
53 | } |
54 | |
55 | @Override |
56 | public boolean next() { |
57 | if (current == null) { |
58 | return false; |
59 | } |
60 | if (i >= current.getEntryCount()) { |
61 | current.nextPage(this); |
62 | if (current == null) { |
63 | return false; |
64 | } |
65 | } |
66 | currentSearchRow = current.getRow(i); |
67 | currentRow = null; |
68 | if (last != null && index.compareRows(currentSearchRow, last) > 0) { |
69 | currentSearchRow = null; |
70 | return false; |
71 | } |
72 | i++; |
73 | return true; |
74 | } |
75 | |
76 | @Override |
77 | public boolean previous() { |
78 | if (current == null) { |
79 | return false; |
80 | } |
81 | if (i < 0) { |
82 | current.previousPage(this); |
83 | if (current == null) { |
84 | return false; |
85 | } |
86 | } |
87 | currentSearchRow = current.getRow(i); |
88 | currentRow = null; |
89 | i--; |
90 | return true; |
91 | } |
92 | |
93 | } |