KickJava   Java API By Example, From Geeks To Geeks.

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


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.Exceptions4;
26 import com.db4o.inside.slots.Slot;
27
28 /**
29  * representation to collect and hold all IDs of one class
30  */

31  public class ClassIndex extends YapMeta implements ReadWriteable {
32      
33      
34     private final YapClass _yapClass;
35      
36     /**
37      * contains TreeInt with object IDs
38      */

39     private TreeInt i_root;
40     
41     ClassIndex(YapClass yapClass){
42         _yapClass = yapClass;
43     }
44     
45     public void add(int a_id){
46         i_root = TreeInt.add(i_root, a_id);
47     }
48
49     public final int byteCount() {
50         return YapConst.INT_LENGTH * (Tree.size(i_root) + 1);
51     }
52
53     public final void clear() {
54         i_root = null;
55     }
56     
57     void ensureActive(Transaction trans){
58         if (!isActive()) {
59             setStateDirty();
60             read(trans);
61         }
62     }
63
64     int entryCount(Transaction ta){
65         if(isActive() || isNew()){
66             return Tree.size(i_root);
67         }
68         Slot slot = ((YapFileTransaction)ta).getCurrentSlotOfID(getID());
69         int length = YapConst.INT_LENGTH;
70         if(Deploy.debug){
71             length += YapConst.LEADING_LENGTH;
72         }
73         YapReader reader = new YapReader(length);
74         reader.readEncrypt(ta.stream(), slot._address);
75         if (Deploy.debug) {
76             reader.readBegin(getIdentifier());
77         }
78         return reader.readInt();
79     }
80     
81     public final byte getIdentifier() {
82         return YapConst.YAPINDEX;
83     }
84     
85     TreeInt getRoot(){
86         return i_root;
87     }
88     
89     public final int ownLength() {
90         return YapConst.OBJECT_LENGTH + byteCount();
91     }
92
93     public final Object JavaDoc read(YapReader a_reader) {
94         throw Exceptions4.virtualException();
95     }
96
97     public final void readThis(Transaction a_trans, YapReader a_reader) {
98         i_root = (TreeInt)new TreeReader(a_reader, new TreeInt(0)).read();
99     }
100
101     public void remove(int a_id){
102         i_root = TreeInt.removeLike(i_root, a_id);
103     }
104
105     void setDirty(YapStream a_stream) {
106         // TODO: get rid of the setDirty call
107
a_stream.setDirtyInSystemTransaction(this);
108     }
109
110     public void write(YapReader a_writer) {
111         writeThis(null, a_writer);
112     }
113
114     public final void writeThis(Transaction trans, final YapReader a_writer) {
115         TreeInt.write(a_writer, i_root);
116     }
117     
118     public String JavaDoc toString(){
119         if(! Debug4.prettyToStrings){
120             return super.toString();
121         }
122         return _yapClass + " index";
123     }
124 }
Popular Tags