Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
pa4
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
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Nilesh Jagdish
pa4
Commits
a9e2787b
Commit
a9e2787b
authored
Nov 05, 2020
by
Nilesh Jagdish
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added client and delete function in cache
parent
5ffd666b
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
122 additions
and
14 deletions
+122
-14
cache.cpp
cache.cpp
+16
-10
client.cpp
client.cpp
+97
-0
client.h
client.h
+9
-4
No files found.
cache.cpp
View file @
a9e2787b
...
@@ -57,17 +57,8 @@ void lru(string key, string value) {
...
@@ -57,17 +57,8 @@ void lru(string key, string value) {
// Write back to persistent storage
// Write back to persistent storage
}
}
else
{
else
{
for
(
auto
p
:
hashTable
)
{
cout
<<
p
.
first
<<
" : "
;
cout
<<
p
.
second
->
key
<<
" "
<<
p
.
second
->
value
<<
" "
<<
p
.
second
->
dirtyBit
<<
endl
;
}
int
num
=
hashTable
.
erase
(
it
->
key
);
int
num
=
hashTable
.
erase
(
it
->
key
);
cache
.
pop_back
();
cache
.
pop_back
();
for
(
auto
p
:
hashTable
)
{
cout
<<
p
.
first
<<
" : "
;
cout
<<
p
.
second
->
key
<<
" "
<<
p
.
second
->
value
<<
" "
<<
p
.
second
->
dirtyBit
<<
endl
;
}
printf
(
"-----------------------------
\n
"
);
cacheNode
cnode
;
cacheNode
cnode
;
cnode
.
key
=
key
;
cnode
.
key
=
key
;
cnode
.
value
=
value
;
cnode
.
value
=
value
;
...
@@ -95,6 +86,16 @@ void insert(string key, string value) {
...
@@ -95,6 +86,16 @@ void insert(string key, string value) {
}
}
}
}
/*
* Delete from cache
*/
void
del
(
string
key
)
{
unordered_map
<
string
,
list
<
cacheNode
>
::
iterator
>
::
iterator
it
=
hashTable
.
find
(
key
);
list
<
cacheNode
>
::
iterator
cnode_it
=
it
->
second
;
cache
.
erase
(
cnode_it
);
hashTable
.
erase
(
key
);
}
/*
/*
* Testing code
* Testing code
*/
*/
...
@@ -129,7 +130,6 @@ int main() {
...
@@ -129,7 +130,6 @@ int main() {
cout
<<
key3
<<
" "
<<
search
(
key3
)
<<
endl
;
cout
<<
key3
<<
" "
<<
search
(
key3
)
<<
endl
;
cout
<<
key4
<<
" "
<<
search
(
key4
)
<<
endl
;
cout
<<
key4
<<
" "
<<
search
(
key4
)
<<
endl
;
printf
(
"-----------------------------
\n
"
);
printf
(
"-----------------------------
\n
"
);
for
(
auto
p
:
hashTable
)
{
for
(
auto
p
:
hashTable
)
{
cout
<<
p
.
first
<<
" : "
;
cout
<<
p
.
first
<<
" : "
;
cout
<<
p
.
second
->
key
<<
" "
<<
p
.
second
->
value
<<
" "
<<
p
.
second
->
dirtyBit
<<
endl
;
cout
<<
p
.
second
->
key
<<
" "
<<
p
.
second
->
value
<<
" "
<<
p
.
second
->
dirtyBit
<<
endl
;
...
@@ -140,4 +140,10 @@ int main() {
...
@@ -140,4 +140,10 @@ int main() {
cout
<<
p
.
first
<<
" : "
;
cout
<<
p
.
first
<<
" : "
;
cout
<<
p
.
second
->
key
<<
" "
<<
p
.
second
->
value
<<
" "
<<
p
.
second
->
dirtyBit
<<
endl
;
cout
<<
p
.
second
->
key
<<
" "
<<
p
.
second
->
value
<<
" "
<<
p
.
second
->
dirtyBit
<<
endl
;
}
}
printf
(
"-----------------------------
\n
"
);
del
(
key5
);
for
(
auto
p
:
hashTable
)
{
cout
<<
p
.
first
<<
" : "
;
cout
<<
p
.
second
->
key
<<
" "
<<
p
.
second
->
value
<<
" "
<<
p
.
second
->
dirtyBit
<<
endl
;
}
}
}
client.cpp
0 → 100644
View file @
a9e2787b
#include <stdio.h>
#include "client.h"
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
void
padding
(
char
*
str
,
char
*
key
,
int
padTo
)
{
int
n
=
strlen
(
str
);
for
(
int
i
=
n
+
1
;
i
<
padTo
;
i
++
)
{
key
[
i
]
=
' '
;
}
}
int
connectToServer
(
char
*
addr
,
char
*
portNo
)
{
int
sockfd
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
);
struct
addrinfo
hints
,
*
result
;
memset
(
&
hints
,
0
,
sizeof
(
struct
addrinfo
));
hints
.
ai_family
=
AF_INET
;
hints
.
ai_socktype
=
SOCK_STREAM
;
// getaddrinfo return getaddr objects by searching using the provided hints, name, service
int
s
=
getaddrinfo
(
addr
,
portNo
,
&
hints
,
&
result
);
if
(
s
!=
0
)
{
fprintf
(
stderr
,
"getaddrinfo: %s
\n
"
,
gai_strerror
(
s
));
return
-
1
;
}
connect
(
sockfd
,
result
->
ai_addr
,
result
->
ai_addrlen
);
cout
<<
GET
(
"ABC"
,
sockfd
)
<<
endl
;
return
0
;
}
int
GET
(
char
*
key
,
int
sockfd
)
{
int
padTo
=
513
;
char
*
buffer
=
(
char
*
)
malloc
(
padTo
+
1
);
strncpy
(
&
buffer
[
1
],
key
,
strlen
(
key
));
padding
(
key
,
buffer
,
padTo
);
buffer
[
padTo
]
=
'\0'
;
buffer
[
0
]
=
'1'
;
write
(
sockfd
,
buffer
,
strlen
(
buffer
));
char
recvBuffer
[
padTo
];
int
len
=
read
(
sockfd
,
recvBuffer
,
padTo
);
if
(
recvBuffer
[
0
]
==
200
)
{
printf
(
"Value : %s
\n
"
,
recvBuffer
[
1
]);
return
1
;
}
else
return
0
;
}
int
PUT
(
char
*
key
,
char
*
value
,
int
sockfd
)
{
int
padTo
=
257
;
char
*
buffer
=
(
char
*
)
malloc
(
2
*
padTo
-
1
);
strncpy
(
&
buffer
[
1
],
key
,
strlen
(
key
));
padding
(
key
,
buffer
,
padTo
);
strncpy
(
&
buffer
[
padTo
],
value
,
strlen
(
value
));
padding
(
value
,
&
buffer
[
padTo
],
padTo
);
buffer
[
0
]
=
'2'
;
write
(
sockfd
,
buffer
,
strlen
(
buffer
));
char
recvBuffer
[
padTo
];
int
len
=
read
(
sockfd
,
recvBuffer
,
padTo
);
if
(
recvBuffer
[
0
]
==
200
)
{
printf
(
"Success
\n
"
);
return
1
;
}
else
{
printf
(
"Failure
\n
"
);
return
0
;
}
}
int
DEL
(
char
*
key
,
int
sockfd
)
{
int
padTo
=
513
;
char
*
buffer
=
(
char
*
)
malloc
(
padTo
+
1
);
strncpy
(
&
buffer
[
1
],
key
,
strlen
(
key
));
padding
(
key
,
buffer
,
padTo
);
buffer
[
padTo
]
=
'\0'
;
buffer
[
0
]
=
'3'
;
write
(
sockfd
,
buffer
,
strlen
(
buffer
));
char
recvBuffer
[
padTo
];
int
len
=
read
(
sockfd
,
recvBuffer
,
padTo
);
if
(
recvBuffer
[
0
]
==
200
)
{
printf
(
"Value : %s
\n
"
,
recvBuffer
[
1
]);
return
1
;
}
else
return
0
;
}
int
main
(
int
argc
,
char
**
argv
)
{
printf
(
"IP = %s, Port = %s
\n
"
,
argv
[
1
],
argv
[
2
]);
connectToServer
(
argv
[
1
],
argv
[
2
]);
return
0
;
}
client.h
View file @
a9e2787b
// 1. connection to server
// 1. connection to server
// 2. KV communication functions
// 2. KV communication functions
#include <iostream>
#include <list>
#include <iterator>
#include <string>
#include <unordered_map>
using
namespace
std
;
int
connectToServer
(
char
*
addr
,
char
*
portNo
);
int
connectToServer
(
char
*
addr
,
char
*
portNo
);
void
sendToServer
(
int
sockfd
);
int
GET
(
char
*
key
,
char
*
request
);
int
GET
(
char
*
key
,
int
sockfd
);
int
PUT
(
char
*
key
,
char
*
value
,
char
*
request
);
int
PUT
(
char
*
key
,
char
*
value
,
int
sockfd
);
int
DEL
ETE
(
char
*
key
,
char
*
request
);
int
DEL
(
char
*
key
,
int
sockfd
);
void
terminateConnection
();
void
terminateConnection
();
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