-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKVFSDirectory.h
172 lines (150 loc) · 4.97 KB
/
KVFSDirectory.h
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* KVFSDirectory.h
*
* Created on: Oct 15, 2015
* Author: Prasanna Ponnada
*/
#ifndef KV_FS_DIRECTORY
#define KV_FS_DIRECTORY
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <boost/serialization/vector.hpp>
#include "KVFSMetaData.h"
#include "KVFSObject.h"
class KVFSFile;
class KVFSDirectory;
struct KVFSDirectoryEntry {
KVFSDirectoryEntry():directoryName(""), directoryEntries() {}
KVFSDirectoryEntry(std::string directoryName):directoryName(directoryName),
directoryEntries() {}
std::string directoryName;
std::vector<std::string> directoryEntries;
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive& ar, const unsigned int version) {
ar& directoryName;
ar& directoryEntries;
}
};
class KVFSDirectory : public KVFSObject {
public:
KVFSDirectory():KVFSObject(-1, "", "", KVFS_DIRECTORY),
meta(KVFSMetaData()),
handle(0) {}
KVFSDirectory(uint64_t fsId, const std::string& parent_dir_name,
const std::string& dir_name, KVFSMetaData metadata,
uint64_t handle) :
KVFSObject(fsId, parent_dir_name, dir_name, KVFS_DIRECTORY),
meta(metadata),
handle(handle) {}
~KVFSDirectory() {};
virtual bool exists(KVStore* store, void* fsHandle) {
std::string absolutePath = (parent == name) ? parent : (parent + name);
char dirKey[512];
snprintf(dirKey, 512, "%lu",
CityHash64(absolutePath.c_str(), absolutePath.length()));
std::string value;
if (store->get(fsHandle, dirKey, value) == true)
return true;
else
return false;
}
virtual bool create(KVStore* store, void* fsHandle) {
std::stringstream ss;
{
boost::archive::text_oarchive oa(ss);
oa << *this;
}
std::string absolutePath = (parent == name) ? parent : (parent + name);
char dirKey[512];
snprintf(dirKey, 512, "%lu",
CityHash64(absolutePath.c_str(), absolutePath.length()));
if (store->put(fsHandle, dirKey, ss.str()) == true) {
std::string parentDirEntryKeyStr = parent + ".entry";
char parentDirEntryKey[512];
snprintf(parentDirEntryKey, 512, "%lu",
CityHash64(parentDirEntryKeyStr.c_str(),
parentDirEntryKeyStr.length()));
std::string dirEntriesStr;
if (store->get(fsHandle, parentDirEntryKey, dirEntriesStr) == true)
{
std::stringstream dirEntriesReadStream;
dirEntriesReadStream << dirEntriesStr;
KVFSDirectoryEntry kvfsDirectoryEntry;
boost::archive::text_iarchive ia(dirEntriesReadStream);
ia >> kvfsDirectoryEntry;
kvfsDirectoryEntry.directoryEntries.push_back(name);
std::stringstream dirEntriesWriteStream;
boost::archive::text_oarchive oa(dirEntriesWriteStream);
oa << kvfsDirectoryEntry;
if (store->put(fsHandle, parentDirEntryKey,
dirEntriesWriteStream.str()) != true) {
std::cerr << "Couldn't persist directory entries for: " <<
parent << std::endl;
return false;
}
} else {
KVFSDirectoryEntry kvfsDirectoryEntry(parent);
std::stringstream dirEntriesWriteStream;
boost::archive::text_oarchive oa(dirEntriesWriteStream);
oa << kvfsDirectoryEntry;
if (store->put(fsHandle, parentDirEntryKey,
dirEntriesWriteStream.str()) != true) {
std::cerr << "Couldn't persist empty directory entries "
"for: " << parent << std::endl;
return false;
}
}
return true;
}
else
return false;
}
virtual void listDirectory(KVFSDirectoryEntry* entries,
uint64_t* entryCount,
KVStore* store,
void* fsHandle) {
std::string absolutePath = (parent == name) ? parent : (parent + name);
char dirKey[512];
std::string absolutePathEntry = absolutePath + ".entry";
snprintf(dirKey, 512, "%lu",
CityHash64(absolutePathEntry.c_str(),
absolutePathEntry.length()));
std::string dirEntriesStr;
if (store->get(fsHandle, dirKey, dirEntriesStr) == true)
{
std::stringstream dirEntriesReadStream;
dirEntriesReadStream << dirEntriesStr;
KVFSDirectoryEntry kvfsDirectoryEntry;
boost::archive::text_iarchive ia(dirEntriesReadStream);
ia >> kvfsDirectoryEntry;
*entryCount = kvfsDirectoryEntry.directoryEntries.size();
if (entryCount > 0) {
for ( std::string entry : kvfsDirectoryEntry.directoryEntries)
entries->directoryEntries.push_back(entry);
}
}
}
virtual bool remove(KVStore* store, void* fsHandle) {
return true;
}
virtual bool addFile(uint64_t fsId, std::string parent_dir_name,
std::string file_name, KVFSMetaData metadata) {
return true;
}
inline KVFSMetaData getLogFSMetaData() { return meta; }
inline uint64_t getHandle() { return handle; }
private:
struct KVFSMetaData meta;
uint64_t handle;
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive& ar, const unsigned int version) {
ar& boost::serialization::base_object<KVFSObject>(*this);
ar& meta;
ar& handle;
}
};
#endif /* KV_FS_DIRECTORY */