Coverage Report - org.apache.giraph.edge.HashMapEdges
 
Classes in this File Line Coverage Branch Coverage Complexity
HashMapEdges
0%
0/33
0%
0/6
1.2
HashMapEdges$1
0%
0/9
N/A
1.2
 
 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;
 20  
 
 21  
 import com.google.common.collect.Maps;
 22  
 
 23  
 import org.apache.giraph.utils.EdgeIterables;
 24  
 import org.apache.hadoop.io.Writable;
 25  
 import org.apache.hadoop.io.WritableComparable;
 26  
 
 27  
 import java.io.DataInput;
 28  
 import java.io.DataOutput;
 29  
 import java.io.IOException;
 30  
 import java.util.HashMap;
 31  
 import java.util.Iterator;
 32  
 import java.util.Map;
 33  
 
 34  
 /**
 35  
  * {@link OutEdges} implementation backed by a {@link HashMap}.
 36  
  * Parallel edges are not allowed.
 37  
  * Note: this implementation is optimized for fast random access and mutations,
 38  
  * but uses more space.
 39  
  *
 40  
  * @param <I> Vertex id
 41  
  * @param <E> Edge value
 42  
  */
 43  0
 public class HashMapEdges<I extends WritableComparable, E extends Writable>
 44  
     extends ConfigurableOutEdges<I, E>
 45  
     implements StrictRandomAccessOutEdges<I, E>,
 46  
     MutableOutEdges<I, E> {
 47  
   /** Map from target vertex id to edge value. */
 48  
   private HashMap<I, E> edgeMap;
 49  
 
 50  
   @Override
 51  
   public void initialize(Iterable<Edge<I, E>> edges) {
 52  0
     EdgeIterables.initialize(this, edges);
 53  0
   }
 54  
 
 55  
   @Override
 56  
   public void initialize(int capacity) {
 57  0
     edgeMap = Maps.newHashMapWithExpectedSize(capacity);
 58  0
   }
 59  
 
 60  
   @Override
 61  
   public void initialize() {
 62  0
     edgeMap = Maps.newHashMap();
 63  0
   }
 64  
 
 65  
   @Override
 66  
   public void add(Edge<I, E> edge) {
 67  0
     edgeMap.put(edge.getTargetVertexId(), edge.getValue());
 68  0
   }
 69  
 
 70  
   @Override
 71  
   public void remove(I targetVertexId) {
 72  0
     edgeMap.remove(targetVertexId);
 73  0
   }
 74  
 
 75  
   @Override
 76  
   public E getEdgeValue(I targetVertexId) {
 77  0
     return edgeMap.get(targetVertexId);
 78  
   }
 79  
 
 80  
   @Override
 81  
   public void setEdgeValue(I targetVertexId, E edgeValue) {
 82  0
     if (edgeMap.containsKey(targetVertexId)) {
 83  0
       edgeMap.put(targetVertexId, edgeValue);
 84  
     }
 85  0
   }
 86  
 
 87  
   @Override
 88  
   public int size() {
 89  0
     return edgeMap.size();
 90  
   }
 91  
 
 92  
   @Override
 93  
   public Iterator<Edge<I, E>> iterator() {
 94  
     // Returns an iterator that reuses objects.
 95  
     // The downcast is fine because all concrete Edge implementations are
 96  
     // mutable, but we only expose the mutation functionality when appropriate.
 97  0
     return (Iterator) mutableIterator();
 98  
   }
 99  
 
 100  
   @Override
 101  
   public Iterator<MutableEdge<I, E>> mutableIterator() {
 102  0
     return new Iterator<MutableEdge<I, E>>() {
 103  
       /** Wrapped map iterator. */
 104  0
       private Iterator<Map.Entry<I, E>> mapIterator =
 105  0
           edgeMap.entrySet().iterator();
 106  
       /** Representative edge object. */
 107  0
       private MapMutableEdge<I, E> representativeEdge =
 108  
           new MapMutableEdge<I, E>();
 109  
 
 110  
       @Override
 111  
       public boolean hasNext() {
 112  0
         return mapIterator.hasNext();
 113  
       }
 114  
 
 115  
       @Override
 116  
       public MutableEdge<I, E> next() {
 117  0
         representativeEdge.setEntry(mapIterator.next());
 118  0
         return representativeEdge;
 119  
       }
 120  
 
 121  
       @Override
 122  
       public void remove() {
 123  0
         mapIterator.remove();
 124  0
       }
 125  
     };
 126  
   }
 127  
 
 128  
   @Override
 129  
   public void write(DataOutput out) throws IOException {
 130  0
     out.writeInt(edgeMap.size());
 131  0
     for (Map.Entry<I, E> entry : edgeMap.entrySet()) {
 132  0
       entry.getKey().write(out);
 133  0
       entry.getValue().write(out);
 134  0
     }
 135  0
   }
 136  
 
 137  
   @Override
 138  
   public void readFields(DataInput in) throws IOException {
 139  0
     int numEdges = in.readInt();
 140  0
     initialize(numEdges);
 141  0
     for (int i = 0; i < numEdges; ++i) {
 142  0
       I targetVertexId = getConf().createVertexId();
 143  0
       targetVertexId.readFields(in);
 144  0
       E edgeValue = getConf().createEdgeValue();
 145  0
       edgeValue.readFields(in);
 146  0
       edgeMap.put(targetVertexId, edgeValue);
 147  
     }
 148  0
   }
 149  
 }