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

Made changes to cache

parent 67c23ef2
......@@ -3,6 +3,7 @@
#include <iterator>
#include <unordered_map>
#include <string>
#include "cs_thread.h"
#include "cache.h"
using namespace std;
/*
......@@ -37,17 +38,19 @@ unordered_map<string, list <cacheNode> :: iterator> hashTable;
* 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)
*/
int search(string key) {
string search(string key) {
unordered_map<string, list <cacheNode> :: iterator> :: iterator it = hashTable.find(key);
if(it == hashTable.end()) {
return 0;
string ans = "";
return ans;
}
else {
list <cacheNode> :: iterator cnode_it = it->second;
cache.push_front(*cnode_it);
cache.erase(cnode_it);
hashTable[key] = cache.begin();
return 1;
string ans = cnode_it->value;
return ans;
}
}
......@@ -89,13 +92,29 @@ void insert(string key, string value) {
/*
* Delete from cache
*/
void del(string key) {
void DEL(string 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);
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
*/
......@@ -141,7 +160,7 @@ int main() {
cout << p.second->key << " " << p.second->value << " " << p.second->dirtyBit << endl;
}
printf("-----------------------------\n");
del(key5);
DEL(key5);
for (auto p : hashTable) {
cout << p.first << " : ";
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