1 2 3 package net.nutch.ndfs; 4 5 import net.nutch.io.*; 6 7 import java.io.*; 8 import java.util.*; 9 10 16 public class Block implements Writable, Comparable { 17 static Random r = new Random(); 18 19 21 public static boolean isBlockFilename(File f) { 22 if (f.getName().startsWith("blk_")) { 23 return true; 24 } else { 25 return false; 26 } 27 } 28 29 long blkid; 30 long len; 31 32 34 public Block() { 35 this.blkid = r.nextLong(); 36 this.len = 0; 37 } 38 39 41 public Block(long blkid, long len) { 42 this.blkid = blkid; 43 this.len = len; 44 } 45 46 49 public Block(File f, long len) { 50 String name = f.getName(); 51 name = name.substring("blk_".length()); 52 this.blkid = Long.parseLong(name); 53 this.len = len; 54 } 55 56 58 public long getBlockId() { 59 return blkid; 60 } 61 62 64 public String getBlockName() { 65 return "blk_" + String.valueOf(blkid); 66 } 67 68 70 public long getNumBytes() { 71 return len; 72 } 73 public void setNumBytes(long len) { 74 this.len = len; 75 } 76 77 79 public String toString() { 80 return getBlockName(); 81 } 82 83 public void write(DataOutput out) throws IOException { 87 out.writeLong(blkid); 88 out.writeLong(len); 89 } 90 91 public void readFields(DataInput in) throws IOException { 92 this.blkid = in.readLong(); 93 this.len = in.readLong(); 94 } 95 96 public int compareTo(Object o) { 100 Block b = (Block) o; 101 if (getBlockId() < b.getBlockId()) { 102 return -1; 103 } else if (getBlockId() == b.getBlockId()) { 104 return 0; 105 } else { 106 return 1; 107 } 108 } 109 } 110 | Popular Tags |