Commit a78ebd73 authored by Saikumar's avatar Saikumar

modified persistent cache

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