This project has retired. For details please refer to its Attic page.
WritableRequest xref
View Javadoc

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.giraph.comm.requests;
20  
21  import java.io.DataInput;
22  import java.io.DataOutput;
23  import java.io.IOException;
24  
25  import org.apache.giraph.conf.ImmutableClassesGiraphConfigurable;
26  import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration;
27  import org.apache.hadoop.io.Writable;
28  import org.apache.hadoop.io.WritableComparable;
29  
30  /**
31   * Interface for requests to implement
32   *
33   * @param <I> Vertex id
34   * @param <V> Vertex data
35   * @param <E> Edge data
36   */
37  public abstract class 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 known
42     * or too expensive to calculate
43     */
44    public static final int UNKNOWN_SIZE = -1;
45  
46    /** Configuration */
47    protected ImmutableClassesGiraphConfiguration<I, V, E> conf;
48    /** Client id */
49    private int clientId = -1;
50    /** Request id */
51    private long requestId = -1;
52  
53    public int getClientId() {
54      return clientId;
55    }
56  
57    public void setClientId(int clientId) {
58      this.clientId = clientId;
59    }
60  
61    public long getRequestId() {
62      return requestId;
63    }
64  
65    public void setRequestId(long requestId) {
66      this.requestId = requestId;
67    }
68  
69    /**
70     * Get the size of the request in serialized form. The number returned by
71     * this function can't be less than the actual size - if the size can't be
72     * 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 known
76     * or too expensive to calculate.
77     */
78    public int getSerializedSize() {
79      // 4 for clientId, 8 for requestId
80      return 4 + 8;
81    }
82  
83    /**
84     * Get the type of the request
85     *
86     * @return Request type
87     */
88    public abstract RequestType getType();
89  
90    /**
91     * Serialize the request
92     *
93     * @param input Input to read fields from
94     */
95    abstract void readFieldsRequest(DataInput input) throws IOException;
96  
97    /**
98     * Deserialize the request
99     *
100    * @param output Output to write the request to
101    */
102   abstract void writeRequest(DataOutput output) throws IOException;
103 
104   @Override
105   public final ImmutableClassesGiraphConfiguration<I, V, E> getConf() {
106     return conf;
107   }
108 
109   @Override
110   public final void setConf(ImmutableClassesGiraphConfiguration<I, V, E> conf) {
111     this.conf = conf;
112   }
113 
114   @Override
115   public final void readFields(DataInput input) throws IOException {
116     clientId = input.readInt();
117     requestId = input.readLong();
118     readFieldsRequest(input);
119   }
120 
121   @Override
122   public final void write(DataOutput output) throws IOException {
123     output.writeInt(clientId);
124     output.writeLong(requestId);
125     writeRequest(output);
126   }
127 }