Commit 158e764c authored by Nilesh Jagdish's avatar Nilesh Jagdish

Made changes to cache

parent 67c23ef2
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include <iterator> #include <iterator>
#include <unordered_map> #include <unordered_map>
#include <string> #include <string>
#include "cs_thread.h"
#include "cache.h" #include "cache.h"
using namespace std; using namespace std;
/* /*
...@@ -37,17 +38,19 @@ unordered_map<string, list <cacheNode> :: iterator> hashTable; ...@@ -37,17 +38,19 @@ unordered_map<string, list <cacheNode> :: iterator> hashTable;
* Returns 1 if key-value pair is present in cache and 0 otherwise * Returns 1 if key-value pair is present in cache and 0 otherwise
* If key is present, brings correspoding node to front of the cache(To identify recently used element) * If key is present, brings correspoding node to front of the cache(To identify recently used element)
*/ */
int search(string key) { string search(string key) {
unordered_map<string, list <cacheNode> :: iterator> :: iterator it = hashTable.find(key); unordered_map<string, list <cacheNode> :: iterator> :: iterator it = hashTable.find(key);
if(it == hashTable.end()) { if(it == hashTable.end()) {
return 0; string ans = "";
return ans;
} }
else { else {
list <cacheNode> :: iterator cnode_it = it->second; list <cacheNode> :: iterator cnode_it = it->second;
cache.push_front(*cnode_it); cache.push_front(*cnode_it);
cache.erase(cnode_it); cache.erase(cnode_it);
hashTable[key] = cache.begin(); hashTable[key] = cache.begin();
return 1; string ans = cnode_it->value;
return ans;
} }
} }
...@@ -89,13 +92,29 @@ void insert(string key, string value) { ...@@ -89,13 +92,29 @@ void insert(string key, string value) {
/* /*
* Delete from cache * Delete from cache
*/ */
void del(string key) { void DEL(string key) {
unordered_map<string, list <cacheNode> :: iterator> :: iterator it = hashTable.find(key); unordered_map<string, list <cacheNode> :: iterator> :: iterator it = hashTable.find(key);
list <cacheNode> :: iterator cnode_it = it->second; list<cacheNode> :: iterator cnode_it = it->second;
cache.erase(cnode_it); cache.erase(cnode_it);
hashTable.erase(key); hashTable.erase(key);
} }
string GET(string key) {
string ans = search(key);
return ans;
}
string PUT(string key, string value) {
string ans = search(key);
if(ans == "") {
insert(key, value);
}
else {
unordered_map<string, list <cacheNode> :: iterator> :: iterator it = hashTable.find(key);
it->second->value = value;
}
}
/* /*
* Testing code * Testing code
*/ */
...@@ -141,7 +160,7 @@ int main() { ...@@ -141,7 +160,7 @@ int main() {
cout << p.second->key << " " << p.second->value << " " << p.second->dirtyBit << endl; cout << p.second->key << " " << p.second->value << " " << p.second->dirtyBit << endl;
} }
printf("-----------------------------\n"); printf("-----------------------------\n");
del(key5); DEL(key5);
for (auto p : hashTable) { for (auto p : hashTable) {
cout << p.first << " : "; cout << p.first << " : ";
cout << p.second->key << " " << p.second->value << " " << p.second->dirtyBit << endl; cout << p.second->key << " " << p.second->value << " " << p.second->dirtyBit << endl;
......
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