Debian Codegraph @

src/main/org/h2/index/VirtualTableCursor.java

raw
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.index;
7
8 import org.h2.message.DbException;
9 import org.h2.result.ResultInterface;
10 import org.h2.result.Row;
11 import org.h2.result.SearchRow;
12 import org.h2.value.Value;
13
14 /**
15 * A cursor for a virtual table. This implementation filters the rows (only
16 * returns entries that are larger or equal to "first", and smaller than last or
17 * equal to "last").
18 */
19 class VirtualTableCursor implements Cursor {
20
21 private final VirtualTableIndex index;
22
23 private final SearchRow first;
24
25 private final SearchRow last;
26
27 private final ResultInterface result;
28
29 Value[] values;
30
31 Row row;
32
33 /**
34 * @param index
35 * index
36 * @param first
37 * first row
38 * @param last
39 * last row
40 * @param result
41 * the result
42 */
43 VirtualTableCursor(VirtualTableIndex index, SearchRow first, SearchRow last,
44 ResultInterface result) {
45 this.index = index;
46 this.first = first;
47 this.last = last;
48 this.result = result;
49 }
50
51 @Override
52 public Row get() {
53 if (values == null) {
54 return null;
55 }
56 if (row == null) {
57 row = Row.get(values, 1);
58 }
59 return row;
60 }
61
62 @Override
63 public SearchRow getSearchRow() {
64 return get();
65 }
66
67 @Override
68 public boolean next() {
69 final SearchRow first = this.first, last = this.last;
70 if (first == null && last == null) {
71 return nextImpl();
72 }
73 while (nextImpl()) {
74 Row current = get();
75 if (first != null) {
76 int comp = index.compareRows(current, first);
77 if (comp < 0) {
78 continue;
79 }
80 }
81 if (last != null) {
82 int comp = index.compareRows(current, last);
83 if (comp > 0) {
84 continue;
85 }
86 }
87 return true;
88 }
89 return false;
90 }
91
92 /**
93 * Skip to the next row if one is available. This method does not filter.
94 *
95 * @return true if another row is available
96 */
97 private boolean nextImpl() {
98 row = null;
99 if (result != null && result.next()) {
100 values = result.currentRow();
101 } else {
102 values = null;
103 }
104 return values != null;
105 }
106
107 @Override
108 public boolean previous() {
109 throw DbException.getInternalError(toString());
110 }
111
112 }