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.hadoop.io.WritableComparable;
2223import java.io.IOException;
24import javax.annotation.concurrent.NotThreadSafe;
2526/**27 * Special iterator that reuses vertex ids and data objects so that the28 * lifetime of the object is only until next() is called.29 *30 * Vertex id ownership can be released if desired through31 * releaseCurrentVertexId(). This optimization allows us to cut down32 * on the number of objects instantiated and garbage collected.33 *34 * @param <I> vertexId type parameter35 * @param <T> vertexData type parameter36 */37 @NotThreadSafe
38publicclass ByteStructVertexIdDataIterator<I extends WritableComparable, T>
39extends ByteStructVertexIdIterator<I> implements VertexIdDataIterator<I, T> {
40/**VertexIdData to iterate over */41protected AbstractVertexIdData<I, T> vertexIdData;
42/** Serialized size of the message object in bytestore */43protectedint dataSize;
44/** Current data. */45private T data;
4647/**48 * Constructor49 *50 * @param vertexIdData vertexIdData51 */52publicByteStructVertexIdDataIterator(
53 AbstractVertexIdData<I, T> vertexIdData) {
54super(vertexIdData.extendedDataOutput, vertexIdData.getConf());
55this.vertexIdData = vertexIdData;
56 }
5758 @Override
59publicvoid next() {
60if (vertexId == null) {
61 vertexId = vertexIdData.getConf().createVertexId();
62 }
63if (data == null) {
64 data = vertexIdData.createData();
65 }
66try {
67 vertexId.readFields(extendedDataInput);
68int initial = extendedDataInput.getPos();
69 vertexIdData.readData(extendedDataInput, data);
70 dataSize = extendedDataInput.getPos() - initial;
71 } catch (IOException e) {
72thrownew IllegalStateException("next: IOException", e);
73 }
74 }
7576 @Override
77publicint getCurrentDataSize() {
78return dataSize;
79 }
8081 @Override
82public T getCurrentData() {
83return data;
84 }
8586 @Override
87public T releaseCurrentData() {
88 T releasedData = data;
89 data = null;
90return releasedData;
91 }
92 }