| 1 9 package org.ozoneDB.core.storage.classicStore; 10 11 import org.ozoneDB.DxLib.DxCollection; 12 import org.ozoneDB.DxLib.DxIterator; 13 import org.ozoneDB.Setup; 14 import org.ozoneDB.OzoneInternalException; 15 import org.ozoneDB.core.*; 16 import org.ozoneDB.core.storage.classicStore.ClassicObjectContainer; 17 import org.ozoneDB.core.storage.classicStore.ClassicStore; 18 import org.ozoneDB.core.storage.classicStore.Cluster; 19 import org.ozoneDB.core.storage.classicStore.ClusterID; 20 import org.ozoneDB.util.LogWriter; 21 22 import java.io.IOException ; 23 24 25 28 public class ClusterSpace { 29 30 protected Env env; 31 protected ClassicStore classicStore; 32 33 protected PersistenceSpace persistenceSpace = null; 34 protected DeathObjectBuffer dobjBuffer = new DeathObjectBuffer(); 35 protected int activatedObjects = 0; 36 protected int loadedClusters = 0; 37 protected int maxBufferSize = 10 * Cluster.MAX_SIZE; 38 39 40 43 public ClusterSpace( Env _env ) { 44 env = _env; 45 classicStore = (ClassicStore)env.getStoreManager(); 46 persistenceSpace = new PersistenceSpace( env ); 47 48 Cluster.MAX_SIZE = env.config.intProperty( Setup.CS_CLUSTER_SIZE, -1 ); 49 if (Cluster.MAX_SIZE == -1) { 50 env.logWriter.newEntry( this, "Property " + Setup.CS_TABLE_BUFF_SIZE + " is not set.", LogWriter.WARN ); 51 env.logWriter.newEntry( this, " " + Setup.CS_TABLE_BUFF_SIZE + " = 64*1024", LogWriter.WARN ); 52 Cluster.MAX_SIZE = 64 * 1024; 53 } 54 55 maxBufferSize = env.config.intProperty( Setup.CS_CLUSTER_SPACE_SIZE, -1 ); 56 if (maxBufferSize == -1) { 57 env.logWriter.newEntry( this, "Property " + Setup.CS_CLUSTER_SPACE_SIZE + " is not set.", LogWriter.WARN ); 58 env.logWriter.newEntry( this, " " + Setup.CS_CLUSTER_SPACE_SIZE + " = 5MB", LogWriter.WARN ); 59 maxBufferSize = 5 * 1024 * 1024; 60 } 61 } 62 63 64 66 protected synchronized void startup() throws Exception { 67 env.logWriter.newEntry( this, "startup...", LogWriter.INFO ); 68 persistenceSpace.startup(); 69 persistenceSpace.fillObjectSpace(); 70 } 71 72 73 75 protected synchronized void shutdown() throws Exception { 76 env.logWriter.newEntry( this, "shutdown...", LogWriter.INFO ); 77 persistenceSpace.shutdown(); 78 } 79 80 81 83 protected void setSizes( int csSize, int clSize ) { 84 Cluster.MAX_SIZE = csSize; 85 maxBufferSize = csSize; 86 if (maxBufferSize < Cluster.MAX_SIZE) { 87 maxBufferSize = Cluster.MAX_SIZE; 88 } 89 if (csSize < dobjBuffer.size()) { 90 freeSpace( dobjBuffer.size() - csSize ); 91 } 92 } 93 94 95 97 protected synchronized void activateObject( ObjectContainer container ) throws Exception { 98 ClassicObjectContainer os = (ClassicObjectContainer)container; 100 if (os.target() == null) { 101 DeathObject dobj = dobjBuffer.objectForId( os.id() ); 102 if (dobj == null) { 103 loadCluster( os.clusterID() ); 104 dobj = dobjBuffer.objectForId( os.id() ); 105 } 106 if (dobj != null) { 107 activatedObjects++; 108 os.setObject( dobj.enlive() ); 111 } 112 } 113 } 115 116 117 119 protected synchronized void prepareCommit( TransactionID tid, DxCollection created, DxCollection modified ) 120 throws OzoneInternalException { 121 env.logWriter.newEntry( this, "ClusterSpace.commitObjects: " + created.count() + " : " + modified.count(), 122 LogWriter.DEBUG3 ); 123 125 persistenceSpace.startTransaction( tid ); 126 127 DeathObject dobj; 128 129 DxIterator it = modified.iterator(); 131 ObjectID id; 132 while ((id = (ObjectID)it.next()) != null) { 133 ClassicObjectContainer os = (ClassicObjectContainer)classicStore.objectSpace.objectForID( id ); 134 dobj = dobjBuffer.objectForId( id ); 135 if (dobj == null) { 136 loadCluster( os.clusterID ); 137 dobj = dobjBuffer.objectForId( id ); 138 } 139 140 persistenceSpace.writeLeak( dobj.clusterID(), dobj ); 142 143 if (!os.isDeleted()) { 144 dobjBuffer.remove( id ); 145 try { 146 persistenceSpace.writeObject( dobj, true, true ); 147 } catch (IOException e) { 148 throw new OzoneInternalException("prepareCommit() failed for modified objects (failed to write object)",e); 149 } 150 freeSpace( dobj.size() ); 151 dobjBuffer.add( dobj ); 152 } else { 153 dobjBuffer.remove( id ); 154 os.setObject( null ); 155 } 156 } 157 158 it = created.iterator(); 160 ObjectContainer os; 161 while ((os = (ObjectContainer)it.next()) != null) { 162 dobj = new DeathObject( os.id() ); 163 try { 164 persistenceSpace.writeObject( dobj, true, false ); 165 } catch (IOException e) { 166 throw new OzoneInternalException("prepareCommit() failed for new objects (failed to write object)",e); 167 } 168 freeSpace( dobj.size() ); 169 dobjBuffer.add( dobj ); 170 } 171 172 persistenceSpace.prepareCommitTransaction( tid ); 173 } 175 176 177 179 protected synchronized void commitTransaction( TransactionID tid ) { 180 persistenceSpace.commitTransaction( tid ); 181 } 182 183 184 186 protected synchronized void abortTransaction( TransactionID tid ) throws Exception { 187 persistenceSpace.abortTransaction( tid ); 188 } 189 190 191 193 protected synchronized void touchObject( ObjectID oid ) { 194 dobjBuffer.moveToTop( oid ); 196 } 197 198 199 202 private boolean loadCluster( ClusterID cid ) throws OzoneInternalException { 203 loadedClusters++; 205 Cluster cl = persistenceSpace.readCluster( cid, Cluster.DATA ); 207 208 freeSpace( cl.size() ); 210 211 DxIterator it = cl.objects().iterator(); 213 DeathObject dobj; 214 while ((dobj = (DeathObject)it.next()) != null) { 215 dobjBuffer.add( dobj ); 216 } 217 218 return true; 219 } 220 221 222 private boolean freeSpace( long size ) { 223 while (dobjBuffer.size() + size > maxBufferSize) { 225 DeathObject dobj = dobjBuffer.pushFromBottom(); 226 if (dobj != null) { 227 dobj.container().setObject( null ); 228 } 229 } 230 return true; 231 } 232 } 233 | Popular Tags |