This project has retired. For details please refer to its Attic page.
ByteArrayOneMessageToManyIds 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.utils;
20  
21  import org.apache.giraph.factories.MessageValueFactory;
22  import org.apache.hadoop.io.Writable;
23  import org.apache.hadoop.io.WritableComparable;
24  
25  import java.io.IOException;
26  
27  /**
28   * Stores a message and a list of target vertex ids.
29   *
30   * @param <I> Vertex id
31   * @param <M> Message data
32   */
33  @SuppressWarnings("unchecked")
34  public class ByteArrayOneMessageToManyIds<I extends WritableComparable,
35    M extends Writable> extends ByteArrayVertexIdData<I, M>
36    implements VertexIdMessages<I, M> {
37    /** Message value class */
38    private MessageValueFactory<M> messageValueFactory;
39  
40    /**
41     * Constructor.
42     *
43     * @param messageValueFactory Class for messages
44     */
45    public ByteArrayOneMessageToManyIds(
46        MessageValueFactory<M> messageValueFactory) {
47      this.messageValueFactory = messageValueFactory;
48    }
49  
50    @Override
51    public M createData() {
52      return messageValueFactory.newInstance();
53    }
54  
55    @Override
56    public void writeData(ExtendedDataOutput out, M message) throws IOException {
57      message.write(out);
58    }
59  
60    @Override
61    public void readData(ExtendedDataInput in, M message) throws IOException {
62      message.readFields(in);
63    }
64  
65    /**
66     * Add a message.
67     * The order is: the message &gt; id count &gt; ids .
68     *
69     * @param ids   The byte array which holds target ids
70     *              of this message on the worker
71     * @param idPos The end position of the ids
72     *              information in the byte array above.
73     * @param count The number of ids
74     * @param msg   The message sent
75     */
76    public void add(byte[] ids, int idPos, int count, M msg) {
77      try {
78        msg.write(extendedDataOutput);
79        extendedDataOutput.writeInt(count);
80        extendedDataOutput.write(ids, 0, idPos);
81      } catch (IOException e) {
82        throw new IllegalStateException("add: IOException", e);
83      }
84    }
85  
86    @Override
87    public void add(I vertexId, M data) {
88      throw new UnsupportedOperationException();
89    }
90  
91    @Override
92    public void add(byte[] serializedId, int idPos, M data) {
93      throw new UnsupportedOperationException();
94    }
95  
96    @Override
97    public VertexIdMessageBytesIterator<I, M> getVertexIdMessageBytesIterator() {
98      return null;
99    }
100 
101   @Override
102   public VertexIdMessageIterator<I, M> getVertexIdMessageIterator() {
103     return new OneMessageToManyIdsIterator<>(this);
104   }
105 }