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
d893258e
Commit
d893258e
authored
Nov 10, 2020
by
Samarth Joshi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Updated modulus
parent
ea8ae751
Changes
8
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
115 additions
and
215 deletions
+115
-215
.File0.txt.swp
.File0.txt.swp
+0
-0
File0.txt
File0.txt
+0
-11
File1.txt
File1.txt
+0
-0
KVServer.c
KVServer.c
+7
-6
Server
Server
+0
-0
StorageHandler.c
StorageHandler.c
+57
-91
StorageHandler.h
StorageHandler.h
+1
-1
temp.c
temp.c
+50
-106
No files found.
.File0.txt.swp
0 → 100644
View file @
d893258e
File added
File0.txt
deleted
100755 → 0
View file @
ea8ae751
1:test
2:test
3:test
4:test
5:test
6:test
7:test
8:test
9:test
10:tet
11:test
File1.txt
deleted
100644 → 0
View file @
ea8ae751
KVServer.c
View file @
d893258e
...
...
@@ -93,7 +93,8 @@ void *worker(void *args) {
/* Parse the actual message from client */
struct
message
*
requestMessage
=
malloc
(
sizeof
(
struct
message
));
int
readlength
=
read
(
events
[
i
].
data
.
fd
,
requestMessage
,
sizeof
(
struct
message
));
memset
(
requestMessage
->
key
,
0
,
256
);
memset
(
requestMessage
->
value
,
0
,
256
);
if
DEBUG
printf
(
"[%s][EVENT][EPOLLIN]
\n
"
,
name
);
switch
(
requestMessage
->
status
)
{
case
STATUS_GET
:
...
...
@@ -121,10 +122,10 @@ void *worker(void *args) {
if
DEBUG
printf
(
"[%s] DEL
\n
"
,
name
);
status
=
cache_del
(
requestMessage
->
key
);
file_del
(
requestMessage
->
key
);
if
(
status
==
0
)
{
requestMessage
->
status
=
240
;
}
else
{
if
(
status
)
{
requestMessage
->
status
=
200
;
}
else
{
requestMessage
->
status
=
240
;
}
break
;
}
...
...
@@ -149,12 +150,12 @@ int main (int argc, int argv) {
int
i
;
int
next_thread_to_assign
=
0
;
pthread_t
*
worker_threads
;
int
pool_thread_size
=
1
0
;
// TODO: get pool thread size from config file
int
pool_thread_size
=
1
;
// TODO: get pool thread size from config file
int
sockfd
,
newsockfd
,
portno
,
clilen
,
n
;
struct
sockaddr_in
serv_addr
,
cli_addr
;
//int *pipes = (int*) malloc(pool_thread_size * 2 * sizeof(pipes));
int
pipes
[
1
0
][
2
];
// TODO: initialize pipes dynamically
int
pipes
[
1
][
2
];
// TODO: initialize pipes dynamically
worker_threads
=
malloc
(
pool_thread_size
*
sizeof
(
pthread_t
));
for
(
i
=
0
;
i
<
pool_thread_size
;
i
++
)
{
...
...
Server
View file @
d893258e
No preview for this file type
StorageHandler.c
View file @
d893258e
...
...
@@ -24,44 +24,50 @@ int readercount = 0;
unsigned
modulus
(
unsigned
char
*
num
,
size_t
size
,
unsigned
divisor
)
{
unsigned
rem
=
0
;
while
(
size
--
>
0
)
{
rem
=
((
UCHAR_MAX
+
1ULL
)
*
rem
+
*
num
)
%
divisor
;
num
++
;
while
(
size
>
0
)
{
rem
+=
num
[
size
--
]
%
divisor
;
}
return
rem
;
return
rem
%
divisor
;
}
int
file_search
(
char
*
key
,
char
*
value
,
int
index
)
{
lseek
(
fds
[
index
],
0
,
SEEK_SET
);
int
offset
=
0
;
int
size
;
char
fkey
[
256
];
char
fval
[
256
];
do
{
size
=
read
(
fds
[
index
],
fkey
,
sizeof
(
fkey
));
if
(
size
<=
0
)
break
;
size
=
read
(
fds
[
index
],
fval
,
sizeof
(
fval
));
if
(
size
<=
0
)
break
;
if
(
strcmp
(
fkey
,
key
)
==
0
)
{
if
(
value
)
memcpy
(
value
,
fval
,
sizeof
(
fval
));
return
offset
;
}
offset
+=
sizeof
(
fkey
)
+
sizeof
(
fval
);
}
while
(
1
);
return
-
1
;
}
void
file_del
(
char
*
key
)
{
int
offset
;
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
],
0
,
SEEK_SET
);
memset
(
blankspace
,
0
,
512
);
char
*
line
;
char
temp
[
514
];
size_t
len
=
0
;
off_t
position
=
0
;
FILE
*
fp
=
fdopen
(
fds
[
index
],
"rb+"
);
while
((
getline
(
&
line
,
&
len
,
fp
))
!=
-
1
)
{
memcpy
(
temp
,
line
,
strlen
(
line
)
-
1
);
char
*
fkey
=
strtok
(
line
,
":"
);
char
*
fvalue
=
strtok
(
NULL
,
":"
);
if
(
strcmp
(
key
,
fkey
)
==
0
){
lseek
(
fds
[
index
],
position
,
SEEK_SET
);
write
(
fds
[
index
],
blankspace
,
strlen
(
temp
));
break
;
offset
=
file_search
(
key
,
NULL
,
index
);
if
(
offset
>=
0
)
{
lseek
(
fds
[
index
],
offset
,
SEEK_SET
);
write
(
fds
[
index
],
0
,
256
);
write
(
fds
[
index
],
0
,
256
);
}
position
=
ftell
(
fp
);
}
printf
(
"length %d"
,
length
);
sem_post
(
&
mutex
[
index
]);
}
...
...
@@ -70,28 +76,17 @@ int file_get(char *key, char *value)
/* Gets the value stored at offset */
/* Does not depend on key argument */
int
found
=
0
;
int
index
=
modulus
(
key
,
256
,
setSize
);
lseek
(
fds
[
index
],
0
,
SEEK_SET
);
sem_wait
(
&
readerLocks
[
index
]);
readCounters
[
index
]
+=
1
;
if
(
readCounters
[
index
]
==
1
)
sem_wait
(
&
mutex
[
index
]);
sem_post
(
&
readerLocks
[
index
]);
char
*
line
;
size_t
len
=
0
;
FILE
*
fp
=
fdopen
(
fds
[
index
],
"rb+"
);
while
((
getline
(
&
line
,
&
len
,
fp
))
!=
-
1
)
{
char
*
fkey
=
strtok
(
line
,
":"
);
char
*
fvalue
=
strtok
(
NULL
,
":"
);
if
(
strcmp
(
key
,
fkey
)
==
0
){
memcpy
(
value
,
fvalue
,
strlen
(
fvalue
)
-
1
);
found
=
1
;
break
;
}
if
(
file_search
(
key
,
value
,
index
)
>=
0
)
{
found
=
1
;
}
sem_wait
(
&
readerLocks
[
index
]);
readCounters
[
index
]
-=
1
;
if
(
readCounters
[
index
]
==
0
)
...
...
@@ -102,55 +97,26 @@ int file_get(char *key, char *value)
return
found
;
}
off_t
file_put
(
char
*
key
,
char
*
value
)
{
void
file_put
(
char
*
key
,
char
*
value
)
{
/*
if found then ask the offset where it is present and if the value noy matches with the present value ,update the given line
if not present then search for empty line and insert there!
*/
unsigned
int
index
=
modulus
(
key
,
256
,
setSize
);
lseek
(
fds
[
index
],
0
,
SEEK_SET
);
off_t
position
;
//sem_wait(&mutex[index]);
printf
(
"[Write to File: %d]
\n
"
,
index
);
char
*
line
;
char
lin
[
514
];
size_t
len
=
0
;
FILE
*
fp
=
fdopen
(
fds
[
index
],
"rb+"
);
position
=
ftell
(
fp
);
while
((
getline
(
&
line
,
&
len
,
fp
))
!=
-
1
)
{
char
*
fkey
=
strtok
(
line
,
":"
);
char
*
fvalue
=
strtok
(
NULL
,
":"
);
if
(
strcmp
(
key
,
fkey
)
==
0
){
if
(
strcmp
(
value
,
strtok
(
fvalue
,
"
\n
"
))
==
0
)
{
printf
(
"(%s)(%s)
\n
"
,
fkey
,
key
);
///everything same then why insert?
return
0
;
}
else
{
printf
(
"key:(%s)(%s)
\n
"
,
fkey
,
key
);
printf
(
"value:(%s)(%s)
\n
"
,
fvalue
,
value
);
fflush
(
stdout
);
fseek
(
fp
,
position
,
SEEK_SET
);
snprintf
(
lin
,
strlen
(
key
)
+
strlen
(
value
)
+
2
,
"%s:%s
\n
"
,
key
,
value
);
if
(
fputs
(
lin
,
fp
)
<
0
)
printf
(
"
\n
[unable to perform file_put]
\n
"
);
return
0
;
}
}
position
=
ftell
(
fp
);
}
snprintf
(
lin
,
sizeof
(
lin
),
"%s:%s
\n
"
,
key
,
value
);
if
(
write
(
fds
[
index
],
lin
,
strlen
(
lin
))
<
0
)
printf
(
"
\n
[unable to perform file_put]
\n
"
);
int
offset
;
int
index
=
modulus
(
key
,
256
,
setSize
);
sem_wait
(
&
mutex
[
index
]);
offset
=
file_search
(
key
,
NULL
,
index
);
if
(
offset
<
0
)
{
lseek
(
fds
[
index
],
0
,
SEEK_END
);
}
else
{
lseek
(
fds
[
index
],
offset
,
SEEK_SET
);
}
write
(
fds
[
index
],
key
,
256
);
write
(
fds
[
index
],
value
,
256
);
//
sem_post(&mutex[index]);
sem_post
(
&
mutex
[
index
]);
return
position
;
}
int
storage_init
()
{
...
...
@@ -179,5 +145,5 @@ int storage_init()
sem_init
(
&
mutex
[
i
],
0
,
1
);
readCounters
[
i
]
=
0
;
}
return
0
;
}
StorageHandler.h
View file @
d893258e
...
...
@@ -4,7 +4,7 @@ int storage_init();
void
file_del
(
char
*
key
);
off_t
file_put
(
char
*
key
,
char
*
value
);
void
file_put
(
char
*
key
,
char
*
value
);
int
file_get
(
char
*
key
,
char
*
value
);
...
...
temp.c
View file @
d893258e
...
...
@@ -30,38 +30,40 @@ unsigned modulus( unsigned char *num, size_t size, unsigned divisor) {
}
return
rem
;
}
int
file_search
(
char
*
key
,
cahr
*
value
,
int
index
)
{
lseek
(
fds
[
index
],
0
,
SEEK_SET
);
int
offset
=
0
;
char
fkey
[
256
];
char
fval
[
256
];
do
{
if
(
read
(
fds
[
index
],
fkey
,
sizeof
(
fkey
))
<
0
)
break
;
if
(
read
(
fds
[
index
],
fval
,
sizeof
(
fval
))
<
0
)
break
;
if
(
strcmp
(
fkey
,
key
)
==
0
)
{
memcpy
(
value
,
fval
,
sizeof
(
fval
));
return
offset
;
}
offset
+=
sizeof
(
fkey
)
+
sizeof
(
fval
);
}
while
(
1
);
return
-
1
;
}
void
file_del
(
char
*
key
)
{
int
offset
;
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
],
0
,
SEEK_SET
);
memset
(
blankspace
,
0
,
512
);
char
*
line
;
char
temp
[
514
];
size_t
len
=
0
;
off_t
position
=
0
;
FILE
*
fp
=
fdopen
(
fds
[
index
],
"rb+"
);
while
((
getline
(
&
line
,
&
len
,
fp
))
!=
-
1
)
{
memcpy
(
temp
,
line
,
strlen
(
line
)
-
1
);
char
*
fkey
=
strtok
(
line
,
":"
);
char
*
fvalue
=
strtok
(
NULL
,
":"
);
if
(
strcmp
(
key
,
fkey
)
==
0
){
lseek
(
fds
[
index
],
position
,
SEEK_SET
);
write
(
fds
[
index
],
blankspace
,
strlen
(
temp
)
);
break
;
offset
=
file_search
(
key
,
value
,
index
)
if
(
offset
>=
0
)
{
lseek
(
fds
[
index
],
offset
,
SEEK_SET
);
write
(
fds
[
index
],
0
,
256
);
write
(
fds
[
index
],
0
,
256
)
;
}
position
=
ftell
(
fp
);
}
printf
(
"length %d"
,
length
);
sem_post
(
&
mutex
[
index
]);
}
...
...
@@ -70,28 +72,17 @@ int file_get(char *key, char *value)
/* Gets the value stored at offset */
/* Does not depend on key argument */
int
found
=
0
;
int
index
=
modulus
(
key
,
strlen
(
key
),
setSize
);
lseek
(
fds
[
index
],
0
,
SEEK_SET
);
int
index
=
modulus
(
key
,
strlen
(
key
),
setSize
);
sem_wait
(
&
readerLocks
[
index
]);
readCounters
[
index
]
+=
1
;
if
(
readCounters
[
index
]
==
1
)
sem_wait
(
&
mutex
[
index
]);
sem_post
(
&
readerLocks
[
index
]);
char
*
line
;
size_t
len
=
0
;
FILE
*
fp
=
fdopen
(
fds
[
index
],
"rb+"
);
while
((
getline
(
&
line
,
&
len
,
fp
))
!=
-
1
)
{
char
*
fkey
=
strtok
(
line
,
":"
);
char
*
fvalue
=
strtok
(
NULL
,
":"
);
if
(
strcmp
(
key
,
fkey
)
==
0
){
memcpy
(
value
,
fvalue
,
strlen
(
fvalue
)
-
1
);
found
=
1
;
break
;
}
if
(
file_search
(
key
,
value
,
index
)
>=
0
)
{
found
=
1
;
}
sem_wait
(
&
readerLocks
[
index
]);
readCounters
[
index
]
-=
1
;
if
(
readCounters
[
index
]
==
0
)
...
...
@@ -102,58 +93,29 @@ int file_get(char *key, char *value)
return
found
;
}
off_t
file_put
(
char
*
key
,
char
*
value
)
{
off_t
file_put
(
char
*
key
,
char
*
value
)
{
/*
if found then ask the offset where it is present and if the value noy matches with the present value ,update the given line
if not present then search for empty line and insert there!
*/
unsigned
int
index
=
modulus
(
key
,
256
,
setSize
);
lseek
(
fds
[
index
],
0
,
SEEK_SET
);
off_t
position
;
//sem_wait(&mutex[index]);
printf
(
"[Write to File: %d]
\n
"
,
index
);
char
*
line
;
char
lin
[
514
];
size_t
len
=
0
;
FILE
*
fp
=
fdopen
(
fds
[
index
],
"rb+"
);
position
=
ftell
(
fp
);
while
((
getline
(
&
line
,
&
len
,
fp
))
!=
-
1
)
{
char
*
fkey
=
strtok
(
line
,
":"
);
char
*
fvalue
=
strtok
(
NULL
,
":"
);
printf
(
"key:(%s)(%s)
\n
"
,
fkey
,
key
);
if
(
strcmp
(
key
,
fkey
)
==
0
){
if
(
strcmp
(
value
,
strtok
(
fvalue
,
"
\n
"
))
==
0
)
{
printf
(
"(%s)(%s)
\n
"
,
fkey
,
key
);
///everything same then why insert?
return
0
;
}
else
{
int
offset
;
int
index
=
modulus
(
key
,
strlen
(
key
),
setSize
);
sem_wait
(
&
mutex
[
index
]);
printf
(
"value:(%s)(%s)
\n
"
,
fvalue
,
value
);
fflush
(
stdout
);
fseek
(
fp
,
position
,
SEEK_SET
);
snprintf
(
lin
,
sizeof
(
lin
),
"%s:%s
\n
"
,
key
,
value
);
if
(
fputs
(
lin
,
fp
)
<
0
)
printf
(
"
\n
[unable to perform file_put]
\n
"
);
return
0
;
}
}
position
=
ftell
(
fp
);
offset
=
file_search
(
key
,
value
,
index
)
if
(
offset
<
0
)
{
lseek
(
fds
[
index
],
0
,
SEEK_END
);
}
else
{
lseek
(
fds
[
index
],
offset
,
SEEK_SET
);
}
snprintf
(
lin
,
sizeof
(
lin
),
"%s:%s
\n
"
,
key
,
value
);
if
(
write
(
fds
[
index
],
lin
,
strlen
(
lin
))
<
0
)
printf
(
"
\n
[unable to perform file_put]
\n
"
);
write
(
fds
[
index
],
key
,
sizeof
(
key
));
write
(
fds
[
index
],
value
,
sizeof
(
value
));
// sem_post(&mutex[index]);
sem_post
(
&
mutex
[
index
]);
return
position
;
}
int
main
()
int
storage_init
()
{
/*
define the array of file descriptors depending on the prefix
...
...
@@ -162,13 +124,6 @@ int main()
GET would use read lock
each write should return the line number
*/
int
n2
=
0
;
char
prevkey
[
256
]
=
"24"
;
char
key
[
256
]
=
"25"
;
char
value
[
256
]
=
"value2"
;
off_t
offset
;
bzero
(
key
+
strlen
(
key
),
sizeof
(
key
)
-
strlen
(
key
));
bzero
(
value
+
strlen
(
value
),
sizeof
(
value
)
-
strlen
(
value
));
fds
=
(
int
*
)
malloc
(
sizeof
(
int
)
*
setSize
);
readCounters
=
(
int
*
)
malloc
(
sizeof
(
int
)
*
setSize
);
readerLocks
=
(
sem_t
*
)
malloc
(
sizeof
(
sem_t
)
*
setSize
);
...
...
@@ -187,16 +142,5 @@ int main()
sem_init
(
&
mutex
[
i
],
0
,
1
);
readCounters
[
i
]
=
0
;
}
//offset = file_put("24", "value245");
offset
=
file_put
(
"24"
,
"value1"
);
file_put
(
"29"
,
"update2"
);
file_put
(
"28"
,
"update2"
);
//file_get(prevkey, value); // Doesnot depend on key arg, returns key and value at offset 0
//printf("(%s)", value);
//file_del("29");
//file_get("24",value);
// printf("(%s)\n", value);
//file_del("21");
//printf("(%s)\n", value);
return
0
;
}
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