Mercurial > hg > Database > Cassandra
comparison src/java/org/apache/cassandra/db/ReadResponse.java @ 0:d485154379c8 default tip
apache-cassandra-0.6.0-rc1-src
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 02 Apr 2010 13:36:02 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:d485154379c8 |
---|---|
1 /** | |
2 * Licensed to the Apache Software Foundation (ASF) under one | |
3 * or more contributor license agreements. See the NOTICE file | |
4 * distributed with this work for additional information | |
5 * regarding copyright ownership. The ASF licenses this file | |
6 * to you under the Apache License, Version 2.0 (the | |
7 * "License"); you may not use this file except in compliance | |
8 * with the License. You may obtain a copy of the License at | |
9 * | |
10 * http://www.apache.org/licenses/LICENSE-2.0 | |
11 * | |
12 * Unless required by applicable law or agreed to in writing, software | |
13 * distributed under the License is distributed on an "AS IS" BASIS, | |
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
15 * See the License for the specific language governing permissions and | |
16 * limitations under the License. | |
17 */ | |
18 | |
19 package org.apache.cassandra.db; | |
20 | |
21 import java.io.ByteArrayOutputStream; | |
22 import java.io.DataInputStream; | |
23 import java.io.DataOutputStream; | |
24 import java.io.IOException; | |
25 | |
26 import org.apache.cassandra.concurrent.StageManager; | |
27 import org.apache.cassandra.io.ICompactSerializer; | |
28 import org.apache.cassandra.net.Message; | |
29 import org.apache.cassandra.net.MessagingService; | |
30 import org.apache.cassandra.utils.FBUtilities; | |
31 | |
32 import org.apache.commons.lang.ArrayUtils; | |
33 | |
34 | |
35 /* | |
36 * The read response message is sent by the server when reading data | |
37 * this encapsulates the tablename and the row that has been read. | |
38 * The table name is needed so that we can use it to create repairs. | |
39 */ | |
40 public class ReadResponse | |
41 { | |
42 private static ICompactSerializer<ReadResponse> serializer_; | |
43 | |
44 static | |
45 { | |
46 serializer_ = new ReadResponseSerializer(); | |
47 } | |
48 | |
49 public static ICompactSerializer<ReadResponse> serializer() | |
50 { | |
51 return serializer_; | |
52 } | |
53 | |
54 private Row row_; | |
55 private byte[] digest_ = ArrayUtils.EMPTY_BYTE_ARRAY; | |
56 private boolean isDigestQuery_ = false; | |
57 | |
58 public ReadResponse(byte[] digest ) | |
59 { | |
60 assert digest != null; | |
61 digest_= digest; | |
62 } | |
63 | |
64 public ReadResponse(Row row) | |
65 { | |
66 row_ = row; | |
67 } | |
68 | |
69 public Row row() | |
70 { | |
71 return row_; | |
72 } | |
73 | |
74 public byte[] digest() | |
75 { | |
76 return digest_; | |
77 } | |
78 | |
79 public boolean isDigestQuery() | |
80 { | |
81 return isDigestQuery_; | |
82 } | |
83 | |
84 public void setIsDigestQuery(boolean isDigestQuery) | |
85 { | |
86 isDigestQuery_ = isDigestQuery; | |
87 } | |
88 } | |
89 | |
90 class ReadResponseSerializer implements ICompactSerializer<ReadResponse> | |
91 { | |
92 public void serialize(ReadResponse rm, DataOutputStream dos) throws IOException | |
93 { | |
94 dos.writeInt(rm.digest().length); | |
95 dos.write(rm.digest()); | |
96 dos.writeBoolean(rm.isDigestQuery()); | |
97 | |
98 if( !rm.isDigestQuery() && rm.row() != null ) | |
99 { | |
100 Row.serializer().serialize(rm.row(), dos); | |
101 } | |
102 } | |
103 | |
104 public ReadResponse deserialize(DataInputStream dis) throws IOException | |
105 { | |
106 int digestSize = dis.readInt(); | |
107 byte[] digest = new byte[digestSize]; | |
108 dis.read(digest, 0 , digestSize); | |
109 boolean isDigest = dis.readBoolean(); | |
110 | |
111 Row row = null; | |
112 if (!isDigest) | |
113 { | |
114 row = Row.serializer().deserialize(dis); | |
115 } | |
116 | |
117 ReadResponse rmsg = isDigest ? new ReadResponse(digest) : new ReadResponse(row); | |
118 rmsg.setIsDigestQuery(isDigest); | |
119 return rmsg; | |
120 } | |
121 } |