1 2 3 package net.nutch.ndfs; 4 5 import net.nutch.io.*; 6 import net.nutch.util.*; 7 8 import java.io.*; 9 import java.util.*; 10 11 16 public class DatanodeInfo implements Writable, Comparable { 17 UTF8 name; 18 long capacity, remaining, lastUpdate, lastObsoleteCheck; 19 volatile TreeSet blocks; 20 21 23 public DatanodeInfo() { 24 this(new UTF8(), 0, 0); 25 } 26 27 public DatanodeInfo(UTF8 name) { 28 this.name = name; 29 int colon = name.toString().indexOf(":"); 30 this.blocks = new TreeSet(); 31 this.lastObsoleteCheck = System.currentTimeMillis(); 32 updateHeartbeat(0, 0); 33 } 34 35 37 public DatanodeInfo(UTF8 name, long capacity, long remaining) { 38 this.name = name; 39 this.blocks = new TreeSet(); 40 this.lastObsoleteCheck = System.currentTimeMillis(); 41 updateHeartbeat(capacity, remaining); 42 } 43 44 46 public void updateBlocks(Block newBlocks[]) { 47 blocks.clear(); 48 for (int i = 0; i < newBlocks.length; i++) { 49 blocks.add(newBlocks[i]); 50 } 51 } 52 53 55 public void addBlock(Block b) { 56 blocks.add(b); 57 } 58 59 61 public void updateHeartbeat(long capacity, long remaining) { 62 this.capacity = capacity; 63 this.remaining = remaining; 64 this.lastUpdate = System.currentTimeMillis(); 65 } 66 public UTF8 getName() { 67 return name; 68 } 69 public String toString() { 70 return name.toString(); 71 } 72 public Block[] getBlocks() { 73 return (Block[]) blocks.toArray(new Block[blocks.size()]); 74 } 75 public Iterator getBlockIterator() { 76 return blocks.iterator(); 77 } 78 public long getCapacity() { 79 return capacity; 80 } 81 public long getRemaining() { 82 return remaining; 83 } 84 public long lastUpdate() { 85 return lastUpdate; 86 } 87 public void updateObsoleteCheck() { 88 this.lastObsoleteCheck = System.currentTimeMillis(); 89 } 90 public long lastObsoleteCheck() { 91 return lastObsoleteCheck; 92 } 93 94 public int compareTo(Object o) { 98 DatanodeInfo d = (DatanodeInfo) o; 99 return name.compareTo(d.getName()); 100 } 101 102 107 public void write(DataOutput out) throws IOException { 108 name.write(out); 109 out.writeLong(capacity); 110 out.writeLong(remaining); 111 out.writeLong(lastUpdate); 112 113 119 } 120 121 123 public void readFields(DataInput in) throws IOException { 124 this.name = new UTF8(); 125 this.name.readFields(in); 126 this.capacity = in.readLong(); 127 this.remaining = in.readLong(); 128 this.lastUpdate = in.readLong(); 129 130 138 } 139 } 140 141 | Popular Tags |