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.partition;
20  
21  import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration;
22  import org.apache.giraph.graph.VertexValueCombiner;
23  import org.apache.giraph.utils.VertexIterator;
24  import org.apache.hadoop.io.Writable;
25  import org.apache.hadoop.io.WritableComparable;
26  import org.apache.hadoop.util.Progressable;
27  
28  import java.io.DataInput;
29  import java.io.DataOutput;
30  import java.io.IOException;
31  
32  /**
33   * Basic partition class for other partitions to extend. Holds partition id,
34   * configuration, progressable and partition context
35   *
36   * @param <I> Vertex index value
37   * @param <V> Vertex value
38   * @param <E> Edge value
39   */
40  public abstract class BasicPartition<I extends WritableComparable,
41      V extends Writable, E extends Writable>
42      implements Partition<I, V, E> {
43    /** Configuration from the worker */
44    private ImmutableClassesGiraphConfiguration<I, V, E> conf;
45    /** Partition id */
46    private int id;
47    /** Context used to report progress */
48    private Progressable progressable;
49    /** Vertex value combiner */
50    private VertexValueCombiner<V> vertexValueCombiner;
51  
52    @Override
53    public void initialize(int partitionId, Progressable progressable) {
54      setId(partitionId);
55      setProgressable(progressable);
56      vertexValueCombiner = conf.createVertexValueCombiner();
57    }
58  
59    @Override
60    public void setConf(
61        ImmutableClassesGiraphConfiguration<I, V, E> configuration) {
62      conf = configuration;
63    }
64  
65    @Override
66    public ImmutableClassesGiraphConfiguration<I, V, E> getConf() {
67      return conf;
68    }
69  
70    @Override
71    public int getId() {
72      return id;
73    }
74  
75    @Override
76    public void setId(int id) {
77      this.id = id;
78    }
79  
80    @Override
81    public void progress() {
82      if (progressable != null) {
83        progressable.progress();
84      }
85    }
86  
87    @Override
88    public void setProgressable(Progressable progressable) {
89      this.progressable = progressable;
90    }
91  
92    public VertexValueCombiner<V> getVertexValueCombiner() {
93      return vertexValueCombiner;
94    }
95  
96    @Override
97    public void addPartitionVertices(VertexIterator<I, V, E> vertexIterator) {
98      while (vertexIterator.hasNext()) {
99        vertexIterator.next();
100       // Release the vertex if it was put, otherwise reuse as an optimization
101       if (putOrCombine(vertexIterator.getVertex())) {
102         vertexIterator.releaseVertex();
103       }
104     }
105   }
106 
107   @Override
108   public void write(DataOutput output) throws IOException {
109     output.writeInt(id);
110   }
111 
112   @Override
113   public void readFields(DataInput input) throws IOException {
114     id = input.readInt();
115   }
116 }