-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage_manager.hpp
More file actions
127 lines (106 loc) · 3.79 KB
/
Copy pathstorage_manager.hpp
File metadata and controls
127 lines (106 loc) · 3.79 KB
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
/**
* @file storage_manager.hpp
* @brief Storage Manager for persistent database files
*/
#ifndef CLOUDSQL_STORAGE_STORAGE_MANAGER_HPP
#define CLOUDSQL_STORAGE_STORAGE_MANAGER_HPP
#include <atomic>
#include <fstream>
#include <memory>
#include <mutex>
#include <string>
#include <unordered_map>
#include <vector>
namespace cloudsql::storage {
/**
* @brief Manages low-level disk I/O and page-level access
* @note StorageManager is not thread-safe for concurrent file operations.
* The atomics in Stats are for aggregation of counters, not concurrent
* file access. Use external synchronization (e.g., a lock) if multiple
* threads will access the same StorageManager instance.
*/
class StorageManager {
public:
static constexpr uint32_t PAGE_SIZE = 4096;
static constexpr int DEFAULT_DIR_MODE = 0755;
struct Stats {
std::atomic<uint64_t> pages_read{0};
std::atomic<uint64_t> pages_written{0};
std::atomic<uint64_t> bytes_read{0};
std::atomic<uint64_t> bytes_written{0};
std::atomic<uint32_t> files_opened{0};
};
explicit StorageManager(std::string data_dir);
~StorageManager();
// Disable copy/move for storage manager (due to atomic stats)
StorageManager(const StorageManager&) = delete;
StorageManager& operator=(const StorageManager&) = delete;
StorageManager(StorageManager&&) = delete;
StorageManager& operator=(StorageManager&&) = delete;
/**
* @brief Get storage statistics
*/
[[nodiscard]] const Stats& get_stats() const { return stats_; }
/**
* @brief Open a database file
*/
bool open_file(const std::string& filename);
/**
* @brief Close a database file
*/
bool close_file(const std::string& filename);
/**
* @brief Read a page from disk into buffer
* @param filename Name of the database file
* @param page_num Page index
* @param buffer Pre-allocated buffer of at least PAGE_SIZE
* @return true on success
*/
bool read_page(const std::string& filename, uint32_t page_num, char* buffer);
/**
* @brief Write a page from buffer to disk
* @param filename Name of the database file
* @param page_num Page index
* @param buffer Buffer containing data to write
* @return true on success
*/
bool write_page(const std::string& filename, uint32_t page_num, const char* buffer);
/**
* @brief Allocate a new page in the database file
* @param filename Name of the database file
* @return index of the newly allocated page
*/
uint32_t allocate_page(const std::string& filename);
/**
* @brief Deallocate a page (stub for future use)
*/
static void deallocate_page(const std::string& filename, uint32_t page_num);
/**
* @brief Resolves the full filesystem path for a given filename within the storage directory.
* @param filename The relative name of the file.
* @return The absolute or relative path from the process root.
*/
[[nodiscard]] std::string get_full_path(const std::string& filename) const;
/**
* @brief Check if a file exists
*/
[[nodiscard]] bool file_exists(const std::string& filename) const;
/**
* @brief Create data directory if not exists
*/
bool create_dir_if_not_exists();
/**
* @brief Delete a file from storage
* @param filename The relative name of the file
* @return true if deleted, false otherwise
*/
bool delete_file(const std::string& filename);
private:
std::string data_dir_;
std::unordered_map<std::string, std::fstream*> open_files_;
std::unordered_map<std::string, std::streamoff> file_sizes_;
std::mutex file_sizes_mutex_;
Stats stats_;
};
} // namespace cloudsql::storage
#endif // CLOUDSQL_STORAGE_STORAGE_MANAGER_HPP