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.IntIterator4;
024: import com.db4o.internal.*;
025: import com.db4o.internal.cs.messages.*;
026:
027: /**
028: * Prefetchs multiples objects at once (in a single message).
029: *
030: * @exclude
031: */
032: public class SingleMessagePrefetchingStrategy implements
033: PrefetchingStrategy {
034:
035: public static final PrefetchingStrategy INSTANCE = new SingleMessagePrefetchingStrategy();
036:
037: private SingleMessagePrefetchingStrategy() {
038: }
039:
040: public int prefetchObjects(ClientObjectContainer container,
041: IntIterator4 ids, Object[] prefetched, int prefetchCount) {
042: int count = 0;
043:
044: int toGet = 0;
045: int[] idsToGet = new int[prefetchCount];
046: int[] position = new int[prefetchCount];
047:
048: while (count < prefetchCount) {
049: if (!ids.moveNext()) {
050: break;
051: }
052: int id = ids.currentInt();
053: if (id > 0) {
054: Object obj = container.transaction()
055: .objectForIdFromCache(id);
056: if (obj != null) {
057: prefetched[count] = obj;
058: } else {
059: idsToGet[toGet] = id;
060: position[toGet] = count;
061: toGet++;
062: }
063: count++;
064: }
065: }
066:
067: if (toGet > 0) {
068: Transaction trans = container.transaction();
069: MsgD msg = Msg.READ_MULTIPLE_OBJECTS.getWriterForIntArray(
070: trans, idsToGet, toGet);
071: container.write(msg);
072: MsgD response = (MsgD) container
073: .expectedResponse(Msg.READ_MULTIPLE_OBJECTS);
074: int embeddedMessageCount = response.readInt();
075: for (int i = 0; i < embeddedMessageCount; i++) {
076: MsgObject mso = (MsgObject) Msg.OBJECT_TO_CLIENT
077: .publicClone();
078: mso.setTransaction(trans);
079: mso.payLoad(response.payLoad().readYapBytes());
080: if (mso.payLoad() != null) {
081: mso.payLoad()
082: .incrementOffset(Const4.MESSAGE_LENGTH);
083: StatefulBuffer reader = mso
084: .unmarshall(Const4.MESSAGE_LENGTH);
085: Object obj = trans
086: .objectForIdFromCache(idsToGet[i]);
087: if (obj != null) {
088: prefetched[position[i]] = obj;
089: } else {
090: prefetched[position[i]] = new ObjectReference(
091: idsToGet[i])
092: .readPrefetch(trans, reader);
093: }
094: }
095: }
096: }
097: return count;
098: }
099:
100: }
|