KickJava   Java API By Example, From Geeks To Geeks.

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


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

32 public class LazyClientQueryResult extends AbstractQueryResult{
33     
34     private static final int SIZE_NOT_SET = -1;
35     
36     private final YapClient _client;
37     
38     private final int _queryResultID;
39     
40     private int _size = SIZE_NOT_SET;
41     
42     private final LazyClientIdIterator _iterator;
43
44     public LazyClientQueryResult(Transaction trans, YapClient client, int queryResultID) {
45         super(trans);
46         _client = client;
47         _queryResultID = queryResultID;
48         _iterator = new LazyClientIdIterator(this);
49     }
50
51     public Object JavaDoc get(int index) {
52         synchronized (streamLock()) {
53             return activatedObject(getId(index));
54         }
55     }
56     
57     public int getId(int index) {
58         return askServer(Msg.OBJECTSET_GET_ID, index);
59     }
60
61     public int indexOf(int id) {
62         return askServer(Msg.OBJECTSET_INDEXOF, id);
63     }
64     
65     private int askServer(MsgD message, int param){
66         _client.writeMsg(message.getWriterForInts(_transaction, new int[]{_queryResultID, param}));
67         return ((MsgD)_client.expectedResponse(message)).readInt();
68     }
69
70     public IntIterator4 iterateIDs() {
71         return _iterator;
72     }
73     
74     public Iterator4 iterator() {
75         return new ClientQueryResultIterator(this);
76     }
77
78     public int size() {
79         if(_size == SIZE_NOT_SET){
80             _client.writeMsg(Msg.OBJECTSET_SIZE.getWriterForInt(_transaction, _queryResultID));
81             _size = ((MsgD)_client.expectedResponse(Msg.OBJECTSET_SIZE)).readInt();
82         }
83         return _size;
84     }
85
86     protected void finalize() throws Throwable JavaDoc {
87         _client.writeMsg(Msg.OBJECTSET_FINALIZED.getWriterForInt(_transaction, _queryResultID));
88     }
89     
90     public void loadFromIdReader(YapReader reader) {
91         _iterator.loadFromIdReader(reader, reader.readInt());
92     }
93
94     public void reset() {
95         _client.writeMsg(Msg.OBJECTSET_RESET.getWriterForInt(_transaction, _queryResultID));
96     }
97
98     public void fetchIDs(int batchSize) {
99         _client.writeMsg(Msg.OBJECTSET_FETCH.getWriterForInts(_transaction, new int[]{_queryResultID, batchSize }));
100         YapReader reader = _client.expectedByteResponse(Msg.ID_LIST);
101         loadFromIdReader(reader);
102     }
103     
104
105 }
106
Popular Tags