Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
CS744 DECS-PA4-KEYVALUE-SERVER
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kamal Khodabhai
CS744 DECS-PA4-KEYVALUE-SERVER
Commits
36cd6757
Commit
36cd6757
authored
Nov 21, 2021
by
Mayank Manoj
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'mayank' into 'master'
Mayank See merge request
!3
parents
e94cec16
2cf6ddb2
Changes
7
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
1311 additions
and
402 deletions
+1311
-402
Backend.h
Backend.h
+49
-25
LFU.h
LFU.h
+147
-39
LRU.h
LRU.h
+132
-27
client.cpp
client.cpp
+1
-0
config
config
+1
-1
dns.cpp
dns.cpp
+6
-0
server.cpp
server.cpp
+975
-310
No files found.
Backend.h
View file @
36cd6757
...
@@ -3,36 +3,46 @@
...
@@ -3,36 +3,46 @@
#include "LFU.h"
#include "LFU.h"
using
namespace
std
;
using
namespace
std
;
class
memoryManagement
{
class
memoryManagement
{
public:
public:
virtual
string
get
(
int
*
a
,
string
s
)
{
virtual
string
get
(
int
*
a
,
string
s
)
{
return
"This will never run"
;
return
"This will never run"
;
}
}
virtual
void
put
(
string
a
,
string
b
)
{
virtual
void
put
(
string
a
,
string
b
)
{
return
;
return
;
}
}
virtual
void
del
(
int
*
a
,
string
s
)
{
virtual
void
del
(
int
*
a
,
string
s
)
{
return
;
return
;
}
}
virtual
void
traverse
()
{
virtual
void
traverse
()
{
return
;
return
;
}
}
virtual
void
pushAll
()
{
virtual
void
pushAll
()
{
return
;
return
;
}
}
virtual
string
getKeyValuePairs
(
int
id
)
{
virtual
string
getKeyValuePairs
(
int
id
)
{
return
"This will never run"
;
return
"This will never run"
;
}
}
};
};
class
storageLRU
:
public
memoryManagement
{
class
storageLRU
:
public
memoryManagement
{
public:
public:
LRUcache
mycache
;
LRUcache
mycache
;
storageLRU
(
int
capacity
)
{
storageLRU
(
int
capacity
)
{
mycache
.
setCap
(
capacity
);
mycache
.
setCap
(
capacity
);
}
}
string
get
(
int
*
status
,
string
key
)
{
string
get
(
int
*
status
,
string
key
)
{
string
result
=
""
;
string
result
=
""
;
int
status2
=
200
;
int
status2
=
200
;
...
@@ -42,37 +52,45 @@ public:
...
@@ -42,37 +52,45 @@ public:
return
result
;
return
result
;
}
}
void
put
(
string
key
,
string
val
)
{
void
put
(
string
key
,
string
val
)
{
mycache
.
put
(
key
,
val
);
mycache
.
put
(
key
,
val
);
}
}
void
del
(
int
*
status
,
string
key
)
{
void
del
(
int
*
status
,
string
key
)
{
int
status2
=
200
;
int
status2
=
200
;
mycache
.
del
(
key
,
&
status2
);
mycache
.
del
(
key
,
&
status2
);
*
status
=
status2
;
*
status
=
status2
;
}
}
void
traverse
()
{
void
traverse
()
{
mycache
.
traverse
();
mycache
.
traverse
();
}
}
void
pushAll
()
{
void
pushAll
()
{
mycache
.
pushAll
();
mycache
.
pushAll
();
}
}
string
getKeyValuePairs
(
int
id
)
{
string
getKeyValuePairs
(
int
id
)
return
"keyvaluepairs"
;
{
return
mycache
.
getKeyValuePairs
(
id
);
}
}
};
};
class
storageLFU
:
public
memoryManagement
{
class
storageLFU
:
public
memoryManagement
{
public:
public:
LFUCache
mycache
;
LFUCache
mycache
;
storageLFU
(
int
capacity
)
{
storageLFU
(
int
capacity
)
{
mycache
.
setCap
(
capacity
);
mycache
.
setCap
(
capacity
);
}
}
string
get
(
int
*
status
,
string
key
)
{
string
get
(
int
*
status
,
string
key
)
{
string
result
=
""
;
string
result
=
""
;
int
status2
=
200
;
int
status2
=
200
;
...
@@ -82,26 +100,32 @@ public:
...
@@ -82,26 +100,32 @@ public:
return
result
;
return
result
;
}
}
void
put
(
string
key
,
string
val
)
{
void
put
(
string
key
,
string
val
)
{
mycache
.
PUT
(
key
,
val
);
mycache
.
PUT
(
key
,
val
);
}
}
void
del
(
int
*
status
,
string
key
)
{
void
del
(
int
*
status
,
string
key
)
{
int
status2
=
200
;
int
status2
=
200
;
mycache
.
DEL
(
key
,
&
status2
);
mycache
.
DEL
(
key
,
&
status2
);
*
status
=
status2
;
*
status
=
status2
;
}
}
void
traverse
()
{
void
traverse
()
{
mycache
.
traverse
();
mycache
.
traverse
();
}
}
void
pushAll
()
{
void
pushAll
()
{
mycache
.
pushAll
();
mycache
.
pushAll
();
}
}
string
getKeyValuePairs
(
int
id
)
{
// key1;key2;key3;;value1;value2;value3;
return
"keyvaluepairs"
;
string
getKeyValuePairs
(
int
id
)
{
return
mycache
.
getKeyValuePairs
(
id
);
}
}
};
};
\ No newline at end of file
LFU.h
View file @
36cd6757
This diff is collapsed.
Click to expand it.
LRU.h
View file @
36cd6757
#include <bits/stdc++.h>
#include <bits/stdc++.h>
#include "LRUNode.h"
#include "LRUNode.h"
#include <dirent.h>
#include <sys/stat.h>
#include <sys/stat.h>
#define NOT_EXIST "KEY NOT EXIST"
#define NOT_EXIST "KEY NOT EXIST"
using
namespace
std
;
using
namespace
std
;
inline
bool
fileExists
(
const
std
::
string
&
name
)
{
inline
bool
fileExists
(
const
std
::
string
&
name
)
{
struct
stat
buffer
;
struct
stat
buffer
;
return
(
stat
(
name
.
c_str
(),
&
buffer
)
==
0
);
return
(
stat
(
name
.
c_str
(),
&
buffer
)
==
0
);
}
}
string
getFilename
(
string
key
)
{
string
getFilename
(
string
key
)
{
string
filename
=
"."
;
string
filename
=
"."
;
int
length
=
key
.
size
();
int
length
=
key
.
size
();
if
(
length
==
1
)
{
if
(
length
==
1
)
{
return
"_.kvm"
;
return
"_.kvm"
;
}
}
filename
+=
char
(
length
-
1
);
filename
+=
char
(
length
-
1
);
...
@@ -24,7 +28,8 @@ string getFilename(string key) {
...
@@ -24,7 +28,8 @@ string getFilename(string key) {
return
filename
;
return
filename
;
}
}
class
LRUcache
{
class
LRUcache
{
private:
private:
int
capacity
;
int
capacity
;
string
deleted
;
string
deleted
;
...
@@ -34,7 +39,8 @@ private:
...
@@ -34,7 +39,8 @@ private:
unordered_map
<
string
,
Node
*>
cache
;
unordered_map
<
string
,
Node
*>
cache
;
public:
public:
LRUcache
()
{
LRUcache
()
{
head
=
new
Node
(
"HEAD"
,
"HEAD"
);
head
=
new
Node
(
"HEAD"
,
"HEAD"
);
tail
=
new
Node
(
"TAIL"
,
"TAIL"
);
tail
=
new
Node
(
"TAIL"
,
"TAIL"
);
head
->
next
=
tail
;
head
->
next
=
tail
;
...
@@ -43,11 +49,13 @@ public:
...
@@ -43,11 +49,13 @@ public:
deleted
+=
char
(
0
);
deleted
+=
char
(
0
);
}
}
void
setCap
(
int
capacity
)
{
void
setCap
(
int
capacity
)
{
this
->
capacity
=
capacity
;
this
->
capacity
=
capacity
;
}
}
LRUcache
(
int
capacity
)
{
LRUcache
(
int
capacity
)
{
this
->
capacity
=
capacity
;
this
->
capacity
=
capacity
;
head
=
new
Node
(
"HEAD"
,
"HEAD"
);
head
=
new
Node
(
"HEAD"
,
"HEAD"
);
tail
=
new
Node
(
"TAIL"
,
"TAIL"
);
tail
=
new
Node
(
"TAIL"
,
"TAIL"
);
...
@@ -57,7 +65,8 @@ public:
...
@@ -57,7 +65,8 @@ public:
deleted
+=
char
(
0
);
deleted
+=
char
(
0
);
}
}
Node
*
getUtil
(
string
key
)
{
Node
*
getUtil
(
string
key
)
{
if
(
cache
.
find
(
key
)
==
cache
.
end
())
if
(
cache
.
find
(
key
)
==
cache
.
end
())
return
NULL
;
return
NULL
;
Node
*
x
=
cache
[
key
];
Node
*
x
=
cache
[
key
];
...
@@ -73,11 +82,14 @@ public:
...
@@ -73,11 +82,14 @@ public:
return
x
;
return
x
;
}
}
string
get
(
string
key
,
int
*
status
)
{
string
get
(
string
key
,
int
*
status
)
{
//std::lock_guard<std::mutex> guard(mtx);
//std::lock_guard<std::mutex> guard(mtx);
Node
*
x
=
getUtil
(
key
);
Node
*
x
=
getUtil
(
key
);
if
(
x
)
{
if
(
x
)
if
(
x
->
payload
==
deleted
)
{
{
if
(
x
->
payload
==
deleted
)
{
*
status
=
400
;
*
status
=
400
;
return
NOT_EXIST
;
return
NOT_EXIST
;
}
}
...
@@ -86,7 +98,8 @@ public:
...
@@ -86,7 +98,8 @@ public:
string
fileName
=
getFilename
(
key
);
string
fileName
=
getFilename
(
key
);
if
(
!
fileExists
(
fileName
))
{
if
(
!
fileExists
(
fileName
))
{
*
status
=
400
;
*
status
=
400
;
return
NOT_EXIST
;
return
NOT_EXIST
;
}
}
...
@@ -97,7 +110,8 @@ public:
...
@@ -97,7 +110,8 @@ public:
string
_key
,
val
;
string
_key
,
val
;
unordered_map
<
string
,
string
>
flush
;
unordered_map
<
string
,
string
>
flush
;
do
{
do
{
getline
(
fin
,
_key
);
getline
(
fin
,
_key
);
if
(
_key
.
size
()
==
0
)
if
(
_key
.
size
()
==
0
)
break
;
break
;
...
@@ -113,12 +127,14 @@ public:
...
@@ -113,12 +127,14 @@ public:
fin
.
close
();
fin
.
close
();
unordered_map
<
string
,
string
>::
iterator
itr
;
unordered_map
<
string
,
string
>::
iterator
itr
;
for
(
itr
=
flush
.
begin
();
itr
!=
flush
.
end
()
&&
cache
.
size
()
<
capacity
-
1
;
itr
++
)
{
for
(
itr
=
flush
.
begin
();
itr
!=
flush
.
end
()
&&
cache
.
size
()
<
capacity
-
1
;
itr
++
)
{
this
->
put
(
itr
->
first
,
itr
->
second
);
this
->
put
(
itr
->
first
,
itr
->
second
);
flush
[
itr
->
first
]
=
deleted
;
flush
[
itr
->
first
]
=
deleted
;
}
}
if
(
flush
.
find
(
key
)
!=
flush
.
end
())
{
if
(
flush
.
find
(
key
)
!=
flush
.
end
())
{
this
->
put
(
key
,
flush
[
key
]);
this
->
put
(
key
,
flush
[
key
]);
flush
[
key
]
=
deleted
;
flush
[
key
]
=
deleted
;
}
}
...
@@ -128,7 +144,8 @@ public:
...
@@ -128,7 +144,8 @@ public:
ofstream
fout
;
ofstream
fout
;
fout
.
open
(
fileName
);
fout
.
open
(
fileName
);
for
(
itr
=
flush
.
begin
();
itr
!=
flush
.
end
();
itr
++
)
{
for
(
itr
=
flush
.
begin
();
itr
!=
flush
.
end
();
itr
++
)
{
if
(
itr
->
second
==
deleted
)
if
(
itr
->
second
==
deleted
)
continue
;
continue
;
fout
<<
itr
->
first
<<
"
\n
"
;
fout
<<
itr
->
first
<<
"
\n
"
;
...
@@ -136,7 +153,8 @@ public:
...
@@ -136,7 +153,8 @@ public:
}
}
fout
.
close
();
fout
.
close
();
x
=
getUtil
(
key
);
x
=
getUtil
(
key
);
if
(
x
)
{
if
(
x
)
{
return
x
->
payload
;
return
x
->
payload
;
}
}
*
status
=
400
;
*
status
=
400
;
...
@@ -144,12 +162,14 @@ public:
...
@@ -144,12 +162,14 @@ public:
return
NOT_EXIST
;
return
NOT_EXIST
;
}
}
void
del
(
string
key
,
int
*
status
)
{
void
del
(
string
key
,
int
*
status
)
{
int
status2
=
200
;
int
status2
=
200
;
string
result
=
get
(
key
,
&
status2
);
string
result
=
get
(
key
,
&
status2
);
// mtx.lock();
// mtx.lock();
if
(
status2
==
400
)
{
if
(
status2
==
400
)
{
*
status
=
400
;
*
status
=
400
;
// mtx.unlock();
// mtx.unlock();
return
;
return
;
...
@@ -159,9 +179,11 @@ public:
...
@@ -159,9 +179,11 @@ public:
// mtx.unlock();
// mtx.unlock();
}
}
void
pushAll
()
{
void
pushAll
()
{
unordered_map
<
string
,
Node
*>::
iterator
itr
;
unordered_map
<
string
,
Node
*>::
iterator
itr
;
for
(
itr
=
cache
.
begin
();
itr
!=
cache
.
end
();
itr
++
)
{
for
(
itr
=
cache
.
begin
();
itr
!=
cache
.
end
();
itr
++
)
{
string
fileName
=
getFilename
(
itr
->
first
);
string
fileName
=
getFilename
(
itr
->
first
);
ofstream
fout
;
ofstream
fout
;
...
@@ -177,12 +199,15 @@ public:
...
@@ -177,12 +199,15 @@ public:
tail
->
prev
=
head
;
tail
->
prev
=
head
;
}
}
void
put
(
string
key
,
string
payload
)
{
void
put
(
string
key
,
string
payload
)
{
//mtx.lock();
//mtx.lock();
if
(
getUtil
(
key
)
!=
NULL
)
if
(
getUtil
(
key
)
!=
NULL
)
cache
[
key
]
->
payload
=
payload
;
cache
[
key
]
->
payload
=
payload
;
else
{
else
if
(
cache
.
size
()
==
capacity
)
{
{
if
(
cache
.
size
()
==
capacity
)
{
Node
*
x
=
tail
->
prev
;
Node
*
x
=
tail
->
prev
;
string
keyToBeFlushed
=
x
->
key
;
string
keyToBeFlushed
=
x
->
key
;
cache
.
erase
(
keyToBeFlushed
);
cache
.
erase
(
keyToBeFlushed
);
...
@@ -206,7 +231,9 @@ public:
...
@@ -206,7 +231,9 @@ public:
head
->
next
=
x
;
head
->
next
=
x
;
x
->
prev
=
head
;
x
->
prev
=
head
;
}
else
{
}
else
{
Node
*
x
=
new
Node
(
key
,
payload
);
Node
*
x
=
new
Node
(
key
,
payload
);
cache
[
key
]
=
x
;
cache
[
key
]
=
x
;
x
->
next
=
head
->
next
;
x
->
next
=
head
->
next
;
...
@@ -219,12 +246,90 @@ public:
...
@@ -219,12 +246,90 @@ public:
//mtx.unlock();
//mtx.unlock();
}
}
void
traverse
()
{
void
traverse
()
{
Node
*
temp
=
head
->
next
;
Node
*
temp
=
head
->
next
;
while
(
temp
->
next
)
{
while
(
temp
->
next
)
{
cout
<<
temp
->
key
<<
"
\n
"
;
cout
<<
temp
->
key
<<
"
\n
"
;
temp
=
temp
->
next
;
temp
=
temp
->
next
;
}
}
cout
<<
"
\n
"
;
cout
<<
"
\n
"
;
}
}
int
hash
(
string
s
)
{
return
(((
int
)
s
.
at
(
0
))
<<
8
)
+
((
int
)
s
.
at
(
1
));
}
// key1;key2;key3;;value1;value2;value3;
string
getKeyValuePairs
(
int
id
)
{
unordered_map
<
string
,
string
>
flush
;
string
keyValPairs
=
""
;
DIR
*
dirFile
=
opendir
(
"."
);
if
(
dirFile
)
{
struct
dirent
*
hFile
;
while
((
hFile
=
readdir
(
dirFile
))
!=
NULL
)
{
string
fName
(
hFile
->
d_name
);
string
fileName
(
hFile
->
d_name
);
int
n
;
if
((
n
=
fileName
.
size
())
<
8
)
continue
;
if
(
fileName
[
n
-
3
]
==
'k'
&&
fileName
[
n
-
2
]
==
'v'
&&
fileName
[
n
-
1
]
==
'm'
)
{
fileName
.
erase
(
n
-
4
);
fileName
=
fileName
.
substr
(
2
);
if
(
hash
(
fileName
)
<=
id
)
{
ifstream
fin
;
fin
.
open
(
fName
);
string
_key
,
val
;
do
{
getline
(
fin
,
_key
);
if
(
_key
.
size
()
==
0
)
break
;
getline
(
fin
,
val
);
if
(
val
!=
deleted
)
flush
[
_key
]
=
val
;
else
flush
.
erase
(
_key
);
if
(
fin
.
eof
())
break
;
}
while
(
fin
);
fin
.
close
();
const
char
*
c
=
fName
.
c_str
();
remove
(
c
);
}
}
}
closedir
(
dirFile
);
}
Node
*
temp
=
head
->
next
;
while
(
temp
->
next
)
{
if
(
hash
(
temp
->
key
)
<=
id
)
{
flush
[
temp
->
key
]
=
temp
->
payload
;
}
temp
=
temp
->
next
;
}
unordered_map
<
string
,
string
>::
iterator
itr
;
for
(
itr
=
flush
.
begin
();
itr
!=
flush
.
end
();
itr
++
)
keyValPairs
+=
itr
->
first
+
";"
;
keyValPairs
+=
";"
;
for
(
itr
=
flush
.
begin
();
itr
!=
flush
.
end
();
itr
++
)
keyValPairs
+=
itr
->
second
+
";"
;
return
keyValPairs
;
}
};
};
client.cpp
View file @
36cd6757
...
@@ -160,6 +160,7 @@ void RunClient() {
...
@@ -160,6 +160,7 @@ void RunClient() {
Info
info
;
Info
info
;
ClientContext
context
;
ClientContext
context
;
Status
status
=
stub
->
GETADDRESS
(
&
context
,
null
,
&
info
);
Status
status
=
stub
->
GETADDRESS
(
&
context
,
null
,
&
info
);
std
::
cout
<<
"Server: "
<<
info
.
address
()
<<
std
::
endl
;
std
::
string
target_address
(
info
.
address
());
std
::
string
target_address
(
info
.
address
());
// Instantiates the client
// Instantiates the client
KeyValueServicesClient
client
(
KeyValueServicesClient
client
(
...
...
config
View file @
36cd6757
LISTENING_PORT=5005
1
LISTENING_PORT=5005
5
CACHE_REPLACEMENT_TYPE=LRU
CACHE_REPLACEMENT_TYPE=LRU
CACHE_SIZE=256
CACHE_SIZE=256
NUM_SERVER_THREADS=4
NUM_SERVER_THREADS=4
dns.cpp
View file @
36cd6757
...
@@ -186,7 +186,13 @@ private:
...
@@ -186,7 +186,13 @@ private:
RequestType
reqType
;
RequestType
reqType
;
};
};
void
signalHandler
(
int
signum
)
{
remove
(
SERVERS
);
exit
(
0
);
}
int
main
(
int
argc
,
char
**
argv
)
{
int
main
(
int
argc
,
char
**
argv
)
{
signal
(
SIGINT
,
signalHandler
);
srand
(
time
(
0
));
srand
(
time
(
0
));
string
server_address
(
"0.0.0.0:1234"
);
string
server_address
(
"0.0.0.0:1234"
);
builder
.
AddListeningPort
(
server_address
,
grpc
::
InsecureServerCredentials
());
builder
.
AddListeningPort
(
server_address
,
grpc
::
InsecureServerCredentials
());
...
...
server.cpp
View file @
36cd6757
This diff is collapsed.
Click to expand it.
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