1 21 package com.db4o.inside.slots; 22 23 import com.db4o.*; 24 25 28 public class SlotChange extends TreeInt { 29 30 private int _action; 31 32 private Slot _newSlot; 33 34 private ReferencedSlot _shared; 35 36 private static final int FREE_ON_COMMIT_BIT = 1; 37 38 private static final int FREE_ON_ROLLBACK_BIT = 2; 39 40 private static final int SET_POINTER_BIT = 3; 41 42 public SlotChange(int id) { 43 super(id); 44 } 45 46 public Object shallowClone() { 47 SlotChange sc = new SlotChange(0); 48 sc._action = _action; 49 sc._newSlot = _newSlot; 50 sc._shared = _shared; 51 return super.shallowCloneInternal(sc); 52 } 53 54 private final void doFreeOnCommit() { 55 setBit(FREE_ON_COMMIT_BIT); 56 } 57 58 private final void doFreeOnRollback() { 59 setBit(FREE_ON_ROLLBACK_BIT); 60 } 61 62 private final void doSetPointer() { 63 setBit(SET_POINTER_BIT); 64 } 65 66 public void freeDuringCommit(YapFile file) { 67 if (isFreeOnCommit()) { 68 file.freeDuringCommit(_shared, _newSlot); 69 } 70 } 71 72 public void freeOnCommit(YapFile file, Slot slot) { 73 74 if (_shared != null) { 75 76 80 file.free(slot); 81 return; 82 } 83 84 doFreeOnCommit(); 85 86 ReferencedSlot refSlot = file.produceFreeOnCommitEntry(_key); 87 88 if (refSlot.addReferenceIsFirst()) { 89 refSlot.pointTo(slot); 90 } 91 92 _shared = refSlot; 93 } 94 95 public void freeOnRollback(int address, int length) { 96 doFreeOnRollback(); 97 _newSlot = new Slot(address, length); 98 } 99 100 public void freeOnRollbackSetPointer(int address, int length) { 101 doSetPointer(); 102 freeOnRollback(address, length); 103 } 104 105 private final boolean isBitSet(int bitPos) { 106 return (_action | (1 << bitPos)) == _action; 107 } 108 109 public boolean isDeleted() { 110 return isSetPointer() && (_newSlot._address == 0); 111 } 112 113 private final boolean isFreeOnCommit() { 114 return isBitSet(FREE_ON_COMMIT_BIT); 115 } 116 117 private final boolean isFreeOnRollback() { 118 return isBitSet(FREE_ON_ROLLBACK_BIT); 119 } 120 121 public final boolean isSetPointer() { 122 return isBitSet(SET_POINTER_BIT); 123 } 124 125 public Slot newSlot() { 126 return _newSlot; 127 } 128 129 public Slot oldSlot() { 130 if(_shared == null){ 131 return null; 132 } 133 return _shared.slot(); 134 } 135 136 public Object read(YapReader reader) { 137 SlotChange change = new SlotChange(reader.readInt()); 138 change._newSlot = new Slot(reader.readInt(), reader.readInt()); 139 change.doSetPointer(); 140 return change; 141 } 142 143 public void rollback(YapFile yapFile) { 144 if (_shared != null) { 145 yapFile.reduceFreeOnCommitReferences(_shared); 146 } 147 if (isFreeOnRollback()) { 148 yapFile.free(_newSlot); 149 } 150 } 151 152 private final void setBit(int bitPos) { 153 _action |= (1 << bitPos); 154 } 155 156 public void setPointer(int address, int length) { 157 doSetPointer(); 158 _newSlot = new Slot(address, length); 159 } 160 161 public void write(YapReader writer) { 162 if (isSetPointer()) { 163 writer.writeInt(_key); 164 writer.writeInt(_newSlot._address); 165 writer.writeInt(_newSlot._length); 166 } 167 } 168 169 public void writePointer(Transaction trans) { 170 if (isSetPointer()) { 171 trans.writePointer(_key, _newSlot._address, _newSlot._length); 172 } 173 } 174 } 175 | Popular Tags |