KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > YapArray


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

33 public class YapArray extends YapIndependantType {
34     
35     public final TypeHandler4 i_handler;
36     public final boolean i_isPrimitive;
37     public final ReflectArray _reflectArray;
38
39     public YapArray(YapStream stream, TypeHandler4 a_handler, boolean a_isPrimitive) {
40         super(stream);
41         i_handler = a_handler;
42         i_isPrimitive = a_isPrimitive;
43         _reflectArray = stream.reflector().array();
44     }
45
46     public Object JavaDoc[] allElements(Object JavaDoc a_object) {
47         Object JavaDoc[] all = new Object JavaDoc[_reflectArray.getLength(a_object)];
48         for (int i = all.length - 1; i >= 0; i--) {
49             all[i] = _reflectArray.get(a_object, i);
50         }
51         return all;
52     }
53
54     public boolean canHold(ReflectClass claxx) {
55         return i_handler.canHold(claxx);
56     }
57
58     public final void cascadeActivation(
59         Transaction a_trans,
60         Object JavaDoc a_object,
61         int a_depth,
62         boolean a_activate) {
63         // We simply activate all Objects here
64
if (i_handler instanceof YapClass) {
65             
66             a_depth --;
67             
68             Object JavaDoc[] all = allElements(a_object);
69             if (a_activate) {
70                 for (int i = all.length - 1; i >= 0; i--) {
71                     _stream.stillToActivate(all[i], a_depth);
72                 }
73             } else {
74                 for (int i = all.length - 1; i >= 0; i--) {
75                   _stream.stillToDeactivate(all[i], a_depth, false);
76                 }
77             }
78         }
79     }
80     
81     public ReflectClass classReflector(){
82         return i_handler.classReflector();
83     }
84
85     final TreeInt collectIDs(MarshallerFamily mf, TreeInt tree, YapWriter reader){
86         return mf._array.collectIDs(this, tree, reader);
87     }
88     
89     public final TreeInt collectIDs1(Transaction trans, TreeInt tree,
90             YapReader reader) {
91         if (reader == null) {
92             return tree;
93         }
94         if (Deploy.debug) {
95             reader.readBegin(identifier());
96         }
97         int count = elementCount(trans, reader);
98         for (int i = 0; i < count; i++) {
99             tree = (TreeInt) Tree.add(tree, new TreeInt(reader.readInt()));
100         }
101         return tree;
102     }
103     
104     public Object JavaDoc comparableObject(Transaction a_trans, Object JavaDoc a_object){
105         throw Exceptions4.virtualException();
106     }
107
108     public final void deleteEmbedded(MarshallerFamily mf, YapWriter a_bytes) {
109         mf._array.deleteEmbedded(this, a_bytes);
110     }
111
112     
113     // FIXME: This code has not been called in any test case when the
114
// new ArrayMarshaller was written.
115
// Apparently it only frees slots.
116
// For now the code simply returns without freeing.
117
public final void deletePrimitiveEmbedded(
118         
119         YapWriter a_bytes,
120         YapClassPrimitive a_classPrimitive) {
121         
122         int address = a_bytes.readInt();
123         int length = a_bytes.readInt();
124         
125
126         if(true){
127             return;
128         }
129         
130         
131 // if (address > 0) {
132
// Transaction trans = a_bytes.getTransaction();
133
// YapReader bytes =
134
// a_bytes.getStream().readWriterByAddress(trans, address, length);
135
// if (bytes != null) {
136
// if (Deploy.debug) {
137
// bytes.readBegin(identifier());
138
// }
139
// for (int i = elementCount(trans, bytes); i > 0; i--) {
140
// int id = bytes.readInt();
141
// Slot slot = trans.getCurrentSlotOfID(id);
142
//
143
// a_classPrimitive.free(trans, id, slot._address,slot._length);
144
// }
145
// }
146
//
147
// trans.slotFreeOnCommit(address, address, length);
148
// }
149
}
150
151     public int elementCount(Transaction a_trans, SlotReader reader) {
152         int typeOrLength = reader.readInt();
153         if (typeOrLength >= 0) {
154             return typeOrLength;
155         }
156         return reader.readInt();
157     }
158
159     public final boolean equals(TypeHandler4 a_dataType) {
160         if (a_dataType instanceof YapArray) {
161             if (((YapArray) a_dataType).identifier() == identifier()) {
162                 return (i_handler.equals(((YapArray) a_dataType).i_handler));
163             }
164         }
165         return false;
166     }
167
168     public final int getID() {
169         return i_handler.getID();
170     }
171
172     public int getTypeID() {
173         return i_handler.getTypeID();
174     }
175
176     public YapClass getYapClass(YapStream a_stream) {
177         return i_handler.getYapClass(a_stream);
178     }
179
180     public byte identifier() {
181         return YapConst.YAPARRAY;
182     }
183     
184     public Object JavaDoc indexEntryToObject(Transaction trans, Object JavaDoc indexEntry){
185         return null; // not supported
186
}
187     
188     public boolean indexNullHandling() {
189         return i_handler.indexNullHandling();
190     }
191     
192     public int isSecondClass(){
193         return i_handler.isSecondClass();
194     }
195     
196     public void calculateLengths(Transaction trans, ObjectHeaderAttributes header, boolean topLevel, Object JavaDoc obj, boolean withIndirection) {
197         MarshallerFamily.current()._array.calculateLengths(trans, header, this, obj, topLevel);
198     }
199
200     public int objectLength(Object JavaDoc obj) {
201         return ownLength(obj) + (_reflectArray.getLength(obj) * i_handler.linkLength());
202     }
203
204     
205     public int ownLength(Object JavaDoc obj){
206         return ownLength();
207     }
208
209     private int ownLength() {
210         return YapConst.OBJECT_LENGTH + YapConst.INT_LENGTH * 2;
211     }
212     
213     public void prepareComparison(Transaction a_trans, Object JavaDoc obj) {
214         prepareComparison(obj);
215     }
216
217     public final Object JavaDoc read(MarshallerFamily mf, YapWriter a_bytes, boolean redirect) throws CorruptionException{
218         return mf._array.read(this, a_bytes);
219     }
220     
221     public Object JavaDoc readIndexEntry(YapReader a_reader) {
222         // TODO: implement
223
throw Exceptions4.virtualException();
224     }
225     
226     public final Object JavaDoc readQuery(Transaction a_trans, MarshallerFamily mf, boolean withRedirection, YapReader a_reader, boolean a_toArray) throws CorruptionException{
227         return mf._array.readQuery(this, a_trans, a_reader);
228     }
229     
230     public Object JavaDoc read1Query(Transaction a_trans, MarshallerFamily mf, YapReader a_reader) throws CorruptionException{
231
232         if(Deploy.debug){
233             a_reader.readBegin(identifier());
234         }
235
236         int[] elements = new int[1];
237         Object JavaDoc ret = readCreate(a_trans, a_reader, elements);
238         if(ret != null){
239             for (int i = 0; i < elements[0]; i++) {
240                 _reflectArray.set(ret, i, i_handler.readQuery(a_trans, mf, true, a_reader, true));
241             }
242         }
243         if (Deploy.debug) {
244             a_reader.readEnd();
245         }
246         
247         return ret;
248     }
249
250     public Object JavaDoc read1(MarshallerFamily mf, YapWriter reader) throws CorruptionException{
251         
252         if (Deploy.debug) {
253             reader.readBegin(identifier());
254         }
255
256         int[] elements = new int[1];
257         Object JavaDoc ret = readCreate(reader.getTransaction(), reader, elements);
258         if (ret != null){
259             if(i_handler.readArray(ret, reader)){
260                 return ret;
261             }
262             for (int i = 0; i < elements[0]; i++) {
263                 _reflectArray.set(ret, i, i_handler.read(mf, reader, true));
264             }
265         }
266         
267         if (Deploy.debug) {
268             reader.readEnd();
269         }
270
271         return ret;
272     }
273
274     private Object JavaDoc readCreate(Transaction a_trans, YapReader a_reader, int[] a_elements) {
275         ReflectClass[] clazz = new ReflectClass[1];
276         a_elements[0] = readElementsAndClass(a_trans, a_reader, clazz);
277         if (i_isPrimitive) {
278             return _reflectArray.newInstance(i_handler.primitiveClassReflector(), a_elements[0]);
279         }
280         if (clazz[0] != null) {
281             return _reflectArray.newInstance(clazz[0], a_elements[0]);
282         }
283         return null;
284     }
285
286     public TypeHandler4 readArrayHandler(Transaction a_trans, MarshallerFamily mf, YapReader[] a_bytes) {
287         return this;
288     }
289
290     public void readCandidates(MarshallerFamily mf, YapReader reader, QCandidates candidates) {
291         mf._array.readCandidates(this, reader, candidates);
292     }
293     
294     public void read1Candidates(MarshallerFamily mf, YapReader reader, QCandidates candidates) {
295         if(Deploy.debug){
296             reader.readBegin(identifier());
297         }
298         
299         int[] elements = new int[1];
300         Object JavaDoc ret = readCreate(candidates.i_trans, reader, elements);
301         if(ret != null){
302             for (int i = 0; i < elements[0]; i++) {
303                 QCandidate qc = i_handler.readSubCandidate(mf, reader, candidates, true);
304                 if(qc != null){
305                     candidates.addByIdentity(qc);
306                 }
307             }
308         }
309     }
310     
311     public QCandidate readSubCandidate(MarshallerFamily mf, YapReader reader, QCandidates candidates, boolean withIndirection) {
312         reader.incrementOffset(linkLength());
313         
314         return null;
315         
316         // TODO: Here we should theoretically read through the array and collect candidates.
317
// The respective construct is wild: "Contains query through an array in an array."
318
// Ignore for now.
319

320     }
321     
322     final int readElementsAndClass(Transaction a_trans, YapReader a_bytes, ReflectClass[] clazz){
323         int elements = a_bytes.readInt();
324         if (elements < 0) {
325             clazz[0]=reflectClassFromElementsEntry(a_trans, elements);
326             elements = a_bytes.readInt();
327         }
328         else {
329             clazz[0]=i_handler.classReflector();
330         }
331         if(Debug.exceedsMaximumArrayEntries(elements, i_isPrimitive)){
332             return 0;
333         }
334         return elements;
335     }
336
337    final protected int mapElementsEntry(int orig,IDMapping mapping) {
338         if(orig>=0||orig==YapConst.IGNORE_ID) {
339             return orig;
340         }
341         boolean primitive=!Deploy.csharp&&orig<YapConst.PRIMITIVE;
342         if(primitive) {
343             orig-=YapConst.PRIMITIVE;
344         }
345         int origID=-orig;
346         int mappedID=mapping.mappedID(origID);
347         int mapped=-mappedID;
348         if(primitive) {
349             mapped+=YapConst.PRIMITIVE;
350         }
351         return mapped;
352     }
353     
354     private ReflectClass reflectClassFromElementsEntry(Transaction a_trans,int elements) {
355
356         // TODO: Here is a low-frequency mistake, extremely unlikely.
357
// If YapClass-ID == 99999 by accident then we will get ignore.
358

359         if(elements != YapConst.IGNORE_ID){
360             boolean primitive = false;
361             if(!Deploy.csharp){
362                 if(elements < YapConst.PRIMITIVE){
363                     primitive = true;
364                     elements -= YapConst.PRIMITIVE;
365                 }
366             }
367             int classID = - elements;
368             YapClass yc = a_trans.stream().getYapClass(classID);
369             if (yc != null) {
370                 return (primitive ? yc.primitiveClassReflector() : yc.classReflector());
371             }
372         }
373         return i_handler.classReflector();
374     }
375     
376     static Object JavaDoc[] toArray(YapStream a_stream, Object JavaDoc a_object) {
377         if (a_object != null) {
378             ReflectClass claxx = a_stream.reflector().forObject(a_object);
379             if (claxx.isArray()) {
380                 YapArray ya;
381                 if(a_stream.reflector().array().isNDimensional(claxx)){
382                     ya = new YapArrayN(a_stream, null, false);
383                 } else {
384                     ya = new YapArray(a_stream, null, false);
385                 }
386                 return ya.allElements(a_object);
387             }
388         }
389         return new Object JavaDoc[0];
390     }
391
392     void writeClass(Object JavaDoc a_object, YapWriter a_bytes){
393         int yapClassID = 0;
394         
395         Reflector reflector = a_bytes.i_trans.reflector();
396         
397         ReflectClass claxx = _reflectArray.getComponentType(reflector.forObject(a_object));
398         
399         boolean primitive = false;
400         if(! Deploy.csharp){
401             if(claxx.isPrimitive()){
402                 primitive = true;
403             }
404         }
405         YapStream stream = a_bytes.getStream();
406         if(primitive){
407             claxx = stream.i_handlers.handlerForClass(stream,claxx).classReflector();
408         }
409         YapClass yc = stream.produceYapClass(claxx);
410         if (yc != null) {
411             yapClassID = yc.getID();
412         }
413         if(yapClassID == 0){
414             
415             // TODO: This one is a terrible low-frequency blunder !!!
416
// If YapClass-ID == 99999 then we will get IGNORE back.
417
// Discovered on adding the primitives
418
yapClassID = - YapConst.IGNORE_ID;
419             
420         } else{
421             if(primitive){
422                 yapClassID -= YapConst.PRIMITIVE;
423             }
424         }
425
426         a_bytes.writeInt(- yapClassID);
427     }
428     
429     public void writeIndexEntry(YapReader a_writer, Object JavaDoc a_object) {
430         // TODO: implement
431
throw Exceptions4.virtualException();
432     }
433     
434     public final Object JavaDoc writeNew(MarshallerFamily mf, Object JavaDoc a_object, boolean topLevel, YapWriter a_bytes, boolean withIndirection, boolean restoreLinkOffset) {
435         return mf._array.writeNew(this, a_object, restoreLinkOffset, a_bytes);
436     }
437
438     public void writeNew1(Object JavaDoc obj, YapWriter writer) {
439         
440         if (Deploy.debug) {
441             writer.writeBegin(identifier());
442         }
443         
444         writeClass(obj, writer);
445         
446         int elements = _reflectArray.getLength(obj);
447         writer.writeInt(elements);
448         
449         if(! i_handler.writeArray(obj, writer)){
450             for (int i = 0; i < elements; i++) {
451                 i_handler.writeNew(MarshallerFamily.current(), _reflectArray.get(obj, i), false, writer, true, true);
452             }
453         }
454         
455         if (Deploy.debug) {
456             writer.writeEnd();
457         }
458         
459     }
460
461     // Comparison_______________________
462

463     public YapComparable prepareComparison(Object JavaDoc obj) {
464         i_handler.prepareComparison(obj);
465         return this;
466     }
467     
468     public Object JavaDoc current(){
469         return i_handler.current();
470     }
471     
472     public int compareTo(Object JavaDoc a_obj) {
473         return -1;
474     }
475     
476     public boolean isEqual(Object JavaDoc obj) {
477         if(obj == null){
478             return false;
479         }
480         Object JavaDoc[] compareWith = allElements(obj);
481         for (int j = 0; j < compareWith.length; j++) {
482             if (i_handler.isEqual(compareWith[j])) {
483                 return true;
484             }
485         }
486         return false;
487     }
488
489     public boolean isGreater(Object JavaDoc obj) {
490         Object JavaDoc[] compareWith = allElements(obj);
491         for (int j = 0; j < compareWith.length; j++) {
492             if (i_handler.isGreater(compareWith[j])) {
493                 return true;
494             }
495         }
496         return false;
497     }
498
499     public boolean isSmaller(Object JavaDoc obj) {
500         Object JavaDoc[] compareWith = allElements(obj);
501         for (int j = 0; j < compareWith.length; j++) {
502             if (i_handler.isSmaller(compareWith[j])) {
503                 return true;
504             }
505         }
506         return false;
507     }
508
509     public boolean supportsIndex() {
510         return false;
511     }
512     
513     public final void defrag(MarshallerFamily mf, ReaderPair readers, boolean redirect) {
514         if(!(i_handler.isSecondClass()==YapConst.YES)) {
515             mf._array.defragIDs(this, readers);
516         }
517         else {
518             readers.incrementOffset(linkLength());
519         }
520     }
521
522     public void defrag1(MarshallerFamily mf,ReaderPair readers) {
523         if (Deploy.debug) {
524             readers.readBegin(identifier());
525         }
526         int elements = readElementsDefrag(readers);
527         for (int i = 0; i < elements; i++) {
528             i_handler.defrag(mf,readers, true);
529         }
530         if (Deploy.debug) {
531             readers.readEnd();
532         }
533     }
534
535     protected int readElementsDefrag(ReaderPair readers) {
536         int elements = readers.source().readInt();
537         readers.target().writeInt(mapElementsEntry(elements,readers.mapping()));
538         if (elements < 0) {
539             elements = readers.readInt();
540         }
541         return elements;
542     }
543
544     public void defragIndexEntry(ReaderPair readers) {
545         throw Exceptions4.virtualException();
546     }
547
548 }
549
Popular Tags