001: // You can redistribute this software and/or modify it under the terms of
002: // the Ozone Core License version 1 published by ozone-db.org.
003: //
004: // The original code and portions created by SMB are
005: // Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
006: //
007: // $Id: ObjectSpace.java,v 1.1 2001/12/18 10:31:31 per_nyfelt Exp $
008:
009: package org.ozoneDB.core.classicStore;
010:
011: import java.io.*;
012: import org.ozoneDB.DxLib.*;
013: import org.ozoneDB.*;
014: import org.ozoneDB.core.*;
015: import org.ozoneDB.util.*;
016:
017: /** */
018: public final class ObjectSpace extends Object {
019: /** */
020: final static long serialVersionUID = 2;
021: final static byte subSerialVersionUID = 1;
022:
023: /** The environment of this object. */
024: protected transient Env env;
025: protected transient ClusterSpace clusterSpace;
026:
027: /** */
028: protected DxMap objectTable;
029: protected DxMap nameTable;
030:
031: /**
032: */
033: public ObjectSpace(Env _env) {
034: env = _env;
035: clusterSpace = new ClusterSpace(_env);
036: }
037:
038: /**
039: */
040: public void startup() throws Exception {
041: env.logWriter.newEntry(this , "startup...", LogWriter.INFO);
042: int tableBuffSize = env.config.intProperty(
043: Setup.CS_TABLE_BUFF_SIZE, -1);
044: if (tableBuffSize == -1) {
045: env.logWriter.newEntry(this , "Property "
046: + Setup.CS_TABLE_BUFF_SIZE + " is not set.",
047: LogWriter.WARN);
048: tableBuffSize = 20;
049: }
050: //objectTable = new DxDiskHashMap (env.dir + Env.OS_DIR + File.separator + "idtable", tableBuffSize / 256, 12);
051: objectTable = new DxHashMap(1000);
052: nameTable = new DxHashMap(10);
053: clusterSpace.startup();
054: }
055:
056: /**
057: */
058: public void shutdown() throws Exception {
059: env.logWriter.newEntry(this , "shutdown...", LogWriter.INFO);
060: clusterSpace.shutdown();
061: //objectTable.printCacheStatistics();
062: if (objectTable instanceof DxDiskHashMap) {
063: ((DxDiskHashMap) objectTable).cleanFiles();
064: }
065: }
066:
067: /**
068: */
069: public synchronized void addObject(ObjectContainer container)
070: throws Exception {
071: //use a copy of the objID because objectTable is a diskHash
072: objectTable.addForKey(container, container.id().clone());
073: if (container.name() != null) {
074: nameTable
075: .addForKey(container, new String(container.name()));
076: }
077: }
078:
079: /**
080: * Creates a new object container for the given target with the given oid
081: * and an optional name.
082: */
083: public synchronized ObjectContainer newContainer(Transaction ta,
084: OzoneCompatible target, ObjectID objID,
085: Permissions permissions) throws ClassicStoreExc {
086: ClassicObjectContainer container = new ClassicObjectContainer(
087: target, objID, permissions);
088: if (!objectTable.addForKey(container, objID.clone())) {
089: throw new ClassicStoreExc("ObjectID " + objID
090: + " already exists !");
091: }
092:
093: return container;
094: }
095:
096: /**
097: * @param id The object id to search for.
098: * @return The object container with the given id or null.
099: */
100: public ObjectContainer objectForID(ObjectID id) {
101: return (ObjectContainer) objectTable.elementForKey(id);
102: }
103:
104: /**
105: * Applies a name to an object.
106: * @param container The container to name.
107: * @param name The new name of the container.
108: */
109: public void nameObject(ObjectContainer container, String name)
110: throws ClassicStoreExc {
111: if (container.name() != null) {
112: nameTable.removeForKey(container.name());
113: }
114: if (name != null) {
115: if (!nameTable.addForKey(container, new String(name))) {
116: throw new ClassicStoreExc("Name '" + name
117: + "' already exists !");
118: }
119: container.nameTarget(name);
120: }
121: }
122:
123: /**
124: * @param name
125: * @return The object container with the given name.
126: */
127: public ObjectContainer objectForName(String name) {
128: return (ObjectContainer) nameTable.elementForKey(name);
129: }
130:
131: /**
132: * entfernt ObjectContainer fuer entsprechende ObjectID;
133: */
134: public synchronized void deleteObject(ObjectContainer toRemove) {
135: synchronized (toRemove) {
136: objectTable.removeForKey(toRemove.id());
137: if (toRemove.name() != null) {
138: nameTable.removeForKey(toRemove.name());
139: }
140: }
141: }
142:
143: /** */
144: public synchronized void prepareCommitObjects(Transaction ta)
145: throws Exception {
146: env.logWriter.newEntry(this ,
147: "prepareCommitObjects: transaction " + ta.taID(),
148: LogWriter.DEBUG3);
149:
150: //at first we have to insert new objects + containers into the object
151: //space, so that we have access to them while further commiting
152: DxListBag created = new DxListBag();
153: DxListBag modified = new DxListBag();
154: DxIterator it = ta.idTable.iterator();
155: ClassicObjectContainer container;
156:
157: while ((container = (ClassicObjectContainer) it.next()) != null) {
158: if (container.isCreated()) {
159: addObject(container);
160: created.add(container);
161: } else {
162: if (container.lockLevel(ta) > Lock.LEVEL_READ) {
163: modified.add(container.id());
164: }
165: }
166: }
167:
168: //make all changes persistent
169: clusterSpace.prepareCommit(ta.taID(), created, modified);
170:
171: // remove all deleted objects from the object space _after_ commiting
172: // them; otherwise we wouldn't have access to them while commiting
173: // commit all written objects _after_ we know everthing worked fine;
174: // if not, the transaction has to abort
175: it = ta.idTable.iterator();
176: while ((container = (ClassicObjectContainer) it.next()) != null) {
177: if (container.isDeleted()) {
178: deleteObject(container);
179: } else {
180: container.commitTarget(ta);
181: }
182: }
183: }
184:
185: /** */
186: public synchronized void commitObjects(Transaction ta) {
187: clusterSpace.commitTransaction(ta.taID());
188: }
189:
190: /** */
191: public synchronized void abortObjects(Transaction ta)
192: throws Exception {
193: DxIterator it = ta.idTable.iterator();
194: ClassicObjectContainer container;
195: while ((container = (ClassicObjectContainer) it.next()) != null) {
196: container.abortTarget(ta);
197: }
198: clusterSpace.abortTransaction(ta.taID());
199: }
200: }
|