001: // Space4J(TM) - Object Persistence in RAM
002: // Copyright (C) 2003 Sergio Oliveira Junior
003: // This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. This library 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
004:
005: package org.space4j.implementation;
006:
007: import org.space4j.*;
008: import org.space4j.indexing.*;
009: import org.space4j.commands.*;
010:
011: import java.io.*;
012: import java.util.*;
013: import java.net.*;
014:
015: /**
016: * The simplest possible implementation of a Space4J.
017: */
018: public class SimpleSpace4J implements Space4J {
019:
020: protected Space space;
021: protected Logger logger;
022: protected String dirname;
023: protected IndexManager im;
024:
025: /**
026: * Initializes the SimpleSpace4J, initializes the Logger, recover the Space from disk if there is a snapshot saved
027: * and reapply the commands to the Space.
028: */
029: public SimpleSpace4J(String dirname) throws LoggerException,
030: CommandException {
031: this .dirname = dirname;
032: logger = new SimpleLogger(dirname);
033: space = readSnapshot();
034: if (space == null)
035: space = new SimpleSpace();
036: reapplyCommandsFromLog();
037: }
038:
039: public String getDirName() {
040: return dirname;
041: }
042:
043: public Space getSpace() {
044: return space;
045: }
046:
047: public void start() throws UnknownHostException, IOException,
048: ClassNotFoundException {
049: // initialize the IndexManager
050: this .im = (IndexManager) space.getObject("sys_IndexManager");
051: }
052:
053: public IndexManager getIndexManager() {
054: if (im != null)
055: return im;
056: try {
057: executeCommand(new ObjCreateCmd("sys_IndexManager",
058: new IndexManager()));
059: this .im = (IndexManager) space
060: .getObject("sys_IndexManager");
061: } catch (Exception e) {
062: e.printStackTrace();
063: }
064:
065: return im;
066: }
067:
068: /**
069: * Execute a command and log it.
070: * @param cmd The command to be executed and logged.
071: * @return The number of objects modified by this operation.
072: */
073: public synchronized int executeCommand(Command cmd)
074: throws CommandException, LoggerException {
075: return executeCommand(cmd, true);
076: }
077:
078: // when we are reapplying, there is no need for logging, of cource...
079: public synchronized int executeCommand(Command cmd, boolean log)
080: throws CommandException, LoggerException {
081: int x = cmd.execute(space);
082: if (log)
083: logger.logCommand(cmd);
084: return x;
085: }
086:
087: // gateway to logger
088: public void executeSnapshot() throws LoggerException {
089: logger.takeSnapshot(space);
090: }
091:
092: // gateway to logger
093: public Space readSnapshot() throws LoggerException {
094: return logger.readSnapshot();
095: }
096:
097: // gateway to logger
098: public synchronized void reapplyCommandsFromLog()
099: throws CommandException, LoggerException {
100: logger.reapplyCommandsFromLog(this);
101: }
102: }
|