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 java.util.Iterator; |
9 | import org.h2.engine.Session; |
10 | import org.h2.message.DbException; |
11 | import org.h2.result.Row; |
12 | import org.h2.result.SearchRow; |
13 | |
14 | /** |
15 | * The cursor implementation for the scan index. |
16 | */ |
17 | public class ScanCursor implements Cursor { |
18 | private final ScanIndex scan; |
19 | private Row row; |
20 | private final Session session; |
21 | private final boolean multiVersion; |
22 | private Iterator<Row> delta; |
23 | |
24 | ScanCursor(Session session, ScanIndex scan, boolean multiVersion) { |
25 | this.session = session; |
26 | this.scan = scan; |
27 | this.multiVersion = multiVersion; |
28 | if (multiVersion) { |
29 | delta = scan.getDelta(); |
30 | } |
31 | row = null; |
32 | } |
33 | |
34 | @Override |
35 | public Row get() { |
36 | return row; |
37 | } |
38 | |
39 | @Override |
40 | public SearchRow getSearchRow() { |
41 | return row; |
42 | } |
43 | |
44 | @Override |
45 | public boolean next() { |
46 | if (multiVersion) { |
47 | while (true) { |
48 | if (delta != null) { |
49 | if (!delta.hasNext()) { |
50 | delta = null; |
51 | row = null; |
52 | continue; |
53 | } |
54 | row = delta.next(); |
55 | if (!row.isDeleted() || row.getSessionId() == session.getId()) { |
56 | continue; |
57 | } |
58 | } else { |
59 | row = scan.getNextRow(row); |
60 | if (row != null && row.getSessionId() != 0 && |
61 | row.getSessionId() != session.getId()) { |
62 | continue; |
63 | } |
64 | } |
65 | break; |
66 | } |
67 | return row != null; |
68 | } |
69 | row = scan.getNextRow(row); |
70 | return row != null; |
71 | } |
72 | |
73 | @Override |
74 | public boolean previous() { |
75 | throw DbException.throwInternalError(); |
76 | } |
77 | |
78 | } |