-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathFileManager.hpp
148 lines (129 loc) · 4.84 KB
/
FileManager.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
**/
#ifndef QUICKSTEP_STORAGE_FILE_MANAGER_HPP_
#define QUICKSTEP_STORAGE_FILE_MANAGER_HPP_
#include <cstddef>
#include <string>
#include "storage/StorageBlockInfo.hpp"
#include "utility/Macros.hpp"
#include "utility/StringUtil.hpp"
namespace quickstep {
/** \addtogroup Storage
* @{
*/
/**
* @brief A class which manages file I/Os between memory and
* the persistent storage.
**/
class FileManager {
public:
/**
* @brief Constructor.
*
* @param storage_path the filesystem directory where blocks have persistent
* storage. It is expected to end with a path separator (e.g., '\\'
* for Windows or '/' for POSIX systems).
* @param block_domain The domain of a block id.
**/
explicit FileManager(const std::string &storage_path)
: storage_path_(storage_path) {}
/**
* @brief Virtual destructor.
**/
virtual ~FileManager() {}
/**
* @brief Get a block or blob's relative filename, which uses storage_path_
* as a path prefix.
*
* @param block The id of the block or blob.
* @return The relative filename for the given id.
**/
virtual std::string blockFilename(const block_id block) const {
std::string filepath(storage_path_);
filepath.append("qsblk_");
filepath.append(ToZeroPaddedString(BlockIdUtil::Domain(block), kBlockIdDomainLengthInDigits));
filepath.append("_");
filepath.append(ToZeroPaddedString(BlockIdUtil::Counter(block), kBlockIdCounterLengthInDigits));
filepath.append(".qsb");
return filepath;
}
/**
* @brief Get the highest block counter in the persistent storage.
* @exception CorruptPersistentStorage The storage directory layout is not
* in the expected format.
*
* @param domain The domain of a block or blob.
* @return The highest block-id in the persistent storage. Return 0 if no
* block files found for the current domain.
**/
virtual block_id_counter getMaxUsedBlockCounter(const block_id_domain block_domain) const = 0;
/**
* @brief Get the size of a block or blob in slots.
*
* @param block The id of the block or blob.
* @exception CorruptPersistentStorage The storage directory layout is not
* in the expected format.
*
* @return The number of slots for the given id (0 if it doesn't
* exist).
**/
virtual std::size_t numSlots(const block_id block) const = 0;
/**
* @brief Delete a block or blob's file.
*
* @param block The id of the block or blob to delete.
*
* @return False if the block exists but is failed to delete. True
* if the block is successfully deleted OR is not found.
**/
virtual bool deleteBlockOrBlob(const block_id block) = 0;
/**
* @brief Read a block or blob's file from the persistent storage.
*
* @param block The id of the block or blob to read.
* @param buffer The data contents of the block or blob to read.
* @param length The number of bytes to read. It should be multiple of
* kSlotSizeBytes.
*
* @return False if the block is not found in the persistent storage OR
* fails to load. True if the block is successfully loaded to memory.
**/
virtual bool readBlockOrBlob(const block_id block, void *buffer, const std::size_t length) = 0;
/**
* @brief Write a block or blob in memory to the persistent storage.
*
* @param block The id of the block or blob to write.
* @param buffer The data content of the block or blob to write.
* @param length The number of bytes to write. It should be multiple of
* kSlotSizeBytes.
*
* @return False if the block fails to write to the persistent storage. True
* if the block is written successfully.
**/
virtual bool writeBlockOrBlob(const block_id block, const void *buffer, const std::size_t length) = 0;
protected:
// File system path where block files are stored. Fixed when FileManager
// is created.
const std::string storage_path_;
private:
DISALLOW_COPY_AND_ASSIGN(FileManager);
};
/** @} */
} // namespace quickstep
#endif // QUICKSTEP_STORAGE_FILE_MANAGER_HPP_