1/**
2 * Copyright 2021 Alibaba, Inc. and its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15
16 * \author Haichao.chc
17 * \date Oct 2020
18 * \brief Helper class for index file or directory operations
19 */
20
21#pragma once
22
23#include <stdint.h>
24#include <string>
25#include <ailego/io/file.h>
26#include <ailego/utility/file_helper.h>
27#include <ailego/utility/string_helper.h>
28
29namespace proxima {
30namespace be {
31namespace index {
32
33/*
34 * File type and id
35 */
36enum class FileID : uint32_t {
37 UNDEFINED = 0,
38 ID_FILE,
39 DELETE_FILE,
40 FORWARD_FILE,
41 PROXIMA_FILE,
42 SEGMENT_FILE,
43 LSN_FILE,
44 MANIFEST_FILE
45};
46
47/*
48 * File name coresponding to file id
49 */
50static const char *GetFileName(FileID t) {
51 switch (t) {
52 case FileID::ID_FILE:
53 return "data.id";
54 case FileID::DELETE_FILE:
55 return "data.del";
56 case FileID::FORWARD_FILE:
57 return "data.fwd";
58 case FileID::PROXIMA_FILE:
59 return "data.pxa";
60 case FileID::SEGMENT_FILE:
61 return "data.seg";
62 case FileID::LSN_FILE:
63 return "data.lsn";
64 case FileID::MANIFEST_FILE:
65 return "data.manifest";
66 default:
67 return "UnknownFile";
68 };
69}
70
71/*
72 * This helper class is mainly to wrapper filesystem operations.
73 */
74class FileHelper {
75 public:
76 //! Make file path with ${prefix_path}/${file_name}
77 static std::string MakeFilePath(const std::string &prefix_path,
78 FileID file_id) {
79 return ailego::StringHelper::Concat(prefix_path, "/", GetFileName(file_id));
80 }
81
82 //! Make file path with ${prefix_path}/${file_name}.${number}
83 static std::string MakeFilePath(const std::string &prefix_path,
84 FileID file_id, uint32_t number) {
85 return ailego::StringHelper::Concat(prefix_path, "/", GetFileName(file_id),
86 ".", number);
87 }
88
89 //! Make file path with ${prefix_path}/${file_name}.${suffix_name}.${number}
90 static std::string MakeFilePath(const std::string &prefix_path,
91 FileID file_id, uint32_t number,
92 const std::string &suffix_name) {
93 return ailego::StringHelper::Concat(prefix_path, "/", GetFileName(file_id),
94 ".", suffix_name, ".", number);
95 }
96
97 //! Create directory
98 static bool CreateDirectory(const std::string &dir_path) {
99 return ailego::File::MakePath(dir_path);
100 }
101
102 //! Remove directory
103 static bool RemoveDirectory(const std::string &dir_path) {
104 return ailego::File::RemoveDirectory(dir_path);
105 }
106
107 //! Remove file
108 static bool RemoveFile(const std::string &file_path) {
109 return ailego::File::Delete(file_path);
110 }
111
112 //! Check if file exists
113 static bool FileExists(const std::string &file_path) {
114 return ailego::File::IsExist(file_path);
115 }
116
117 //! Check if directory exists
118 static bool DirectoryExists(const std::string &dir_path) {
119 return ailego::File::IsExist(dir_path);
120 }
121
122 //! Return file size
123 static size_t FileSize(const std::string &file_path) {
124 return ailego::FileHelper::FileSize(file_path.c_str());
125 }
126};
127
128
129} // end namespace index
130} // namespace be
131} // end namespace proxima
132