1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * 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 and16 * limitations under the License.17 */1819package org.apache.giraph.comm.requests;
2021import java.io.DataInput;
22import java.io.DataOutput;
23import java.io.IOException;
2425import org.apache.giraph.conf.ImmutableClassesGiraphConfigurable;
26import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration;
27import org.apache.hadoop.io.Writable;
28import org.apache.hadoop.io.WritableComparable;
2930/**31 * Interface for requests to implement32 *33 * @param <I> Vertex id34 * @param <V> Vertex data35 * @param <E> Edge data36 */37publicabstractclass WritableRequest<I extends WritableComparable,
38 V extends Writable, E extends Writable> implements Writable,
39 ImmutableClassesGiraphConfigurable<I, V, E> {
40/**41 * Value to use when size of the request in serialized form is not known42 * or too expensive to calculate43 */44publicstaticfinalint UNKNOWN_SIZE = -1;
4546/** Configuration */47protected ImmutableClassesGiraphConfiguration<I, V, E> conf;
48/** Client id */49privateint clientId = -1;
50/** Request id */51privatelong requestId = -1;
5253publicint getClientId() {
54return clientId;
55 }
5657publicvoid setClientId(int clientId) {
58this.clientId = clientId;
59 }
6061publiclong getRequestId() {
62return requestId;
63 }
6465publicvoid setRequestId(long requestId) {
66this.requestId = requestId;
67 }
6869/**70 * Get the size of the request in serialized form. The number returned by71 * this function can't be less than the actual size - if the size can't be72 * calculated correctly return WritableRequest.UNKNOWN_SIZE.73 *74 * @return The size (in bytes) of serialized request,75 * or WritableRequest.UNKNOWN_SIZE if the size is not known76 * or too expensive to calculate.77 */78publicint getSerializedSize() {
79// 4 for clientId, 8 for requestId80return 4 + 8;
81 }
8283/**84 * Get the type of the request85 *86 * @return Request type87 */88publicabstractRequestType getType();
8990/**91 * Serialize the request92 *93 * @param input Input to read fields from94 */95abstractvoid readFieldsRequest(DataInput input) throws IOException;
9697/**98 * Deserialize the request99 *100 * @param output Output to write the request to101 */102abstractvoid writeRequest(DataOutput output) throws IOException;
103104 @Override
105publicfinal ImmutableClassesGiraphConfiguration<I, V, E> getConf() {
106return conf;
107 }
108109 @Override
110publicfinalvoid setConf(ImmutableClassesGiraphConfiguration<I, V, E> conf) {
111this.conf = conf;
112 }
113114 @Override
115publicfinalvoid readFields(DataInput input) throws IOException {
116 clientId = input.readInt();
117 requestId = input.readLong();
118 readFieldsRequest(input);
119 }
120121 @Override
122publicfinalvoid write(DataOutput output) throws IOException {
123 output.writeInt(clientId);
124 output.writeLong(requestId);
125 writeRequest(output);
126 }
127 }