1 9 package org.ozoneDB.core.storage.classicStore; 10 11 import org.ozoneDB.DxLib.*; 12 import org.ozoneDB.OzoneCompatible; 13 import org.ozoneDB.Setup; 14 import org.ozoneDB.core.*; 15 import org.ozoneDB.core.storage.classicStore.ClassicObjectContainer; 16 import org.ozoneDB.core.storage.classicStore.ClassicStoreException; 17 import org.ozoneDB.core.storage.classicStore.ClassicTransaction; 18 import org.ozoneDB.core.storage.classicStore.ClusterSpace; 19 import org.ozoneDB.util.LogWriter; 20 21 22 23 public final class ObjectSpace extends Object { 24 25 final static long serialVersionUID = 2; 26 final static byte subSerialVersionUID = 1; 27 28 29 protected transient Env env; 30 protected transient ClusterSpace clusterSpace; 31 32 33 protected DxMap objectTable; 34 protected DxMap nameTable; 35 36 37 39 public ObjectSpace( Env _env ) { 40 env = _env; 41 clusterSpace = new ClusterSpace( _env ); 42 } 43 44 45 47 public void startup() throws Exception { 48 env.logWriter.newEntry( this, "startup...", LogWriter.INFO ); 49 int tableBuffSize = env.config.intProperty( Setup.CS_TABLE_BUFF_SIZE, -1 ); 50 if (tableBuffSize == -1) { 51 env.logWriter.newEntry( this, "Property " + Setup.CS_TABLE_BUFF_SIZE + " is not set.", LogWriter.WARN ); 52 tableBuffSize = 20; 53 } 54 objectTable = new DxHashMap( 1000 ); 56 nameTable = new DxHashMap( 10 ); 57 clusterSpace.startup(); 58 } 59 60 61 63 public void shutdown() throws Exception { 64 env.logWriter.newEntry( this, "shutdown...", LogWriter.INFO ); 65 clusterSpace.shutdown(); 66 if (objectTable instanceof DxDiskHashMap) { 68 ((DxDiskHashMap)objectTable).cleanFiles(); 69 } 70 } 71 72 73 75 public synchronized void addObject( ObjectContainer container ) { 76 objectTable.addForKey( container, container.id().clone() ); 78 if (container.name() != null) { 79 nameTable.addForKey( container, new String ( container.name() ) ); 80 } 81 } 82 83 84 88 public synchronized ObjectContainer newContainer( Transaction ta, OzoneCompatible target, ObjectID objID, 89 Permissions permissions ) throws ClassicStoreException { 90 ClassicObjectContainer container = new ClassicObjectContainer( target, objID, permissions ); 91 if (!objectTable.addForKey( container, objID.clone() )) { 92 throw new ClassicStoreException( "ObjectID " + objID + " already exists !" ); 93 } 94 95 return container; 96 } 97 98 99 103 public ObjectContainer objectForID( ObjectID id ) { 104 return (ObjectContainer)objectTable.elementForKey( id ); 105 } 106 107 108 113 public void nameObject( ObjectContainer container, String name ) throws ClassicStoreException { 114 if (container.name() != null) { 115 nameTable.removeForKey( container.name() ); 116 } 117 if (name != null) { 118 if (!nameTable.addForKey( container, new String ( name ) )) { 119 throw new ClassicStoreException( "Name '" + name + "' already exists !" ); 120 } 121 container.nameTarget( name ); 122 } 123 } 124 125 126 130 public ObjectContainer objectForName( String name ) { 131 return (ObjectContainer)nameTable.elementForKey( name ); 132 } 133 134 135 138 public synchronized void deleteObject( ObjectContainer toRemove ) { 139 synchronized (toRemove) { 140 objectTable.removeForKey( toRemove.id() ); 141 if (toRemove.name() != null) { 142 nameTable.removeForKey( toRemove.name() ); 143 } 144 } 145 } 146 147 148 149 public synchronized void prepareCommitObjects( ClassicTransaction ta ) { 150 env.logWriter.newEntry( this, "prepareCommitObjects: transaction " + ta.taID(), LogWriter.DEBUG3 ); 151 152 DxListBag created = new DxListBag(); 155 DxListBag modified = new DxListBag(); 156 DxIterator it = ta.idTable.iterator(); 157 ClassicObjectContainer container; 158 159 while ((container = (ClassicObjectContainer)it.next()) != null) { 160 if (container.isCreated()) { 161 addObject( container ); 162 created.add( container ); 163 } else { 164 if (container.lockLevel( ta ) > Lock.LEVEL_READ) { 165 modified.add( container.id() ); 166 } 167 } 168 } 169 170 clusterSpace.prepareCommit( ta.taID(), created, modified ); 172 173 it = ta.idTable.iterator(); 178 while ((container = (ClassicObjectContainer)it.next()) != null) { 179 if (container.isDeleted()) { 180 deleteObject( container ); 181 } else { 182 container.commitTarget( ta ); 183 } 184 } 185 } 186 187 188 189 public synchronized void commitObjects( Transaction ta ) { 190 clusterSpace.commitTransaction( ta.taID() ); 191 } 192 193 194 195 public synchronized void abortObjects( ClassicTransaction ta ) throws Exception { 196 DxIterator it = ta.idTable.iterator(); 197 ClassicObjectContainer container; 198 while ((container = (ClassicObjectContainer)it.next()) != null) { 199 container.abortTarget( ta ); 200 } 201 clusterSpace.abortTransaction( ta.taID() ); 202 } 203 } 204 | Popular Tags |