|
| 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, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.hadoop.fs.impl.prefetch; |
| 21 | + |
| 22 | +import static org.apache.hadoop.fs.impl.prefetch.Validate.checkNotNegative; |
| 23 | +import static org.apache.hadoop.fs.impl.prefetch.Validate.checkPositiveInteger; |
| 24 | +import static org.apache.hadoop.fs.impl.prefetch.Validate.checkWithinRange; |
| 25 | + |
| 26 | +/** |
| 27 | + * Holds information about blocks of data in a file. |
| 28 | + */ |
| 29 | +public final class BlockData { |
| 30 | + |
| 31 | + // State of each block of data. |
| 32 | + enum State { |
| 33 | + |
| 34 | + /** Data is not yet ready to be read from this block (still being prefetched). */ |
| 35 | + NOT_READY, |
| 36 | + |
| 37 | + /** A read of this block has been enqueued in the prefetch queue. */ |
| 38 | + QUEUED, |
| 39 | + |
| 40 | + /** A read of this block has been enqueued in the prefetch queue. */ |
| 41 | + READY, |
| 42 | + |
| 43 | + /** This block has been cached in the local disk cache. */ |
| 44 | + CACHED |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * State of all blocks in a file. |
| 49 | + */ |
| 50 | + private State[] state; |
| 51 | + |
| 52 | + /** |
| 53 | + * The size of a file. |
| 54 | + */ |
| 55 | + private final long fileSize; |
| 56 | + |
| 57 | + /** |
| 58 | + * The file is divided into blocks of this size. |
| 59 | + */ |
| 60 | + private final int blockSize; |
| 61 | + |
| 62 | + /** |
| 63 | + * The file has these many blocks. |
| 64 | + */ |
| 65 | + private final int numBlocks; |
| 66 | + |
| 67 | + /** |
| 68 | + * Constructs an instance of {@link BlockData}. |
| 69 | + * @param fileSize the size of a file. |
| 70 | + * @param blockSize the file is divided into blocks of this size. |
| 71 | + * @throws IllegalArgumentException if fileSize is negative. |
| 72 | + * @throws IllegalArgumentException if blockSize is negative. |
| 73 | + * @throws IllegalArgumentException if blockSize is zero or negative. |
| 74 | + */ |
| 75 | + public BlockData(long fileSize, int blockSize) { |
| 76 | + checkNotNegative(fileSize, "fileSize"); |
| 77 | + if (fileSize == 0) { |
| 78 | + checkNotNegative(blockSize, "blockSize"); |
| 79 | + } else { |
| 80 | + checkPositiveInteger(blockSize, "blockSize"); |
| 81 | + } |
| 82 | + |
| 83 | + this.fileSize = fileSize; |
| 84 | + this.blockSize = blockSize; |
| 85 | + this.numBlocks = |
| 86 | + (fileSize == 0) |
| 87 | + ? 0 |
| 88 | + : ((int) (fileSize / blockSize)) + (fileSize % blockSize > 0 |
| 89 | + ? 1 |
| 90 | + : 0); |
| 91 | + this.state = new State[this.numBlocks]; |
| 92 | + for (int b = 0; b < this.numBlocks; b++) { |
| 93 | + setState(b, State.NOT_READY); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Gets the size of each block. |
| 99 | + * @return the size of each block. |
| 100 | + */ |
| 101 | + public int getBlockSize() { |
| 102 | + return blockSize; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Gets the size of the associated file. |
| 107 | + * @return the size of the associated file. |
| 108 | + */ |
| 109 | + public long getFileSize() { |
| 110 | + return fileSize; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Gets the number of blocks in the associated file. |
| 115 | + * @return the number of blocks in the associated file. |
| 116 | + */ |
| 117 | + public int getNumBlocks() { |
| 118 | + return numBlocks; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Indicates whether the given block is the last block in the associated file. |
| 123 | + * @param blockNumber the id of the desired block. |
| 124 | + * @return true if the given block is the last block in the associated file, false otherwise. |
| 125 | + * @throws IllegalArgumentException if blockNumber is invalid. |
| 126 | + */ |
| 127 | + public boolean isLastBlock(int blockNumber) { |
| 128 | + if (fileSize == 0) { |
| 129 | + return false; |
| 130 | + } |
| 131 | + |
| 132 | + throwIfInvalidBlockNumber(blockNumber); |
| 133 | + |
| 134 | + return blockNumber == (numBlocks - 1); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Gets the id of the block that contains the given absolute offset. |
| 139 | + * @param offset the absolute offset to check. |
| 140 | + * @return the id of the block that contains the given absolute offset. |
| 141 | + * @throws IllegalArgumentException if offset is invalid. |
| 142 | + */ |
| 143 | + public int getBlockNumber(long offset) { |
| 144 | + throwIfInvalidOffset(offset); |
| 145 | + |
| 146 | + return (int) (offset / blockSize); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Gets the size of the given block. |
| 151 | + * @param blockNumber the id of the desired block. |
| 152 | + * @return the size of the given block. |
| 153 | + */ |
| 154 | + public int getSize(int blockNumber) { |
| 155 | + if (fileSize == 0) { |
| 156 | + return 0; |
| 157 | + } |
| 158 | + |
| 159 | + if (isLastBlock(blockNumber)) { |
| 160 | + return (int) (fileSize - (((long) blockSize) * (numBlocks - 1))); |
| 161 | + } else { |
| 162 | + return blockSize; |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Indicates whether the given absolute offset is valid. |
| 168 | + * @param offset absolute offset in the file.. |
| 169 | + * @return true if the given absolute offset is valid, false otherwise. |
| 170 | + */ |
| 171 | + public boolean isValidOffset(long offset) { |
| 172 | + return (offset >= 0) && (offset < fileSize); |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Gets the start offset of the given block. |
| 177 | + * @param blockNumber the id of the given block. |
| 178 | + * @return the start offset of the given block. |
| 179 | + * @throws IllegalArgumentException if blockNumber is invalid. |
| 180 | + */ |
| 181 | + public long getStartOffset(int blockNumber) { |
| 182 | + throwIfInvalidBlockNumber(blockNumber); |
| 183 | + |
| 184 | + return blockNumber * (long) blockSize; |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Gets the relative offset corresponding to the given block and the absolute offset. |
| 189 | + * @param blockNumber the id of the given block. |
| 190 | + * @param offset absolute offset in the file. |
| 191 | + * @return the relative offset corresponding to the given block and the absolute offset. |
| 192 | + * @throws IllegalArgumentException if either blockNumber or offset is invalid. |
| 193 | + */ |
| 194 | + public int getRelativeOffset(int blockNumber, long offset) { |
| 195 | + throwIfInvalidOffset(offset); |
| 196 | + |
| 197 | + return (int) (offset - getStartOffset(blockNumber)); |
| 198 | + } |
| 199 | + |
| 200 | + /** |
| 201 | + * Gets the state of the given block. |
| 202 | + * @param blockNumber the id of the given block. |
| 203 | + * @return the state of the given block. |
| 204 | + * @throws IllegalArgumentException if blockNumber is invalid. |
| 205 | + */ |
| 206 | + public State getState(int blockNumber) { |
| 207 | + throwIfInvalidBlockNumber(blockNumber); |
| 208 | + |
| 209 | + return state[blockNumber]; |
| 210 | + } |
| 211 | + |
| 212 | + /** |
| 213 | + * Sets the state of the given block to the given value. |
| 214 | + * @param blockNumber the id of the given block. |
| 215 | + * @param blockState the target state. |
| 216 | + * @throws IllegalArgumentException if blockNumber is invalid. |
| 217 | + */ |
| 218 | + public void setState(int blockNumber, State blockState) { |
| 219 | + throwIfInvalidBlockNumber(blockNumber); |
| 220 | + |
| 221 | + state[blockNumber] = blockState; |
| 222 | + } |
| 223 | + |
| 224 | + // Debug helper. |
| 225 | + public String getStateString() { |
| 226 | + StringBuilder sb = new StringBuilder(); |
| 227 | + int blockNumber = 0; |
| 228 | + while (blockNumber < numBlocks) { |
| 229 | + State tstate = getState(blockNumber); |
| 230 | + int endBlockNumber = blockNumber; |
| 231 | + while ((endBlockNumber < numBlocks) && (getState(endBlockNumber) |
| 232 | + == tstate)) { |
| 233 | + endBlockNumber++; |
| 234 | + } |
| 235 | + sb.append( |
| 236 | + String.format("[%03d ~ %03d] %s%n", blockNumber, endBlockNumber - 1, |
| 237 | + tstate)); |
| 238 | + blockNumber = endBlockNumber; |
| 239 | + } |
| 240 | + return sb.toString(); |
| 241 | + } |
| 242 | + |
| 243 | + private void throwIfInvalidBlockNumber(int blockNumber) { |
| 244 | + checkWithinRange(blockNumber, "blockNumber", 0, numBlocks - 1); |
| 245 | + } |
| 246 | + |
| 247 | + private void throwIfInvalidOffset(long offset) { |
| 248 | + checkWithinRange(offset, "offset", 0, fileSize - 1); |
| 249 | + } |
| 250 | +} |
0 commit comments