Commit 36a3cde3 authored by mayankkakad's avatar mayankkakad

final. running perfectlygit add Backend.h LFU.h LRU.h server.cpp

parent afb42e10
...@@ -26,7 +26,7 @@ public: ...@@ -26,7 +26,7 @@ public:
{ {
return; return;
} }
virtual string getKeyValuePairs(int new_id,int id) virtual string getKeyValuePairs(int new_id,int id,int pred_id)
{ {
return "This will never run"; return "This will never run";
} }
...@@ -75,9 +75,9 @@ public: ...@@ -75,9 +75,9 @@ public:
mycache.pushAll(); mycache.pushAll();
} }
string getKeyValuePairs(int new_id,int id) string getKeyValuePairs(int new_id,int id,int pred_id)
{ {
return mycache.getKeyValuePairs(new_id,id); return mycache.getKeyValuePairs(new_id,id,pred_id);
} }
}; };
...@@ -124,8 +124,8 @@ public: ...@@ -124,8 +124,8 @@ public:
} }
// key1;key2;key3;;value1;value2;value3; // key1;key2;key3;;value1;value2;value3;
string getKeyValuePairs(int new_id,int id) string getKeyValuePairs(int new_id,int id,int pred_id)
{ {
return mycache.getKeyValuePairs(new_id,id); return mycache.getKeyValuePairs(new_id,id,pred_id);
} }
}; };
\ No newline at end of file
...@@ -334,7 +334,7 @@ public: ...@@ -334,7 +334,7 @@ public:
{ {
return (((int)s.at(0)) << 8) + ((int)s.at(1)); return (((int)s.at(0)) << 8) + ((int)s.at(1));
} }
string getKeyValuePairs(int newId, int id) string getKeyValuePairs(int newId, int id,int pred_id)
{ {
unordered_map<string, string> flush; unordered_map<string, string> flush;
string keyValPairs = ""; string keyValPairs = "";
...@@ -353,30 +353,58 @@ public: ...@@ -353,30 +353,58 @@ public:
{ {
fileName.erase(n - 4); fileName.erase(n - 4);
fileName = fileName.substr(2); fileName = fileName.substr(2);
if (hash(fileName) <= newId || hash(fileName) > id)
{
ifstream fin;
fin.open(fName);
string _key, val;
do if (newId > id) { //wrapAround
if(hash(fileName) <=newId&&hash(fileName)>id) {
ifstream fin;
fin.open(fName);
string _key, val;
do {
getline(fin, _key);
if(_key.size() == 0)
break;
getline(fin, val);
if (val != deleted)
flush[_key] = val;
else
flush.erase(_key);
if (fin.eof())
break;
} while (fin);
fin.close();
const char *c = fName.c_str();
remove(c);
}
}
else {
if (hash(fileName) <= newId||(pred_id>id&&hash(fileName)>pred_id))
{ {
getline(fin, _key); ifstream fin;
if (_key.size() == 0) fin.open(fName);
break; string _key, val;
getline(fin, val);
if (val != deleted) do
flush[_key] = val; {
else getline(fin, _key);
flush.erase(_key); if (_key.size() == 0)
break;
if (fin.eof()) getline(fin, val);
break; if (val != deleted)
} while (fin); flush[_key] = val;
else
fin.close(); flush.erase(_key);
const char *c = fName.c_str();
remove(c); if (fin.eof())
break;
} while (fin);
fin.close();
const char *c = fName.c_str();
remove(c);
}
} }
} }
} }
...@@ -387,7 +415,7 @@ public: ...@@ -387,7 +415,7 @@ public:
for (int i = 0; i <= curr_pos; i++) for (int i = 0; i <= curr_pos; i++)
{ {
if (hash(cacheHeap[i]->key) <= newId || hash(cacheHeap[i]->key) > id) if ((newId>id&&hash(cacheHeap[i]->key)<=newId&&hash(cacheHeap[i]->key)>id)||(newId<id&&hash(cacheHeap[i]->key)<=newId)||(pred_id>id&&hash(cacheHeap[i]->key)>pred_id))
{ {
toBeDeleted.push_back(cacheHeap[i]->key); toBeDeleted.push_back(cacheHeap[i]->key);
flush[cacheHeap[i]->key] = cacheHeap[i]->value; flush[cacheHeap[i]->key] = cacheHeap[i]->value;
......
...@@ -126,19 +126,21 @@ public: ...@@ -126,19 +126,21 @@ public:
} while (fin); } while (fin);
fin.close(); fin.close();
unordered_map<string, string>::iterator itr;
for (itr = flush.begin(); itr != flush.end() && cache.size() < capacity - 1; itr++)
{
this->put(itr->first, itr->second);
flush[itr->first] = deleted;
}
if (flush.find(key) != flush.end()) if (flush.find(key) != flush.end())
{ {
this->put(key, flush[key]); this->put(key, flush[key]);
flush[key] = deleted; flush[key] = deleted;
} }
unordered_map<string, string>::iterator itr;
for (itr = flush.begin(); itr != flush.end() && cache.size() < capacity; itr++)
{
if (itr->second == deleted)
continue;
this->put(itr->first, itr->second);
flush[itr->first] = deleted;
}
const char *c = fileName.c_str(); const char *c = fileName.c_str();
remove(c); remove(c);
...@@ -263,7 +265,7 @@ public: ...@@ -263,7 +265,7 @@ public:
} }
// key1;key2;key3;;value1;value2;value3; // key1;key2;key3;;value1;value2;value3;
string getKeyValuePairs(int newId, int id) string getKeyValuePairs(int newId, int id,int pred_id)
{ {
unordered_map<string, string> flush; unordered_map<string, string> flush;
string keyValPairs = ""; string keyValPairs = "";
...@@ -282,30 +284,58 @@ public: ...@@ -282,30 +284,58 @@ public:
{ {
fileName.erase(n - 4); fileName.erase(n - 4);
fileName = fileName.substr(2); fileName = fileName.substr(2);
if (hash(fileName) <= newId && hash(fileName) > id)
{
ifstream fin;
fin.open(fName);
string _key, val;
do if (newId > id) { //wrapAround
if(hash(fileName) <=newId&&hash(fileName)>id) {
ifstream fin;
fin.open(fName);
string _key, val;
do {
getline(fin, _key);
if(_key.size() == 0)
break;
getline(fin, val);
if (val != deleted)
flush[_key] = val;
else
flush.erase(_key);
if (fin.eof())
break;
} while (fin);
fin.close();
const char *c = fName.c_str();
remove(c);
}
}
else {
if (hash(fileName) <= newId||(pred_id>id&&hash(fileName)>pred_id))
{ {
getline(fin, _key); ifstream fin;
if (_key.size() == 0) fin.open(fName);
break; string _key, val;
getline(fin, val);
if (val != deleted) do
flush[_key] = val; {
else getline(fin, _key);
flush.erase(_key); if (_key.size() == 0)
break;
if (fin.eof()) getline(fin, val);
break; if (val != deleted)
} while (fin); flush[_key] = val;
else
fin.close(); flush.erase(_key);
const char *c = fName.c_str();
remove(c); if (fin.eof())
break;
} while (fin);
fin.close();
const char *c = fName.c_str();
remove(c);
}
} }
} }
} }
...@@ -315,7 +345,7 @@ public: ...@@ -315,7 +345,7 @@ public:
Node *temp = head->next; Node *temp = head->next;
while (temp->next) while (temp->next)
{ {
if (hash(temp->key) <= newId && hash(temp->key) > id) if ((newId>id&&hash(temp->key)<=newId&&hash(temp->key)>id)||((hash(temp->key)<=newId&&newId<id)||(pred_id>id&&hash(temp->key)>pred_id)))
{ {
flush[temp->key] = temp->payload; flush[temp->key] = temp->payload;
} }
...@@ -329,7 +359,7 @@ public: ...@@ -329,7 +359,7 @@ public:
keyValPairs += ";"; keyValPairs += ";";
for (itr = flush.begin(); itr != flush.end(); itr++) for (itr = flush.begin(); itr != flush.end(); itr++)
keyValPairs += itr->second + ";"; keyValPairs += itr->second + ";";
return keyValPairs; return keyValPairs;
} }
}; };
\ No newline at end of file
...@@ -118,12 +118,27 @@ public: ...@@ -118,12 +118,27 @@ public:
break; break;
fingers[nums++]=stoi(temp); fingers[nums++]=stoi(temp);
}while(fin); }while(fin);
fin.close();
int node=-1; int node=-1;
int next=-1; int next=-1;
bool fl=false; bool fl=false;
fin.close();
string my_pred;
fin.open(NEIGHBOURS);
getline(fin,my_pred);
getline(fin,my_pred);
fin.close();
int my_pred_id;
int my_id=stoi(params.find("LISTENING_PORT")->second); int my_id=stoi(params.find("LISTENING_PORT")->second);
if(nums>0&&fingers[nums-1]<id&&my_id>=id) { if(my_pred=="-1")
my_pred_id=-1;
else
my_pred_id=stoi(my_pred.substr(my_pred.find(':')+1));
if(my_pred_id!=-1&&my_pred_id<id&&my_id>id) {
node=my_pred_id;
next=my_id;
}
else if(nums>0&&fingers[nums-1]<id&&my_id>=id) {
node=fingers[nums-1]; node=fingers[nums-1];
next=my_id; next=my_id;
} }
...@@ -348,17 +363,29 @@ public: ...@@ -348,17 +363,29 @@ public:
//return half of the keyvalue pairs to the requesting node //return half of the keyvalue pairs to the requesting node
string address=info.address(); string address=info.address();
int id=stoi(address.substr(address.find(':')+1)); int id=stoi(address.substr(address.find(':')+1));
string keyvalues=memManager->getKeyValuePairs(id,stoi(params["LISTENING_PORT"]));
string keys=keyvalues.substr(0,keyvalues.find(";;")+1);
string values=keyvalues.substr(keyvalues.find(";;")+2);
cout<<"Keys: "<<keys<<endl;
cout<<"Values: "<<values<<endl;
ifstream fin; ifstream fin;
fin.open(NEIGHBOURS); fin.open(NEIGHBOURS);
string successor,predecessor; string successor,predecessor;
getline(fin,successor); getline(fin,successor);
getline(fin,predecessor); getline(fin,predecessor);
fin.close(); fin.close();
int pred_id;
if(predecessor=="-1")
pred_id=-1;
else
pred_id=stoi(predecessor.substr(predecessor.find(':')+1));
string keyvalues=memManager->getKeyValuePairs(id,stoi(params["LISTENING_PORT"]),pred_id);
string keys,values;
if(keyvalues==";") {
keys="null";
values="null";
}
else {
keys=keyvalues.substr(0,keyvalues.find(";;")+1);
values=keyvalues.substr(keyvalues.find(";;")+2);
}
cout<<"Keys: "<<keys<<endl;
cout<<"Values: "<<values<<endl;
predecessor=info.address(); predecessor=info.address();
ofstream fout; ofstream fout;
fout.open(NEIGHBOURS); fout.open(NEIGHBOURS);
...@@ -391,7 +418,23 @@ public: ...@@ -391,7 +418,23 @@ public:
bool fl=false; bool fl=false;
int next=-1; int next=-1;
int my_id=stoi(params.find("LISTENING_PORT")->second); int my_id=stoi(params.find("LISTENING_PORT")->second);
if(nums>0&&fingers[nums-1]<id&&my_id>=id) {
fin.close();
string my_pred;
fin.open(NEIGHBOURS);
getline(fin,my_pred);
getline(fin,my_pred);
fin.close();
int my_pred_id;
if(my_pred=="-1")
my_pred_id=-1;
else
my_pred_id=stoi(my_pred.substr(my_pred.find(':')+1));
if(my_pred_id!=-1&&my_pred_id<id&&my_id>id) {
node=my_pred_id;
next=my_id;
}
else if(nums>0&&fingers[nums-1]<id&&my_id>=id) {
node=fingers[nums-1]; node=fingers[nums-1];
next=my_id; next=my_id;
} }
...@@ -720,7 +763,23 @@ public: ...@@ -720,7 +763,23 @@ public:
bool fl=false; bool fl=false;
int next=-1; int next=-1;
int my_id=stoi(params.find("LISTENING_PORT")->second); int my_id=stoi(params.find("LISTENING_PORT")->second);
if(nums>0&&fingers[nums-1]<key_id&&my_id>=key_id) {
fin.close();
string my_pred;
fin.open(NEIGHBOURS);
getline(fin,my_pred);
getline(fin,my_pred);
fin.close();
int my_pred_id;
if(my_pred=="-1")
my_pred_id=-1;
else
my_pred_id=stoi(my_pred.substr(my_pred.find(':')+1));
if(my_pred_id!=-1&&my_pred_id<key_id&&my_id>key_id) {
node=my_pred_id;
next=my_id;
}
else if(nums>0&&fingers[nums-1]<key_id&&my_id>=key_id) {
node=fingers[nums-1]; node=fingers[nums-1];
next=my_id; next=my_id;
} }
...@@ -918,7 +977,23 @@ public: ...@@ -918,7 +977,23 @@ public:
bool fl=false; bool fl=false;
int next=-1; int next=-1;
int my_id=stoi(params.find("LISTENING_PORT")->second); int my_id=stoi(params.find("LISTENING_PORT")->second);
if(nums>0&&fingers[nums-1]<key_id&&my_id>=key_id) {
fin.close();
string my_pred;
fin.open(NEIGHBOURS);
getline(fin,my_pred);
getline(fin,my_pred);
fin.close();
int my_pred_id;
if(my_pred=="-1")
my_pred_id=-1;
else
my_pred_id=stoi(my_pred.substr(my_pred.find(':')+1));
if(my_pred_id!=-1&&my_pred_id<key_id&&my_id>key_id) {
node=my_pred_id;
next=my_id;
}
else if(nums>0&&fingers[nums-1]<key_id&&my_id>=key_id) {
node=fingers[nums-1]; node=fingers[nums-1];
next=my_id; next=my_id;
} }
...@@ -1102,7 +1177,23 @@ public: ...@@ -1102,7 +1177,23 @@ public:
bool fl=false; bool fl=false;
int next=-1; int next=-1;
int my_id=stoi(params.find("LISTENING_PORT")->second); int my_id=stoi(params.find("LISTENING_PORT")->second);
if(nums>0&&fingers[nums-1]<key_id&&my_id>=key_id) {
fin.close();
string my_pred;
fin.open(NEIGHBOURS);
getline(fin,my_pred);
getline(fin,my_pred);
fin.close();
int my_pred_id;
if(my_pred=="-1")
my_pred_id=-1;
else
my_pred_id=stoi(my_pred.substr(my_pred.find(':')+1));
if(my_pred_id!=-1&&my_pred_id<key_id&&my_id>key_id) {
node=my_pred_id;
next=my_id;
}
else if(nums>0&&fingers[nums-1]<key_id&&my_id>=key_id) {
node=fingers[nums-1]; node=fingers[nums-1];
next=my_id; next=my_id;
} }
...@@ -1288,7 +1379,23 @@ public: ...@@ -1288,7 +1379,23 @@ public:
bool fl=false; bool fl=false;
int next=-1; int next=-1;
int my_id=stoi(params.find("LISTENING_PORT")->second); int my_id=stoi(params.find("LISTENING_PORT")->second);
if(nums>0&&fingers[nums-1]<key_id&&my_id>=key_id) {
fin.close();
string my_pred;
fin.open(NEIGHBOURS);
getline(fin,my_pred);
getline(fin,my_pred);
fin.close();
int my_pred_id;
if(my_pred=="-1")
my_pred_id=-1;
else
my_pred_id=stoi(my_pred.substr(my_pred.find(':')+1));
if(my_pred_id!=-1&&my_pred_id<key_id&&my_id>key_id) {
node=my_pred_id;
next=my_id;
}
else if(nums>0&&fingers[nums-1]<key_id&&my_id>=key_id) {
node=fingers[nums-1]; node=fingers[nums-1];
next=my_id; next=my_id;
} }
...@@ -1468,7 +1575,22 @@ public: ...@@ -1468,7 +1575,22 @@ public:
bool fl=false; bool fl=false;
int next=-1; int next=-1;
int my_id=stoi(params.find("LISTENING_PORT")->second); int my_id=stoi(params.find("LISTENING_PORT")->second);
if(nums>0&&fingers[nums-1]<key_id&&my_id>=key_id) { fin.close();
string my_pred;
fin.open(NEIGHBOURS);
getline(fin,my_pred);
getline(fin,my_pred);
fin.close();
int my_pred_id;
if(my_pred=="-1")
my_pred_id=-1;
else
my_pred_id=stoi(my_pred.substr(my_pred.find(':')+1));
if(my_pred_id!=-1&&my_pred_id<key_id&&my_id>key_id) {
node=my_pred_id;
next=my_id;
}
else if(nums>0&&fingers[nums-1]<key_id&&my_id>=key_id) {
node=fingers[nums-1]; node=fingers[nums-1];
next=my_id; next=my_id;
} }
...@@ -1665,7 +1787,23 @@ public: ...@@ -1665,7 +1787,23 @@ public:
bool fl=false; bool fl=false;
int next=-1; int next=-1;
int my_id=stoi(params.find("LISTENING_PORT")->second); int my_id=stoi(params.find("LISTENING_PORT")->second);
if(nums>0&&fingers[nums-1]<key_id&&my_id>=key_id) {
fin.close();
string my_pred;
fin.open(NEIGHBOURS);
getline(fin,my_pred);
getline(fin,my_pred);
fin.close();
int my_pred_id;
if(my_pred=="-1")
my_pred_id=-1;
else
my_pred_id=stoi(my_pred.substr(my_pred.find(':')+1));
if(my_pred_id!=-1&&my_pred_id<key_id&&my_id>key_id) {
node=my_pred_id;
next=my_id;
}
else if(nums>0&&fingers[nums-1]<key_id&&my_id>=key_id) {
node=fingers[nums-1]; node=fingers[nums-1];
next=my_id; next=my_id;
} }
...@@ -2023,18 +2161,20 @@ void register_server_DNS(string my_address) { ...@@ -2023,18 +2161,20 @@ void register_server_DNS(string my_address) {
status=stub->INFORMSUCCESSOR(&context2,info,&keyValues); status=stub->INFORMSUCCESSOR(&context2,info,&keyValues);
string keys=keyValues.keys(); string keys=keyValues.keys();
string values=keyValues.values(); string values=keyValues.values();
while(true) { if(keys!="null") {
cout<<"Keys: "<<keys<<endl; while(true) {
cout<<"Values: "<<values<<endl; cout<<"Keys: "<<keys<<endl;
string key=keys.substr(0,keys.find(';')); cout<<"Values: "<<values<<endl;
string value=values.substr(0,values.find(';')); string key=keys.substr(0,keys.find(';'));
if(key.size()==0) string value=values.substr(0,values.find(';'));
break; if(key.size()==0)
memManager->put(key,value); break;
if(keys.find(';')+1==keys.size()) memManager->put(key,value);
break; if(keys.find(';')+1==keys.size())
keys=keys.substr(keys.find(';')+1); break;
values=values.substr(values.find(';')+1); keys=keys.substr(keys.find(';')+1);
values=values.substr(values.find(';')+1);
}
} }
channel=grpc::CreateChannel(predecessor,grpc::InsecureChannelCredentials()); channel=grpc::CreateChannel(predecessor,grpc::InsecureChannelCredentials());
stub=KeyValueServices::NewStub(channel); stub=KeyValueServices::NewStub(channel);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment