1 21 package com.db4o.inside.btree; 22 23 26 import com.db4o.Transaction; 27 import com.db4o.foundation.No4; 28 29 public abstract class BTreeUpdate extends BTreePatch { 30 31 protected BTreeUpdate _next; 32 33 public BTreeUpdate(Transaction transaction, Object obj) { 34 super(transaction, obj); 35 } 36 37 protected boolean hasNext() { 38 return _next != null; 39 } 40 41 public BTreePatch forTransaction(Transaction trans) { 42 if(_transaction == trans){ 43 return this; 44 } 45 if(_next == null){ 46 return null; 47 } 48 return _next.forTransaction(trans); 49 } 50 51 public BTreeUpdate removeFor(Transaction trans) { 52 if (_transaction == trans) { 53 return _next; 54 } 55 if (_next == null) { 56 return this; 57 } 58 return _next.removeFor(trans); 59 } 60 61 public void append(BTreeUpdate patch) { 62 if(_transaction == patch._transaction){ 63 throw new IllegalArgumentException (); 65 } 66 if(!hasNext()){ 67 _next = patch; 68 }else{ 69 _next.append(patch); 70 } 71 } 72 73 protected void applyKeyChange(Object obj) { 74 _object = obj; 75 if (hasNext()) { 76 _next.applyKeyChange(obj); 77 } 78 } 79 80 protected abstract void committed(BTree btree); 81 82 public Object commit(Transaction trans, BTree btree) { 83 final BTreeUpdate patch = (BTreeUpdate) forTransaction(trans); 84 if (patch instanceof BTreeCancelledRemoval) { 85 Object obj = patch.getCommittedObject(); 86 applyKeyChange(obj); 87 } 88 return internalCommit(trans, btree); 89 } 90 91 protected Object internalCommit(Transaction trans, BTree btree) { 92 if(_transaction == trans){ 93 committed(btree); 94 if (hasNext()){ 95 return _next; 96 } 97 return getCommittedObject(); 98 } 99 if(hasNext()){ 100 setNextIfPatch(_next.internalCommit(trans, btree)); 101 } 102 return this; 103 } 104 105 private void setNextIfPatch(Object newNext) { 106 if(newNext instanceof BTreeUpdate){ 107 _next = (BTreeUpdate)newNext; 108 } else { 109 _next = null; 110 } 111 } 112 113 protected abstract Object getCommittedObject(); 114 115 public Object rollback(Transaction trans, BTree btree) { 116 if(_transaction == trans){ 117 if(hasNext()){ 118 return _next; 119 } 120 return getObject(); 121 } 122 if(hasNext()){ 123 setNextIfPatch(_next.rollback(trans, btree)); 124 } 125 return this; 126 } 127 128 public Object key(Transaction trans) { 129 BTreePatch patch = forTransaction(trans); 130 if (patch == null) { 131 return getObject(); 132 } 133 if (patch.isRemove()) { 134 return No4.INSTANCE; 135 } 136 return patch.getObject(); 137 } 138 139 } | Popular Tags |