1 21 package com.db4o; 22 23 import com.db4o.foundation.*; 24 import com.db4o.inside.slots.*; 25 26 31 public abstract class YapMeta { 32 33 37 public abstract byte getIdentifier(); 38 39 public abstract int ownLength(); 40 41 public abstract void readThis(Transaction trans, YapReader reader); 42 43 public abstract void writeThis(Transaction trans, YapReader writer); 44 45 46 protected int i_id; 48 protected int i_state = 2; 50 final boolean beginProcessing() { 51 if (bitIsTrue(YapConst.PROCESSING)) { 52 return false; 53 } 54 bitTrue(YapConst.PROCESSING); 55 return true; 56 } 57 58 final void bitFalse(int bitPos) { 59 i_state &= ~(1 << bitPos); 60 } 61 62 final boolean bitIsFalse(int bitPos) { 63 return (i_state | (1 << bitPos)) != i_state; 64 } 65 66 final boolean bitIsTrue(int bitPos) { 67 return (i_state | (1 << bitPos)) == i_state; 68 } 69 70 final void bitTrue(int bitPos) { 71 i_state |= (1 << bitPos); 72 } 73 74 void cacheDirty(Collection4 col) { 75 if (!bitIsTrue(YapConst.CACHED_DIRTY)) { 76 bitTrue(YapConst.CACHED_DIRTY); 77 col.add(this); 78 } 79 } 80 81 public void endProcessing() { 82 bitFalse(YapConst.PROCESSING); 83 } 84 85 public int getID() { 86 return i_id; 87 } 88 89 public final boolean isActive() { 90 return bitIsTrue(YapConst.ACTIVE); 91 } 92 93 public boolean isDirty() { 94 return bitIsTrue(YapConst.ACTIVE) && (!bitIsTrue(YapConst.CLEAN)); 95 } 96 97 public final boolean isNew(){ 98 return i_id == 0; 99 } 100 101 public int linkLength() { 102 return YapConst.ID_LENGTH; 103 } 104 105 final void notCachedDirty() { 106 bitFalse(YapConst.CACHED_DIRTY); 107 } 108 109 public void read(Transaction trans) { 110 try { 111 if (beginProcessing()) { 112 YapReader reader = trans.stream().readReaderByID(trans, getID()); 113 if (reader != null) { 114 if (Deploy.debug) { 115 reader.readBegin(getIdentifier()); 116 } 117 readThis(trans, reader); 118 setStateOnRead(reader); 119 } 120 endProcessing(); 121 } 122 } catch (Throwable t) { 123 if (Debug.atHome) { 124 t.printStackTrace(); 125 } 126 } 127 } 128 129 public void setID(int a_id) { 130 if(DTrace.enabled){ 131 DTrace.YAPMETA_SET_ID.log(a_id); 132 } 133 i_id = a_id; 134 } 135 136 public final void setStateClean() { 137 bitTrue(YapConst.ACTIVE); 138 bitTrue(YapConst.CLEAN); 139 } 140 141 public final void setStateDeactivated() { 142 bitFalse(YapConst.ACTIVE); 143 } 144 145 public void setStateDirty() { 146 bitTrue(YapConst.ACTIVE); 147 bitFalse(YapConst.CLEAN); 148 } 149 150 void setStateOnRead(YapReader reader) { 151 if (Deploy.debug) { 152 reader.readEnd(); 153 } 154 if (bitIsTrue(YapConst.CACHED_DIRTY)) { 155 setStateDirty(); 156 } else { 157 setStateClean(); 158 } 159 } 160 161 public final void write(Transaction trans) { 162 163 if (! writeObjectBegin()) { 164 return; 165 } 166 167 if(DTrace.enabled){ 168 DTrace.YAPMETA_WRITE.log(getID()); 169 } 170 171 YapFile stream = (YapFile)trans.stream(); 172 173 int address = 0; 174 int length = ownLength(); 175 176 YapReader writer = new YapReader(length); 177 178 if(isNew()){ 179 Pointer4 ptr = stream.newSlot(trans, length); 180 setID(ptr._id); 181 address = ptr._address; 182 183 }else{ 185 address = stream.getSlot(length); 186 trans.slotFreeOnRollbackCommitSetPointer(i_id, address, length); 187 } 188 189 if (Deploy.debug) { 190 writer.writeBegin(getIdentifier()); 191 } 192 193 writeThis(trans, writer); 194 195 if (Deploy.debug) { 196 writer.writeEnd(); 197 } 198 199 writer.writeEncrypt(stream, address, 0); 200 201 if (isActive()) { 202 setStateClean(); 203 } 204 endProcessing(); 205 206 } 207 208 public boolean writeObjectBegin() { 209 if (isDirty()) { 210 return beginProcessing(); 211 } 212 return false; 213 } 214 215 public void writeOwnID(Transaction trans, YapReader writer) { 216 write(trans); 217 writer.writeInt(getID()); 218 } 219 220 221 } 222 | Popular Tags |