| 1 |
|
| // Copyright (c) 2011 The LevelDB Authors. All rights reserved. |
| 2 |
|
| // Use of this source code is governed by a BSD-style license that can be |
| 3 |
|
| // found in the LICENSE file. See the AUTHORS file for names of contributors. |
| 4 |
|
| |
| 5 |
|
| #include <ctype.h> |
| 6 |
|
| #include <stdio.h> |
| 7 |
|
| #include "db/filename.h" |
| 8 |
|
| #include "db/dbformat.h" |
| 9 |
|
| #include "leveldb/env.h" |
| 10 |
|
| #include "util/logging.h" |
| 11 |
|
| |
| 12 |
|
| namespace leveldb { |
| 13 |
|
| |
| 14 |
11 |
| static std::string MakeFileName(const std::string& name, uint64_t number, |
| 15 |
|
| const char* suffix) { |
| 16 |
|
| char buf[100]; |
| 17 |
11 |
| snprintf(buf, sizeof(buf), "/%06llu.%s", |
| 18 |
|
| static_cast<unsigned long long>(number), |
| 19 |
|
| suffix); |
| 20 |
22 |
▶ | return name + buf; |
Branch: 1 branch
| Branch | True | False | Total |
| return name + buf; |
11 |
0 |
11 |
|
| 21 |
|
| } |
| 22 |
|
| |
| 23 |
4 |
| std::string LogFileName(const std::string& name, uint64_t number) { |
| 24 |
4 |
▶ | assert(number > 0); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(number > 0); |
0 |
4 |
4 |
|
| 25 |
4 |
| return MakeFileName(name, number, "log"); |
| 26 |
|
| } |
| 27 |
|
| |
| 28 |
2 |
| std::string TableFileName(const std::string& name, uint64_t number) { |
| 29 |
2 |
▶ | assert(number > 0); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(number > 0); |
0 |
2 |
2 |
|
| 30 |
2 |
| return MakeFileName(name, number, "sst"); |
| 31 |
|
| } |
| 32 |
|
| |
| 33 |
10 |
| std::string DescriptorFileName(const std::string& dbname, uint64_t number) { |
| 34 |
10 |
▶ | assert(number > 0); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(number > 0); |
0 |
10 |
10 |
|
| 35 |
|
| char buf[100]; |
| 36 |
10 |
| snprintf(buf, sizeof(buf), "/MANIFEST-%06llu", |
| 37 |
|
| static_cast<unsigned long long>(number)); |
| 38 |
20 |
▶ | return dbname + buf; |
Branch: 1 branch
| Branch | True | False | Total |
| return dbname + buf; |
10 |
0 |
10 |
|
| 39 |
|
| } |
| 40 |
|
| |
| 41 |
11 |
| std::string CurrentFileName(const std::string& dbname) { |
| 42 |
11 |
| return dbname + "/CURRENT"; |
| 43 |
|
| } |
| 44 |
|
| |
| 45 |
4 |
| std::string LockFileName(const std::string& dbname) { |
| 46 |
4 |
| return dbname + "/LOCK"; |
| 47 |
|
| } |
| 48 |
|
| |
| 49 |
5 |
| std::string TempFileName(const std::string& dbname, uint64_t number) { |
| 50 |
5 |
▶ | assert(number > 0); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(number > 0); |
0 |
5 |
5 |
|
| 51 |
5 |
| return MakeFileName(dbname, number, "dbtmp"); |
| 52 |
|
| } |
| 53 |
|
| |
| 54 |
8 |
| std::string InfoLogFileName(const std::string& dbname) { |
| 55 |
8 |
| return dbname + "/LOG"; |
| 56 |
|
| } |
| 57 |
|
| |
| 58 |
|
| // Return the name of the old info log file for "dbname". |
| 59 |
4 |
| std::string OldInfoLogFileName(const std::string& dbname) { |
| 60 |
4 |
| return dbname + "/LOG.old"; |
| 61 |
|
| } |
| 62 |
|
| |
| 63 |
|
| |
| 64 |
|
| // Owned filenames have the form: |
| 65 |
|
| // dbname/CURRENT |
| 66 |
|
| // dbname/LOCK |
| 67 |
|
| // dbname/LOG |
| 68 |
|
| // dbname/LOG.old |
| 69 |
|
| // dbname/MANIFEST-[0-9]+ |
| 70 |
|
| // dbname/[0-9]+.(log|sst) |
| 71 |
47 |
| bool ParseFileName(const std::string& fname, |
| 72 |
|
| uint64_t* number, |
| 73 |
|
| FileType* type) { |
| 74 |
47 |
| Slice rest(fname); |
| 75 |
47 |
▶ | if (rest == "CURRENT") { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rest == "CURRENT") { |
6 |
41 |
47 |
|
| 76 |
6 |
| *number = 0; |
| 77 |
6 |
| *type = kCurrentFile; |
| 78 |
41 |
▶ | } else if (rest == "LOCK") { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (rest == "LOCK") { |
6 |
35 |
41 |
|
| 79 |
6 |
| *number = 0; |
| 80 |
6 |
| *type = kDBLockFile; |
| 81 |
35 |
▶ | } else if (rest == "LOG" || rest == "LOG.old") { |
Branch: 3 branches
| Branch | True | False | Total |
| } else if (rest == "LOG" || rest == "LOG.old") { |
29 |
6 |
35 |
| } else if (rest == "LOG" || rest == "LOG.old") { |
2 |
27 |
29 |
| } else if (rest == "LOG" || rest == "LOG.old") { |
8 |
27 |
35 |
|
| 82 |
8 |
| *number = 0; |
| 83 |
8 |
| *type = kInfoLogFile; |
| 84 |
27 |
▶ | } else if (rest.starts_with("MANIFEST-")) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (rest.starts_with("MANIFEST-")) { |
9 |
18 |
27 |
|
| 85 |
9 |
| rest.remove_prefix(strlen("MANIFEST-")); |
| 86 |
|
| uint64_t num; |
| 87 |
9 |
▶ | if (!ConsumeDecimalNumber(&rest, &num)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!ConsumeDecimalNumber(&rest, &num)) { |
9 |
0 |
9 |
| if (!ConsumeDecimalNumber(&rest, &num)) { |
0 |
9 |
9 |
|
| 88 |
0 |
| return false; |
| 89 |
|
| } |
| 90 |
9 |
▶ | if (!rest.empty()) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!rest.empty()) { |
0 |
9 |
9 |
|
| 91 |
0 |
| return false; |
| 92 |
|
| } |
| 93 |
9 |
| *type = kDescriptorFile; |
| 94 |
9 |
| *number = num; |
| 95 |
|
| } else { |
| 96 |
|
| // Avoid strtoull() to keep filename format independent of the |
| 97 |
|
| // current locale |
| 98 |
|
| uint64_t num; |
| 99 |
18 |
▶ | if (!ConsumeDecimalNumber(&rest, &num)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!ConsumeDecimalNumber(&rest, &num)) { |
18 |
0 |
18 |
| if (!ConsumeDecimalNumber(&rest, &num)) { |
12 |
6 |
18 |
|
| 100 |
12 |
| return false; |
| 101 |
|
| } |
| 102 |
6 |
| Slice suffix = rest; |
| 103 |
6 |
▶ | if (suffix == Slice(".log")) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (suffix == Slice(".log")) { |
5 |
1 |
6 |
|
| 104 |
5 |
| *type = kLogFile; |
| 105 |
1 |
▶ | } else if (suffix == Slice(".sst")) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (suffix == Slice(".sst")) { |
1 |
0 |
1 |
|
| 106 |
1 |
| *type = kTableFile; |
| 107 |
0 |
▶ | } else if (suffix == Slice(".dbtmp")) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (suffix == Slice(".dbtmp")) { |
0 |
0 |
0 |
|
| 108 |
0 |
| *type = kTempFile; |
| 109 |
|
| } else { |
| 110 |
0 |
| return false; |
| 111 |
|
| } |
| 112 |
6 |
| *number = num; |
| 113 |
|
| } |
| 114 |
35 |
| return true; |
| 115 |
|
| } |
| 116 |
|
| |
| 117 |
5 |
| Status SetCurrentFile(Env* env, const std::string& dbname, |
| 118 |
|
| uint64_t descriptor_number) { |
| 119 |
|
| // Remove leading "dbname/" and add newline to manifest file name |
| 120 |
5 |
▶ | std::string manifest = DescriptorFileName(dbname, descriptor_number); |
Branch: 1 branch
| Branch | True | False | Total |
| std::string manifest = DescriptorFileName(dbname, descriptor_number); |
5 |
0 |
5 |
|
| 121 |
5 |
| Slice contents = manifest; |
| 122 |
5 |
▶ | assert(contents.starts_with(dbname + "/")); |
Branch: 2 branches
| Branch | True | False | Total |
| assert(contents.starts_with(dbname + "/")); |
5 |
0 |
5 |
| assert(contents.starts_with(dbname + "/")); |
0 |
5 |
5 |
|
| 123 |
5 |
| contents.remove_prefix(dbname.size() + 1); |
| 124 |
5 |
▶ | std::string tmp = TempFileName(dbname, descriptor_number); |
Branch: 1 branch
| Branch | True | False | Total |
| std::string tmp = TempFileName(dbname, descriptor_number); |
5 |
0 |
5 |
|
| 125 |
5 |
▶ | Status s = WriteStringToFile(env, contents.ToString() + "\n", tmp); |
Branch: 3 branches
| Branch | True | False | Total |
| Status s = WriteStringToFile(env, contents.ToString() + "\n", tmp); |
5 |
0 |
5 |
| Status s = WriteStringToFile(env, contents.ToString() + "\n", tmp); |
5 |
0 |
5 |
| Status s = WriteStringToFile(env, contents.ToString() + "\n", tmp); |
5 |
0 |
5 |
|
| 126 |
5 |
▶ | if (s.ok()) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s.ok()) { |
5 |
0 |
5 |
|
| 127 |
5 |
▶ | s = env->RenameFile(tmp, CurrentFileName(dbname)); |
Branch: 3 branches
| Branch | True | False | Total |
| s = env->RenameFile(tmp, CurrentFileName(dbname)); |
5 |
0 |
5 |
| s = env->RenameFile(tmp, CurrentFileName(dbname)); |
5 |
0 |
5 |
| s = env->RenameFile(tmp, CurrentFileName(dbname)); |
5 |
0 |
5 |
|
| 128 |
|
| } |
| 129 |
5 |
▶ | if (!s.ok()) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s.ok()) { |
0 |
5 |
5 |
|
| 130 |
0 |
▶ | env->DeleteFile(tmp); |
Branch: 1 branch
| Branch | True | False | Total |
| env->DeleteFile(tmp); |
0 |
0 |
0 |
|
| 131 |
|
| } |
| 132 |
10 |
| return s; |
| 133 |
5 |
| } |
| 134 |
|
| |
| 135 |
|
| } |