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.messages;
022:
023: import java.io.*;
024:
025: import com.db4o.ext.*;
026: import com.db4o.foundation.*;
027: import com.db4o.internal.*;
028:
029: /**
030: * @exclude
031: */
032: public class MCommittedInfo extends MsgD implements ClientSideMessage {
033:
034: public MCommittedInfo encode(
035: CallbackObjectInfoCollections callbackInfo) {
036: ByteArrayOutputStream os = new ByteArrayOutputStream();
037:
038: encodeObjectInfoCollection(os, callbackInfo.added);
039: encodeObjectInfoCollection(os, callbackInfo.deleted);
040: encodeObjectInfoCollection(os, callbackInfo.updated);
041:
042: byte[] bytes = os.toByteArray();
043: MCommittedInfo committedInfo = (MCommittedInfo) getWriterForLength(
044: transaction(), bytes.length);
045: committedInfo._payLoad.append(bytes);
046: return committedInfo;
047: }
048:
049: private void encodeObjectInfoCollection(ByteArrayOutputStream os,
050: ObjectInfoCollection collection) {
051: Iterator4 iter = collection.iterator();
052: while (iter.moveNext()) {
053: LazyObjectReference obj = (LazyObjectReference) iter
054: .current();
055: writeLong(os, obj.getInternalID());
056: }
057: writeLong(os, -1);
058: }
059:
060: public CallbackObjectInfoCollections decode() {
061: CallbackObjectInfoCollections callbackInfo = CallbackObjectInfoCollections.EMTPY;
062: ByteArrayInputStream is = new ByteArrayInputStream(
063: _payLoad._buffer);
064: callbackInfo.added = decodeObjectInfoCollection(is);
065: callbackInfo.deleted = decodeObjectInfoCollection(is);
066: callbackInfo.updated = decodeObjectInfoCollection(is);
067: return callbackInfo;
068: }
069:
070: private ObjectInfoCollection decodeObjectInfoCollection(
071: ByteArrayInputStream is) {
072: final Collection4 collection = new Collection4();
073: while (true) {
074: long id = readLong(is);
075: if (id == -1) {
076: break;
077: }
078: collection.add(new LazyObjectReference(transaction(),
079: (int) id));
080: }
081: return new ObjectInfoCollectionImpl(collection);
082: }
083:
084: private void writeLong(ByteArrayOutputStream os, long l) {
085: for (int i = 0; i < 64; i += 8) {
086: os.write((int) (l >> i));
087: }
088: }
089:
090: private long readLong(ByteArrayInputStream is) {
091: long l = 0;
092: for (int i = 0; i < 64; i += 8) {
093: l += ((long) (is.read())) << i;
094: }
095: return l;
096: }
097:
098: public boolean processAtClient() {
099: final CallbackObjectInfoCollections callbackInfos = decode();
100: new Thread(new Runnable() {
101: public void run() {
102: if (stream().isClosed()) {
103: return;
104: }
105: stream().callbacks().commitOnCompleted(transaction(),
106: callbackInfos);
107: }
108: }).start();
109: return true;
110: }
111:
112: }
|