KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > YapString


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.inside.marshall.*;
24 import com.db4o.inside.slots.Slot;
25 import com.db4o.reflect.*;
26
27
28 /**
29  * YapString
30  * Legacy rename for C# obfuscator production trouble
31  *
32  * @exclude
33  */

34 public final class YapString extends YapIndependantType {
35     
36     public YapStringIO i_stringIo;
37     
38     public YapString(YapStream stream, YapStringIO stringIO) {
39         super(stream);
40         i_stringIo = stringIO;
41     }
42     
43     public boolean canHold(ReflectClass claxx) {
44         return claxx.equals(classReflector());
45     }
46
47     public void cascadeActivation(
48         Transaction a_trans,
49         Object JavaDoc a_object,
50         int a_depth,
51         boolean a_activate) {
52         // default: do nothing
53
}
54
55     public ReflectClass classReflector(){
56         return _stream.i_handlers.ICLASS_STRING;
57     }
58
59     public Object JavaDoc comparableObject(Transaction a_trans, Object JavaDoc a_object){
60         if(a_object == null){
61             return null;
62         }
63         if(a_object instanceof YapReader){
64             return a_object;
65         }
66         Slot s = (Slot) a_object;
67         return a_trans.stream().readReaderByAddress(s._address, s._length);
68     }
69     
70     public void deleteEmbedded(MarshallerFamily mf, YapWriter a_bytes){
71         
72         int address = a_bytes.readInt();
73         int length = a_bytes.readInt();
74         
75         if(address > 0 && ! mf._string.inlinedStrings()){
76             a_bytes.getTransaction().slotFreeOnCommit(address, address, length);
77         }
78     }
79     
80     public boolean equals(TypeHandler4 a_dataType) {
81         return (this == a_dataType);
82     }
83
84     public int getID() {
85         return 9;
86     }
87
88     byte getIdentifier() {
89         return YapConst.YAPSTRING;
90     }
91
92     public YapClass getYapClass(YapStream a_stream) {
93         return a_stream.i_handlers.i_yapClasses[getID() - 1];
94     }
95     
96     public Object JavaDoc indexEntryToObject(Transaction trans, Object JavaDoc indexEntry){
97         try {
98             return StringMarshaller.readShort(_stream, (YapReader)indexEntry);
99         } catch (CorruptionException e) {
100             
101         }
102         return null;
103     }
104
105     public boolean indexNullHandling() {
106         return true;
107     }
108     
109     public int isSecondClass(){
110         return YapConst.YES;
111     }
112     
113     public void calculateLengths(Transaction trans, ObjectHeaderAttributes header, boolean topLevel, Object JavaDoc obj, boolean withIndirection){
114         MarshallerFamily.current()._string.calculateLengths(trans, header, topLevel, obj, withIndirection);
115     }
116
117     public Object JavaDoc read(MarshallerFamily mf, YapWriter a_bytes, boolean redirect) throws CorruptionException {
118         return mf._string.readFromParentSlot(a_bytes.getStream(), a_bytes, redirect);
119     }
120     
121     public TypeHandler4 readArrayHandler(Transaction a_trans, MarshallerFamily mf, YapReader[] a_bytes) {
122         // virtual and do nothing
123
return null;
124     }
125
126     public void readCandidates(MarshallerFamily mf, YapReader a_bytes, QCandidates a_candidates) {
127         // do nothing
128
}
129     
130     public QCandidate readSubCandidate(MarshallerFamily mf, YapReader reader, QCandidates candidates, boolean withIndirection) {
131         try {
132             Object JavaDoc obj = null;
133             if(withIndirection){
134                 obj = readQuery(candidates.i_trans, mf, withIndirection, reader, true);
135             }else{
136                 obj = mf._string.read(_stream, reader);
137             }
138             if(obj != null){
139                 return new QCandidate(candidates, obj, 0, true);
140             }
141         } catch (CorruptionException e) {
142         }
143         return null;
144     }
145
146
147     /**
148      * This readIndexEntry method reads from the parent slot.
149      * TODO: Consider renaming methods in Indexable4 and Typhandler4 to make direction clear.
150      */

151     public Object JavaDoc readIndexEntry(MarshallerFamily mf, YapWriter a_writer) throws CorruptionException{
152         return mf._string.readIndexEntry(a_writer);
153     }
154
155     /**
156      * This readIndexEntry method reads from the actual index in the file.
157      * TODO: Consider renaming methods in Indexable4 and Typhandler4 to make direction clear.
158      */

159     public Object JavaDoc readIndexEntry(YapReader reader) {
160         Slot s = new Slot(reader.readInt(), reader.readInt());
161         if (isInvalidSlot(s))
162             return null;
163         
164         return s;
165     }
166
167     private boolean isInvalidSlot(Slot slot) {
168         return (slot._address == 0) && (slot._length == 0);
169     }
170     
171     public Object JavaDoc readQuery(Transaction a_trans, MarshallerFamily mf, boolean withRedirection, YapReader a_reader, boolean a_toArray) throws CorruptionException{
172         if(! withRedirection){
173             return mf._string.read(a_trans.stream(), a_reader);
174         }
175         YapReader reader = mf._string.readSlotFromParentSlot(a_trans.stream(), a_reader);
176         if(a_toArray) {
177             if(reader != null) {
178                 return mf._string.readFromOwnSlot(a_trans.stream(), reader);
179             }
180         }
181         return reader;
182     }
183     
184     void setStringIo(YapStringIO a_io) {
185         i_stringIo = a_io;
186     }
187     
188     public boolean supportsIndex() {
189         return true;
190     }
191
192     public void writeIndexEntry(YapReader writer, Object JavaDoc entry) {
193         if(entry == null){
194             writer.writeInt(0);
195             writer.writeInt(0);
196             return;
197         }
198          if(entry instanceof YapWriter){
199              YapWriter entryAsWriter = (YapWriter)entry;
200              writer.writeInt(entryAsWriter.getAddress());
201              writer.writeInt(entryAsWriter.getLength());
202              return;
203          }
204          if(entry instanceof Slot){
205              Slot s = (Slot) entry;
206              writer.writeInt(s._address);
207              writer.writeInt(s._length);
208              return;
209          }
210          throw new IllegalArgumentException JavaDoc();
211     }
212     
213     public Object JavaDoc writeNew(MarshallerFamily mf, Object JavaDoc a_object, boolean topLevel, YapWriter a_bytes, boolean withIndirection, boolean restoreLinkeOffset) {
214         return mf._string.writeNew(a_object, topLevel, a_bytes, withIndirection);
215     }
216
217     final void writeShort(String JavaDoc a_string, YapReader a_bytes) {
218         if (a_string == null) {
219             a_bytes.writeInt(0);
220         } else {
221             a_bytes.writeInt(a_string.length());
222             i_stringIo.write(a_bytes, a_string);
223         }
224     }
225
226     public int getTypeID() {
227         return YapConst.TYPE_SIMPLE;
228     }
229
230
231     // Comparison_______________________
232

233     private YapReader i_compareTo;
234
235     private YapReader val(Object JavaDoc obj) {
236         if(obj instanceof YapReader) {
237             return (YapReader)obj;
238         }
239         if(obj instanceof String JavaDoc) {
240             return StringMarshaller.writeShort(_stream, (String JavaDoc)obj);
241         }
242         if(obj instanceof Slot){
243             Slot s = (Slot) obj;
244             return _stream.readReaderByAddress(s._address, s._length);
245         }
246         return null;
247     }
248     
249     public void prepareComparison(Transaction a_trans, Object JavaDoc obj) {
250         i_compareTo = (YapReader)obj;
251     }
252
253     public YapComparable prepareComparison(Object JavaDoc obj) {
254         if (obj == null) {
255             i_compareTo = null;
256             return Null.INSTANCE;
257         }
258         i_compareTo = val(obj);
259         return this;
260     }
261     
262     public Object JavaDoc current(){
263         return i_compareTo;
264     }
265     
266     public int compareTo(Object JavaDoc obj) {
267         if(i_compareTo == null) {
268             if(obj == null) {
269                 return 0;
270             }
271             return 1;
272         }
273         return compare(i_compareTo, val(obj));
274     }
275
276     public boolean isEqual(Object JavaDoc obj) {
277         if(i_compareTo == null){
278             return obj == null;
279         }
280         return i_compareTo.containsTheSame(val(obj));
281     }
282
283     public boolean isGreater(Object JavaDoc obj) {
284         if(i_compareTo == null){
285             // this should be called for indexing only
286
// object is always greater
287
return obj != null;
288         }
289         return compare(i_compareTo, val(obj)) > 0;
290     }
291
292     public boolean isSmaller(Object JavaDoc obj) {
293         if(i_compareTo == null){
294             // this should be called for indexing only
295
// object is always greater
296
return false;
297         }
298         return compare(i_compareTo, val(obj)) < 0;
299     }
300
301     /**
302      * returns: -x for left is greater and +x for right is greater
303      *
304      * TODO: You will need collators here for different languages.
305      */

306     final int compare(YapReader a_compare, YapReader a_with) {
307         if (a_compare == null) {
308             if (a_with == null) {
309                 return 0;
310             }
311             return 1;
312         }
313         if (a_with == null) {
314             return -1;
315         }
316         return compare(a_compare._buffer, a_with._buffer);
317     }
318     
319     static final int compare(byte[] compare, byte[] with){
320         int min = compare.length < with.length ? compare.length : with.length;
321         int start = YapConst.INT_LENGTH;
322         if(Deploy.debug) {
323             start += YapConst.LEADING_LENGTH;
324             min -= YapConst.BRACKETS_BYTES;
325         }
326         for(int i = start;i < min;i++) {
327             if (compare[i] != with[i]) {
328                 return with[i] - compare[i];
329             }
330         }
331         return with.length - compare.length;
332     }
333
334     public void defragIndexEntry(ReaderPair readers) {
335         // address
336
readers.copyID(false,true);
337         // length
338
readers.incrementIntSize();
339     }
340
341     public void defrag(MarshallerFamily mf, ReaderPair readers, boolean redirect) {
342         if(! redirect){
343             readers.incrementOffset(linkLength());
344         }
345         else {
346             mf._string.defrag(readers);
347         }
348     }
349 }
350
Popular Tags