KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > YapMeta


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

31 public abstract class YapMeta {
32
33     /**
34      * @moveto new com.db4o.inside.Persistent interface
35      * all four of the following abstract methods
36      */

37     public abstract byte getIdentifier();
38     
39     public abstract int ownLength();
40     
41     public abstract void readThis(Transaction trans, YapReader reader);
42     
43     public abstract void writeThis(Transaction trans, YapReader writer);
44
45     
46     protected int i_id; // UID and address of pointer to the object in our file
47

48     protected int i_state = 2; // DIRTY and ACTIVE
49

50     final boolean beginProcessing() {
51         if (bitIsTrue(YapConst.PROCESSING)) {
52             return false;
53         }
54         bitTrue(YapConst.PROCESSING);
55         return true;
56     }
57
58     final void bitFalse(int bitPos) {
59         i_state &= ~(1 << bitPos);
60     }
61     
62     final boolean bitIsFalse(int bitPos) {
63         return (i_state | (1 << bitPos)) != i_state;
64     }
65
66     final boolean bitIsTrue(int bitPos) {
67         return (i_state | (1 << bitPos)) == i_state;
68     }
69
70     final void bitTrue(int bitPos) {
71         i_state |= (1 << bitPos);
72     }
73
74     void cacheDirty(Collection4 col) {
75         if (!bitIsTrue(YapConst.CACHED_DIRTY)) {
76             bitTrue(YapConst.CACHED_DIRTY);
77             col.add(this);
78         }
79     }
80
81     public void endProcessing() {
82         bitFalse(YapConst.PROCESSING);
83     }
84
85     public int getID() {
86         return i_id;
87     }
88
89     public final boolean isActive() {
90         return bitIsTrue(YapConst.ACTIVE);
91     }
92
93     public boolean isDirty() {
94         return bitIsTrue(YapConst.ACTIVE) && (!bitIsTrue(YapConst.CLEAN));
95     }
96     
97     public final boolean isNew(){
98         return i_id == 0;
99     }
100
101     public int linkLength() {
102         return YapConst.ID_LENGTH;
103     }
104
105     final void notCachedDirty() {
106         bitFalse(YapConst.CACHED_DIRTY);
107     }
108
109     public void read(Transaction trans) {
110         try {
111             if (beginProcessing()) {
112                 YapReader reader = trans.stream().readReaderByID(trans, getID());
113                 if (reader != null) {
114                     if (Deploy.debug) {
115                         reader.readBegin(getIdentifier());
116                     }
117                     readThis(trans, reader);
118                     setStateOnRead(reader);
119                 }
120                 endProcessing();
121             }
122         } catch (Throwable JavaDoc t) {
123             if (Debug.atHome) {
124                 t.printStackTrace();
125             }
126         }
127     }
128     
129     public void setID(int a_id) {
130         if(DTrace.enabled){
131             DTrace.YAPMETA_SET_ID.log(a_id);
132         }
133         i_id = a_id;
134     }
135
136     public final void setStateClean() {
137         bitTrue(YapConst.ACTIVE);
138         bitTrue(YapConst.CLEAN);
139     }
140
141     public final void setStateDeactivated() {
142         bitFalse(YapConst.ACTIVE);
143     }
144
145     public void setStateDirty() {
146         bitTrue(YapConst.ACTIVE);
147         bitFalse(YapConst.CLEAN);
148     }
149
150     void setStateOnRead(YapReader reader) {
151         if (Deploy.debug) {
152             reader.readEnd();
153         }
154         if (bitIsTrue(YapConst.CACHED_DIRTY)) {
155             setStateDirty();
156         } else {
157             setStateClean();
158         }
159     }
160
161     public final void write(Transaction trans) {
162         
163         if (! writeObjectBegin()) {
164             return;
165         }
166         
167         if(DTrace.enabled){
168             DTrace.YAPMETA_WRITE.log(getID());
169         }
170             
171         YapFile stream = (YapFile)trans.stream();
172         
173         int address = 0;
174         int length = ownLength();
175         
176         YapReader writer = new YapReader(length);
177         
178         if(isNew()){
179             Pointer4 ptr = stream.newSlot(trans, length);
180             setID(ptr._id);
181             address = ptr._address;
182             
183             // FIXME: Free everything on rollback here ?
184
}else{
185             address = stream.getSlot(length);
186             trans.slotFreeOnRollbackCommitSetPointer(i_id, address, length);
187         }
188         
189         if (Deploy.debug) {
190             writer.writeBegin(getIdentifier());
191         }
192
193         writeThis(trans, writer);
194
195         if (Deploy.debug) {
196             writer.writeEnd();
197         }
198
199         writer.writeEncrypt(stream, address, 0);
200
201         if (isActive()) {
202             setStateClean();
203         }
204         endProcessing();
205
206     }
207
208     public boolean writeObjectBegin() {
209         if (isDirty()) {
210             return beginProcessing();
211         }
212         return false;
213     }
214
215     public void writeOwnID(Transaction trans, YapReader writer) {
216         write(trans);
217         writer.writeInt(getID());
218     }
219
220
221 }
222
Popular Tags