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.store;
007:
008: import java.sql.SQLException;
009:
010: import org.h2.log.LogSystem;
011: import org.h2.util.CacheObject;
012:
013: /**
014: * A record represents a persisted row in a table, or a index page. When a
015: * record is persisted to disk, it is first written into a {@link DataPage}
016: * buffer.
017: */
018: public abstract class Record extends CacheObject {
019: private boolean deleted;
020: private int sessionId;
021: private int storageId;
022: private int lastLog = LogSystem.LOG_WRITTEN;
023: private int lastPos = LogSystem.LOG_WRITTEN;
024:
025: /**
026: * Get the number of bytes required for the data if the given data page
027: * would be used.
028: *
029: * @param dummy the template data page
030: * @return the number of bytes
031: */
032: public abstract int getByteCount(DataPage dummy)
033: throws SQLException;
034:
035: /**
036: * Write the record to the data page.
037: *
038: * @param buff the data page
039: */
040: public abstract void write(DataPage buff) throws SQLException;
041:
042: /**
043: * This method is called just before the page is written.
044: * If a read operation is required before writing, this needs to be done here.
045: * Because the data page buffer is shared for read and write operations.
046: * The method may read data and change the file pointer.
047: */
048: public void prepareWrite() throws SQLException {
049: }
050:
051: public boolean isEmpty() {
052: return false;
053: }
054:
055: public void setDeleted(boolean deleted) {
056: this .deleted = deleted;
057: }
058:
059: public void setSessionId(int sessionId) {
060: this .sessionId = sessionId;
061: }
062:
063: public int getSessionId() {
064: return sessionId;
065: }
066:
067: public void commit() {
068: this .sessionId = 0;
069: }
070:
071: public boolean getDeleted() {
072: return deleted;
073: }
074:
075: public void setStorageId(int storageId) {
076: this .storageId = storageId;
077: }
078:
079: public int getStorageId() {
080: return storageId;
081: }
082:
083: public void setLastLog(int log, int pos) {
084: lastLog = log;
085: lastPos = pos;
086: }
087:
088: public void setLogWritten(int log, int pos) {
089: if (log < lastLog) {
090: return;
091: }
092: if (log > lastLog || pos >= lastPos) {
093: lastLog = LogSystem.LOG_WRITTEN;
094: lastPos = LogSystem.LOG_WRITTEN;
095: }
096: }
097:
098: public boolean canRemove() {
099: if ((isChanged() && !isLogWritten()) || isPinned()) {
100: return false;
101: }
102: // TODO not required if we write the log only when committed
103: if (sessionId != 0) {
104: return false;
105: }
106: return true;
107: }
108:
109: public boolean isLogWritten() {
110: return lastLog == LogSystem.LOG_WRITTEN;
111: }
112:
113: }
|