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
1b5d4448
Commit
1b5d4448
authored
Nov 09, 2020
by
Roshan Rabinarayan
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of
https://git.cse.iitb.ac.in/samarthjoshi/key-value-store
parents
4f004cad
4fbaf0a8
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
341 additions
and
159 deletions
+341
-159
KVServer.c
KVServer.c
+5
-4
LRU.c
LRU.c
+182
-153
LRU.h
LRU.h
+4
-2
StorageHandler.c
StorageHandler.c
+140
-0
StorageHandler.h
StorageHandler.h
+10
-0
No files found.
KVServer.c
View file @
1b5d4448
...
...
@@ -8,6 +8,7 @@
#include <sys/epoll.h>
#include <string.h>
#include "LRU.h"
#include "StorageHandler.h"
#include "KVMessageFormat.h"
#define MAX_EVENTS 10
#define DEBUG (0)
...
...
@@ -33,7 +34,7 @@ void *worker(void *args) {
int
status
;
/
/ Generate name for each thread for debugging
/
* Generate name for each thread for debugging */
char
*
name
=
(
char
*
)
malloc
(
5
*
sizeof
(
char
));
gen_random
(
name
,
5
);
if
DEBUG
printf
(
"[%s] Thread started!
\n
"
,
name
);
...
...
@@ -51,7 +52,7 @@ void *worker(void *args) {
perror
(
"epoll_ctl: read_pipe"
);
exit
(
EXIT_FAILURE
);
}
//if DEBUG printf("[%s] Added read pipe to epoll fd set!\n", name);
while
(
1
)
{
if
DEBUG
printf
(
"[%s] waiting for epoll event!
\n
"
,
name
);
...
...
@@ -91,8 +92,7 @@ void *worker(void *args) {
if
(
flag
&
EPOLLIN
)
{
/* Parse the actual message from client */
struct
message
*
requestMessage
=
malloc
(
sizeof
(
struct
message
));
int
readlength
=
read
(
events
[
i
].
data
.
fd
,
requestMessage
,
sizeof
(
struct
message
));
//printf("[Message Received from client] (%d, %s, %s)\n",requestMessage->status,requestMessage->key,requestMessage->value);
int
readlength
=
read
(
events
[
i
].
data
.
fd
,
requestMessage
,
sizeof
(
struct
message
));
if
DEBUG
printf
(
"[%s][EVENT][EPOLLIN]
\n
"
,
name
);
switch
(
requestMessage
->
status
)
{
...
...
@@ -175,6 +175,7 @@ int main (int argc, int argv) {
clilen
=
sizeof
(
cli_addr
);
init_cache
();
init_storage
()
while
(
1
)
{
...
...
LRU.c
View file @
1b5d4448
#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 @
1b5d4448
...
...
@@ -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
StorageHandler.c
0 → 100644
View file @
1b5d4448
#include<semaphore.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
#include <stdint.h>
#include <limits.h>
#include<fcntl.h>
#include<unistd.h>
#include<string.h>
#include<sys/stat.h>
#include <sys/types.h>
int
*
fds
;
int
setSize
=
2
;
int
*
readCounters
;
sem_t
*
mutex
;
sem_t
*
readerLocks
;
sem_t
x
,
y
;
pthread_t
tid
;
pthread_t
writerthreads
[
100
],
readerthreads
[
100
];
int
readercount
=
0
;
int
modulus
(
char
*
num
,
int
size
,
int
divisor
)
{
int
rem
=
0
;
while
(
size
--
>
0
)
{
rem
=
((
UCHAR_MAX
+
1
)
*
rem
+
*
num
)
%
divisor
;
num
++
;
}
return
rem
;
}
void
file_del
(
off_t
offset
,
char
*
key
)
{
int
index
=
modulus
(
key
,
256
,
setSize
);
fflush
(
stdout
);
int
length
=
0
;
sem_wait
(
&
mutex
[
index
]);
char
blankspace
[
512
];
char
ch
;
int
k
=-
1
;
lseek
(
fds
[
index
],
offset
,
SEEK_SET
);
while
(
read
(
fds
[
index
],
&
ch
,
sizeof
(
ch
))
!=-
1
&&
ch
!=
'\n'
)
{
length
++
;
}
printf
(
"length %d"
,
length
);
memset
(
blankspace
,
0
,
length
);
lseek
(
fds
[
index
],
offset
,
SEEK_SET
);
write
(
fds
[
index
],
blankspace
,
length
);
sem_post
(
&
mutex
[
index
]);
}
void
file_get
(
off_t
offset
,
char
*
key
,
char
*
value
)
{
/* Gets the value stored at offset */
/* Does not depend on key argument */
int
index
=
modulus
(
key
,
256
,
setSize
);
sem_wait
(
&
readerLocks
[
index
]);
readCounters
[
index
]
+=
1
;
if
(
readCounters
[
index
]
==
1
)
sem_wait
(
&
mutex
[
index
]);
sem_post
(
&
readerLocks
[
index
]);
lseek
(
fds
[
index
],
offset
,
SEEK_SET
);
char
line
[
10
];
//FILE *fp =fdopen(fds[index],"r+");
// fseek(fp, offset,SEEK_SET);
size_t
len
=
0
;
char
ch
;
int
k
=-
1
;
while
(
read
(
fds
[
index
],
&
ch
,
sizeof
(
ch
))
!=-
1
&&
ch
!=
'\n'
)
{
if
(
k
>=
0
)
{
value
[
k
++
]
=
ch
;
}
if
(
ch
==
':'
)
{
k
=
0
;
}
}
sem_wait
(
&
readerLocks
[
index
]);
readCounters
[
index
]
-=
1
;
if
(
readCounters
[
index
]
==
0
)
{
sem_post
(
&
mutex
[
index
]);
}
sem_post
(
&
readerLocks
[
index
]);
}
off_t
file_put
(
char
*
key
,
char
*
value
)
{
int
index
=
modulus
(
key
,
256
,
setSize
);
int
bytes
,
rembytes
,
i
;
off_t
position
;
sem_wait
(
&
mutex
[
index
]);
printf
(
"[Write to File: %d]
\n
"
,
index
);
position
=
lseek
(
fds
[
index
],
0
,
SEEK_END
);
char
line
[
514
];
snprintf
(
line
,
sizeof
(
line
),
"%s:%s
\n
"
,
key
,
value
);
if
(
write
(
fds
[
index
],
line
,
strlen
(
line
))
<
0
)
printf
(
"
\n
[unable to perform file_put]
\n
"
);
sem_post
(
&
mutex
[
index
]);
return
position
;
}
int
init_storage
()
{
/*
define the array of file descriptors depending on the prefix
define the array of readCount as well as the semaphore (read x and write y) for the same
PUT,DEL would use write lock
GET would use read lock
each write should return the line number
*/
fds
=
(
int
*
)
malloc
(
sizeof
(
int
)
*
setSize
);
readCounters
=
(
int
*
)
malloc
(
sizeof
(
int
)
*
setSize
);
readerLocks
=
(
sem_t
*
)
malloc
(
sizeof
(
sem_t
)
*
setSize
);
mutex
=
(
sem_t
*
)
malloc
(
sizeof
(
sem_t
)
*
setSize
);
int
i
=
0
;
char
fileName
[
setSize
+
20
];
for
(
i
=
0
;
i
<
setSize
;
i
++
)
{
snprintf
(
fileName
,
sizeof
(
fileName
),
"File%d.txt"
,
i
);
fds
[
i
]
=
open
(
fileName
,
O_CREAT
|
O_RDWR
,
S_IRWXU
);
if
(
fds
[
i
]
<
0
)
{
printf
(
"
\n
[Unable to Open File%d.txt]
\n
"
,
i
);
}
sem_init
(
&
readerLocks
[
i
],
0
,
1
);
sem_init
(
&
mutex
[
i
],
0
,
1
);
readCounters
[
i
]
=
0
;
}
return
0
;
}
\ No newline at end of file
StorageHandler.h
0 → 100644
View file @
1b5d4448
#include <sys/types.h>
void
*
file_del
(
off_t
offset
,
char
*
key
);
void
file_get
(
off_t
offset
,
char
*
key
,
char
*
value
);
off_t
file_put
(
char
*
key
,
char
*
value
);
int
init_storage
();
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