-
Notifications
You must be signed in to change notification settings - Fork 0
/
AIFBS_CS.cpp
106 lines (92 loc) · 2.78 KB
/
AIFBS_CS.cpp
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
#ifndef AIFBS_CS_DEF
#define AIFBS_CS_DEF
#include "AIFBS_CS.hpp"
namespace AIFBS
{
void AIFBS_CS::insert(std::string t_name, int payload) {
SplittedNames sn = split(t_name);
AIFBS_FileNodeKey tempNode;
tempNode.setKey(sn.m_fileName);
AIFBS_FileNodeKey *fileNodeKey = fileTree->searchKeyRef(tempNode);
if(fileNodeKey == NULL) {
AIFBS_BTree<AIFBS_ChunkNodeKey> *chunkTree = new AIFBS_BTree<AIFBS_ChunkNodeKey>(m_chunkT);
AIFBS_FileNodeKey newFileNodeKey(sn.m_fileName, chunkTree);
fileTree->insert(newFileNodeKey);
fileNodeKey = &newFileNodeKey;
}
AIFBS_ChunkNodeKey *chunkNodeKey = new AIFBS_ChunkNodeKey();
chunkNodeKey->setKey(sn.m_chunkName);
fileNodeKey->m_chunkTree->insert(*chunkNodeKey);
}
AIFBS_ChunkNodeKey* AIFBS_CS::find(std::string t_name){
SplittedNames sn = split(t_name);
AIFBS_FileNodeKey tempFileKey;
tempFileKey.setKey(sn.m_fileName);
AIFBS_FileNodeKey *fileNodeKey = fileTree->searchKeyRef(tempFileKey);
if(fileNodeKey == NULL) {
//required file does not exists and therefore chunks does not exists
return NULL;
}
else {
AIFBS_ChunkNodeKey tempChunkKey;
tempChunkKey.setKey(sn.m_chunkName);
AIFBS_ChunkNodeKey* requiredChunk = fileNodeKey->m_chunkTree->searchKeyRef(tempChunkKey);
if(requiredChunk == NULL){
//required chunk not found
return NULL;
}
else{
return requiredChunk;
}
}
}
SplittedNames AIFBS_CS::split(std::string name) {
int index;
for(int i = (name.size()-1); i>=0;i--) {
if(name.at(i) == '/') {
index = i;
break;
}
}
SplittedNames SN;
SN.m_fileName = name.substr(0, index);
SN.m_chunkName = name.substr(index+1, name.size()-1);
return SN;
}
bool AIFBS_CS::remove(std::string chunkDetails) {
SplittedNames sn = this->split(chunkDetails);
AIFBS_FileNodeKey tempFileKey;
tempFileKey.setKey(sn.m_fileName);
AIFBS_FileNodeKey *fileNodeKey = fileTree->searchKeyRef(tempFileKey);
if (fileNodeKey == NULL) {
return false;
}
else {
AIFBS_ChunkNodeKey tempChunkKey;
tempChunkKey.setKey(sn.m_chunkName);
AIFBS_ChunkNodeKey* requiredChunk = fileNodeKey->m_chunkTree->searchKeyRef(tempChunkKey);
if (requiredChunk == NULL) {
return false;
}
else {
fileNodeKey->m_chunkTree->remove(*requiredChunk);
if (fileNodeKey->m_chunkTree->isEmpty()) {
fileTree->remove(*fileNodeKey);
}
}
}
}
bool AIFBS_CS::removeFile(std::string fileName) {
AIFBS_FileNodeKey tempFileKey;
tempFileKey.setKey(fileName);
AIFBS_FileNodeKey *fileNodeKey = fileTree->searchKeyRef(tempFileKey);
if (fileNodeKey == NULL) {
return false;
}
else {
fileNodeKey->m_chunkTree->deleteAll();
}
fileTree->remove(*fileNodeKey);
}
}
#endif