001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.defragment;
022:
023: import com.db4o.*;
024: import com.db4o.foundation.*;
025: import com.db4o.internal.*;
026: import com.db4o.internal.btree.*;
027: import com.db4o.internal.slots.*;
028:
029: /**
030: * First step in the defragmenting process: Allocates pointer slots in the target file for
031: * each ID (but doesn't fill them in, yet) and registers the mapping from source pointer address
032: * to target pointer address.
033: *
034: * @exclude
035: */
036: final class FirstPassCommand implements PassCommand {
037: private final static int ID_BATCH_SIZE = 4096;
038:
039: private TreeInt _ids;
040:
041: void process(DefragContextImpl context, int objectID,
042: boolean isClassID) {
043: if (batchFull()) {
044: flush(context);
045: }
046: _ids = TreeInt.add(_ids, (isClassID ? -objectID : objectID));
047: }
048:
049: private boolean batchFull() {
050: return _ids != null && _ids.size() == ID_BATCH_SIZE;
051: }
052:
053: public void processClass(DefragContextImpl context,
054: ClassMetadata yapClass, int id, int classIndexID) {
055: process(context, id, true);
056: for (int fieldIdx = 0; fieldIdx < yapClass.i_fields.length; fieldIdx++) {
057: FieldMetadata field = yapClass.i_fields[fieldIdx];
058: if (!field.isVirtual() && field.hasIndex()) {
059: processBTree(context, field.getIndex(context
060: .systemTrans()));
061: }
062: }
063: }
064:
065: public void processObjectSlot(DefragContextImpl context,
066: ClassMetadata yapClass, int sourceID) {
067: process(context, sourceID, false);
068: }
069:
070: public void processClassCollection(DefragContextImpl context)
071: throws CorruptionException {
072: process(context, context.sourceClassCollectionID(), false);
073: }
074:
075: public void processBTree(final DefragContextImpl context,
076: final BTree btree) {
077: process(context, btree.getID(), false);
078: context.traverseAllIndexSlots(btree, new Visitor4() {
079: public void visit(Object obj) {
080: int id = ((Integer) obj).intValue();
081: process(context, id, false);
082: }
083: });
084: }
085:
086: public void flush(DefragContextImpl context) {
087: if (_ids == null) {
088: return;
089: }
090: int blockSize = context.blockSize();
091: int blockLength = Math.max(Const4.POINTER_LENGTH, blockSize);
092: boolean overlapping = (Const4.POINTER_LENGTH % blockSize > 0);
093: int blocksPerPointer = Const4.POINTER_LENGTH / blockSize;
094: if (overlapping) {
095: blocksPerPointer++;
096: }
097: int batchSize = _ids.size() * blockLength;
098: Slot pointerSlot = context.allocateTargetSlot(batchSize);
099: int pointerAddress = pointerSlot.address();
100: Iterator4 idIter = new TreeKeyIterator(_ids);
101: while (idIter.moveNext()) {
102: int objectID = ((Integer) idIter.current()).intValue();
103: boolean isClassID = false;
104: if (objectID < 0) {
105: objectID = -objectID;
106: isClassID = true;
107: }
108:
109: if (DefragmentConfig.DEBUG) {
110: int mappedID = context.mappedID(objectID, -1);
111: // seen object ids don't come by here anymore - any other candidates?
112: if (mappedID >= 0) {
113: throw new IllegalStateException();
114: }
115: }
116:
117: context.mapIDs(objectID, pointerAddress, isClassID);
118: pointerAddress += blocksPerPointer;
119: }
120: _ids = null;
121: }
122: }
|