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.server.namenode; |
---|
19 | |
---|
20 | import java.util.Arrays; |
---|
21 | |
---|
22 | /** |
---|
23 | * File name generator. |
---|
24 | * |
---|
25 | * Each directory contains not more than a fixed number (filesPerDir) |
---|
26 | * of files and directories. |
---|
27 | * When the number of files in one directory reaches the maximum, |
---|
28 | * the generator creates a new directory and proceeds generating files in it. |
---|
29 | * The generated namespace tree is balanced that is any path to a leaf |
---|
30 | * file is not less than the height of the tree minus one. |
---|
31 | */ |
---|
32 | public class FileNameGenerator { |
---|
33 | private static final int DEFAULT_FILES_PER_DIRECTORY = 32; |
---|
34 | |
---|
35 | private int[] pathIndecies = new int[20]; // this will support up to 32**20 = 2**100 = 10**30 files |
---|
36 | private String baseDir; |
---|
37 | private String currentDir; |
---|
38 | private int filesPerDirectory; |
---|
39 | private long fileCount; |
---|
40 | |
---|
41 | FileNameGenerator(String baseDir) { |
---|
42 | this(baseDir, DEFAULT_FILES_PER_DIRECTORY); |
---|
43 | } |
---|
44 | |
---|
45 | FileNameGenerator(String baseDir, int filesPerDir) { |
---|
46 | this.baseDir = baseDir; |
---|
47 | this.filesPerDirectory = filesPerDir; |
---|
48 | reset(); |
---|
49 | } |
---|
50 | |
---|
51 | String getNextDirName(String prefix) { |
---|
52 | int depth = 0; |
---|
53 | while(pathIndecies[depth] >= 0) |
---|
54 | depth++; |
---|
55 | int level; |
---|
56 | for(level = depth-1; |
---|
57 | level >= 0 && pathIndecies[level] == filesPerDirectory-1; level--) |
---|
58 | pathIndecies[level] = 0; |
---|
59 | if(level < 0) |
---|
60 | pathIndecies[depth] = 0; |
---|
61 | else |
---|
62 | pathIndecies[level]++; |
---|
63 | level = 0; |
---|
64 | String next = baseDir; |
---|
65 | while(pathIndecies[level] >= 0) |
---|
66 | next = next + "/" + prefix + pathIndecies[level++]; |
---|
67 | return next; |
---|
68 | } |
---|
69 | |
---|
70 | synchronized String getNextFileName(String fileNamePrefix) { |
---|
71 | long fNum = fileCount % filesPerDirectory; |
---|
72 | if(fNum == 0) { |
---|
73 | currentDir = getNextDirName(fileNamePrefix + "Dir"); |
---|
74 | } |
---|
75 | String fn = currentDir + "/" + fileNamePrefix + fileCount; |
---|
76 | fileCount++; |
---|
77 | return fn; |
---|
78 | } |
---|
79 | |
---|
80 | private synchronized void reset() { |
---|
81 | Arrays.fill(pathIndecies, -1); |
---|
82 | fileCount = 0L; |
---|
83 | currentDir = ""; |
---|
84 | } |
---|
85 | |
---|
86 | synchronized int getFilesPerDirectory() { |
---|
87 | return filesPerDirectory; |
---|
88 | } |
---|
89 | |
---|
90 | synchronized String getCurrentDir() { |
---|
91 | return currentDir; |
---|
92 | } |
---|
93 | } |
---|