1 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 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 a_object, 50 int a_depth, 51 boolean a_activate) { 52 } 54 55 public ReflectClass classReflector(){ 56 return _stream.i_handlers.ICLASS_STRING; 57 } 58 59 public Object comparableObject(Transaction a_trans, Object 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 indexEntryToObject(Transaction trans, Object 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 obj, boolean withIndirection){ 114 MarshallerFamily.current()._string.calculateLengths(trans, header, topLevel, obj, withIndirection); 115 } 116 117 public Object 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 return null; 124 } 125 126 public void readCandidates(MarshallerFamily mf, YapReader a_bytes, QCandidates a_candidates) { 127 } 129 130 public QCandidate readSubCandidate(MarshallerFamily mf, YapReader reader, QCandidates candidates, boolean withIndirection) { 131 try { 132 Object 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 151 public Object readIndexEntry(MarshallerFamily mf, YapWriter a_writer) throws CorruptionException{ 152 return mf._string.readIndexEntry(a_writer); 153 } 154 155 159 public Object 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 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 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 (); 211 } 212 213 public Object writeNew(MarshallerFamily mf, Object 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 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 233 private YapReader i_compareTo; 234 235 private YapReader val(Object obj) { 236 if(obj instanceof YapReader) { 237 return (YapReader)obj; 238 } 239 if(obj instanceof String ) { 240 return StringMarshaller.writeShort(_stream, (String )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 obj) { 250 i_compareTo = (YapReader)obj; 251 } 252 253 public YapComparable prepareComparison(Object 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 current(){ 263 return i_compareTo; 264 } 265 266 public int compareTo(Object 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 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 obj) { 284 if(i_compareTo == null){ 285 return obj != null; 288 } 289 return compare(i_compareTo, val(obj)) > 0; 290 } 291 292 public boolean isSmaller(Object obj) { 293 if(i_compareTo == null){ 294 return false; 297 } 298 return compare(i_compareTo, val(obj)) < 0; 299 } 300 301 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 readers.copyID(false,true); 337 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 |