/*
* Copyright (C) 2006 http://www.chaidb.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
/**
* User: Administrator
* Date: Nov 11, 2003
* Time: 3:20:36 PM
*/
package org.chaidb.db.index;
import org.chaidb.db.Db;
import org.chaidb.db.KernelContext;
import org.chaidb.db.api.Converter;
import org.chaidb.db.exception.ChaiDBException;
import org.chaidb.db.index.btree.BTree;
import org.chaidb.db.lock.LockManager;
import org.chaidb.db.log.MemLogManager;
public abstract class InMemIndex extends LockableBTree implements ISingleKeyBTree {
/**
* Persistent storage, for BTree, it's useless
*/
protected ISingleKeyBTree storage;
/**
* The status can be OPEN or CLOSE
*/
protected short status = OPEN;
/**
* Associate in-memory log manager to this object
*/
protected MemLogManager memLogMgr = Db.getMemLogManager();
/**
* Used for in-memory index
* Flush the changed data into B-Tree
*
* @param mode The mode of operation
* STORE_REPLACE, STORE_INSERT, DELETE
* @param key The key associated with the index entry to be flashed
* @param newObj The object of new value
*/
abstract public void memFlush(short mode, KernelContext kContext, Key key, Object newObj) throws ChaiDBException;
/**
* Used for in-memory index.
* Undo the done operation.
*
* @param mode The mode of operation
* STORE_REPLACE, STORE_INSERT, DELETE
* @param oldKernelContext Old kernel context to restore after undo
* @param key The key associated with the index entry to be flashed
* @param oldObj The object of old value for recover
*/
public void memUndo(short mode, KernelContext oldKernelContext, Key key, Object oldObj) throws ChaiDBException {
// if the index is closed, do none
if (status == CLOSE) return;
oldKernelContext.setOnlyMemory(true);
switch (mode) {
case STORE_REPLACE:
if (storage instanceof BTree) {
BTree btree = (BTree) storage;
if (btree.isReady()) {
//Lookup from BTree to get original object and store.
store(key, storage.lookup(key, oldKernelContext), STORE_REPLACE, oldKernelContext);
}
}
return;
case STORE_INSERT:
delete(key, oldKernelContext);
return;
case DELETE:
store(key, oldObj, STORE_INSERT, oldKernelContext);
return;
}
}
/**
* The mode can be CREATE or RUNTIME, INMEM
*/
protected short mode = RUNTIME;
public boolean isCreateMode() {
return (mode == CREATE);
}
/**
* Sets the key size.
*
* @param keySize The key size.
*/
public void setKeySize(int keySize) {
storage.setKeySize(keySize);
}
public void setConverter(Converter converter) {
this.storage.setConverter(converter);
}
public Converter getConverter() {
return this.storage.getConverter();
}
public String getBTreeName() {
return ((LockableBTree) storage).getBTreeName();
}
public void acquire(KernelContext kContext, int mode) throws ChaiDBException {
storage.acquire(kContext, mode);
}
public void release(KernelContext kContext) {
storage.release(kContext);
}
/**
* For memory index, It never is invoked.
*
* @param dataSource
* @param treeId
* @throws ChaiDBException
*/
public void openForRecovery(String dataSource, int treeId) throws ChaiDBException {
}
/**
* only one lockManager exist change it to static
*/
protected static LockManager newLockManager = Db.getLockManager();
}