-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilesystem.hpp
More file actions
132 lines (116 loc) · 3.62 KB
/
filesystem.hpp
File metadata and controls
132 lines (116 loc) · 3.62 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
128
129
130
131
132
#ifndef CUSTOM_FILESYSTEM_HPP
#define CUSTOM_FILESYSTEM_HPP
#include <string>
#include <vector>
#include <iostream>
#include <cctype>
#include "hashmap.hpp"
#include "heap.hpp"
#include "file.hpp"
// FileSystem.. holds filename -> File* map
// analytics functions use our BinaryMaxHeap
// simple and to the point
struct FileSystem {
HashMap<std::string, File*> files;
FileSystem() : files() {}
~FileSystem() {
std::vector<std::string> names;
files.keys(names);
for (const auto& name : names) {
File** pf = files.getPtr(name);
if (pf && *pf) delete *pf;
}
}
bool create(const std::string& name) {
if (files.contains(name)) {
std::cout << "ERROR: File already exists\n";
return false;
}
File* f = new File();
f->init_empty();
files.put(name, f);
return true;
}
File* get(const std::string& name) {
File** p = files.getPtr(name);
if (!p || !(*p)) {
std::cout << "ERROR: File not found\n";
return nullptr;
}
return *p;
}
void read(const std::string& name) {
File* f = get(name);
if (!f) return;
f->read();
}
void insert(const std::string& name, const std::string& content) {
File* f = get(name);
if (!f) return;
f->insert(content);
}
void update(const std::string& name, const std::string& content) {
File* f = get(name);
if (!f) return;
f->update(content);
}
void snapshot(const std::string& name, const std::string& msg) {
File* f = get(name);
if (!f) return;
f->snapshot(msg);
}
void rollback(const std::string& name, bool has_id, int version_id) {
File* f = get(name);
if (!f) return;
f->rollback_to(version_id, has_id);
}
void history(const std::string& name) {
File* f = get(name);
if (!f) return;
f->history();
}
void recent_files(int limit = -1) {
struct Item { long long key; std::string name; };
struct KeyEx { long long operator()(const Item& it) const { return it.key; } };
BinaryMaxHeap<Item, KeyEx> heap;
std::vector<std::string> names;
files.keys(names);
for (const auto& nm : names) {
File** pf = files.getPtr(nm);
if (!pf || !(*pf)) continue;
heap.push(Item{ (long long)(*pf)->last_modified, nm });
}
int count = 0;
while (!heap.empty()) {
auto it = heap.top(); heap.pop();
std::cout << it.name << " " << it.key << "\n";
++count;
if (limit > 0 && count >= limit) break;
}
}
void biggest_trees(int limit = -1) {
struct Item { long long key; std::string name; };
struct KeyEx { long long operator()(const Item& it) const { return it.key; } };
BinaryMaxHeap<Item, KeyEx> heap;
std::vector<std::string> names;
files.keys(names);
for (const auto& nm : names) {
File** pf = files.getPtr(nm);
if (!pf || !(*pf)) continue;
heap.push(Item{ (long long)(*pf)->total_versions, nm });
}
int count = 0;
while (!heap.empty()) {
auto it = heap.top(); heap.pop();
std::cout << it.name << " " << it.key << "\n";
++count;
if (limit > 0 && count >= limit) break;
}
}
int get_active_version_id(const std::string& name) {
File* f = get(name);
if (!f || !f->active_version) return -1;
return f->active_version->version_id;
}
};
#endif