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.utils;
2021import org.apache.giraph.factories.MessageValueFactory;
22import org.apache.hadoop.io.Writable;
23import org.apache.hadoop.io.WritableComparable;
2425import java.io.IOException;
2627/**28 * Stores a message and a list of target vertex ids.29 *30 * @param <I> Vertex id31 * @param <M> Message data32 */33 @SuppressWarnings("unchecked")
34publicclass ByteArrayOneMessageToManyIds<I extends WritableComparable,
35 M extends Writable> extends ByteArrayVertexIdData<I, M>
36implements VertexIdMessages<I, M> {
37/** Message value class */38private MessageValueFactory<M> messageValueFactory;
3940/**41 * Constructor.42 *43 * @param messageValueFactory Class for messages44 */45publicByteArrayOneMessageToManyIds(
46 MessageValueFactory<M> messageValueFactory) {
47this.messageValueFactory = messageValueFactory;
48 }
4950 @Override
51public M createData() {
52return messageValueFactory.newInstance();
53 }
5455 @Override
56publicvoid writeData(ExtendedDataOutput out, M message) throws IOException {
57 message.write(out);
58 }
5960 @Override
61publicvoid readData(ExtendedDataInput in, M message) throws IOException {
62 message.readFields(in);
63 }
6465/**66 * Add a message.67 * The order is: the message > id count > ids .68 *69 * @param ids The byte array which holds target ids70 * of this message on the worker71 * @param idPos The end position of the ids72 * information in the byte array above.73 * @param count The number of ids74 * @param msg The message sent75 */76publicvoid add(byte[] ids, int idPos, int count, M msg) {
77try {
78 msg.write(extendedDataOutput);
79 extendedDataOutput.writeInt(count);
80 extendedDataOutput.write(ids, 0, idPos);
81 } catch (IOException e) {
82thrownew IllegalStateException("add: IOException", e);
83 }
84 }
8586 @Override
87publicvoid add(I vertexId, M data) {
88thrownew UnsupportedOperationException();
89 }
9091 @Override
92publicvoid add(byte[] serializedId, int idPos, M data) {
93thrownew UnsupportedOperationException();
94 }
9596 @Override
97public VertexIdMessageBytesIterator<I, M> getVertexIdMessageBytesIterator() {
98returnnull;
99 }
100101 @Override
102public VertexIdMessageIterator<I, M> getVertexIdMessageIterator() {
103returnnew OneMessageToManyIdsIterator<>(this);
104 }
105 }