-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.hpp
More file actions
151 lines (143 loc) · 4.91 KB
/
file.hpp
File metadata and controls
151 lines (143 loc) · 4.91 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#ifndef CUSTOM_FILE_HPP
#define CUSTOM_FILE_HPP
#include <string>
#include <vector>
#include <ctime>
#include <iostream>
#include "tree.hpp"
#include "hashmap.hpp"
// File object..holds version tree..and a map from id->node
// simple methods .. create, read, insert, update, snapshot, rollback, history
struct File {
TreeNode* root;
TreeNode* active_version;
HashMap<int, TreeNode*> version_map; // custom hash map
int total_versions;
std::time_t last_modified; // for analytics ..
File() : root(nullptr), active_version(nullptr), version_map(), total_versions(0), last_modified(0) {}
static std::time_t now() {
return std::time(nullptr);
}
void init_empty() {
std::time_t t = now();
root = new TreeNode(0, "", "Initial root", t);
root->snapshot_timestamp = t;
active_version = root;
version_map.put(0, root);
total_versions = 1;
last_modified = t;
}
void touch() {
last_modified = now();
}
void read() const {
if (!active_version) {
std::cout << "ERROR: No active version\n";
return;
}
std::cout << active_version->content << "\n";
}
TreeNode* fork_from_active(const std::string& new_content) {
int new_id = total_versions;
TreeNode* child = new TreeNode(new_id, new_content, "", now());
child->parent = active_version;
active_version->children.push_back(child);
version_map.put(new_id, child);
total_versions++;
active_version = child;
touch();
return child;
}
void insert(const std::string& extra) {
if (!active_version) { std::cout << "ERROR: File not initialized\n"; return; }
if (active_version->snapshot_timestamp != 0) {
std::string combined = active_version->content + extra;
fork_from_active(combined);
} else {
active_version->content += extra;
active_version->created_timestamp = now();
touch();
}
}
void update(const std::string& new_content) {
if (!active_version) { std::cout << "ERROR: File not initialized\n"; return; }
if (active_version->snapshot_timestamp != 0) {
fork_from_active(new_content);
} else {
active_version->content = new_content;
active_version->created_timestamp = now();
touch();
}
}
void snapshot(const std::string& msg) {
if (!active_version) { std::cout << "ERROR: File not initialized\n"; return; }
if (active_version->snapshot_timestamp != 0) {
std::cout << "WARN: Active version is already a snapshot\n";
return;
}
active_version->message = msg;
active_version->snapshot_timestamp = now();
touch();
}
void rollback_to(int version_id, bool has_id) {
if (!active_version) { std::cout << "ERROR: File not initialized\n"; return; }
if (has_id) {
TreeNode** p = version_map.getPtr(version_id);
if (!p || !(*p)) {
std::cout << "ERROR: Version " << version_id << " not found\n";
return;
}
active_version = *p;
touch();
} else {
if (!active_version->parent) {
std::cout << "ERROR: Already at root; cannot rollback further\n";
return;
}
active_version = active_version->parent;
touch();
}
}
void history() const {
std::vector<int> ids;
version_map.keys(ids);
struct Pair { int id; std::time_t ts; };
std::vector<Pair> snaps;
for (int id : ids) {
const TreeNode* const* p = version_map.getPtr(id);
if (!p || !(*p)) continue;
const TreeNode* node = *p;
if (node->snapshot_timestamp != 0) {
snaps.push_back({id, node->snapshot_timestamp});
}
}
for (size_t i = 1; i < snaps.size(); ++i) {
Pair key = snaps[i];
size_t j = i;
while (j > 0 && snaps[j-1].ts > key.ts) {
snaps[j] = snaps[j-1];
--j;
}
snaps[j] = key;
}
for (const auto& p : snaps) {
const TreeNode* const* nodePtr = version_map.getPtr(p.id);
const TreeNode* node = nodePtr ? *nodePtr : nullptr;
if (!node) continue;
std::cout << node->version_id << " " << (long long)node->snapshot_timestamp << " " << node->message << "\n";
}
}
~File() {
if (!root) return;
std::vector<TreeNode*> stack;
stack.push_back(root);
while (!stack.empty()) {
TreeNode* cur = stack.back(); stack.pop_back();
for (TreeNode* ch : cur->children) {
if (ch) stack.push_back(ch);
}
delete cur;
}
}
};
#endif