1 21 package com.db4o.inside.ix; 22 23 import com.db4o.*; 24 import com.db4o.foundation.*; 25 26 30 public class IndexTransaction implements Visitor4{ 31 32 final Index4 i_index; 33 final Transaction i_trans; 34 int i_version; 35 private Tree i_root; 36 37 IndexTransaction(Transaction a_trans, Index4 a_index){ 38 i_trans = a_trans; 39 i_index = a_index; 40 } 41 42 public boolean equals(Object obj) { 43 return i_trans == ((IndexTransaction)obj).i_trans; 44 } 45 46 48 public void add(int id, Object value){ 49 patch(new IxAdd(this, id, value)); 50 } 51 52 public void remove(int id, Object value){ 53 patch(new IxRemove(this, id, value)); 54 } 55 56 private void patch(IxPatch patch){ 57 i_root = Tree.add(i_root,patch); 58 } 59 60 61 65 public Tree getRoot(){ 66 return i_root; 67 } 68 69 public void commit(){ 70 i_index.commit(this); 71 } 72 73 public void rollback(){ 74 i_index.rollback(this); 75 } 76 77 void merge(IndexTransaction a_ft){ 78 Tree otherRoot = a_ft.getRoot(); 79 if(otherRoot != null){ 80 otherRoot.traverseFromLeaves(this); 81 } 82 } 83 84 88 public void visit(Object obj){ 89 if(obj instanceof IxPatch){ 90 IxPatch tree = (IxPatch)obj; 91 if(tree.hasQueue()){ 92 Queue4 queue = tree.detachQueue(); 93 while((tree = (IxPatch)queue.next()) != null){ 94 tree.detachQueue(); 95 addPatchToRoot(tree); 96 } 97 }else{ 98 addPatchToRoot(tree); 99 } 100 } 101 } 102 103 private void addPatchToRoot(IxPatch tree){ 104 if(tree._version != i_version){ 105 tree.beginMerge(); 106 tree.handler().prepareComparison(tree.handler().comparableObject(i_trans, tree._value)); 107 if(i_root == null){ 108 i_root = tree; 109 } else{ 110 i_root = i_root.add(tree); 111 } 112 } 113 } 114 115 int countLeaves(){ 116 if(i_root == null){ 117 return 0; 118 } 119 final int[] leaves ={0}; 120 i_root.traverse(new Visitor4() { 121 public void visit(Object a_object) { 122 leaves[0] ++; 123 } 124 }); 125 return leaves[0]; 126 } 127 128 public void setRoot(Tree a_tree) { 129 i_root = a_tree; 130 } 131 132 public String toString(){ 133 if(! Debug4.prettyToStrings){ 134 return super.toString(); 135 } 136 final StringBuffer sb = new StringBuffer (); 137 sb.append("IxFieldTransaction "); 138 sb.append(System.identityHashCode(this)); 139 if(i_root == null){ 140 sb.append("\n Empty"); 141 }else{ 142 i_root.traverse(new Visitor4() { 143 public void visit(Object a_object) { 144 sb.append("\n"); 145 sb.append(a_object.toString()); 146 } 147 }); 148 } 149 return sb.toString(); 150 } 151 152 153 154 155 } 156
| Popular Tags
|