KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > inside > classindex > BTreeClassIndexStrategy


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.inside.classindex;
22
23 import com.db4o.*;
24 import com.db4o.foundation.*;
25 import com.db4o.inside.btree.*;
26
27 /**
28  * @exclude
29  */

30 public class BTreeClassIndexStrategy extends AbstractClassIndexStrategy {
31     
32     private BTree _btreeIndex;
33     
34     public BTreeClassIndexStrategy(YapClass yapClass) {
35         super(yapClass);
36     }
37     
38     public BTree btree() {
39         return _btreeIndex;
40     }
41
42     public int entryCount(Transaction ta) {
43         return _btreeIndex != null
44             ? _btreeIndex.size(ta)
45             : 0;
46     }
47
48     public void initialize(YapStream stream) {
49         createBTreeIndex(stream, 0);
50     }
51
52     public void purge() {
53     }
54
55     public void read(YapStream stream, int indexID) {
56         readBTreeIndex(stream, indexID);
57     }
58
59     public int write(Transaction trans) {
60         if (_btreeIndex == null){
61             return 0;
62         }
63         _btreeIndex.write(trans);
64         return _btreeIndex.getID();
65     }
66     
67     public void traverseAll(Transaction ta,Visitor4 command) {
68         // better alternatives for this null check? (has been moved as is from YapFile)
69
if(_btreeIndex!=null) {
70             _btreeIndex.traverseKeys(ta,command);
71         }
72     }
73
74     private void createBTreeIndex(final YapStream stream, int btreeID){
75         if (stream.isClient()) {
76             return;
77         }
78         _btreeIndex = ((YapFile)stream).createBTreeClassIndex(btreeID);
79         _btreeIndex.setRemoveListener(new Visitor4() {
80             public void visit(Object JavaDoc obj) {
81                 int id = ((Integer JavaDoc)obj).intValue();
82                 YapObject yo = stream.getYapObject(id);
83                 if (yo != null) {
84                     stream.removeReference(yo);
85                 }
86             }
87         });
88     }
89     
90     private void readBTreeIndex(YapStream stream, int indexId) {
91         if(! stream.isClient() && _btreeIndex == null){
92             createBTreeIndex(stream, indexId);
93         }
94     }
95
96     protected void internalAdd(Transaction trans, int id) {
97         _btreeIndex.add(trans, new Integer JavaDoc(id));
98     }
99
100     protected void internalRemove(Transaction ta, int id) {
101         _btreeIndex.remove(ta, new Integer JavaDoc(id));
102     }
103
104     public void dontDelete(Transaction transaction, int id) {
105     }
106     
107     public void defragReference(YapClass yapClass, ReaderPair readers,int classIndexID) {
108         int newID = -classIndexID;
109         readers.writeInt(newID);
110     }
111     
112     public int id() {
113         return _btreeIndex.getID();
114     }
115
116     public Iterator4 allSlotIDs(Transaction trans) {
117         return _btreeIndex.allNodeIds(trans);
118     }
119
120     public void defragIndex(ReaderPair readers) {
121         _btreeIndex.defragIndex(readers);
122     }
123     
124     public static BTree btree(YapClass clazz) {
125         ClassIndexStrategy index = clazz.index();
126         if(! (index instanceof BTreeClassIndexStrategy)){
127             throw new IllegalStateException JavaDoc();
128         }
129         return ((BTreeClassIndexStrategy)index).btree();
130     }
131     
132     public static Iterator4 iterate(YapClass clazz, Transaction trans) {
133         return btree(clazz).asRange(trans).keys();
134     }
135     
136     
137     
138 }
139
Popular Tags