Commit 4b423db7 authored by Shivaji's avatar Shivaji

added initial cas

parent 481b3be8
#include <iostream>
#include <string>
#include <vector>
#include <stdio.h>
#include <pthread.h>
#include <map>
#include <bits/stdc++.h>
#include <stdlib.h>
#define _GNU_SOURCE
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define MAX_SIZE 10
#define ALLOTMEMORY (KV *)malloc(sizeof(KV)*MAX_SIZE)
typedef enum{
FALSE,TRUE
}bool;
struct KV{
char *key;
char *value;
bool valid;
bool modified;
int lock;
};
typedef struct KV KV;
using namespace std;
int MAX_SIZE=10;
map<char *,int> mp;
vector<KV *> stor;
deque<char *> dq;
struct queue{
char *key;
struct queue *next;
};
typedef struct queue queue;
KV *array[MAX_SIZE];
queue *qu = NULL;
queue *last=NULL;
// int compare_and_swap(int* reg, int oldval, int newval)
// {
// ATOMIC();
// int old_reg_val = *reg;
// if (old_reg_val == oldval)
// *reg = newval;
// END_ATOMIC();
// return old_reg_val;
// }
bool cas( int *p, int old, int new ) {
if( *p == old ) {
*p = new;
return TRUE;
} else {
return FALSE;
}
}
void remove_element_from_deque(char *key)
{
for (auto it = dq.begin(); it != dq.end(); it++)
queue *present = qu , *previous=NULL;
while(present->next != NULL)
{
if(*it == key)
if(present->key == key)
{
dq.erase(it);
break;
if(previous == NULL)
qu = qu->next;
if(last == present)
last = previous;
if(previous)
previous->next = present->next;
free(present);
return;
}
previous = present;
present = present->next;
}
}
void insert_into_queue(char *key)
{
queue *temp = (queue *)malloc(sizeof(queue));
temp->key = key;
temp->next = NULL;
if(qu == NULL)
{
qu = temp;
last = temp;
}
else
{
last->next = temp;
last = temp;
}
}
......@@ -38,65 +93,79 @@ int find_empty_location()
{
for(int i=0;i<MAX_SIZE;i++)
{
if(stor[i]->valid == 0)
if(array[i]->valid == FALSE)
return i;
}
}
void delet(char *key)
{
for(int i=0;i<MAX_SIZE;i++)
{
if(array[i]->key == key)
{
while(cas(&(array[i]->lock),0,1) == FALSE) ;
remove_element_from_deque(key);
int indx = mp[key];
mp.erase(key);
stor[indx]->valid = 0;
array[i]->valid = FALSE;
cas(&(array[i]->lock),1,0);
break;
}
}
//TODO remove key from file also
}
void put(char *key, char *value)
{
int indx=-1;
if(mp.find(key) != mp.end())
for(int i=0;i<MAX_SIZE;i++)
{
indx = mp[key];
if(array[i]->key == key)
{
indx = i;
remove_element_from_deque(key);
break;
}
else
}
if(indx == -1)
{
indx = find_empty_location();
mp[key] = indx;
// TODO should write to file if modified is true
// replacment from cache
}
stor[indx]->key = key;
stor[indx]->value = value;
stor[indx]->valid = 1;
stor[indx]->modified = 1;
dq.push_front(key);
while(cas(&(array[indx]->lock),0,1) == FALSE) ;
array[indx]->key = key;
array[indx]->value = value;
array[indx]->valid = TRUE;
array[indx]->modified = TRUE;
insert_into_queue(key);
cas(&(array[indx]->lock),1,0);
}
char* get(char *key)
{
if(mp.find(key) != mp.end())
for(int i=0;i<MAX_SIZE;i++)
{
int indx = mp[key];
if(array[i]->key == key)
{
while(cas(&(array[i]->lock),0,1) == FALSE);
remove_element_from_deque(key);
dq.push_front(key);
return stor[indx]->value;
insert_into_queue(key);
cas(&(array[i]->lock),1,0);
return array[i]->value;
}
}
return (char *)"NOT FOUND";
}
int main()
{
// stor.resize(10);
stor.resize(10);
int i=0;
for(int j=0;j<MAX_SIZE;j++)
{
stor[j] = (KV *)malloc(sizeof(KV));
stor[j]->valid = false;
array[j] = (KV *)malloc(sizeof(KV));
array[j]->valid = FALSE;
}
char key1[250]="Hello world1";
char value1[256] = "THIS IS Vlaue1";
......@@ -126,61 +195,86 @@ int main()
temp1->key = key1;
temp1->value = value1;
temp1->valid = 1;
temp1->valid = TRUE;
temp2->key = key2;
temp2->value = value2;
temp2->valid = 1;
temp2->valid = TRUE;
temp3->key = key3;
temp3->value = value3;
temp3->valid = 1;
temp3->valid = TRUE;
temp4->key = key4;
temp4->value = value4;
temp4->valid = 1;
temp4->valid = TRUE;
temp5->key = key5;
temp5->value = value5;
temp5->valid = 1;
temp5->valid = TRUE;
temp6->key = key6;
temp6->value = value6;
temp6->valid = 1;
stor[i++]=(temp1);
stor[i++] = (temp2);
stor[i++] = (temp3);
stor[i++] = (temp4);
stor[i++] = (temp5);
stor[i++] = (temp6);
mp.insert({key6,5});
mp.insert({key1,0});
mp.insert({key2,1});
mp.insert({key3,2});
mp.insert({key4,3});
mp.insert({key5,4});
cout << mp.size() << endl;
cout << stor[0]->key << " " << stor[0]->value << endl;
cout << stor[1]->key << " " << stor[1]->value << endl;
cout << stor[2]->key << " " << stor[2]->value << endl;
cout << stor[3]->key << " " << stor[3]->value << endl;
cout << stor[4]->key << " " << stor[4]->value << endl;
cout << stor[5]->key << " " << stor[5]->value << endl;
cout << get(stor[5]->key) << endl;
put(stor[5]->key, (char *)"This is a good");
cout << get(stor[5]->key) << endl;
delet(stor[5]->key);
delet(stor[0]->key);
temp6->valid = TRUE;
array[i++] = (temp1);
array[i++] = (temp2);
array[i++] = (temp3);
array[i++] = (temp4);
array[i++] = (temp5);
array[i++] = (temp6);
qu = (queue *)malloc(sizeof(queue));
qu->key = temp1->key;
qu->next = NULL;
queue *te = qu;
te->next = (queue *)malloc(sizeof(queue));
te = te->next;
te->key = temp1->key;
te->next = NULL;
te->next = (queue *)malloc(sizeof(queue));
te = te->next;
te->key = temp2->key;
te->next = NULL;
te->next = (queue *)malloc(sizeof(queue));
te = te->next;
te->key = temp3->key;
te->next = NULL;
te->next = (queue *)malloc(sizeof(queue));
te = te->next;
te->key = temp4->key;
te->next = NULL;
te->next = (queue *)malloc(sizeof(queue));
te = te->next;
te->key = temp5->key;
te->next = NULL;
last = te;
printf("%s " " %s\n", array[0]->key, array[0]->value);
printf("%s " " %s\n", array[1]->key, array[1]->value);
printf("%s " " %s\n", array[2]->key, array[2]->value);
printf("%s " " %s\n", array[3]->key, array[3]->value);
printf("%s " " %s\n", array[4]->key, array[4]->value);
printf("%s " " %s\n", array[5]->key, array[5]->value);
printf("%s\n",get(array[5]->key));
put(array[5]->key, (char *)"This is a good");
printf("%s\n",get(array[5]->key));
delet(array[5]->key);
delet(array[0]->key);
put((char *)"Hello World7", (char *)"THis is vaue7");
cout << stor[0]->key << " " << stor[0]->value << "\n";
printf("%s %s\n",array[0]->key, array[0]->value);
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