1 9 package org.ozoneDB.core.DbRemote; 10 11 import java.io.*; 12 import java.lang.reflect.*; 13 import org.ozoneDB.DxLib.*; 14 import org.ozoneDB.*; 15 import org.ozoneDB.io.stream.ResolvingObjectInputStream; 16 import org.ozoneDB.core.*; 17 import org.ozoneDB.util.*; 18 19 20 28 public final class DbCacheChunk extends DbCommand implements Externalizable { 29 30 private final static byte MODE_READ = 1; 31 private final static byte MODE_WRITE = 2; 32 33 private byte mode; 34 35 private int size; 36 37 private ObjectID rootID; 38 39 private byte[] chunk; 40 41 42 public DbCacheChunk() { 43 } 44 45 46 49 public DbCacheChunk( ObjectID _rootID, int _size ) { 50 mode = MODE_READ; 51 rootID = _rootID; 52 size = _size; 53 } 54 55 56 59 public DbCacheChunk( byte[] _chunk ) { 60 mode = MODE_WRITE; 61 chunk = _chunk; 62 } 63 64 65 public void perform( Transaction ta ) throws Exception { 66 env.logWriter.newEntry( this, "DbCacheChunk.perform(): mode:" + mode, LogWriter.DEBUG ); 67 68 switch (mode) { 69 case MODE_READ: 70 readChunk( ta ); 71 break; 72 case MODE_WRITE: 73 writeChunk( ta ); 74 break; 75 default: 76 throw new RuntimeException ( "Unknown mode." ); 77 } 78 } 79 80 81 protected void readChunk( Transaction ta ) throws Exception { 82 env.logWriter.newEntry( this, " readChunk()", LogWriter.DEBUG3 ); 83 84 DxBag bag = env.storeManager.clusterOfID( rootID ); 85 86 result = new DxArrayBag(); 87 88 DxIterator it = bag.iterator(); 89 ObjectID id = null; 90 while ((id = (ObjectID)it.next()) != null) { 91 try { 92 env.logWriter.newEntry( this, "readChunk(): " + id, LogWriter.DEBUG3 ); 93 ObjectContainer container = ta.acquireObject( id, Lock.LEVEL_READ ); 94 95 try { 96 CacheObjectContainer cc = new CacheObjectContainer( container ); 99 100 ((DxArrayBag)result).add( cc ); 101 } finally { 102 container.unpin(); 103 } 104 } catch (OzoneRemoteException e) { 105 env.logWriter.newEntry( this, " readChunk(): " + e.toString(), LogWriter.DEBUG3 ); 106 continue; 108 } 109 } 110 } 111 112 113 protected void writeChunk( Transaction ta ) throws Exception { 114 env.logWriter.newEntry( this, "writeChunk(): size:" + chunk.length, LogWriter.DEBUG3 ); 115 116 120 ByteArrayInputStream bin = new ByteArrayInputStream( chunk ); 121 ObjectInputStream in = new ResolvingObjectInputStream( bin ); 122 123 env.logWriter.newEntry( this, " available:" + bin.available(), LogWriter.DEBUG3 ); 124 while (bin.available() > 0) { 125 CacheObjectContainer cacheContainer = (CacheObjectContainer)in.readObject(); 126 env.logWriter.newEntry( this, 127 " container: id:" + cacheContainer.id() + " state:" + cacheContainer.state(), LogWriter.DEBUG3 ); 128 129 if (cacheContainer.state() == ObjectContainer.STATE_CREATED) { 130 ObjectContainer container = 131 ta.createObject( null, cacheContainer.access(), cacheContainer.name(), null, null, 132 cacheContainer.id() ); 133 try { 134 container.setTarget( cacheContainer.target() ); 135 } finally { 136 container.unpin(); 137 } 138 } else { 139 ObjectContainer container = ta.acquireObject( cacheContainer.id(), Lock.LEVEL_WRITE ); 141 142 if (container.modTime() > cacheContainer.modTime()) { 144 throw new TransactionException( "Object has been changed by another transaction.", 145 TransactionException.OPTIMISTIC ); 146 } 147 148 if (cacheContainer.state() == ObjectContainer.STATE_DELETED) { 149 ta.deleteObject( container.id() ); 150 } else if (cacheContainer.state() == ObjectContainer.STATE_MODIFIED) { 151 container.setTarget( cacheContainer.target() ); 152 153 String name = cacheContainer.name(); 155 container.nameTarget( name ); 157 } else { 158 throw new RuntimeException ( "Wrong container state: " + container.state() ); 159 } 160 } 161 } 162 } 163 164 165 169 protected void findProxies( Object obj, DxDeque deque ) { 170 String name = obj != null ? obj.getClass().getName() : "(null)"; 171 System.out.println( "*** findProxies(): " + name ); 172 173 if (obj == null) { 174 } else if (obj instanceof OzoneProxy) { 176 deque.pushTop( ((OzoneProxy)obj).remoteID() ); 177 } else { 178 Class cl = obj.getClass(); 179 int mdf = cl.getModifiers(); 180 if (Modifier.isTransient( mdf ) || Modifier.isStatic( mdf ) || Modifier.isFinal( mdf )) { 181 System.out.println( "*** transient/static/final" + name ); 183 } else if (cl.isPrimitive()) { 184 System.out.println( "*** primitive" + name ); 186 } else if (cl.isArray()) { 187 System.out.println( "*** array" + name ); 188 int len = Array.getLength( obj ); 189 for (int j = 0; j < len; j++) { 190 Object member = Array.get( obj, j ); 191 findProxies( member, deque ); 192 } 193 } else { 194 Field[] fields = cl.getFields(); 195 for (int i = 0; i < fields.length; i++) { 196 try { 197 System.out.println( "*** " + fields[i].toString() ); 198 Object member = fields[i].get( obj ); 199 findProxies( member, deque ); 200 } catch (Exception e) { 201 System.out.println( "*** exception" + name ); 203 } 204 } 205 } 206 } 207 } 208 209 210 public void writeExternal( ObjectOutput out ) throws IOException { 211 out.writeByte( mode ); 212 switch (mode) { 213 case MODE_WRITE: { 214 out.writeObject( chunk ); 215 break; 216 } 217 case MODE_READ: { 218 out.writeInt( size ); 219 out.writeObject( rootID ); 220 break; 221 } 222 default: 223 throw new RuntimeException ( "Unknown mode." ); 224 } 225 } 226 227 228 public synchronized void readExternal( ObjectInput in ) throws IOException, ClassNotFoundException { 229 mode = in.readByte(); 230 switch (mode) { 231 case MODE_WRITE: { 232 chunk = (byte[])in.readObject(); 233 break; 234 } 235 case MODE_READ: { 236 size = in.readInt(); 237 rootID = (ObjectID)in.readObject(); 238 break; 239 } 240 default: 241 throw new RuntimeException ( "Unknown mode." ); 242 } 243 } 244 245 } 246 247 248 252 final class RObjectOutputStream extends ObjectOutputStream { 253 254 private DxDeque deque; 255 256 257 public RObjectOutputStream( OutputStream out, DxDeque _deque ) throws IOException{ 258 super( out ); 259 deque = _deque; 260 261 System.out.println( getClass().getClassLoader() ); 262 enableReplaceObject( true ); 263 } 264 265 266 protected Object replaceObject( Object obj ) throws IOException { 267 String name = obj != null ? obj.getClass().getName() : "(null)"; 268 System.out.println( "*** replaceObject():" + name ); 269 270 if (obj instanceof OzoneProxy) { 271 System.out.println( "*** proxy!" ); 272 deque.pushTop( ((OzoneProxy)obj).remoteID() ); 273 } 274 275 return obj; 276 } 277 278 } 279 | Popular Tags |