1 19 package org.netbeans.mdr.persistence.btreeimpl.btreeindex; 20 import org.netbeans.mdr.persistence.btreeimpl.btreestorage.*; 21 import org.netbeans.mdr.persistence.SinglevaluedIndex; 22 import org.netbeans.mdr.persistence.Storage; 23 import org.netbeans.mdr.persistence.MOFID; 24 import org.netbeans.mdr.util.Logger; 25 26 32 33 public class MOFIDInfo extends EntryTypeInfo{ 34 35 private BtreeStorage storage; 36 37 MOFIDInfo(Storage storage) { 38 this.storage = (BtreeStorage) storage; 39 } 40 41 public String typeName() { 42 return "MOFID"; 43 } 44 45 58 public byte compare(byte[] key, byte[] buffer, int offset, int targetLength) { 59 60 byte k, b; 61 for (int i = 0; i < MOFID.LENGTH; i++) { 62 k = key[i]; 63 b = buffer[offset+i]; 64 if (k < b) { 65 return LESS; 66 } else if (k > b) { 67 return GREATER; 68 } 69 } 70 return EQUAL; 71 } 72 73 public int getLength() { 74 return MOFID.LENGTH; 75 } 76 77 public boolean isFixedLength() { 78 return true; 79 } 80 81 public byte[] toBuffer(Object object) { 82 83 try { 84 MOFID m = (MOFID) object; 85 if (m != null) { 86 byte[] mofBytes = new byte[MOFID.LENGTH]; 87 Converter.writeLong(mofBytes, 0, m.getSerialNumber()); 88 String storageId = m.getStorageID (); 89 int s; 90 if (storage.getStorageId ().equals(storageId)) { 91 s = BtreeFactory.SAME_PREFIX_CODE; 92 } else if (BtreeFactory.INTERNAL_PREFIX.equals (storageId)) { 93 s = BtreeFactory.INTERNAL_PREFIX_CODE; 94 } else { 95 s = storage.storageIdToNumber (storageId); 96 } 97 Converter.writeShort(mofBytes, 0, (short)s); 98 return mofBytes; 99 } 100 } catch (Exception e) { 101 Logger.getDefault().notify(Logger.INFORMATIONAL, e); 102 } 103 return null; 104 } 105 106 public Object fromBuffer(byte[] buffer) { 107 try { 108 int storageNumber = Converter.readShort (buffer, 0); 109 String storageId = null; 110 switch (storageNumber) { 111 case BtreeFactory.INTERNAL_PREFIX_CODE: 112 storageId = BtreeFactory.INTERNAL_PREFIX; 113 break; 114 case BtreeFactory.SAME_PREFIX_CODE: 115 storageId = storage.getStorageId (); 116 break; 117 default: 118 storageId = storage.numberToStorageId (storageNumber); 119 } 120 buffer [0] = 0; 121 buffer [1] = 0; 122 long serialNumber = Converter.readLong (buffer, 0); 123 return new MOFID (serialNumber, storageId); 124 } catch (Exception e) { 125 Logger.getDefault().notify(Logger.INFORMATIONAL, e); 126 return null; 127 } 128 } 129 130 public Object objectFromBuffer(byte[] buffer, SinglevaluedIndex repos) { 131 try { 132 return storage.resolveObject((MOFID) fromBuffer(buffer)); 133 } catch (Exception e) { 134 Logger.getDefault().notify(Logger.INFORMATIONAL, e); 135 } 136 return null; 137 } 138 } 139 | Popular Tags |