1 5 package com.tc.l2.msg; 6 7 import com.tc.async.api.EventContext; 8 import com.tc.bytes.TCByteBuffer; 9 import com.tc.io.TCByteBufferInputStream; 10 import com.tc.net.groups.AbstractGroupMessage; 11 import com.tc.object.ObjectID; 12 import com.tc.object.dna.impl.ObjectDNAImpl; 13 import com.tc.object.dna.impl.ObjectStringSerializer; 14 import com.tc.util.Assert; 15 16 import java.io.IOException ; 17 import java.io.ObjectInput ; 18 import java.io.ObjectOutput ; 19 import java.util.ArrayList ; 20 import java.util.Collections ; 21 import java.util.HashMap ; 22 import java.util.HashSet ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.Map ; 26 import java.util.Set ; 27 import java.util.Map.Entry; 28 29 public class ObjectSyncMessage extends AbstractGroupMessage implements EventContext { 30 31 public static final int MANAGED_OBJECT_SYNC_TYPE = 0; 32 33 private Set oids; 34 private int dnaCount; 35 private TCByteBuffer[] dnas; 36 private ObjectStringSerializer serializer; 37 private Map rootsMap; 38 39 public ObjectSyncMessage() { 40 super(-1); 42 } 43 44 public ObjectSyncMessage(int type) { 45 super(type); 46 } 47 48 protected void basicReadExternal(int msgType, ObjectInput in) throws IOException { 49 Assert.assertEquals(MANAGED_OBJECT_SYNC_TYPE, msgType); 50 readObjectIDS(in); 51 dnaCount = in.readInt(); 52 readRootsMap(in); 53 serializer = readObjectStringSerializer(in); 54 this.dnas = readByteBuffers(in); 55 } 56 57 protected void basicWriteExternal(int msgType, ObjectOutput out) throws IOException { 58 Assert.assertEquals(MANAGED_OBJECT_SYNC_TYPE, msgType); 59 writeObjectIDS(out); 60 out.writeInt(dnaCount); 61 writeRootsMap(out); 62 writeObjectStringSerializer(out,serializer); 63 writeByteBuffers(out, dnas); 64 recycle(dnas); 65 dnas = null; 66 } 67 68 private void writeRootsMap(ObjectOutput out) throws IOException { 69 out.writeInt(rootsMap.size()); 70 for (Iterator i = rootsMap.entrySet().iterator(); i.hasNext();) { 71 Entry e = (Entry) i.next(); 72 out.writeUTF((String ) e.getKey()); 73 out.writeLong(((ObjectID)e.getValue()).toLong()); 74 } 75 } 76 77 private void readRootsMap(ObjectInput in) throws IOException { 78 int size = in.readInt(); 79 if(size == 0) { 80 this.rootsMap = Collections.EMPTY_MAP; 81 } else { 82 this.rootsMap = new HashMap (size); 83 for (int i = 0; i < size; i++) { 84 this.rootsMap.put(in.readUTF(), new ObjectID(in.readLong())); 85 } 86 } 87 } 88 89 90 private void recycle(TCByteBuffer[] buffers) { 91 for (int i = 0; i < buffers.length; i++) { 92 buffers[i].recycle(); 93 } 94 } 95 96 private void writeObjectIDS(ObjectOutput out) throws IOException { 97 out.writeInt(oids.size()); 98 for (Iterator i = oids.iterator(); i.hasNext();) { 99 ObjectID oid = (ObjectID) i.next(); 100 out.writeLong(oid.toLong()); 101 } 102 } 103 104 private void readObjectIDS(ObjectInput in) throws IOException { 105 int size = in.readInt(); 106 oids = new HashSet (size); 107 for (int i = 0; i < size; i++) { 108 oids.add(new ObjectID(in.readLong())); 109 } 110 } 111 112 public void initialize(Set dnaOids, int count, TCByteBuffer[] serializedDNAs, 113 ObjectStringSerializer objectSerializer, Map roots) { 114 this.oids = dnaOids; 115 this.dnaCount = count; 116 this.dnas = serializedDNAs; 117 this.serializer = objectSerializer; 118 this.rootsMap = roots; 119 } 120 121 public int getDnaCount() { 122 return dnaCount; 123 } 124 125 public Set getOids() { 126 return oids; 127 } 128 129 public ObjectStringSerializer getSerializer() { 130 return serializer; 131 } 132 133 public Map getRootsMap() { 134 return rootsMap; 135 } 136 137 141 public List getDNAs() { 142 Assert.assertNotNull(this.dnas); 143 TCByteBufferInputStream toi = new TCByteBufferInputStream(this.dnas); 144 ArrayList objectDNAs = new ArrayList (dnaCount); 145 for (int i = 0; i < dnaCount; i++) { 146 ObjectDNAImpl dna = new ObjectDNAImpl(serializer, false); 147 try { 148 dna.deserializeFrom(toi); 149 } catch (IOException e) { 150 throw new AssertionError (e); 151 } 152 Assert.assertFalse(dna.isDelta()); 153 objectDNAs.add(dna); 154 } 155 this.dnas = null; 156 return objectDNAs; 157 } 158 159 } 160 | Popular Tags |