001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (http://h2database.com/html/license.html).
004: * Initial Developer: H2 Group
005: */
006: package org.h2.index;
007:
008: import java.sql.SQLException;
009: import org.h2.constant.SysProperties;
010: import org.h2.engine.Session;
011: import org.h2.message.Message;
012: import org.h2.result.Row;
013: import org.h2.result.SearchRow;
014:
015: /**
016: * The cursor implementation for the multi-version index.
017: */
018: public class MultiVersionCursor implements Cursor {
019:
020: private final MultiVersionIndex index;
021: private final Session session;
022: private final Cursor baseCursor, deltaCursor;
023: private final Object sync;
024: private SearchRow baseRow;
025: private Row deltaRow;
026: private boolean onBase;
027: private boolean end;
028: private boolean needNewDelta, needNewBase;
029:
030: MultiVersionCursor(Session session, MultiVersionIndex index,
031: Cursor base, Cursor delta, Object sync) throws SQLException {
032: this .session = session;
033: this .index = index;
034: this .baseCursor = base;
035: this .deltaCursor = delta;
036: this .sync = sync;
037: needNewDelta = needNewBase = true;
038: }
039:
040: private void loadNext(boolean base) throws SQLException {
041: synchronized (sync) {
042: if (base) {
043: if (baseCursor.next()) {
044: baseRow = baseCursor.getSearchRow();
045: } else {
046: baseRow = null;
047: }
048: } else {
049: if (deltaCursor.next()) {
050: deltaRow = deltaCursor.get();
051: } else {
052: deltaRow = null;
053: }
054: }
055: }
056: }
057:
058: public Row get() throws SQLException {
059: synchronized (sync) {
060: if (SysProperties.CHECK && end) {
061: throw Message.getInternalError();
062: }
063: return onBase ? baseCursor.get() : deltaCursor.get();
064: }
065: }
066:
067: public int getPos() {
068: synchronized (sync) {
069: if (SysProperties.CHECK && end) {
070: throw Message.getInternalError();
071: }
072: return onBase ? baseCursor.getPos() : deltaCursor.getPos();
073: }
074: }
075:
076: public SearchRow getSearchRow() throws SQLException {
077: synchronized (sync) {
078: if (SysProperties.CHECK && end) {
079: throw Message.getInternalError();
080: }
081: return onBase ? baseCursor.getSearchRow() : deltaCursor
082: .getSearchRow();
083: }
084: }
085:
086: public boolean next() throws SQLException {
087: synchronized (sync) {
088: if (SysProperties.CHECK && end) {
089: throw Message.getInternalError();
090: }
091: while (true) {
092: if (needNewDelta) {
093: loadNext(false);
094: needNewDelta = false;
095: }
096: if (needNewBase) {
097: loadNext(true);
098: needNewBase = false;
099: }
100: if (deltaRow == null) {
101: if (baseRow == null) {
102: end = true;
103: return false;
104: } else {
105: onBase = true;
106: needNewBase = true;
107: return true;
108: }
109: }
110: int sessionId = deltaRow.getSessionId();
111: boolean isThisSession = sessionId == session.getId();
112: boolean isDeleted = deltaRow.getDeleted();
113: if (isThisSession && isDeleted) {
114: needNewDelta = true;
115: continue;
116: }
117: if (baseRow == null) {
118: if (isDeleted) {
119: if (isThisSession) {
120: end = true;
121: return false;
122: } else {
123: // the row was deleted by another session: return it
124: onBase = false;
125: needNewDelta = true;
126: return true;
127: }
128: }
129: throw Message.getInternalError();
130: }
131: int compare = index.compareRows(deltaRow, baseRow);
132: if (compare == 0) {
133: compare = index.compareKeys(deltaRow, baseRow);
134: }
135: if (compare == 0) {
136: if (isDeleted) {
137: if (isThisSession) {
138: throw Message.getInternalError();
139: } else {
140: // another session updated the row: must be deleted
141: // in base as well
142: throw Message.getInternalError();
143: }
144: } else {
145: if (isThisSession) {
146: onBase = false;
147: needNewBase = true;
148: needNewDelta = true;
149: return true;
150: } else {
151: // another session inserted the row: ignore
152: needNewBase = true;
153: needNewDelta = true;
154: continue;
155: }
156: }
157: }
158: if (compare > 0) {
159: onBase = true;
160: needNewBase = true;
161: return true;
162: }
163: if (!isDeleted) {
164: throw Message.getInternalError();
165: }
166: onBase = false;
167: needNewDelta = true;
168: return true;
169: }
170: }
171: }
172: }
|