Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
K
key-value-store
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
Samarth Joshi
key-value-store
Commits
4fbaf0a8
Commit
4fbaf0a8
authored
Nov 09, 2020
by
Shivaji
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'lru-sample' into 'master'
Lru sample See merge request !1
parents
6dec1e51
996cb357
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
186 additions
and
155 deletions
+186
-155
LRU.c
LRU.c
+182
-153
LRU.h
LRU.h
+4
-2
No files found.
LRU.c
View file @
4fbaf0a8
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#define _GNU_SOURCE
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdatomic.h>
#include "LRU.h"
#define ATOMIC_TEST_AND_SET __atomic_test_and_set
#define CLEAR __atomic_clear
#define MAX_SIZE 10
struct
KV
*
array
[
MAX_SIZE
];
struct
queue
*
qu
;
struct
queue
*
last
;
void
remove_element_from_deque
(
char
*
key
)
{
queue
*
present
=
qu
,
*
previous
=
NULL
;
while
(
present
->
next
!=
NULL
)
{
if
(
present
->
key
==
key
)
{
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
;
}
}
int
find_empty_location
()
{
for
(
int
i
=
0
;
i
<
MAX_SIZE
;
i
++
)
{
if
(
array
[
i
]
->
valid
==
FALSE
)
return
i
;
}
}
int
cache_del
(
char
*
key
)
{
for
(
int
i
=
0
;
i
<
MAX_SIZE
;
i
++
)
{
if
(
strcmp
(
array
[
i
]
->
key
,
key
)
==
0
)
{
while
(
ATOMIC_TEST_AND_SET
(
&
(
array
[
i
]
->
lock
),
1
)
==
1
);
remove_element_from_deque
(
key
);
array
[
i
]
->
valid
=
FALSE
;
CLEAR
(
&
(
array
[
i
]
->
lock
),
0
);
return
1
;
}
}
printf
(
"Cache after DEL:
\n
"
);
print_cache
();
return
0
;
//TODO remove key from file also
}
void
cache_put
(
char
*
key
,
char
*
value
)
{
int
indx
=-
1
;
for
(
int
i
=
0
;
i
<
MAX_SIZE
;
i
++
)
{
if
(
array
[
i
]
->
key
!=
NULL
)
{
if
(
strcmp
(
array
[
i
]
->
key
,
key
)
==
0
)
{
indx
=
i
;
remove_element_from_deque
(
key
);
break
;
}
}
}
printf
(
"key is present at index: %d
\n
"
,
indx
);
if
(
indx
==
-
1
)
{
indx
=
find_empty_location
();
// TODO should write to file if modified is true
// replacment from cache
}
while
(
ATOMIC_TEST_AND_SET
(
&
(
array
[
indx
]
->
lock
),
1
)
==
1
);
memcpy
(
array
[
indx
]
->
key
,
key
,
KEY_SIZE
);
memcpy
(
array
[
indx
]
->
value
,
value
,
VAL_SIZE
);
array
[
indx
]
->
valid
=
TRUE
;
array
[
indx
]
->
modified
=
TRUE
;
insert_into_queue
(
key
);
CLEAR
(
&
(
array
[
indx
]
->
lock
),
0
);
printf
(
"Cache after PUT:
\n
"
);
print_cache
();
}
int
cache_get
(
char
*
key
,
char
*
value
)
{
for
(
int
i
=
0
;
i
<
MAX_SIZE
;
i
++
)
{
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
);
}
}
\ No newline at end of file
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#define _GNU_SOURCE
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdatomic.h>
#include <assert.h>
#include "LRU.h"
#define ATOMIC_TEST_AND_SET __atomic_test_and_set
#define CLEAR __atomic_clear
#define MAX_SIZE 10
struct
KV
*
array
[
MAX_SIZE
];
struct
queue
*
qu
=
NULL
;
struct
queue
*
last
;
void
remove_element_from_deque
(
char
*
key
)
{
queue
*
present
=
qu
,
*
previous
=
NULL
;
if
(
present
==
NULL
)
return
;
if
(
strcmp
(
present
->
key
,
key
)
==
0
)
{
qu
=
qu
->
next
;
if
(
last
==
present
)
last
=
last
->
next
;
free
(
present
);
return
;
}
while
(
present
->
next
!=
NULL
)
{
if
(
strcmp
(
present
->
key
,
key
)
==
0
)
{
if
(
last
==
present
)
last
=
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
));
memcpy
(
temp
->
key
,
key
,
KEY_SIZE
);
temp
->
next
=
NULL
;
if
(
qu
==
NULL
)
{
qu
=
temp
;
last
=
temp
;
}
else
{
last
->
next
=
temp
;
last
=
temp
;
}
}
int
remove_front_element
()
{
queue
*
temp
=
qu
;
qu
=
qu
->
next
;
assert
(
temp
!=
NULL
);
assert
(
qu
!=
NULL
);
for
(
int
i
=
0
;
i
<
MAX_SIZE
;
i
++
)
{
if
(
strcmp
(
array
[
i
]
->
key
,
temp
->
key
)
==
0
)
{
// check if it is modified to write to file
array
[
i
]
->
valid
=
FALSE
;
return
i
;
}
}
}
int
find_empty_location
()
{
for
(
int
i
=
0
;
i
<
MAX_SIZE
;
i
++
)
{
if
(
array
[
i
]
->
valid
==
FALSE
)
return
i
;
}
return
remove_front_element
();
}
int
cache_del
(
char
*
key
)
{
for
(
int
i
=
0
;
i
<
MAX_SIZE
;
i
++
)
{
if
(
strcmp
(
array
[
i
]
->
key
,
key
)
==
0
)
{
while
(
ATOMIC_TEST_AND_SET
(
&
(
array
[
i
]
->
lock
),
1
)
==
1
);
remove_element_from_deque
(
key
);
array
[
i
]
->
valid
=
FALSE
;
CLEAR
(
&
(
array
[
i
]
->
lock
),
0
);
return
1
;
}
}
printf
(
"Cache after DEL:
\n
"
);
print_cache
();
return
0
;
//TODO remove key from file also
}
void
cache_put
(
char
*
key
,
char
*
value
)
{
int
indx
=-
1
;
for
(
int
i
=
0
;
i
<
MAX_SIZE
;
i
++
)
{
if
(
array
[
i
]
->
key
!=
NULL
)
{
if
(
strcmp
(
array
[
i
]
->
key
,
key
)
==
0
)
{
printf
(
"ENTERING INTO QUEUE
\n
"
);
indx
=
i
;
remove_element_from_deque
(
key
);
break
;
}
}
}
printf
(
"key is present at index: %d
\n
"
,
indx
);
if
(
indx
==
-
1
)
{
indx
=
find_empty_location
();
assert
(
indx
>=
0
);
// TODO should write to file if modified is true
// replacment from cache
}
while
(
ATOMIC_TEST_AND_SET
(
&
(
array
[
indx
]
->
lock
),
1
)
==
1
);
memcpy
(
array
[
indx
]
->
key
,
key
,
KEY_SIZE
);
memcpy
(
array
[
indx
]
->
value
,
value
,
VAL_SIZE
);
array
[
indx
]
->
valid
=
TRUE
;
array
[
indx
]
->
modified
=
TRUE
;
insert_into_queue
(
key
);
CLEAR
(
&
(
array
[
indx
]
->
lock
),
0
);
print_cache
();
}
int
cache_get
(
char
*
key
,
char
*
value
)
{
for
(
int
i
=
0
;
i
<
MAX_SIZE
;
i
++
)
{
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
);
}
}
LRU.h
View file @
4fbaf0a8
...
...
@@ -22,7 +22,7 @@ typedef struct KV KV;
typedef
struct
queue
queue
;
struct
queue
{
char
*
key
;
char
key
[
KEY_SIZE
]
;
struct
queue
*
next
;
};
...
...
@@ -44,4 +44,6 @@ int cache_get(char *key, char *value);
void
init_cache
();
void
print_cache
();
\ No newline at end of file
void
print_cache
();
int
remove_front_element
();
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment