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,7 +353,34 @@ public: ...@@ -353,7 +353,34 @@ public:
{ {
fileName.erase(n - 4); fileName.erase(n - 4);
fileName = fileName.substr(2); fileName = fileName.substr(2);
if (hash(fileName) <= newId || hash(fileName) > id)
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))
{ {
ifstream fin; ifstream fin;
fin.open(fName); fin.open(fName);
...@@ -380,6 +407,7 @@ public: ...@@ -380,6 +407,7 @@ public:
} }
} }
} }
}
closedir(dirFile); closedir(dirFile);
} }
...@@ -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,7 +284,34 @@ public: ...@@ -282,7 +284,34 @@ public:
{ {
fileName.erase(n - 4); fileName.erase(n - 4);
fileName = fileName.substr(2); fileName = fileName.substr(2);
if (hash(fileName) <= newId && hash(fileName) > id)
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))
{ {
ifstream fin; ifstream fin;
fin.open(fName); fin.open(fName);
...@@ -309,13 +338,14 @@ public: ...@@ -309,13 +338,14 @@ public:
} }
} }
} }
}
closedir(dirFile); closedir(dirFile);
} }
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;
} }
......
This diff is collapsed.
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