Commit 4fbaf0a8 authored by Shivaji's avatar Shivaji

Merge branch 'lru-sample' into 'master'

Lru sample

See merge request !1
parents 6dec1e51 996cb357
#include <stdio.h> #include <stdio.h>
#include <pthread.h> #include <pthread.h>
#include <stdlib.h> #include <stdlib.h>
#define _GNU_SOURCE #define _GNU_SOURCE
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <stdatomic.h> #include <stdatomic.h>
#include "LRU.h" #include <assert.h>
#define ATOMIC_TEST_AND_SET __atomic_test_and_set #include "LRU.h"
#define CLEAR __atomic_clear #define ATOMIC_TEST_AND_SET __atomic_test_and_set
#define MAX_SIZE 10 #define CLEAR __atomic_clear
#define MAX_SIZE 10
struct KV *array[MAX_SIZE];
struct queue *qu; struct KV *array[MAX_SIZE];
struct queue *last; struct queue *qu=NULL;
struct queue *last;
void remove_element_from_deque(char *key)
{ void remove_element_from_deque(char *key)
queue *present = qu , *previous=NULL; {
while(present->next != NULL) queue *present = qu , *previous=NULL;
{ if(present == NULL)
if(present->key == key) return;
{
if(previous == NULL) if(strcmp(present->key, key) == 0)
qu = qu->next; {
if(last == present) qu = qu->next;
last = previous; if(last == present)
if(previous) last = last->next;
previous->next = present->next; free(present);
free(present); return;
return; }
}
previous = present; while(present->next != NULL)
present = present->next; {
} if(strcmp(present->key, key) == 0)
} {
if(last == present)
void insert_into_queue(char *key) last = previous;
{ previous->next = present->next;
queue *temp = (queue *)malloc(sizeof(queue)); free(present);
temp->key = key; return;
temp->next = NULL; }
if(qu == NULL) previous = present;
{ present = present->next;
qu = temp; }
last = temp; }
}
else void insert_into_queue(char *key)
{ {
last->next = temp; queue *temp = (queue *)malloc(sizeof(queue));
last = temp; memcpy(temp->key, key, KEY_SIZE);
} temp->next = NULL;
} if(qu == NULL)
{
int find_empty_location() qu = temp;
{ last = temp;
for(int i=0;i<MAX_SIZE;i++) }
{ else
if(array[i]->valid == FALSE) {
return i; last->next = temp;
} last = temp;
} }
}
int cache_del(char *key)
{ int remove_front_element()
for(int i=0;i<MAX_SIZE;i++) {
{ queue *temp = qu;
if(strcmp(array[i]->key , key) == 0) qu = qu->next;
{ assert(temp != NULL);
while(ATOMIC_TEST_AND_SET(&(array[i]->lock),1) == 1); assert(qu != NULL);
remove_element_from_deque(key); for(int i=0;i<MAX_SIZE;i++)
array[i]->valid = FALSE; {
CLEAR(&(array[i]->lock),0); if(strcmp(array[i]->key , temp->key) == 0)
return 1; {
} // check if it is modified to write to file
} array[i]->valid = FALSE;
printf("Cache after DEL: \n"); return i;
print_cache(); }
return 0; }
//TODO remove key from file also }
}
int find_empty_location()
void cache_put(char *key, char *value) {
{ for(int i=0;i<MAX_SIZE;i++)
int indx=-1; {
for(int i=0;i<MAX_SIZE;i++) if(array[i]->valid == FALSE)
{ return i;
if(array[i]->key!=NULL) { }
if(strcmp(array[i]->key , key) == 0) return remove_front_element();
{ }
indx = i;
remove_element_from_deque(key); int cache_del(char *key)
break; {
} for(int i=0;i<MAX_SIZE;i++)
} {
} if(strcmp(array[i]->key , key) == 0)
printf("key is present at index: %d\n", indx); {
while(ATOMIC_TEST_AND_SET(&(array[i]->lock),1) == 1);
if(indx == -1) remove_element_from_deque(key);
{ array[i]->valid = FALSE;
indx = find_empty_location(); CLEAR(&(array[i]->lock),0);
// TODO should write to file if modified is true return 1;
// replacment from cache }
} }
printf("Cache after DEL: \n");
while(ATOMIC_TEST_AND_SET(&(array[indx]->lock),1) == 1); print_cache();
memcpy(array[indx]->key, key, KEY_SIZE); return 0;
memcpy(array[indx]->value, value, VAL_SIZE); //TODO remove key from file also
array[indx]->valid = TRUE; }
array[indx]->modified = TRUE;
insert_into_queue(key); void cache_put(char *key, char *value)
CLEAR(&(array[indx]->lock),0); {
printf("Cache after PUT: \n"); int indx=-1;
print_cache(); for(int i=0;i<MAX_SIZE;i++)
} {
if(array[i]->key!=NULL) {
int cache_get(char *key, char *value) if(strcmp(array[i]->key , key) == 0)
{ {
for(int i=0;i<MAX_SIZE;i++) printf("ENTERING INTO QUEUE\n");
{ indx = i;
if(strcmp(array[i]->key , key) == 0 && array[i]->valid) remove_element_from_deque(key);
{ break;
while(ATOMIC_TEST_AND_SET(&(array[i]->lock),1) == 1); }
remove_element_from_deque(key); }
insert_into_queue(key); }
CLEAR(&(array[i]->lock),0); printf("key is present at index: %d\n", indx);
memcpy(value, array[i]->value, VAL_SIZE);
return 1; if(indx == -1)
} {
} indx = find_empty_location();
return 0; assert(indx>=0);
} // TODO should write to file if modified is true
// replacment from cache
void init_cache() { }
for(int j=0;j<MAX_SIZE;j++)
{ while(ATOMIC_TEST_AND_SET(&(array[indx]->lock),1) == 1);
array[j] = (KV *)malloc(sizeof(KV)); memcpy(array[indx]->key, key, KEY_SIZE);
array[j]->valid = FALSE; memcpy(array[indx]->value, value, VAL_SIZE);
array[j]->lock = 0; array[indx]->valid = TRUE;
} array[indx]->modified = TRUE;
} insert_into_queue(key);
CLEAR(&(array[indx]->lock),0);
void print_cache() { print_cache();
for(int j=0;j<MAX_SIZE;j++) }
{
printf("[%d] (%s) : (%s)\n", array[j]->valid, array[j]->key, array[j]->value); int cache_get(char *key, char *value)
} {
} for(int i=0;i<MAX_SIZE;i++)
\ No newline at end of file {
if(strcmp(array[i]->key , key) == 0 && array[i]->valid)
{
while(ATOMIC_TEST_AND_SET(&(array[i]->lock),1) == 1);
remove_element_from_deque(key);
insert_into_queue(key);
CLEAR(&(array[i]->lock),0);
memcpy(value, array[i]->value, VAL_SIZE);
return 1;
}
}
return 0;
}
void init_cache() {
for(int j=0;j<MAX_SIZE;j++)
{
array[j] = (KV *)malloc(sizeof(KV));
array[j]->valid = FALSE;
array[j]->lock = 0;
}
}
void print_cache() {
for(int j=0;j<MAX_SIZE;j++)
{
printf("[%d] (%s) : (%s)\n", array[j]->valid, array[j]->key, array[j]->value);
}
}
...@@ -22,7 +22,7 @@ typedef struct KV KV; ...@@ -22,7 +22,7 @@ typedef struct KV KV;
typedef struct queue queue; typedef struct queue queue;
struct queue{ struct queue{
char *key; char key[KEY_SIZE];
struct queue *next; struct queue *next;
}; };
...@@ -44,4 +44,6 @@ int cache_get(char *key, char *value); ...@@ -44,4 +44,6 @@ int cache_get(char *key, char *value);
void init_cache(); void init_cache();
void print_cache(); void print_cache();
\ No newline at end of file
int remove_front_element();
\ No newline at end of file
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