1 21 package com.db4o.defragment; 22 23 import com.db4o.*; 24 import com.db4o.foundation.*; 25 import com.db4o.inside.btree.*; 26 27 34 final class FirstPassCommand implements PassCommand { 35 private final static int ID_BATCH_SIZE=4096; 36 37 private TreeInt _ids; 38 39 void process(DefragContextImpl context, int objectID, boolean isClassID) { 40 if(batchFull()) { 41 flush(context); 42 } 43 _ids=TreeInt.add(_ids,(isClassID ? -objectID : objectID)); 44 } 45 46 private boolean batchFull() { 47 return _ids!=null&&_ids.size()==ID_BATCH_SIZE; 48 } 49 50 public void processClass(DefragContextImpl context, YapClass yapClass,int id,int classIndexID) { 51 process(context,id, true); 52 for (int fieldIdx = 0; fieldIdx < yapClass.i_fields.length; fieldIdx++) { 53 YapField field=yapClass.i_fields[fieldIdx]; 54 if(!field.isVirtual()&&field.hasIndex()) { 55 processBTree(context,field.getIndex(context.systemTrans())); 56 } 57 } 58 59 } 60 61 public void processObjectSlot(DefragContextImpl context, YapClass yapClass, int sourceID, boolean registerAddresses) { 62 process(context,sourceID, false); 63 } 64 65 public void processClassCollection(DefragContextImpl context) throws CorruptionException { 66 process(context,context.sourceClassCollectionID(), false); 67 } 68 69 public void processBTree(final DefragContextImpl context, final BTree btree) { 70 process(context,btree.getID(), false); 71 context.traverseAllIndexSlots(btree, new Visitor4() { 72 public void visit(Object obj) { 73 int id=((Integer )obj).intValue(); 74 process(context,id, false); 75 } 76 }); 77 } 78 79 public void flush(DefragContextImpl context) { 80 if(_ids==null) { 81 return; 82 } 83 int pointerAddress=context.allocateTargetSlot(_ids.size()*YapConst.POINTER_LENGTH); 84 Iterator4 idIter=new TreeKeyIterator(_ids); 85 while(idIter.moveNext()) { 86 int objectID=((Integer )idIter.current()).intValue(); 87 boolean isClassID=false; 88 if(objectID<0) { 89 objectID=-objectID; 90 isClassID=true; 91 } 92 93 if(DefragmentConfig.DEBUG){ 94 int mappedID = context.mappedID(objectID, -1); 95 if(mappedID>=0) { 97 throw new IllegalStateException (); 98 } 99 } 100 101 context.mapIDs(objectID,pointerAddress, isClassID); 102 pointerAddress+=YapConst.POINTER_LENGTH; 103 } 104 _ids=null; 105 } 106 } | Popular Tags |