package org.hsqldb.persist;
import java.io.IOException;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.hsqldb.HsqlException;
import org.hsqldb.Row;
import org.hsqldb.RowAVL;
import org.hsqldb.RowAVLDiskData;
import org.hsqldb.RowAction;
import org.hsqldb.Session;
import org.hsqldb.Table;
import org.hsqldb.TransactionManager;
import org.hsqldb.error.Error;
import org.hsqldb.error.ErrorCode;
import org.hsqldb.index.Index;
import org.hsqldb.lib.ArrayUtil;
import org.hsqldb.rowio.RowInputInterface;
import org.hsqldb.rowio.RowOutputInterface;
public class RowStoreAVLDiskData extends RowStoreAVL {
DataFileCache cache;
RowOutputInterface rowOut;
public RowStoreAVLDiskData(Table table) {
this.database = table.database;
this.table = table;
this.indexList = table.getIndexList();
this.accessorList = new CachedObject[indexList.length];
lock = new ReentrantReadWriteLock();
readLock = lock.readLock();
writeLock = lock.writeLock();
}
public Object[] getData(RowAVLDiskData row) {
cache.writeLock.lock();
try {
cache.get(row, this, false);
return row.getData();
} finally {
cache.writeLock.unlock();
}
}
public CachedObject get(long key, boolean keep) {
CachedObject object = cache.get(key, this, keep);
return object;
}
public CachedObject get(CachedObject object, boolean keep) {
object = cache.get(object, this, keep);
return object;
}
public void add(Session session, CachedObject object, boolean tx) {
cache.writeLock.lock();
try {
int size = object.getRealSize(cache.rowOut);
object.setStorageSize(size);
long pos = tableSpace.getFilePosition(size, false);
object.setPos(pos);
if (tx) {
RowAction.addInsertAction(session, table, (Row) object);
}
cache.add(object, false);
} finally {
cache.writeLock.unlock();
}
}
public CachedObject get(RowInputInterface in) {
try {
RowAVLDiskData row = new RowAVLDiskData(this, table, in);
row.setPos(in.getFilePosition());
row.setStorageSize(in.getSize());
row.setChanged(false);
((TextCache) cache).addInit(row);
return row;
} catch (IOException e) {
throw Error.error(ErrorCode.TEXT_FILE_IO, e);
}
}
public CachedObject get(CachedObject object, RowInputInterface in) {
Object[] rowData = in.readData(table.getColumnTypes());
((RowAVLDiskData) object).setData(rowData);
return object;
}
public CachedObject getNewCachedObject(Session session, Object object,
boolean tx) {
Row row = new RowAVLDiskData(this, table, (Object[]) object);
add(session, row, tx);
return row;
}
public void indexRow(Session session, Row row) {
super.indexRow(session, row);
}
public boolean isMemory() {
return false;
}
public void set(CachedObject object) {}
public CachedObject get(long key) {
CachedObject object = cache.get(key, this, false);
return object;
}
public void removeAll() {
destroy();
elementCount.set(0);
ArrayUtil.fillArray(accessorList, null);
}
public void remove(CachedObject object) {
cache.remove(object);
}
public CachedObject getAccessor(Index key) {
int position = key.getPosition();
if (position >= accessorList.length) {
throw Error.runtimeError(ErrorCode.U_S0500, "RowStoreAVL");
}
return accessorList[position];
}
public void commitPersistence(CachedObject row) {
try {
cache.saveRow(row);
} catch (HsqlException e1) {}
}
public void postCommitAction(Session session, RowAction action) {
if (action.getType() == RowAction.ACTION_DELETE_FINAL
&& !action.isDeleteComplete()) {
action.setDeleteComplete();
Row row = action.getRow();
if (row == null) {
row = (Row) get(action.getPos(), false);
}
delete(session, row);
remove(row);
}
}
public void commitRow(Session session, Row row, int changeAction,
int txModel) {
switch (changeAction) {
case RowAction.ACTION_DELETE :
cache.removePersistence(row);
break;
case RowAction.ACTION_INSERT :
commitPersistence(row);
break;
case RowAction.ACTION_INSERT_DELETE :
if (txModel == TransactionManager.LOCKS) {
remove(row);
} else {
delete(session, row);
remove(row);
}
break;
case RowAction.ACTION_DELETE_FINAL :
throw Error.runtimeError(ErrorCode.U_S0500, "RowStore");
}
}
public void rollbackRow(Session session, Row row, int changeAction,
int txModel) {
switch (changeAction) {
case RowAction.ACTION_DELETE :
if (txModel == TransactionManager.LOCKS) {
((RowAVL) row).setNewNodes(this);
indexRow(session, row);
}
break;
case RowAction.ACTION_INSERT :
if (txModel == TransactionManager.LOCKS) {
delete(session, row);
remove(row);
}
break;
case RowAction.ACTION_INSERT_DELETE :
if (txModel == TransactionManager.LOCKS) {
remove(row);
} else {
delete(session, row);
remove(row);
}
break;
}
}
public DataFileCache getCache() {
return cache;
}
public void setCache(DataFileCache cache) {
this.cache = cache;
this.tableSpace =
cache.spaceManager.getTableSpace(DataSpaceManager.tableIdDefault);
}
public void release() {
destroy();
table.database.logger.textTableManager.closeTextCache((Table) table);
cache = null;
ArrayUtil.fillArray(accessorList, null);
}
public void readLock() {
readLock.lock();
}
public void readUnlock() {
readLock.unlock();
}
public void writeLock() {
writeLock.lock();
}
public void writeUnlock() {
writeLock.unlock();
}
}