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.message.DbException; |
9 | import org.h2.result.Row; |
10 | import org.h2.result.SearchRow; |
11 | import org.h2.value.Value; |
12 | import org.h2.value.ValueLong; |
13 | |
14 | /** |
15 | * The cursor implementation for the range index. |
16 | */ |
17 | class RangeCursor implements Cursor { |
18 | |
19 | private boolean beforeFirst; |
20 | private long current; |
21 | private Row currentRow; |
22 | private final long start, end, step; |
23 | |
24 | RangeCursor(long start, long end) { |
25 | this(start, end, 1); |
26 | } |
27 | |
28 | RangeCursor(long start, long end, long step) { |
29 | this.start = start; |
30 | this.end = end; |
31 | this.step = step; |
32 | beforeFirst = true; |
33 | } |
34 | |
35 | @Override |
36 | public Row get() { |
37 | return currentRow; |
38 | } |
39 | |
40 | @Override |
41 | public SearchRow getSearchRow() { |
42 | return currentRow; |
43 | } |
44 | |
45 | @Override |
46 | public boolean next() { |
47 | if (beforeFirst) { |
48 | beforeFirst = false; |
49 | current = start; |
50 | } else { |
51 | current += step; |
52 | } |
53 | currentRow = new Row(new Value[]{ValueLong.get(current)}, 1); |
54 | return step > 0 ? current <= end : current >= end; |
55 | } |
56 | |
57 | @Override |
58 | public boolean previous() { |
59 | throw DbException.throwInternalError(); |
60 | } |
61 | |
62 | } |