1 | /** |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
3 | * or more contributor license agreements. See the NOTICE file |
---|
4 | * distributed with this work for additional information |
---|
5 | * regarding copyright ownership. The ASF licenses this file |
---|
6 | * to you under the Apache License, Version 2.0 (the |
---|
7 | * "License"); you may not use this file except in compliance |
---|
8 | * with the License. You may obtain a copy of the License at |
---|
9 | * |
---|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
11 | * |
---|
12 | * Unless required by applicable law or agreed to in writing, software |
---|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
15 | * See the License for the specific language governing permissions and |
---|
16 | * limitations under the License. |
---|
17 | */ |
---|
18 | package org.apache.hadoop.hdfs.protocol; |
---|
19 | |
---|
20 | /** |
---|
21 | * This class provides an interface for accessing list of blocks that |
---|
22 | * has been implemented as long[]. |
---|
23 | * This class is usefull for block report. Rather than send block reports |
---|
24 | * as a Block[] we can send it as a long[]. |
---|
25 | * |
---|
26 | */ |
---|
27 | public class BlockListAsLongs { |
---|
28 | /** |
---|
29 | * A block as 3 longs |
---|
30 | * block-id and block length and generation stamp |
---|
31 | */ |
---|
32 | private static final int LONGS_PER_BLOCK = 3; |
---|
33 | |
---|
34 | private static int index2BlockId(int index) { |
---|
35 | return index*LONGS_PER_BLOCK; |
---|
36 | } |
---|
37 | private static int index2BlockLen(int index) { |
---|
38 | return (index*LONGS_PER_BLOCK) + 1; |
---|
39 | } |
---|
40 | private static int index2BlockGenStamp(int index) { |
---|
41 | return (index*LONGS_PER_BLOCK) + 2; |
---|
42 | } |
---|
43 | |
---|
44 | private long[] blockList; |
---|
45 | |
---|
46 | /** |
---|
47 | * Converting a block[] to a long[] |
---|
48 | * @param blockArray - the input array block[] |
---|
49 | * @return the output array of long[] |
---|
50 | */ |
---|
51 | |
---|
52 | public static long[] convertToArrayLongs(final Block[] blockArray) { |
---|
53 | long[] blocksAsLongs = new long[blockArray.length * LONGS_PER_BLOCK]; |
---|
54 | |
---|
55 | BlockListAsLongs bl = new BlockListAsLongs(blocksAsLongs); |
---|
56 | assert bl.getNumberOfBlocks() == blockArray.length; |
---|
57 | |
---|
58 | for (int i = 0; i < blockArray.length; i++) { |
---|
59 | bl.setBlock(i, blockArray[i]); |
---|
60 | } |
---|
61 | return blocksAsLongs; |
---|
62 | } |
---|
63 | |
---|
64 | /** |
---|
65 | * Constructor |
---|
66 | * @param iBlockList - BlockListALongs create from this long[] parameter |
---|
67 | */ |
---|
68 | public BlockListAsLongs(final long[] iBlockList) { |
---|
69 | if (iBlockList == null) { |
---|
70 | blockList = new long[0]; |
---|
71 | } else { |
---|
72 | if (iBlockList.length%LONGS_PER_BLOCK != 0) { |
---|
73 | // must be multiple of LONGS_PER_BLOCK |
---|
74 | throw new IllegalArgumentException(); |
---|
75 | } |
---|
76 | blockList = iBlockList; |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | |
---|
81 | /** |
---|
82 | * The number of blocks |
---|
83 | * @return - the number of blocks |
---|
84 | */ |
---|
85 | public int getNumberOfBlocks() { |
---|
86 | return blockList.length/LONGS_PER_BLOCK; |
---|
87 | } |
---|
88 | |
---|
89 | |
---|
90 | /** |
---|
91 | * The block-id of the indexTh block |
---|
92 | * @param index - the block whose block-id is desired |
---|
93 | * @return the block-id |
---|
94 | */ |
---|
95 | public long getBlockId(final int index) { |
---|
96 | return blockList[index2BlockId(index)]; |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * The block-len of the indexTh block |
---|
101 | * @param index - the block whose block-len is desired |
---|
102 | * @return - the block-len |
---|
103 | */ |
---|
104 | public long getBlockLen(final int index) { |
---|
105 | return blockList[index2BlockLen(index)]; |
---|
106 | } |
---|
107 | |
---|
108 | /** |
---|
109 | * The generation stamp of the indexTh block |
---|
110 | * @param index - the block whose block-len is desired |
---|
111 | * @return - the generation stamp |
---|
112 | */ |
---|
113 | public long getBlockGenStamp(final int index) { |
---|
114 | return blockList[index2BlockGenStamp(index)]; |
---|
115 | } |
---|
116 | |
---|
117 | /** |
---|
118 | * Set the indexTh block |
---|
119 | * @param index - the index of the block to set |
---|
120 | * @param b - the block is set to the value of the this block |
---|
121 | */ |
---|
122 | void setBlock(final int index, final Block b) { |
---|
123 | blockList[index2BlockId(index)] = b.getBlockId(); |
---|
124 | blockList[index2BlockLen(index)] = b.getNumBytes(); |
---|
125 | blockList[index2BlockGenStamp(index)] = b.getGenerationStamp(); |
---|
126 | } |
---|
127 | } |
---|