|
| 1 | +/** |
| 2 | + * |
| 3 | + * Copyright (c) 2011 Gluster, Inc. <http://www.gluster.com> |
| 4 | + * This file is part of GlusterFS. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 |
| 7 | + * (the "License"); you may not use this file except in compliance with |
| 8 | + * 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 |
| 15 | + * implied. See the License for the specific language governing |
| 16 | + * permissions and limitations under the License. |
| 17 | + * |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.hadoop.fs.glusterfs; |
| 21 | + |
| 22 | +import java.io.*; |
| 23 | + |
| 24 | +public class GlusterFSBrickClass { |
| 25 | + String host; |
| 26 | + String exportedFile; |
| 27 | + long start; |
| 28 | + long end; |
| 29 | + boolean isChunked; |
| 30 | + int stripeSize; // Stripe size in bytes |
| 31 | + int nrStripes; // number of stripes |
| 32 | + int switchCount; // for SR, DSR - number of replicas of each stripe |
| 33 | + // -1 for others |
| 34 | + |
| 35 | + public GlusterFSBrickClass (String brick, long start, long len, boolean flag, |
| 36 | + int stripeSize, int nrStripes, int switchCount) |
| 37 | + throws IOException { |
| 38 | + this.host = brick2host(brick); |
| 39 | + this.exportedFile = brick2file(brick); |
| 40 | + this.start = start; |
| 41 | + this.end = start + len; |
| 42 | + this.isChunked = flag; |
| 43 | + this.stripeSize = stripeSize; |
| 44 | + this.nrStripes = nrStripes; |
| 45 | + this.switchCount = switchCount; |
| 46 | + } |
| 47 | + |
| 48 | + public boolean isChunked () { |
| 49 | + return isChunked; |
| 50 | + } |
| 51 | + |
| 52 | + public String brickIsLocal(String hostname) { |
| 53 | + String path = null; |
| 54 | + File f = null; |
| 55 | + if (host.equals(hostname)) |
| 56 | + path = exportedFile; |
| 57 | + |
| 58 | + return path; |
| 59 | + } |
| 60 | + |
| 61 | + public int[] getBrickNumberInTree(long start, int len) { |
| 62 | + long end = len; |
| 63 | + int startNodeInTree = ((int) (start / stripeSize)) % nrStripes; |
| 64 | + int endNodeInTree = ((int) ((start + len) / stripeSize)) % nrStripes; |
| 65 | + |
| 66 | + if (startNodeInTree != endNodeInTree) { |
| 67 | + end = (start - (start % stripeSize)) + stripeSize; |
| 68 | + end -= start; |
| 69 | + } |
| 70 | + |
| 71 | + return new int[] {startNodeInTree, endNodeInTree, (int) end}; |
| 72 | + } |
| 73 | + |
| 74 | + public boolean brickHasFilePart(int nodeInTree, int nodeLoc) { |
| 75 | + if (switchCount == -1) |
| 76 | + return (nodeInTree == nodeLoc); |
| 77 | + |
| 78 | + nodeInTree *= switchCount; |
| 79 | + for (int i = nodeInTree; i < (nodeInTree + switchCount); i++) { |
| 80 | + if (i == nodeLoc) |
| 81 | + return true; |
| 82 | + } |
| 83 | + |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + public String brick2host (String brick) |
| 88 | + throws IOException { |
| 89 | + String[] hf = null; |
| 90 | + |
| 91 | + hf = brick.split(":"); |
| 92 | + if (hf.length != 2) |
| 93 | + throw new IOException("Error getting hostname from brick"); |
| 94 | + |
| 95 | + return hf[0]; |
| 96 | + } |
| 97 | + |
| 98 | + public String brick2file (String brick) |
| 99 | + throws IOException { |
| 100 | + String[] hf = null; |
| 101 | + |
| 102 | + hf = brick.split(":"); |
| 103 | + if (hf.length != 2) |
| 104 | + throw new IOException("Error getting hostname from brick"); |
| 105 | + |
| 106 | + return hf[1]; |
| 107 | + } |
| 108 | + |
| 109 | +} |
0 commit comments