001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.internal.cs;
022:
023: import com.db4o.foundation.*;
024: import com.db4o.internal.*;
025: import com.db4o.internal.cs.messages.*;
026: import com.db4o.internal.query.result.*;
027:
028: /**
029: * @exclude
030: */
031: public class LazyClientQueryResult extends AbstractQueryResult {
032:
033: private static final int SIZE_NOT_SET = -1;
034:
035: private final ClientObjectContainer _client;
036:
037: private final int _queryResultID;
038:
039: private int _size = SIZE_NOT_SET;
040:
041: private final LazyClientIdIterator _iterator;
042:
043: public LazyClientQueryResult(Transaction trans,
044: ClientObjectContainer client, int queryResultID) {
045: super (trans);
046: _client = client;
047: _queryResultID = queryResultID;
048: _iterator = new LazyClientIdIterator(this );
049: }
050:
051: public Object get(int index) {
052: synchronized (lock()) {
053: return activatedObject(getId(index));
054: }
055: }
056:
057: public int getId(int index) {
058: return askServer(Msg.OBJECTSET_GET_ID, index);
059: }
060:
061: public int indexOf(int id) {
062: return askServer(Msg.OBJECTSET_INDEXOF, id);
063: }
064:
065: private int askServer(MsgD message, int param) {
066: _client.write(message.getWriterForInts(_transaction, new int[] {
067: _queryResultID, param }));
068: return ((MsgD) _client.expectedResponse(message)).readInt();
069: }
070:
071: public IntIterator4 iterateIDs() {
072: return _iterator;
073: }
074:
075: public Iterator4 iterator() {
076: return ClientServerPlatform
077: .createClientQueryResultIterator(this );
078: }
079:
080: public int size() {
081: if (_size == SIZE_NOT_SET) {
082: _client.write(Msg.OBJECTSET_SIZE.getWriterForInt(
083: _transaction, _queryResultID));
084: _size = ((MsgD) _client
085: .expectedResponse(Msg.OBJECTSET_SIZE)).readInt();
086: }
087: return _size;
088: }
089:
090: protected void finalize() {
091: _client.write(Msg.OBJECTSET_FINALIZED.getWriterForInt(
092: _transaction, _queryResultID));
093: }
094:
095: public void loadFromIdReader(Buffer reader) {
096: _iterator.loadFromIdReader(reader, reader.readInt());
097: }
098:
099: public void reset() {
100: _client.write(Msg.OBJECTSET_RESET.getWriterForInt(_transaction,
101: _queryResultID));
102: }
103:
104: public void fetchIDs(int batchSize) {
105: _client.write(Msg.OBJECTSET_FETCH.getWriterForInts(
106: _transaction, new int[] { _queryResultID, batchSize }));
107: Buffer reader = _client.expectedByteResponse(Msg.ID_LIST);
108: loadFromIdReader(reader);
109: }
110:
111: }
|