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: ClusterSpace.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.*;
013: import org.ozoneDB.core.*;
014: import org.ozoneDB.util.*;
015: import org.ozoneDB.DxLib.*;
016: import org.ozoneDB.tools.*;
017:
018: /**
019: */
020: public class ClusterSpace {
021: /** The environment of this object. */
022: protected Env env;
023: protected ClassicStore classicStore;
024:
025: protected PersistenceSpace persistenceSpace = null;
026: protected DeathObjectBuffer dobjBuffer = new DeathObjectBuffer();
027: protected int activatedObjects = 0;
028: protected int loadedClusters = 0;
029: protected int maxBufferSize = 10 * Cluster.MAX_SIZE;
030:
031: /**
032: * Constructor
033: */
034: public ClusterSpace(Env _env) {
035: env = _env;
036: classicStore = (ClassicStore) env.store;
037: persistenceSpace = new PersistenceSpace(env);
038:
039: Cluster.MAX_SIZE = env.config.intProperty(
040: Setup.CS_CLUSTER_SIZE, -1);
041: if (Cluster.MAX_SIZE == -1) {
042: env.logWriter.newEntry(this , "Property "
043: + Setup.CS_TABLE_BUFF_SIZE + " is not set.",
044: LogWriter.WARN);
045: env.logWriter.newEntry(this , " "
046: + Setup.CS_TABLE_BUFF_SIZE + " = 64*1024",
047: LogWriter.WARN);
048: Cluster.MAX_SIZE = 64 * 1024;
049: }
050:
051: maxBufferSize = env.config.intProperty(
052: Setup.CS_CLUSTER_SPACE_SIZE, -1);
053: if (maxBufferSize == -1) {
054: env.logWriter.newEntry(this , "Property "
055: + Setup.CS_CLUSTER_SPACE_SIZE + " is not set.",
056: LogWriter.WARN);
057: env.logWriter.newEntry(this , " "
058: + Setup.CS_CLUSTER_SPACE_SIZE + " = 5MB",
059: LogWriter.WARN);
060: maxBufferSize = 5 * 1024 * 1024;
061: }
062: }
063:
064: /**
065: */
066: protected synchronized void startup() throws Exception {
067: env.logWriter.newEntry(this , "startup...", LogWriter.INFO);
068: persistenceSpace.startup();
069: persistenceSpace.fillObjectSpace();
070: }
071:
072: /**
073: */
074: protected synchronized void shutdown() throws Exception {
075: env.logWriter.newEntry(this , "shutdown...", LogWriter.INFO);
076: persistenceSpace.shutdown();
077: }
078:
079: /**
080: */
081: protected void setSizes(int csSize, int clSize) {
082: Cluster.MAX_SIZE = csSize;
083: maxBufferSize = csSize;
084: if (maxBufferSize < Cluster.MAX_SIZE) {
085: maxBufferSize = Cluster.MAX_SIZE;
086: }
087: if (csSize < dobjBuffer.size()) {
088: freeSpace(dobjBuffer.size() - csSize);
089: }
090: }
091:
092: /**
093: */
094: protected synchronized void activateObject(ObjectContainer container)
095: throws Exception {
096: // ObjectContainer os = env.objectSpace.objectForID (oid);
097: ClassicObjectContainer os = (ClassicObjectContainer) container;
098: if (os.target() == null) {
099: DeathObject dobj = dobjBuffer.objectForId(os.id());
100: if (dobj == null) {
101: loadCluster(os.clusterID());
102: dobj = dobjBuffer.objectForId(os.id());
103: }
104: if (dobj != null) {
105: activatedObjects++;
106: //env.logWriter.newEntry ("activatedObjects / loadedClusters: " + ((double)activatedObjects/loadedClusters), LogWriter.DEBUG3);
107: //env.logWriter.newEntry ("ClusterSpace.activateObject: " + oid + " of " + dobjBuffer.count() + "(" + dobjBuffer.size() + ")", LogWriter.DEBUG3);
108: os.setObject(dobj.enlive());
109: }
110: }
111: //env.logWriter.newEntry ("current buffer size: " + dobjBuffer.size(), LogWriter.DEBUG3);
112: }
113:
114: /**
115: */
116: protected synchronized void prepareCommit(TransactionID tid,
117: DxCollection created, DxCollection modified)
118: throws Exception {
119: env.logWriter.newEntry(this , "ClusterSpace.commitObjects: "
120: + created.count() + " : " + modified.count(),
121: LogWriter.DEBUG3);
122: //long start = System.currentTimeMillis();
123:
124: persistenceSpace.startTransaction(tid);
125:
126: DeathObject dobj;
127:
128: // we commit the modified objects first
129: DxIterator it = modified.iterator();
130: ObjectID id;
131: while ((id = (ObjectID) it.next()) != null) {
132: ClassicObjectContainer os = (ClassicObjectContainer) classicStore.objectSpace
133: .objectForID(id);
134: dobj = dobjBuffer.objectForId(id);
135: if (dobj == null) {
136: loadCluster(os.clusterID);
137: dobj = dobjBuffer.objectForId(id);
138: }
139:
140: // write the leak
141: persistenceSpace.writeLeak(dobj.clusterID(), dobj);
142:
143: if (!os.isDeleted()) {
144: dobjBuffer.remove(id);
145: persistenceSpace.writeObject(dobj, true, true);
146: freeSpace(dobj.size());
147: dobjBuffer.add(dobj);
148: } else {
149: dobjBuffer.remove(id);
150: os.setObject(null);
151: }
152: }
153:
154: // commit the new objects
155: it = created.iterator();
156: ObjectContainer os;
157: while ((os = (ObjectContainer) it.next()) != null) {
158: dobj = new DeathObject(os.id());
159: persistenceSpace.writeObject(dobj, true, false);
160: freeSpace(dobj.size());
161: dobjBuffer.add(dobj);
162: }
163:
164: persistenceSpace.prepareCommitTransaction(tid);
165: //env.logWriter.newEntry ("commit time: " + (System.currentTimeMillis() - start), LogWriter.DEBUG3);
166: }
167:
168: /**
169: */
170: protected synchronized void commitTransaction(TransactionID tid) {
171: persistenceSpace.commitTransaction(tid);
172: }
173:
174: /**
175: */
176: protected synchronized void abortTransaction(TransactionID tid)
177: throws Exception {
178: persistenceSpace.abortTransaction(tid);
179: }
180:
181: /**
182: */
183: protected synchronized void touchObject(ObjectID oid) {
184: //env.logWriter.newEntry ("ClusterSpace.touchObject: " + oid, LogWriter.DEBUG3);
185: dobjBuffer.moveToTop(oid);
186: }
187:
188: /**
189: * laedt den cluster cid und fuegt die objekte in die tabelle ein
190: */
191: private boolean loadCluster(ClusterID cid) throws Exception {
192: //env.logWriter.newEntry ("ClusterSpace.loadCluster: " + cid, LogWriter.DEBUG3);
193: loadedClusters++;
194: // cluster anfordern
195: Cluster cl = persistenceSpace.readCluster(cid, Cluster.DATA);
196:
197: // free enough space to insert all objects
198: freeSpace(cl.size());
199:
200: // insert the objects
201: DxIterator it = cl.objects().iterator();
202: DeathObject dobj;
203: while ((dobj = (DeathObject) it.next()) != null) {
204: dobjBuffer.add(dobj);
205: }
206:
207: return true;
208: }
209:
210: private boolean freeSpace(long size) {
211: //env.logWriter.newEntry ("ClusterSpace.freeSpace: " + size, LogWriter.DEBUG3);
212: while (dobjBuffer.size() + size > maxBufferSize) {
213: DeathObject dobj = dobjBuffer.pushFromBottom();
214: if (dobj != null) {
215: dobj.container().setObject(null);
216: }
217: }
218: return true;
219: }
220: }
|