KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > YapClassPrimitive


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.marshall.*;
25 import com.db4o.reflect.*;
26
27 /**
28  * @exclude
29  */

30 public class YapClassPrimitive extends YapClass{
31     
32     public final TypeHandler4 i_handler;
33     
34     YapClassPrimitive(YapStream a_stream, TypeHandler4 a_handler) {
35         super(a_stream, a_handler.classReflector());
36         i_fields = YapField.EMPTY_ARRAY;
37         i_handler = a_handler;
38     }
39
40     void activateFields(Transaction a_trans, Object JavaDoc a_object, int a_depth) {
41         // Override
42
// do nothing
43
}
44
45
46     final void addToIndex(YapFile a_stream, Transaction a_trans, int a_id) {
47         // Override
48
// Primitive Indices will be created later.
49
}
50
51     boolean allowsQueries() {
52         return false;
53     }
54
55     void cacheDirty(Collection4 col) {
56         // do nothing
57
}
58
59     public boolean canHold(ReflectClass claxx) {
60         // Do we need this at all???
61
// Check if this method is ever called
62
return i_handler.canHold(claxx);
63     }
64     
65     public ReflectClass classReflector(){
66         return i_handler.classReflector();
67     }
68     
69     public void deleteEmbedded(MarshallerFamily mf, YapWriter a_bytes) {
70         if(mf._primitive.useNormalClassRead()){
71             super.deleteEmbedded(mf, a_bytes);
72             return;
73         }
74         
75         // Do nothing here, we should be in the payload area
76
// no action to be taken.
77
}
78
79     
80     public void deleteEmbedded1(MarshallerFamily mf, YapWriter a_bytes, int a_id) {
81         
82         if(i_handler instanceof YapArray){
83             YapArray ya = (YapArray)i_handler;
84             
85             // TODO: the following checks, whether the array stores
86
// primitives. There is one case that is not covered here:
87
// If a primitive array is stored to an untyped array or
88
// to an Object variable, they would need to be deleted
89
// and freed also. However, if they are untyped, every
90
// single one would have to be read an checked and this
91
// would be extremely slow.
92

93             // Solution: Store information, whether an object is
94
// primitive in our pointers, in the highest bit of the
95
// length int.
96

97             if(ya.i_isPrimitive){
98                 ya.deletePrimitiveEmbedded(a_bytes, this);
99                 a_bytes.slotDelete();
100                 return;
101             }
102         }
103         
104        if(i_handler instanceof YapClassAny){
105             // Any-In-Any: Ignore delete
106
a_bytes.incrementOffset(i_handler.linkLength());
107         }else{
108             i_handler.deleteEmbedded(mf, a_bytes);
109         }
110         
111         // TODO: Was this freeing call necessary?
112
// free(a_bytes.getTransaction(), a_id, a_bytes.getAddress(), a_bytes.getLength());
113

114         free(a_bytes, a_id);
115             
116     }
117
118     void deleteMembers(MarshallerFamily mf, ObjectHeaderAttributes attributes, YapWriter a_bytes, int a_type, boolean isUpdate) {
119         if (a_type == YapConst.TYPE_ARRAY) {
120             new YapArray(a_bytes.getStream(),this, true).deletePrimitiveEmbedded(a_bytes, this);
121         } else if (a_type == YapConst.TYPE_NARRAY) {
122             new YapArrayN(a_bytes.getStream(),this, true).deletePrimitiveEmbedded(a_bytes, this);
123         }
124     }
125     
126     final void free(Transaction a_trans, int a_id, int a_address, int a_length) {
127         a_trans.slotFreePointerOnCommit(a_id, a_address, a_length);
128     }
129     
130     final void free(YapWriter a_bytes, int a_id) {
131           a_bytes.getTransaction().slotFreePointerOnCommit(a_id, a_bytes.getAddress(), a_bytes.getLength());
132     }
133     
134     public boolean hasIndex() {
135         return false;
136     }
137
138     Object JavaDoc instantiate(YapObject a_yapObject, Object JavaDoc a_object, MarshallerFamily mf, ObjectHeaderAttributes attributes, YapWriter a_bytes, boolean a_addToIDTree) {
139         if (a_object == null) {
140             try {
141                 a_object = i_handler.read(mf, a_bytes, true);
142             } catch (CorruptionException ce) {
143                 return null;
144             }
145             a_yapObject.setObjectWeak(a_bytes.getStream(), a_object);
146         }
147         a_yapObject.setStateClean();
148         return a_object;
149     }
150     
151     Object JavaDoc instantiateTransient(YapObject a_yapObject, Object JavaDoc a_object, MarshallerFamily mf, ObjectHeaderAttributes attributes, YapWriter a_bytes) {
152         try {
153             return i_handler.read(mf, a_bytes, true);
154         } catch (CorruptionException ce) {
155             return null;
156         }
157     }
158
159     void instantiateFields(YapObject a_yapObject, Object JavaDoc a_onObject, MarshallerFamily mf, ObjectHeaderAttributes attributes, YapWriter a_bytes) {
160         Object JavaDoc obj = null;
161         try {
162             obj = i_handler.read(mf, a_bytes, true);
163         } catch (CorruptionException ce) {
164             
165         }
166         if (obj != null) {
167             i_handler.copyValue(obj, a_onObject);
168         }
169     }
170
171     public boolean isArray() {
172         return i_id == YapHandlers.ANY_ARRAY_ID || i_id == YapHandlers.ANY_ARRAY_N_ID;
173     }
174     
175     public boolean isPrimitive(){
176         return true;
177     }
178     
179     public int isSecondClass(){
180         return YapConst.UNKNOWN;
181     }
182     
183     boolean isStrongTyped(){
184         return false;
185     }
186     
187     public void calculateLengths(Transaction trans, ObjectHeaderAttributes header, boolean topLevel, Object JavaDoc obj, boolean withIndirection) {
188         i_handler.calculateLengths(trans, header, topLevel, obj, withIndirection);
189     }
190     
191     public YapComparable prepareComparison(Object JavaDoc a_constraint) {
192         i_handler.prepareComparison(a_constraint);
193         return i_handler;
194     }
195     
196     public final ReflectClass primitiveClassReflector(){
197         return i_handler.primitiveClassReflector();
198     }
199     
200     public Object JavaDoc read(MarshallerFamily mf, YapWriter a_bytes, boolean redirect) throws CorruptionException{
201         if(mf._primitive.useNormalClassRead()){
202             return super.read(mf, a_bytes, redirect);
203         }
204         return i_handler.read(mf, a_bytes, false);
205     }
206
207     public TypeHandler4 readArrayHandler(Transaction a_trans, MarshallerFamily mf, YapReader[] a_bytes) {
208         if (isArray()) {
209             return i_handler;
210         }
211         return null;
212     }
213     
214     public Object JavaDoc readQuery(Transaction trans, MarshallerFamily mf, boolean withRedirection, YapReader reader, boolean toArray) throws CorruptionException{
215         if(mf._primitive.useNormalClassRead()){
216             return super.readQuery(trans, mf, withRedirection, reader, toArray);
217         }
218         return i_handler.readQuery(trans, mf, withRedirection, reader, toArray);
219     }
220
221     
222     public QCandidate readSubCandidate(MarshallerFamily mf, YapReader reader, QCandidates candidates, boolean withIndirection) {
223         return i_handler.readSubCandidate(mf, reader, candidates, withIndirection);
224     }
225
226     void removeFromIndex(Transaction ta, int id) {
227         // do nothing
228
}
229     
230     public boolean supportsIndex() {
231         return true;
232     }
233
234     public final boolean writeObjectBegin() {
235         return false;
236     }
237     
238     public Object JavaDoc writeNew(MarshallerFamily mf, Object JavaDoc a_object, boolean topLevel, YapWriter a_bytes, boolean withIndirection, boolean restoreLinkOffset) {
239         mf._primitive.writeNew(a_bytes.getTransaction(), this, a_object, topLevel, a_bytes, withIndirection, restoreLinkOffset);
240         return a_object;
241     }
242     
243     public String JavaDoc toString(){
244         return "Wraps " + i_handler.toString() + " in YapClassPrimitive";
245     }
246
247     public void defrag(MarshallerFamily mf, ReaderPair readers, boolean redirect) {
248         if(mf._primitive.useNormalClassRead()){
249             super.defrag(mf,readers, redirect);
250         }
251         else {
252             i_handler.defrag(mf, readers, false);
253         }
254     }
255 }
256
Popular Tags