forked from fangpin/miniDFS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataserver.cpp
99 lines (87 loc) · 2.47 KB
/
dataserver.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
#include <thread>
#include <sys/stat.h>
#include <dirent.h>
#include <fstream>
#include <iostream>
#include <algorithm>
#include "dataserver.h"
int chunkSize = 2 * 1024 * 1024;
DataServer::DataServer(const std::string &name):name_(name), buf(nullptr), finish(true){
std::string cmd = "mkdir -p " + name_;
system(cmd.c_str());
}
void DataServer::operator()(){
while(true){
std::unique_lock<std::mutex> lk(mtx);
cv.wait(lk, [&](){return !this->finish;});
if (cmd == "put"){
size_ += bufSize / 1024.0 / 1024.0;
put();
}
else if(cmd == "read")
read();
else if(cmd == "locate")
locate();
else if(cmd == "fetch")
fetch();
this->finish = true;
lk.unlock();
cv.notify_all();
}
}
void DataServer::put(){
int start = 0;
std::ofstream os;
while(start < bufSize ){
int offset = start / chunkSize;
std::string filePath = name_ + "/" + std::to_string(fid) + " " + std::to_string(offset);
os.open(filePath);
if(!os)
std::cerr << "create file error in dataserver: (file name) " << filePath << std::endl;
os.write(&buf[start], std::min(chunkSize, bufSize - start));
start += chunkSize;
os.close();
}
}
void DataServer::read(){
int start = 0;
buf = new char[bufSize];
while(start < bufSize){
int offset = start / chunkSize;
std::string filePath = name_ + "/" + std::to_string(fid) + " " + std::to_string(offset);
std::ifstream is(filePath);
// file found not in this server.
if(!is){
delete []buf;
bufSize = 0;
break;
}
is.read(&buf[start], std::min(chunkSize, bufSize - start));
start += chunkSize;
}
}
void DataServer::fetch(){
buf = new char[chunkSize];
std::string filePath = name_ + "/" + std::to_string(fid) + " " + std::to_string(offset);
std::ifstream is(filePath);
// file found not in this server.
if(!is){
delete []buf;
bufSize = 0;
}
else{
is.read(buf, std::min(chunkSize, bufSize - chunkSize * offset));
bufSize = is.tellg();
}
}
void DataServer::locate(){
std::string filePath = name_ + "/" + std::to_string(fid) + " " + std::to_string(offset);
std::ifstream is(filePath);
if(is)
bufSize = 1;
else
bufSize = 0;
}
std::string DataServer::get_name()const{
return name_;
}