1 21 package com.db4o.inside.query; 22 23 import com.db4o.*; 24 import com.db4o.foundation.*; 25 import com.db4o.inside.btree.*; 26 import com.db4o.inside.classindex.*; 27 import com.db4o.query.*; 28 import com.db4o.reflect.*; 29 30 33 public class IdListQueryResult extends AbstractQueryResult implements Visitor4{ 34 35 private Tree _candidates; 36 37 private boolean _checkDuplicates; 38 39 public IntArrayList _ids; 40 41 public IdListQueryResult(Transaction trans, int initialSize){ 42 super(trans); 43 _ids = new IntArrayList(initialSize); 44 } 45 46 public IdListQueryResult(Transaction trans) { 47 this(trans, 0); 48 } 49 50 public IntIterator4 iterateIDs() { 51 return _ids.intIterator(); 52 } 53 54 public Object get(int index) { 55 synchronized (streamLock()) { 56 return activatedObject(getId(index)); 57 } 58 } 59 60 public int getId(int index) { 61 if (index < 0 || index >= size()) { 62 throw new IndexOutOfBoundsException (); 63 } 64 return _ids.get(index); 65 } 66 67 public final void checkDuplicates(){ 68 _checkDuplicates = true; 69 } 70 71 public void visit(Object a_tree) { 72 QCandidate candidate = (QCandidate) a_tree; 73 if (candidate.include()) { 74 addKeyCheckDuplicates(candidate._key); 75 } 76 } 77 78 public void addKeyCheckDuplicates(int a_key){ 79 if(_checkDuplicates){ 80 TreeInt newNode = new TreeInt(a_key); 81 _candidates = Tree.add(_candidates, newNode); 82 if(newNode._size == 0){ 83 return; 84 } 85 } 86 add(a_key); 87 } 88 89 public void sort(final QueryComparator cmp) { 90 Algorithms4.qsort(new QuickSortable4() { 91 public void swap(int leftIndex, int rightIndex) { 92 _ids.swap(leftIndex, rightIndex); 93 } 94 public int size() { 95 return IdListQueryResult.this.size(); 96 } 97 public int compare(int leftIndex, int rightIndex) { 98 return cmp.compare(get(leftIndex), get(rightIndex)); 99 } 100 }); 101 } 102 103 public void loadFromClassIndex(YapClass clazz) { 104 final ClassIndexStrategy index = clazz.index(); 105 if(index instanceof BTreeClassIndexStrategy){ 106 BTree btree = ((BTreeClassIndexStrategy)index).btree(); 107 _ids = new IntArrayList(btree.size(transaction())); 108 } 109 index.traverseAll(_transaction, new Visitor4() { 110 public void visit(Object a_object) { 111 add(((Integer )a_object).intValue()); 112 } 113 }); 114 } 115 116 public void loadFromQuery(QQuery query) { 117 query.executeLocal(this); 118 } 119 120 public void loadFromClassIndexes(YapClassCollectionIterator iter){ 121 122 final Tree.ByRef duplicates = new Tree.ByRef(); 124 125 while (iter.moveNext()) { 126 final YapClass yapClass = iter.currentClass(); 127 if (yapClass.getName() != null) { 128 ReflectClass claxx = yapClass.classReflector(); 129 if (claxx == null 130 || !(stream().i_handlers.ICLASS_INTERNAL.isAssignableFrom(claxx))) { 131 final ClassIndexStrategy index = yapClass.index(); 132 index.traverseAll(_transaction, new Visitor4() { 133 public void visit(Object obj) { 134 int id = ((Integer )obj).intValue(); 135 TreeInt newNode = new TreeInt(id); 136 duplicates.value = Tree.add(duplicates.value, newNode); 137 if (newNode.size() != 0) { 138 add(id); 139 } 140 } 141 }); 142 } 143 } 144 } 145 146 } 147 148 public void loadFromIdReader(YapReader reader) { 149 int size = reader.readInt(); 150 for (int i = 0; i < size; i++) { 151 add(reader.readInt()); 152 } 153 } 154 155 public void add(int id){ 156 _ids.add(id); 157 } 158 159 public int indexOf(int id) { 160 return _ids.indexOf(id); 161 } 162 163 public int size() { 164 return _ids.size(); 165 } 166 167 } 168 | Popular Tags |