Advertisement
Guest User

Giraph VertexValue

a guest
Mar 25th, 2014
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.04 KB | None | 0 0
  1. package prediction;
  2.  
  3. import java.io.DataInput;
  4. import java.io.DataOutput;
  5. import java.io.IOException;
  6. import java.util.HashMap;
  7. import java.util.HashSet;
  8. import java.util.Iterator;
  9. import java.util.LinkedList;
  10. import java.util.List;
  11. import java.util.Map.Entry;
  12. import java.util.Set;
  13. import java.util.SortedSet;
  14. import java.util.TreeSet;
  15.  
  16. import org.apache.hadoop.io.Writable;
  17.  
  18. public class PredictionVertexValue implements Writable{
  19.  
  20.     private long stepCount;
  21.    
  22.     /** holds two step neighbors and their first step neighbors */
  23.     private HashMap<Long, Set<Long>> neighbors;
  24.     private Set<Long> myNeighbors;
  25.     private SortedSet<LinkScore> linkScore;
  26.     private HashMap<Long, Double> multiLables;
  27.     private HashMap<Long, Integer> communitySizes;
  28.     private HashMap<Long, HashMap<Long, Double>> nodeCommunityInfo;
  29.    
  30.     public PredictionVertexValue() {
  31.         stepCount = 0;
  32.         neighbors = new HashMap<Long, Set<Long>>(10);
  33.         linkScore = new TreeSet<LinkScore>();
  34.         multiLables = new HashMap<Long, Double>();
  35.         communitySizes = new HashMap<Long, Integer>();
  36.         nodeCommunityInfo =new HashMap<Long, HashMap<Long, Double>>();
  37.     }
  38.    
  39.     public PredictionVertexValue(HashMap<Long, Double> comLabels,
  40.             HashMap<Long, Integer> comSizes) {
  41.         this();
  42.         this.communitySizes = comSizes;
  43.         this.multiLables = comLabels;
  44.     }
  45.  
  46.     public void addLinkScore(long targetNode, double score) {
  47.         LinkScore ls = new LinkScore(targetNode, score);
  48.         linkScore.add(ls);
  49.     }
  50.    
  51.     public double getLinkScore(long target) {
  52.         LinkScore ls = new LinkScore(target, 0);
  53.         if (linkScore.contains(ls)) {
  54.             Iterator<LinkScore> iter = linkScore.iterator();
  55.             for (LinkScore ls2 = iter.next(); iter.hasNext(); iter.next()) {
  56.                 if (target == ls2.getTargetNode()) {
  57.                     return ls2.getScore();
  58.                 }
  59.             }
  60.         }
  61.         return -1;
  62.     }
  63.    
  64.     public HashMap<Long, Double> getMultiLabels() {
  65.         return multiLables;
  66.     }
  67.    
  68.     public HashMap<Long, Integer> getCommunitySizes() {
  69.         return communitySizes;
  70.     }
  71.    
  72.     public void setMyNeighborSet(Set<Long> set) {
  73.         myNeighbors = set;
  74.     }
  75.    
  76.     public Set<Long> getMyNeighborSet() {
  77.         return myNeighbors;
  78.     }
  79.    
  80.     public void addTwoStepNeighborSet(long id, Set<Long> myNeighbors) {
  81.         neighbors.put(id, myNeighbors);        
  82.     }
  83.    
  84.     /**
  85.      * Returns a list of twostep neighbors
  86.      * @return
  87.      */
  88.     public List<Long> getTwoStepNeighbors() {
  89.         return new LinkedList<>(neighbors.keySet());
  90.     }
  91.    
  92.     /**
  93.      * Returns the set of friends of a two step neighbor
  94.      * @param id
  95.      * @return
  96.      */
  97.     public Set<Long> getFriendsOf(Long id) {
  98.         return neighbors.get(id);
  99.     }
  100.  
  101.     public long getStepCount() {
  102.         return stepCount;
  103.     }
  104.    
  105.     public void setCurrentStepDone() {
  106.         stepCount++;
  107.     }
  108.    
  109.     public void addNeighborCommunityInfo(long neighbor,
  110.             HashMap<Long, Double> multiLables) {
  111.         nodeCommunityInfo.put(neighbor, multiLables);
  112.     }
  113.    
  114.     public HashMap<Long, Double> getNeighborCommunityInfo(long neighbor) {
  115.         return nodeCommunityInfo.get(neighbor);
  116.     }
  117.    
  118.     @Override
  119.     public void write(DataOutput out) throws IOException {
  120.    
  121.         // write neighbors
  122.         out.writeInt(neighbors.size());
  123.         for (Entry<Long, Set<Long>> entry : neighbors.entrySet()) {
  124.             out.writeLong(entry.getKey());
  125.             out.writeInt(entry.getValue().size());
  126.             for(Long l : entry.getValue()) {
  127.                 out.writeLong(l);
  128.             }
  129.         }
  130.        
  131.         // write myNeigbors
  132.         out.writeInt(myNeighbors.size());
  133.         for (Long l : myNeighbors) {
  134.             out.writeLong(l);
  135.         }
  136.        
  137.         // write linkScore
  138.         out.writeInt(linkScore.size());
  139.         for (LinkScore ls : linkScore) {
  140.             ls.write(out);
  141.         }
  142.        
  143.         // write multiLables
  144.         out.writeInt(multiLables.size());
  145.         for (Entry<Long, Double> entry : multiLables.entrySet()){
  146.             out.writeLong(entry.getKey());
  147.             out.writeDouble(entry.getValue());
  148.         }
  149.        
  150.         // write communitySizes
  151.         out.writeInt(communitySizes.size());
  152.         for (Entry<Long, Integer> entry : communitySizes.entrySet()){
  153.             out.writeLong(entry.getKey());
  154.             out.writeInt(entry.getValue());
  155.         }
  156.        
  157.         // write nodeCommunityInfo
  158.         out.writeInt(nodeCommunityInfo.size());
  159.         for (Entry<Long, HashMap<Long, Double>> entry : nodeCommunityInfo.entrySet()){
  160.             out.writeLong(entry.getKey());
  161.             out.writeInt(entry.getValue().size());
  162.             for (Entry<Long, Double> entry2 : entry.getValue().entrySet()){
  163.                 out.writeLong(entry2.getKey());
  164.                 out.writeDouble(entry2.getValue());        
  165.             }
  166.         }
  167.     }
  168.  
  169.     @Override
  170.     public void readFields(DataInput in) throws IOException {
  171.         // read neighbors
  172.         int size = in.readInt();
  173.         neighbors = new HashMap<Long, Set<Long>>(size);
  174.         for (int i = 0; i < size; i++) {
  175.             long key = in.readLong();
  176.             int size2 = in.readInt();
  177.             Set<Long> nSet = new HashSet<Long>(size2);
  178.             for (int j = 0; j < size2; j++) {
  179.                 long l = in.readLong();
  180.                 nSet.add(l);
  181.             }
  182.             neighbors.put(key, nSet);
  183.         }
  184.        
  185.         // read myNeigbors
  186.         size = in.readInt();
  187.         myNeighbors = new HashSet<Long>(size);
  188.         for (int i = 0; i < size; i++) {
  189.             myNeighbors.add(in.readLong());
  190.         }
  191.        
  192.         // read linkScore
  193.         size = in.readInt();
  194.         linkScore = new TreeSet<LinkScore>();
  195.         for (int i = 0; i < size; i++) {
  196.             LinkScore ls = new LinkScore();
  197.             ls.readFields(in);
  198.             linkScore.add(ls);
  199.         }
  200.        
  201.         // read multiLables
  202.         size = in.readInt();
  203.         multiLables = new HashMap<Long, Double>(size);
  204.         for (int i = 0; i < size; i++) {
  205.             long key = in.readLong();
  206.             double value = in.readDouble();
  207.             multiLables.put(key, value);
  208.         }
  209.        
  210.         // read communitySizes
  211.         size = in.readInt();
  212.         communitySizes = new HashMap<Long, Integer>(size);
  213.         for (int i = 0; i < size; i++) {
  214.             long key = in.readLong();
  215.             int value = in.readInt();
  216.             communitySizes.put(key, value);
  217.         }      
  218.        
  219.         // read nodeCommunityInfo
  220.         size = in.readInt();
  221.         nodeCommunityInfo = new HashMap<Long, HashMap<Long,Double>>(size);
  222.         for (int i = 0; i < size; i++) {
  223.             long key = in.readLong();
  224.             int size2 = in.readInt();
  225.             HashMap<Long, Double> tmp = new HashMap<Long, Double>(size2);
  226.             for (int j = 0; j < size2; j++) {
  227.                 long key2 = in.readLong();
  228.                 double value = in.readDouble();
  229.                 tmp.put(key2, value);
  230.             }
  231.             nodeCommunityInfo.put(key, tmp);
  232.         }
  233.     }
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement