This project has retired. For details please refer to its Attic page.
IntEdgeStore 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.edge.primitives;
20  
21  import org.apache.giraph.bsp.CentralizedServiceWorker;
22  import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration;
23  import org.apache.giraph.edge.AbstractEdgeStore;
24  import org.apache.giraph.edge.OutEdges;
25  import org.apache.giraph.utils.VertexIdEdgeIterator;
26  import org.apache.hadoop.io.IntWritable;
27  import org.apache.hadoop.io.Writable;
28  import org.apache.hadoop.util.Progressable;
29  
30  import it.unimi.dsi.fastutil.ints.Int2ObjectMaps;
31  import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
32  import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
33  
34  import java.io.DataInput;
35  import java.io.DataOutput;
36  import java.io.IOException;
37  import java.util.Iterator;
38  import java.util.Map;
39  
40  /**
41   * Special edge store to be used when ids are IntWritable.
42   * Uses fastutil primitive maps in order to decrease number of objects and
43   * get better performance.
44   *
45   * @param <V> Vertex value
46   * @param <E> Edge value
47   */
48  public class IntEdgeStore<V extends Writable, E extends Writable>
49    extends AbstractEdgeStore<IntWritable, V, E, Integer,
50    Int2ObjectMap.Entry<OutEdges<IntWritable, E>>> {
51  
52    /**
53     * Constructor.
54     *
55     * @param service       Service worker
56     * @param configuration Configuration
57     * @param progressable  Progressable
58     */
59    public IntEdgeStore(
60        CentralizedServiceWorker<IntWritable, V, E> service,
61        ImmutableClassesGiraphConfiguration<IntWritable, V, E> configuration,
62        Progressable progressable) {
63      super(service, configuration, progressable);
64    }
65  
66    @Override
67    protected IntWritable getVertexId(
68      Int2ObjectMap.Entry<OutEdges<IntWritable, E>> entry,
69      IntWritable representativeVertexId) {
70      representativeVertexId.set(entry.getIntKey());
71      return representativeVertexId;
72    }
73  
74    @Override
75    protected IntWritable createVertexId(
76      Int2ObjectMap.Entry<OutEdges<IntWritable, E>> entry) {
77      return new IntWritable(entry.getIntKey());
78    }
79  
80    @Override
81    protected OutEdges<IntWritable, E> getPartitionEdges(
82      Int2ObjectMap.Entry<OutEdges<IntWritable, E>> entry) {
83      return entry.getValue();
84    }
85  
86    @Override
87    protected void writeVertexKey(Integer key, DataOutput output)
88        throws IOException {
89      output.writeInt(key);
90    }
91  
92    @Override
93    protected Integer readVertexKey(DataInput input)
94        throws IOException {
95      return input.readInt();
96    }
97  
98    @Override
99    protected Iterator<Int2ObjectMap.Entry<OutEdges<IntWritable, E>>>
100   getPartitionEdgesIterator(
101     Map<Integer, OutEdges<IntWritable, E>> partitionEdges) {
102     return  ((Int2ObjectMap<OutEdges<IntWritable, E>>) partitionEdges)
103         .int2ObjectEntrySet()
104         .iterator();
105   }
106 
107   @Override
108   protected Int2ObjectMap<OutEdges<IntWritable, E>> getPartitionEdges(
109       int partitionId) {
110     Int2ObjectMap<OutEdges<IntWritable, E>> partitionEdges =
111         (Int2ObjectMap<OutEdges<IntWritable, E>>)
112             transientEdges.get(partitionId);
113     if (partitionEdges == null) {
114       Int2ObjectMap<OutEdges<IntWritable, E>> newPartitionEdges =
115           Int2ObjectMaps.synchronize(
116               new Int2ObjectOpenHashMap<OutEdges<IntWritable, E>>());
117       partitionEdges = (Int2ObjectMap<OutEdges<IntWritable, E>>)
118           transientEdges.putIfAbsent(partitionId,
119               newPartitionEdges);
120       if (partitionEdges == null) {
121         partitionEdges = newPartitionEdges;
122       }
123     }
124     return partitionEdges;
125   }
126 
127   @Override
128   protected OutEdges<IntWritable, E> getVertexOutEdges(
129       VertexIdEdgeIterator<IntWritable, E> vertexIdEdgeIterator,
130       Map<Integer, OutEdges<IntWritable, E>> partitionEdgesIn) {
131     Int2ObjectMap<OutEdges<IntWritable, E>> partitionEdges =
132         (Int2ObjectMap<OutEdges<IntWritable, E>>) partitionEdgesIn;
133     IntWritable vertexId = vertexIdEdgeIterator.getCurrentVertexId();
134     OutEdges<IntWritable, E> outEdges = partitionEdges.get(vertexId.get());
135     if (outEdges == null) {
136       synchronized (partitionEdges) {
137         outEdges = partitionEdges.get(vertexId.get());
138         if (outEdges == null) {
139           outEdges = configuration.createAndInitializeInputOutEdges();
140           partitionEdges.put(vertexId.get(), outEdges);
141         }
142       }
143     }
144     return outEdges;
145   }
146 }