-
Notifications
You must be signed in to change notification settings - Fork 0
/
Block.java
214 lines (181 loc) · 5.63 KB
/
Block.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.hdfs.protocol;
import java.io.*;
import org.apache.hadoop.hdfs.server.common.GenerationStamp;
import org.apache.hadoop.io.*;
/**************************************************
* A Block is a Hadoop FS primitive, identified by a
* long.
*
**************************************************/
public class Block implements Writable, Comparable<Block> {
static { // register a ctor
WritableFactories.setFactory
(Block.class,
new WritableFactory() {
public Writable newInstance() { return new Block(); }
});
}
// generation stamp of blocks that pre-date the introduction of
// a generation stamp.
public static final long GRANDFATHER_GENERATION_STAMP = 0;
/**
*/
public static boolean isBlockFilename(File f) {
String name = f.getName();
if ( name.startsWith( "blk_" ) &&
name.indexOf( '.' ) < 0 ) {
return true;
} else {
return false;
}
}
static long filename2id(String name) {
return Long.parseLong(name.substring("blk_".length()));
}
private long blockId;
private long numBytes;
private long generationStamp;
private long activeConnection;//PROJECT
public Block() {this(0, 0, 0);}
public Block(final long blkid, final long len, final long generationStamp) {
set(blkid, len, generationStamp);
activeConnection = 0;//PROJECT
}
public Block(final long blkid) {this(blkid, 0, GenerationStamp.WILDCARD_STAMP);}
public Block(Block blk) {this(blk.blockId, blk.numBytes, blk.generationStamp);
setActiveConnection((int)blk.activeConnection);
}
/**
* Find the blockid from the given filename
*/
public Block(File f, long len, long genstamp) {
this(filename2id(f.getName()), len, genstamp);
}
public void set(long blkid, long len, long genStamp) {
this.blockId = blkid;
this.numBytes = len;
this.generationStamp = genStamp;
}
/**
*/
public void set(long blkid, long len, long genStamp,long activeconnection){
this.blockId = blkid;
this.numBytes = len;
this.generationStamp = genStamp;
this.activeConnection = activeconnection;//Project
}
public long getBlockId() {
return blockId;
}
public void setBlockId(long bid) {
blockId = bid;
}
//PROJECT
public void incrementActiveConnection() {
this.activeConnection = this.activeConnection + 1;
}
public void decrementActiveConnection(){
this.activeConnection = this.activeConnection -1 ;
}
public long getActiveConnection(){
return activeConnection;
}
public void setActiveConnection(int count){
this.activeConnection = count;
}
/**
*/
public String getBlockName() {
return "blk_" + String.valueOf(blockId);
}
/**
*/
public long getNumBytes() {
return numBytes;
}
public void setNumBytes(long len) {
this.numBytes = len;
}
public long getGenerationStamp() {
return generationStamp;
}
public void setGenerationStamp(long stamp) {
generationStamp = stamp;
}
/**
*/
public String toString() {
return getBlockName() + "_" + getGenerationStamp();
}
/////////////////////////////////////
// Writable
/////////////////////////////////////
public void write(DataOutput out) throws IOException {
out.writeLong(blockId);
out.writeLong(numBytes);
out.writeLong(generationStamp);
}
public void readFields(DataInput in) throws IOException {
this.blockId = in.readLong();
this.numBytes = in.readLong();
this.generationStamp = in.readLong();
if (numBytes < 0) {
throw new IOException("Unexpected block size: " + numBytes);
}
}
/////////////////////////////////////
// Comparable
/////////////////////////////////////
static void validateGenerationStamp(long generationstamp) {
if (generationstamp == GenerationStamp.WILDCARD_STAMP) {
throw new IllegalStateException("generationStamp (=" + generationstamp
+ ") == GenerationStamp.WILDCARD_STAMP");
}
}
/** {@inheritDoc} */
public int compareTo(Block b) {
//Wildcard generationStamp is NOT ALLOWED here
validateGenerationStamp(this.generationStamp);
validateGenerationStamp(b.generationStamp);
if (blockId < b.blockId) {
return -1;
} else if (blockId == b.blockId) {
return GenerationStamp.compare(generationStamp, b.generationStamp);
} else {
return 1;
}
}
/** {@inheritDoc} */
public boolean equals(Object o) {
if (!(o instanceof Block)) {
return false;
}
final Block that = (Block)o;
//Wildcard generationStamp is ALLOWED here
return this.blockId == that.blockId
&& GenerationStamp.equalsWithWildcard(
this.generationStamp, that.generationStamp);
}
/** {@inheritDoc} */
public int hashCode() {
//GenerationStamp is IRRELEVANT and should not be used here
return 37 * 17 + (int) (blockId^(blockId>>>32));
}
}