1 24 package org.objectweb.jalisto.se.query.btree; 25 26 import org.objectweb.jalisto.se.impl.LogicalOid; 27 import org.objectweb.jalisto.se.impl.InFileAddress; 28 import org.objectweb.jalisto.se.api.query.Index; 29 import org.objectweb.jalisto.se.api.ClassDescription; 30 import org.objectweb.jalisto.se.api.query.FieldDescription; 31 import org.objectweb.jalisto.se.api.query.*; 32 import org.objectweb.jalisto.se.query.IndexManagerImpl; 33 34 import java.io.Serializable ; 35 import java.util.*; 36 37 public class BTree implements Serializable { 38 39 public BTree(Index index, int fieldIndex, ClassDescription meta, 40 IndexManagerImpl indexManager) { 41 this.index = index; 42 this.comparator = meta.getComparator(fieldIndex); 43 this.fieldIndex = fieldIndex; 44 this.indexManager = indexManager; 45 this.meta = meta; 46 loadedElements = new HashMap(); 47 BTreeNode root = new BTreeNode(this, null); 48 rootIfa = root.getIfa(); 49 firstIfa = rootIfa; 50 } 51 52 public Set getOidsFor(Object value) { 53 BTreeNode root = indexManager.readNodeInBase(this, rootIfa); 54 OidCollection oids = indexManager.readOidsInBase(this, root.getOidsIfa(value, comparator)); 55 return oids.asSet(); 56 } 57 58 public void addOid(Object value, LogicalOid oid) { 59 BTreeNode root = indexManager.readNodeInBase(this, rootIfa); 60 root.insertOid(comparator, value, oid); 61 } 62 63 public void removeOid(Object value, LogicalOid oid) { 64 BTreeNode root = indexManager.readNodeInBase(this, rootIfa); 65 root.removeOid(value, oid, comparator); 66 } 67 68 public Set getKeys() { 69 Set set = new HashSet(); 70 InFileAddress nextIfa = firstIfa; 71 while (nextIfa != null) { 72 BTreeNode node = indexManager.readNodeInBase(this, nextIfa); 73 nextIfa = node.getKeys(set); 74 } 75 return set; 76 } 77 78 public IndexManagerImpl getIndexManager() { 79 return indexManager; 80 } 81 82 public void setIndexManager(IndexManagerImpl indexManager) { 83 this.indexManager = indexManager; 84 loadedElements = new HashMap(); 85 } 86 87 public void setRootIfa(InFileAddress rootIfa) { 88 this.rootIfa = rootIfa; 89 updateInBase(); 90 } 91 92 public FielComparator getComparator() { 93 return comparator; 94 } 95 96 public InFileAddress getFirstIfa() { 97 return firstIfa; 98 } 99 100 public void setFirstIfa(InFileAddress firstIfa) { 101 this.firstIfa = firstIfa; 102 updateInBase(); 103 } 104 105 public InFileAddress getLastIfa() { 106 return lastIfa; 107 } 108 109 public void setLastIfa(InFileAddress lastIfa) { 110 this.lastIfa = lastIfa; 111 updateInBase(); 112 } 113 114 public FieldDescription getFieldDescription() { 115 return meta.getFieldDescription(fieldIndex); 116 } 117 118 public Map getLoadedElements() { 119 return loadedElements; 120 } 121 122 public void deleteTree() { 123 BTreeNode root = indexManager.readNodeInBase(this, rootIfa); 124 root.deleteYourself(); 125 } 126 127 private void updateInBase() { 128 indexManager.updateIndexInBase(index); 129 } 130 131 public String toString() { 132 StringBuffer sb = new StringBuffer (); 133 sb.append("BTree on ").append(indexManager).append(" with comparator ").append(comparator); 134 sb.append("\n root ifa : ").append(rootIfa); 135 sb.append("\n firstIfa ifa : ").append(firstIfa); 136 sb.append("\n lastIfa ifa : ").append(lastIfa); 137 if (loadedElements != null) { 138 sb.append("\n loadedElements size : ").append(loadedElements.size()); 139 } 140 sb.append("\n"); 141 if (indexManager != null) { 142 LinkedList toPrintNode = new LinkedList(); 143 LinkedList toPrintOids = new LinkedList(); 144 toPrintNode.add(rootIfa); 145 while (!toPrintNode.isEmpty()) { 146 BTreeNode node = indexManager.readNodeInBase(this, (InFileAddress) toPrintNode.removeFirst()); 147 sb.append(node.toFullString(toPrintNode, toPrintOids)); 148 } 149 while (!toPrintOids.isEmpty()) { 150 OidCollection oids = indexManager.readOidsInBase(this, (InFileAddress) toPrintOids.removeFirst()); 151 sb.append(oids.toFullString()).append("\n"); 152 } 153 } 154 return sb.toString(); 155 } 156 157 private Index index; 158 private ClassDescription meta; 159 private int fieldIndex; 160 private InFileAddress rootIfa; 161 private InFileAddress firstIfa; 162 private InFileAddress lastIfa; 163 private FielComparator comparator; 164 165 private transient IndexManagerImpl indexManager; 166 private transient Map loadedElements; 167 168 169 static final long serialVersionUID = -7589377901514761459L; 170 171 } 172 | Popular Tags |