001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.object.msg;
006:
007: import com.tc.async.api.EventContext;
008: import com.tc.bytes.TCByteBuffer;
009: import com.tc.io.TCByteBufferOutput;
010: import com.tc.net.protocol.tcm.MessageChannel;
011: import com.tc.net.protocol.tcm.MessageMonitor;
012: import com.tc.net.protocol.tcm.TCMessageHeader;
013: import com.tc.net.protocol.tcm.TCMessageType;
014: import com.tc.object.dna.impl.ObjectDNAImpl;
015: import com.tc.object.dna.impl.ObjectStringSerializer;
016: import com.tc.object.session.SessionID;
017: import com.tc.util.CommonShutDownHook;
018:
019: import java.io.IOException;
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.Collections;
023:
024: /**
025: * @author steve
026: */
027: public class RequestManagedObjectResponseMessage extends DSOMessageBase
028: implements EventContext {
029:
030: private final static byte SERIALIZER_ID = 1;
031: private final static byte TOTAL_ID = 2;
032: private final static byte BATCH_ID = 3;
033: private final static byte DNA_COUNT = 4;
034: private final static byte DNA_DATA = 5;
035:
036: private Collection objects;
037: private ObjectStringSerializer serializer;
038: private int total;
039: private long batchID;
040: private TCByteBuffer[] dnaData;
041: private int dnaCount;
042:
043: public RequestManagedObjectResponseMessage(SessionID sessionID,
044: MessageMonitor monitor, TCByteBufferOutput out,
045: MessageChannel channel, TCMessageType type) {
046: super (sessionID, monitor, out, channel, type);
047: }
048:
049: public RequestManagedObjectResponseMessage(SessionID sessionID,
050: MessageMonitor monitor, MessageChannel channel,
051: TCMessageHeader header, TCByteBuffer[] data) {
052: super (sessionID, monitor, channel, header, data);
053: }
054:
055: public Collection getObjects() {
056: return Collections.unmodifiableCollection(objects);
057: }
058:
059: public void initialize(TCByteBuffer[] dnas, int count,
060: ObjectStringSerializer aSerializer, long bid, int tot) {
061: // System.err.println("SARO : dna count = " + count + " dnas[] = " + dnas.length + " tot = " + tot);
062: // for (int i = 0; i < dnas.length; i++) {
063: // System.err.println("SARO : "+ i + " : " + dnas[i].capacity());
064: // }
065: this .dnaCount = count;
066: this .dnaData = dnas;
067: this .serializer = aSerializer;
068: this .batchID = bid;
069: this .total = tot;
070: }
071:
072: public ObjectStringSerializer getSerializer() {
073: return serializer;
074: }
075:
076: public long getBatchID() {
077: return batchID;
078: }
079:
080: public int getTotal() {
081: return total;
082: }
083:
084: protected void dehydrateValues() {
085: putNVPair(SERIALIZER_ID, serializer);
086: putNVPair(BATCH_ID, batchID);
087: putNVPair(TOTAL_ID, total);
088: putNVPair(DNA_COUNT, dnaCount);
089: putNVPair(DNA_DATA, dnaData);
090: dnaData = null;
091: }
092:
093: protected boolean hydrateValue(byte name) throws IOException {
094: switch (name) {
095: case DNA_DATA: {
096: for (int i = 0, n = dnaCount; i < n; i++) {
097: objects.add(getObject(new ObjectDNAImpl(serializer,
098: false)));
099: }
100: return true;
101: }
102: case DNA_COUNT:
103: dnaCount = getIntValue();
104: this .objects = new ArrayList(dnaCount);
105: return true;
106: case BATCH_ID:
107: this .batchID = getLongValue();
108: return true;
109: case TOTAL_ID:
110: this .total = getIntValue();
111: return true;
112: case SERIALIZER_ID:
113: this .serializer = (ObjectStringSerializer) getObject(new ObjectStringSerializer());
114: return true;
115: default:
116: return false;
117: }
118: }
119:
120: static int rcount;
121: static int bufferCount;
122: static {
123: CommonShutDownHook.addShutdownHook(new Runnable() {
124: public void run() {
125: logger.info("No of times Buffers wasted = " + rcount
126: + " Buffers wasted count = " + bufferCount);
127: }
128: });
129: }
130:
131: // TODO :: It is recycled only on write. Not on read.
132: public void doRecycleOnRead() {
133: rcount++;
134: bufferCount += getEntireMessageData().length;
135: }
136: }
|