KickJava   Java API By Example, From Geeks To Geeks.

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


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 import com.db4o.inside.ix.*;
27 import com.db4o.inside.mapping.*;
28
29 /**
30  * BTree mapping for IDs during a defragmentation run.
31  *
32  * @see Defragment
33  */

34 class BTreeIDMapping extends AbstractContextIDMapping {
35
36     private String JavaDoc _fileName;
37
38     private YapFile _mappingDb;
39
40     private BTree _idTree;
41
42     private MappedIDPair _cache = new MappedIDPair(0, 0);
43
44     /**
45      * Will maintain the ID mapping as a BTree in the file with the given path.
46      * If a file exists in this location, it will be DELETED.
47      *
48      * @param fileName The location where the BTree file should be created.
49      */

50     public BTreeIDMapping(String JavaDoc fileName) {
51         _fileName = fileName;
52     }
53
54     public int mappedID(int oldID, boolean lenient) {
55         if (_cache.orig() == oldID) {
56             return _cache.mapped();
57         }
58         int classID = mappedClassID(oldID);
59         if (classID != 0) {
60             return classID;
61         }
62         BTreeRange range = _idTree.search(trans(), new MappedIDPair(oldID, 0));
63         Iterator4 pointers = range.pointers();
64         if (pointers.moveNext()) {
65             BTreePointer pointer = (BTreePointer) pointers.current();
66             _cache = (MappedIDPair) pointer.key();
67             return _cache.mapped();
68         }
69         if (lenient) {
70             return mapLenient(oldID, range);
71         }
72         return 0;
73     }
74
75     private int mapLenient(int oldID, BTreeRange range) {
76         range = range.smaller();
77         BTreePointer pointer = range.lastPointer();
78         if (pointer == null) {
79             return 0;
80         }
81         MappedIDPair mappedIDs = (MappedIDPair) pointer.key();
82         return mappedIDs.mapped() + (oldID - mappedIDs.orig());
83     }
84
85     protected void mapNonClassIDs(int origID, int mappedID) {
86         _cache = new MappedIDPair(origID, mappedID);
87         _idTree.add(trans(), _cache);
88     }
89
90     public void open() {
91         _mappingDb = DefragContextImpl.freshYapFile(_fileName);
92         Indexable4 handler = new MappedIDPairHandler(_mappingDb);
93         _idTree = new BTree(trans(), 0, handler);
94     }
95
96     public void close() {
97         _mappingDb.close();
98     }
99
100     private Transaction trans() {
101         return _mappingDb.getSystemTransaction();
102     }
103 }
Popular Tags