| 1 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 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 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 { 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 |