KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > nutch > ndfs > Block


1 /* Copyright (c) 2004 The Nutch Organization. All rights reserved. */
2 /* Use subject to the conditions in http://www.nutch.org/LICENSE.txt. */
3 package net.nutch.ndfs;
4
5 import net.nutch.io.*;
6
7 import java.io.*;
8 import java.util.*;
9
10 /**************************************************
11  * A Block is a Nutch FS primitive, identified by a
12  * long.
13  *
14  * @author Mike Cafarella
15  **************************************************/

16 public class Block implements Writable, Comparable JavaDoc {
17     static Random r = new Random();
18
19     /**
20      */

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     /**
33      */

34     public Block() {
35         this.blkid = r.nextLong();
36         this.len = 0;
37     }
38
39     /**
40      */

41     public Block(long blkid, long len) {
42         this.blkid = blkid;
43         this.len = len;
44     }
45
46     /**
47      * Find the blockid from the given filename
48      */

49     public Block(File f, long len) {
50         String JavaDoc name = f.getName();
51         name = name.substring("blk_".length());
52         this.blkid = Long.parseLong(name);
53         this.len = len;
54     }
55
56     /**
57      */

58     public long getBlockId() {
59         return blkid;
60     }
61
62     /**
63      */

64     public String JavaDoc getBlockName() {
65         return "blk_" + String.valueOf(blkid);
66     }
67
68     /**
69      */

70     public long getNumBytes() {
71         return len;
72     }
73     public void setNumBytes(long len) {
74         this.len = len;
75     }
76
77     /**
78      */

79     public String JavaDoc toString() {
80         return getBlockName();
81     }
82
83     /////////////////////////////////////
84
// Writable
85
/////////////////////////////////////
86
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     /////////////////////////////////////
97
// Comparable
98
/////////////////////////////////////
99
public int compareTo(Object JavaDoc 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