Commit a78ebd73 authored by Saikumar's avatar Saikumar

modified persistent cache

parent fea09cd8
......@@ -35,8 +35,18 @@ using namespace std;
list<cacheNode> cache;
unordered_map<string, list <cacheNode> :: iterator> hashTable;
// pthread_mutex_t cLock, pLock;
pthread_mutex_t cLock;
void initializeCache()
{
pthread_mutex_init(&cLock, NULL);
// pthread_mutex_init(&pLock, NULL);
initializeStorageVariables();
}
/*
* 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)
......@@ -93,34 +103,29 @@ void insert(string key, string value) {
}
string GET(string key) {
// printf("GET function\n");
printf("GET function\n");
pthread_mutex_lock(&cLock);
string ans = search(key);
pthread_mutex_unlock(&cLock);
/*need to add ravi code*/
if(ans=="")
{
// char * new_key = new char[key.size() + 1];
// copy(key.begin(), key.end(), new_key);
// new_key[key.size()] = '\0';
// string value = GETT(new_key);
// if(value[0] != (char)240) {
// // cLock
// pthread_mutex_lock(&cLock);
// insert(key, value);
// pthread_mutex_unlock(&cLock);
// //uncLock
// }
// return value;
return "ERROR";
}
char * new_key = new char[key.size() + 1];
copy(key.begin(), key.end(), new_key);
new_key[key.size()] = '\0';
string value = GETT(new_key);
if(value[0] != (char)240) {
pthread_mutex_lock(&cLock);
insert(key, value);
pthread_mutex_unlock(&cLock);
}
return value;
}
return ans;
}
string PUT(string key, string value) {
// cout << "PUT started" << endl;
pthread_mutex_lock(&cLock);
string ans = search(key);
if(ans == "") {
......@@ -131,14 +136,13 @@ string PUT(string key, string value) {
it->second->value = value;
}
pthread_mutex_unlock(&cLock);
// uncLock
char * new_key = new char[key.size() + 1];
copy(key.begin(), key.end(), new_key);
new_key[key.size()] = '\0';
char * new_value = new char[value.size() + 1];
copy(value.begin(), value.end(), new_value);
new_value[value.size()] = '\0';
// PUTT(new_key, new_value);
PUTT(new_key, new_value);
string s(1, (char)200);
string response = s + "Put Completed Successfully";
return response;
......@@ -148,8 +152,6 @@ string PUT(string key, string value) {
* Delete from cache
*/
string DEL(string key) {
/*Need to add ravi code*/
//cLock
pthread_mutex_lock(&cLock);
string ans = search(key);
if(ans != "") {
......@@ -157,16 +159,12 @@ string DEL(string key) {
list<cacheNode> :: iterator cnode_it = it->second;
cache.erase(cnode_it);
hashTable.erase(key);
pthread_mutex_unlock(&cLock);
return "SUCCESS";
}
//uncLock
pthread_mutex_unlock(&cLock);
char * new_key = new char[key.size() + 1];
copy(key.begin(), key.end(), new_key);
new_key[key.size()] = '\0';
// string response=DELL(new_key);
string response = "ERROR";
string response=DELL(new_key);
return response;
}
......
......@@ -40,19 +40,34 @@ struct n2
struct n1 *h1=NULL;
struct n2 *h2=NULL;
unsigned int nooffiles=0;
void initializeStorageVariables()
{
pthread_mutex_init(&m, NULL);
sem_init(&w, 0,1);
}
struct n2 * check(struct n2 *h2){
while (1)
{
if(h2==NULL){
return NULL;
}
else{
if((h2->nooffreeline)>0){
return h2;}
else{
h2=h2->pointer;
if(h2==NULL)
{
return NULL;
}
else
{
if((h2->nooffreeline)>0)
{
return h2;
}
else
{
h2=h2->pointer;
}
}
}
}
......@@ -642,7 +657,7 @@ string GETT(char * key){
void PUTT(char *key,char *value){
sem_wait(&w);
printf("key = %s\nvalue=%s",key, value);
// printf("key = %s\nvalue=%s",key, value);
h1=insert(h1,key);
char filename2[10];
strcpy(filename2,add->filename);
......@@ -800,16 +815,6 @@ void pp2(struct n1*r){
// int main()
// {
// printf("Saikumar\n");
// return 0;
// }
......
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