This project has retired. For details please refer to its Attic page.
AbstractVertexIdData 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.conf.ImmutableClassesGiraphConfiguration;
22  import org.apache.hadoop.io.WritableComparable;
23  
24  import java.io.DataInput;
25  import java.io.DataOutput;
26  import java.io.IOException;
27  
28  import static org.apache.giraph.utils.ByteUtils.SIZE_OF_BYTE;
29  import static org.apache.giraph.utils.ByteUtils.SIZE_OF_INT;
30  
31  /**
32   * Partial implementation of vertexIdData
33   *
34   * @param <I> vertexId type parameter
35   * @param <T> vertexData type parameter
36   */
37  @SuppressWarnings("unchecked")
38  public abstract class AbstractVertexIdData<I extends WritableComparable, T>
39    implements VertexIdData<I, T> {
40    /** Extended data output */
41    protected ExtendedDataOutput extendedDataOutput;
42    /** Configuration */
43    private ImmutableClassesGiraphConfiguration<I, ?, ?> configuration;
44  
45    @Override
46    public void initialize() {
47      extendedDataOutput = getConf().createExtendedDataOutput();
48    }
49  
50    @Override
51    public void initialize(int expectedSize) {
52      extendedDataOutput = getConf().createExtendedDataOutput(expectedSize);
53    }
54  
55    @Override
56    public void add(I vertexId, T data) {
57      try {
58        vertexId.write(extendedDataOutput);
59        writeData(extendedDataOutput, data);
60      } catch (IOException e) {
61        throw new IllegalStateException("add: IOException", e);
62      }
63    }
64  
65    @Override
66    public void add(byte[] serializedId, int idPos, T data) {
67      try {
68        extendedDataOutput.write(serializedId, 0, idPos);
69        writeData(extendedDataOutput, data);
70      } catch (IOException e) {
71        throw new IllegalStateException("add: IOException", e);
72      }
73    }
74  
75    @Override
76    public int getSize() {
77      return extendedDataOutput.getPos();
78    }
79  
80  
81    @Override
82    public int getSerializedSize() {
83      return SIZE_OF_BYTE + SIZE_OF_INT + getSize();
84    }
85  
86  
87    @Override
88    public boolean isEmpty() {
89      return extendedDataOutput.getPos() == 0;
90    }
91  
92  
93    @Override
94    public void clear() {
95      extendedDataOutput.reset();
96    }
97  
98    @Override
99    public void setConf(ImmutableClassesGiraphConfiguration configuration) {
100     this.configuration = configuration;
101   }
102 
103   @Override
104   public ImmutableClassesGiraphConfiguration<I, ?, ?> getConf() {
105     return configuration;
106   }
107 
108   @Override
109   public ByteStructVertexIdDataIterator<I, T> getVertexIdDataIterator() {
110     return new ByteStructVertexIdDataIterator<>(this);
111   }
112 
113   @Override
114   public void write(DataOutput output) throws IOException {
115     throw new UnsupportedOperationException("not supported");
116   }
117 
118   @Override
119   public void readFields(DataInput dataInput) throws IOException {
120     throw new UnsupportedOperationException("not supported");
121   }
122 }