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.cluster;
022:
023: import com.db4o.*;
024: import com.db4o.cluster.*;
025: import com.db4o.ext.*;
026: import com.db4o.foundation.*;
027: import com.db4o.internal.*;
028: import com.db4o.internal.query.*;
029: import com.db4o.internal.query.processor.*;
030: import com.db4o.internal.query.result.*;
031: import com.db4o.query.*;
032:
033: /**
034: *
035: * @exclude
036: */
037: public class ClusterQueryResult implements QueryResult {
038:
039: private final Cluster _cluster;
040: private final ObjectSet[] _objectSets;
041: private final int[] _sizes;
042: private final int _size;
043:
044: public ClusterQueryResult(Cluster cluster, Query[] queries) {
045: _cluster = cluster;
046: _objectSets = new ObjectSet[queries.length];
047: _sizes = new int[queries.length];
048: int size = 0;
049: for (int i = 0; i < queries.length; i++) {
050: _objectSets[i] = queries[i].execute();
051: _sizes[i] = _objectSets[i].size();
052: size += _sizes[i];
053: }
054: _size = size;
055: }
056:
057: private static final class ClusterQueryResultIntIterator implements
058: IntIterator4 {
059:
060: private final CompositeIterator4 _delegate;
061:
062: public ClusterQueryResultIntIterator(Iterator4[] iterators) {
063: _delegate = new CompositeIterator4(iterators);
064: }
065:
066: public boolean moveNext() {
067: return _delegate.moveNext();
068: }
069:
070: public Object current() {
071: return _delegate.current();
072: }
073:
074: public void reset() {
075: _delegate.reset();
076: }
077:
078: public int currentInt() {
079: return ((IntIterator4) _delegate.currentIterator())
080: .currentInt();
081: }
082: }
083:
084: public IntIterator4 iterateIDs() {
085: synchronized (_cluster) {
086: final Iterator4[] iterators = new Iterator4[_objectSets.length];
087: for (int i = 0; i < _objectSets.length; i++) {
088: iterators[i] = ((ObjectSetFacade) _objectSets[i])._delegate
089: .iterateIDs();
090: }
091: return new ClusterQueryResultIntIterator(iterators);
092: }
093: }
094:
095: public Iterator4 iterator() {
096: synchronized (_cluster) {
097: Iterator4[] iterators = new Iterator4[_objectSets.length];
098: for (int i = 0; i < _objectSets.length; i++) {
099: iterators[i] = ((ObjectSetFacade) _objectSets[i])._delegate
100: .iterator();
101: }
102: return new CompositeIterator4(iterators);
103: }
104: }
105:
106: public int size() {
107: return _size;
108: }
109:
110: public Object get(int index) {
111: synchronized (_cluster) {
112: if (index < 0 || index >= size()) {
113: throw new IndexOutOfBoundsException();
114: }
115: int i = 0;
116: while (index >= _sizes[i]) {
117: index -= _sizes[i];
118: i++;
119: }
120: return ((ObjectSetFacade) _objectSets[i]).get(index);
121: }
122: }
123:
124: public Object lock() {
125: return _cluster;
126: }
127:
128: public ExtObjectContainer objectContainer() {
129: throw new NotSupportedException();
130: }
131:
132: public int indexOf(int id) {
133: throw new NotSupportedException();
134: }
135:
136: public void sort(QueryComparator cmp) {
137: throw new NotSupportedException();
138: }
139:
140: /** @param c */
141: public void loadFromClassIndex(ClassMetadata c) {
142: throw new NotSupportedException();
143: }
144:
145: /** @param q */
146: public void loadFromQuery(QQuery q) {
147: throw new NotSupportedException();
148: }
149:
150: /** @param i */
151: public void loadFromClassIndexes(ClassMetadataIterator i) {
152: throw new NotSupportedException();
153: }
154:
155: /** @param r */
156: public void loadFromIdReader(Buffer r) {
157: throw new NotSupportedException();
158: }
159: }
|