KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > defragment > FirstPassCommand


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.defragment;
22
23 import com.db4o.*;
24 import com.db4o.foundation.*;
25 import com.db4o.inside.btree.*;
26         
27 /**
28  * First step in the defragmenting process: Allocates pointer slots in the target file for
29  * each ID (but doesn't fill them in, yet) and registers the mapping from source pointer address
30  * to target pointer address.
31  *
32  * @exclude
33  */

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 JavaDoc obj) {
73                 int id=((Integer JavaDoc)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 JavaDoc)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                 // seen object ids don't come by here anymore - any other candidates?
96
if(mappedID>=0) {
97                     throw new IllegalStateException JavaDoc();
98                 }
99             }
100             
101             context.mapIDs(objectID,pointerAddress, isClassID);
102             pointerAddress+=YapConst.POINTER_LENGTH;
103         }
104         _ids=null;
105     }
106 }
Popular Tags