KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > cs > ClientQueryResultIterator


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.cs;
22
23 import com.db4o.foundation.*;
24 import com.db4o.inside.query.*;
25
26 /**
27  * @exclude
28  */

29 class ClientQueryResultIterator implements Iterator4 {
30     
31     private Object JavaDoc[] _prefetchedObjects;
32     private int _remainingObjects;
33     private int _prefetchRight;
34     private final AbstractQueryResult _client;
35     private final IntIterator4 _ids;
36     
37     public ClientQueryResultIterator(AbstractQueryResult client) {
38         _client = client;
39         _ids = client.iterateIDs();
40     }
41
42     public Object JavaDoc current() {
43         synchronized (streamLock()) {
44             return _client.activate(prefetchedCurrent());
45         }
46     }
47
48     private Object JavaDoc streamLock() {
49         return _client.streamLock();
50     }
51     
52     public void reset() {
53         _remainingObjects = 0;
54         _ids.reset();
55     }
56
57     public boolean moveNext() {
58         synchronized (streamLock()) {
59             if (_remainingObjects > 0) {
60                 --_remainingObjects;
61                 return skipNulls();
62             }
63             
64             prefetch();
65             
66             --_remainingObjects;
67             if(_remainingObjects < 0){
68                 return false;
69             }
70             return skipNulls();
71         }
72     }
73
74     private boolean skipNulls() {
75         // skip nulls (deleted objects)
76
if (prefetchedCurrent() == null){
77             return moveNext();
78         }
79         return true;
80     }
81
82     private void prefetch() {
83         ensureObjectCacheAllocated(prefetchCount());
84         _remainingObjects = stream().prefetchObjects(_ids, _prefetchedObjects, prefetchCount());
85         _prefetchRight=_remainingObjects;
86     }
87
88     private int prefetchCount() {
89         return stream().config().prefetchObjectCount();
90     }
91
92     private YapClient stream() {
93         return (YapClient) _client.stream();
94     }
95
96     private Object JavaDoc prefetchedCurrent() {
97         return _prefetchedObjects[_prefetchRight-_remainingObjects-1];
98     }
99     
100     // TODO: open this as an external tuning interface in ExtObjectSet
101

102 // public void prefetch(int count){
103
// if(count < 1){
104
// count = 1;
105
// }
106
// i_prefetchCount = count;
107
// Object[] temp = new Object[i_prefetchCount];
108
// if(i_remainingObjects > 0){
109
// // Potential problem here:
110
// // On reducing the prefetch size, this will crash.
111
// System.arraycopy(i_prefetched, 0, temp, 0, i_remainingObjects);
112
// }
113
// i_prefetched = temp;
114
// }
115

116     private void ensureObjectCacheAllocated(int prefetchObjectCount) {
117         if(_prefetchedObjects==null) {
118             _prefetchedObjects = new Object JavaDoc[prefetchObjectCount];
119             return;
120         }
121         if(prefetchObjectCount>_prefetchedObjects.length) {
122             Object JavaDoc[] newPrefetchedObjects=new Object JavaDoc[prefetchObjectCount];
123             System.arraycopy(_prefetchedObjects, 0, newPrefetchedObjects, 0, _prefetchedObjects.length);
124             _prefetchedObjects=newPrefetchedObjects;
125         }
126     }
127
128
129 }
130
Popular Tags