1 21 package com.db4o.inside.classindex; 22 23 import com.db4o.*; 24 import com.db4o.foundation.*; 25 import com.db4o.inside.btree.*; 26 27 30 public class BTreeClassIndexStrategy extends AbstractClassIndexStrategy { 31 32 private BTree _btreeIndex; 33 34 public BTreeClassIndexStrategy(YapClass yapClass) { 35 super(yapClass); 36 } 37 38 public BTree btree() { 39 return _btreeIndex; 40 } 41 42 public int entryCount(Transaction ta) { 43 return _btreeIndex != null 44 ? _btreeIndex.size(ta) 45 : 0; 46 } 47 48 public void initialize(YapStream stream) { 49 createBTreeIndex(stream, 0); 50 } 51 52 public void purge() { 53 } 54 55 public void read(YapStream stream, int indexID) { 56 readBTreeIndex(stream, indexID); 57 } 58 59 public int write(Transaction trans) { 60 if (_btreeIndex == null){ 61 return 0; 62 } 63 _btreeIndex.write(trans); 64 return _btreeIndex.getID(); 65 } 66 67 public void traverseAll(Transaction ta,Visitor4 command) { 68 if(_btreeIndex!=null) { 70 _btreeIndex.traverseKeys(ta,command); 71 } 72 } 73 74 private void createBTreeIndex(final YapStream stream, int btreeID){ 75 if (stream.isClient()) { 76 return; 77 } 78 _btreeIndex = ((YapFile)stream).createBTreeClassIndex(btreeID); 79 _btreeIndex.setRemoveListener(new Visitor4() { 80 public void visit(Object obj) { 81 int id = ((Integer )obj).intValue(); 82 YapObject yo = stream.getYapObject(id); 83 if (yo != null) { 84 stream.removeReference(yo); 85 } 86 } 87 }); 88 } 89 90 private void readBTreeIndex(YapStream stream, int indexId) { 91 if(! stream.isClient() && _btreeIndex == null){ 92 createBTreeIndex(stream, indexId); 93 } 94 } 95 96 protected void internalAdd(Transaction trans, int id) { 97 _btreeIndex.add(trans, new Integer (id)); 98 } 99 100 protected void internalRemove(Transaction ta, int id) { 101 _btreeIndex.remove(ta, new Integer (id)); 102 } 103 104 public void dontDelete(Transaction transaction, int id) { 105 } 106 107 public void defragReference(YapClass yapClass, ReaderPair readers,int classIndexID) { 108 int newID = -classIndexID; 109 readers.writeInt(newID); 110 } 111 112 public int id() { 113 return _btreeIndex.getID(); 114 } 115 116 public Iterator4 allSlotIDs(Transaction trans) { 117 return _btreeIndex.allNodeIds(trans); 118 } 119 120 public void defragIndex(ReaderPair readers) { 121 _btreeIndex.defragIndex(readers); 122 } 123 124 public static BTree btree(YapClass clazz) { 125 ClassIndexStrategy index = clazz.index(); 126 if(! (index instanceof BTreeClassIndexStrategy)){ 127 throw new IllegalStateException (); 128 } 129 return ((BTreeClassIndexStrategy)index).btree(); 130 } 131 132 public static Iterator4 iterate(YapClass clazz, Transaction trans) { 133 return btree(clazz).asRange(trans).keys(); 134 } 135 136 137 138 } 139 | Popular Tags |