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.query.result.*;
025:
026: /**
027: * @exclude
028: */
029: class ClientQueryResultIterator implements Iterator4 {
030:
031: private static final PrefetchingStrategy _prefetchingStrategy = SingleMessagePrefetchingStrategy.INSTANCE;
032:
033: private Object[] _prefetchedObjects;
034: private int _remainingObjects;
035: private int _prefetchRight;
036: private final AbstractQueryResult _client;
037: private final IntIterator4 _ids;
038:
039: ClientQueryResultIterator(AbstractQueryResult client) {
040: _client = client;
041: _ids = client.iterateIDs();
042: }
043:
044: public Object current() {
045: synchronized (streamLock()) {
046: return _client.activate(prefetchedCurrent());
047: }
048: }
049:
050: private Object streamLock() {
051: return _client.lock();
052: }
053:
054: public void reset() {
055: _remainingObjects = 0;
056: _ids.reset();
057: }
058:
059: public boolean moveNext() {
060: synchronized (streamLock()) {
061: if (_remainingObjects > 0) {
062: --_remainingObjects;
063: return skipNulls();
064: }
065:
066: prefetch();
067:
068: --_remainingObjects;
069: if (_remainingObjects < 0) {
070: return false;
071: }
072: return skipNulls();
073: }
074: }
075:
076: private boolean skipNulls() {
077: // skip nulls (deleted objects)
078: if (prefetchedCurrent() == null) {
079: return moveNext();
080: }
081: return true;
082: }
083:
084: private void prefetch() {
085: ensureObjectCacheAllocated(prefetchCount());
086: _remainingObjects = _prefetchingStrategy.prefetchObjects(
087: stream(), _ids, _prefetchedObjects, prefetchCount());
088: _prefetchRight = _remainingObjects;
089: }
090:
091: private int prefetchCount() {
092: return stream().config().prefetchObjectCount();
093: }
094:
095: private ClientObjectContainer stream() {
096: return (ClientObjectContainer) _client.stream();
097: }
098:
099: private Object prefetchedCurrent() {
100: return _prefetchedObjects[_prefetchRight - _remainingObjects
101: - 1];
102: }
103:
104: // TODO: open this as an external tuning interface in ExtObjectSet
105:
106: // public void prefetch(int count){
107: // if(count < 1){
108: // count = 1;
109: // }
110: // i_prefetchCount = count;
111: // Object[] temp = new Object[i_prefetchCount];
112: // if(i_remainingObjects > 0){
113: // // Potential problem here:
114: // // On reducing the prefetch size, this will crash.
115: // System.arraycopy(i_prefetched, 0, temp, 0, i_remainingObjects);
116: // }
117: // i_prefetched = temp;
118: // }
119:
120: private void ensureObjectCacheAllocated(int prefetchObjectCount) {
121: if (_prefetchedObjects == null) {
122: _prefetchedObjects = new Object[prefetchObjectCount];
123: return;
124: }
125: if (prefetchObjectCount > _prefetchedObjects.length) {
126: Object[] newPrefetchedObjects = new Object[prefetchObjectCount];
127: System.arraycopy(_prefetchedObjects, 0,
128: newPrefetchedObjects, 0, _prefetchedObjects.length);
129: _prefetchedObjects = newPrefetchedObjects;
130: }
131: }
132:
133: }
|