KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > inside > query > IdListQueryResult


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

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 /**
31  * @exclude
32  */

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 JavaDoc 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 JavaDoc();
63         }
64         return _ids.get(index);
65     }
66
67     public final void checkDuplicates(){
68         _checkDuplicates = true;
69     }
70
71     public void visit(Object JavaDoc 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 JavaDoc a_object) {
111                 add(((Integer JavaDoc)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         // duplicates because of inheritance hierarchies
123
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 JavaDoc obj) {
134                             int id = ((Integer JavaDoc)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