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
68222a12
Commit
68222a12
authored
Nov 21, 2021
by
Mayank Manoj
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'mayank' into 'master'
final. running perfectly See merge request
!9
parents
afb42e10
36a3cde3
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
286 additions
and
88 deletions
+286
-88
Backend.h
Backend.h
+5
-5
LFU.h
LFU.h
+52
-24
LRU.h
LRU.h
+63
-33
server.cpp
server.cpp
+166
-26
No files found.
Backend.h
View file @
68222a12
...
...
@@ -26,7 +26,7 @@ public:
{
return
;
}
virtual
string
getKeyValuePairs
(
int
new_id
,
int
id
)
virtual
string
getKeyValuePairs
(
int
new_id
,
int
id
,
int
pred_id
)
{
return
"This will never run"
;
}
...
...
@@ -75,9 +75,9 @@ public:
mycache
.
pushAll
();
}
string
getKeyValuePairs
(
int
new_id
,
int
id
)
string
getKeyValuePairs
(
int
new_id
,
int
id
,
int
pred_id
)
{
return
mycache
.
getKeyValuePairs
(
new_id
,
id
);
return
mycache
.
getKeyValuePairs
(
new_id
,
id
,
pred_id
);
}
};
...
...
@@ -124,8 +124,8 @@ public:
}
// key1;key2;key3;;value1;value2;value3;
string
getKeyValuePairs
(
int
new_id
,
int
id
)
string
getKeyValuePairs
(
int
new_id
,
int
id
,
int
pred_id
)
{
return
mycache
.
getKeyValuePairs
(
new_id
,
id
);
return
mycache
.
getKeyValuePairs
(
new_id
,
id
,
pred_id
);
}
};
\ No newline at end of file
LFU.h
View file @
68222a12
...
...
@@ -334,7 +334,7 @@ public:
{
return
(((
int
)
s
.
at
(
0
))
<<
8
)
+
((
int
)
s
.
at
(
1
));
}
string
getKeyValuePairs
(
int
newId
,
int
id
)
string
getKeyValuePairs
(
int
newId
,
int
id
,
int
pred_id
)
{
unordered_map
<
string
,
string
>
flush
;
string
keyValPairs
=
""
;
...
...
@@ -353,7 +353,34 @@ public:
{
fileName
.
erase
(
n
-
4
);
fileName
=
fileName
.
substr
(
2
);
if
(
hash
(
fileName
)
<=
newId
||
hash
(
fileName
)
>
id
)
if
(
newId
>
id
)
{
//wrapAround
if
(
hash
(
fileName
)
<=
newId
&&
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
);
}
}
else
{
if
(
hash
(
fileName
)
<=
newId
||
(
pred_id
>
id
&&
hash
(
fileName
)
>
pred_id
))
{
ifstream
fin
;
fin
.
open
(
fName
);
...
...
@@ -380,6 +407,7 @@ public:
}
}
}
}
closedir
(
dirFile
);
}
...
...
@@ -387,7 +415,7 @@ public:
for
(
int
i
=
0
;
i
<=
curr_pos
;
i
++
)
{
if
(
hash
(
cacheHeap
[
i
]
->
key
)
<=
newId
||
hash
(
cacheHeap
[
i
]
->
key
)
>
id
)
if
(
(
newId
>
id
&&
hash
(
cacheHeap
[
i
]
->
key
)
<=
newId
&&
hash
(
cacheHeap
[
i
]
->
key
)
>
id
)
||
(
newId
<
id
&&
hash
(
cacheHeap
[
i
]
->
key
)
<=
newId
)
||
(
pred_id
>
id
&&
hash
(
cacheHeap
[
i
]
->
key
)
>
pred_id
)
)
{
toBeDeleted
.
push_back
(
cacheHeap
[
i
]
->
key
);
flush
[
cacheHeap
[
i
]
->
key
]
=
cacheHeap
[
i
]
->
value
;
...
...
LRU.h
View file @
68222a12
...
...
@@ -126,19 +126,21 @@ public:
}
while
(
fin
);
fin
.
close
();
unordered_map
<
string
,
string
>::
iterator
itr
;
for
(
itr
=
flush
.
begin
();
itr
!=
flush
.
end
()
&&
cache
.
size
()
<
capacity
-
1
;
itr
++
)
{
this
->
put
(
itr
->
first
,
itr
->
second
);
flush
[
itr
->
first
]
=
deleted
;
}
if
(
flush
.
find
(
key
)
!=
flush
.
end
())
{
this
->
put
(
key
,
flush
[
key
]);
flush
[
key
]
=
deleted
;
}
unordered_map
<
string
,
string
>::
iterator
itr
;
for
(
itr
=
flush
.
begin
();
itr
!=
flush
.
end
()
&&
cache
.
size
()
<
capacity
;
itr
++
)
{
if
(
itr
->
second
==
deleted
)
continue
;
this
->
put
(
itr
->
first
,
itr
->
second
);
flush
[
itr
->
first
]
=
deleted
;
}
const
char
*
c
=
fileName
.
c_str
();
remove
(
c
);
...
...
@@ -263,7 +265,7 @@ public:
}
// key1;key2;key3;;value1;value2;value3;
string
getKeyValuePairs
(
int
newId
,
int
id
)
string
getKeyValuePairs
(
int
newId
,
int
id
,
int
pred_id
)
{
unordered_map
<
string
,
string
>
flush
;
string
keyValPairs
=
""
;
...
...
@@ -282,7 +284,34 @@ public:
{
fileName
.
erase
(
n
-
4
);
fileName
=
fileName
.
substr
(
2
);
if
(
hash
(
fileName
)
<=
newId
&&
hash
(
fileName
)
>
id
)
if
(
newId
>
id
)
{
//wrapAround
if
(
hash
(
fileName
)
<=
newId
&&
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
);
}
}
else
{
if
(
hash
(
fileName
)
<=
newId
||
(
pred_id
>
id
&&
hash
(
fileName
)
>
pred_id
))
{
ifstream
fin
;
fin
.
open
(
fName
);
...
...
@@ -309,13 +338,14 @@ public:
}
}
}
}
closedir
(
dirFile
);
}
Node
*
temp
=
head
->
next
;
while
(
temp
->
next
)
{
if
(
hash
(
temp
->
key
)
<=
newId
&&
hash
(
temp
->
key
)
>
id
)
if
(
(
newId
>
id
&&
hash
(
temp
->
key
)
<=
newId
&&
hash
(
temp
->
key
)
>
id
)
||
((
hash
(
temp
->
key
)
<=
newId
&&
newId
<
id
)
||
(
pred_id
>
id
&&
hash
(
temp
->
key
)
>
pred_id
))
)
{
flush
[
temp
->
key
]
=
temp
->
payload
;
}
...
...
server.cpp
View file @
68222a12
...
...
@@ -118,12 +118,27 @@ public:
break
;
fingers
[
nums
++
]
=
stoi
(
temp
);
}
while
(
fin
);
fin
.
close
();
int
node
=-
1
;
int
next
=-
1
;
bool
fl
=
false
;
fin
.
close
();
string
my_pred
;
fin
.
open
(
NEIGHBOURS
);
getline
(
fin
,
my_pred
);
getline
(
fin
,
my_pred
);
fin
.
close
();
int
my_pred_id
;
int
my_id
=
stoi
(
params
.
find
(
"LISTENING_PORT"
)
->
second
);
if
(
nums
>
0
&&
fingers
[
nums
-
1
]
<
id
&&
my_id
>=
id
)
{
if
(
my_pred
==
"-1"
)
my_pred_id
=-
1
;
else
my_pred_id
=
stoi
(
my_pred
.
substr
(
my_pred
.
find
(
':'
)
+
1
));
if
(
my_pred_id
!=-
1
&&
my_pred_id
<
id
&&
my_id
>
id
)
{
node
=
my_pred_id
;
next
=
my_id
;
}
else
if
(
nums
>
0
&&
fingers
[
nums
-
1
]
<
id
&&
my_id
>=
id
)
{
node
=
fingers
[
nums
-
1
];
next
=
my_id
;
}
...
...
@@ -348,17 +363,29 @@ public:
//return half of the keyvalue pairs to the requesting node
string
address
=
info
.
address
();
int
id
=
stoi
(
address
.
substr
(
address
.
find
(
':'
)
+
1
));
string
keyvalues
=
memManager
->
getKeyValuePairs
(
id
,
stoi
(
params
[
"LISTENING_PORT"
]));
string
keys
=
keyvalues
.
substr
(
0
,
keyvalues
.
find
(
";;"
)
+
1
);
string
values
=
keyvalues
.
substr
(
keyvalues
.
find
(
";;"
)
+
2
);
cout
<<
"Keys: "
<<
keys
<<
endl
;
cout
<<
"Values: "
<<
values
<<
endl
;
ifstream
fin
;
fin
.
open
(
NEIGHBOURS
);
string
successor
,
predecessor
;
getline
(
fin
,
successor
);
getline
(
fin
,
predecessor
);
fin
.
close
();
int
pred_id
;
if
(
predecessor
==
"-1"
)
pred_id
=-
1
;
else
pred_id
=
stoi
(
predecessor
.
substr
(
predecessor
.
find
(
':'
)
+
1
));
string
keyvalues
=
memManager
->
getKeyValuePairs
(
id
,
stoi
(
params
[
"LISTENING_PORT"
]),
pred_id
);
string
keys
,
values
;
if
(
keyvalues
==
";"
)
{
keys
=
"null"
;
values
=
"null"
;
}
else
{
keys
=
keyvalues
.
substr
(
0
,
keyvalues
.
find
(
";;"
)
+
1
);
values
=
keyvalues
.
substr
(
keyvalues
.
find
(
";;"
)
+
2
);
}
cout
<<
"Keys: "
<<
keys
<<
endl
;
cout
<<
"Values: "
<<
values
<<
endl
;
predecessor
=
info
.
address
();
ofstream
fout
;
fout
.
open
(
NEIGHBOURS
);
...
...
@@ -391,7 +418,23 @@ public:
bool
fl
=
false
;
int
next
=-
1
;
int
my_id
=
stoi
(
params
.
find
(
"LISTENING_PORT"
)
->
second
);
if
(
nums
>
0
&&
fingers
[
nums
-
1
]
<
id
&&
my_id
>=
id
)
{
fin
.
close
();
string
my_pred
;
fin
.
open
(
NEIGHBOURS
);
getline
(
fin
,
my_pred
);
getline
(
fin
,
my_pred
);
fin
.
close
();
int
my_pred_id
;
if
(
my_pred
==
"-1"
)
my_pred_id
=-
1
;
else
my_pred_id
=
stoi
(
my_pred
.
substr
(
my_pred
.
find
(
':'
)
+
1
));
if
(
my_pred_id
!=-
1
&&
my_pred_id
<
id
&&
my_id
>
id
)
{
node
=
my_pred_id
;
next
=
my_id
;
}
else
if
(
nums
>
0
&&
fingers
[
nums
-
1
]
<
id
&&
my_id
>=
id
)
{
node
=
fingers
[
nums
-
1
];
next
=
my_id
;
}
...
...
@@ -720,7 +763,23 @@ public:
bool
fl
=
false
;
int
next
=-
1
;
int
my_id
=
stoi
(
params
.
find
(
"LISTENING_PORT"
)
->
second
);
if
(
nums
>
0
&&
fingers
[
nums
-
1
]
<
key_id
&&
my_id
>=
key_id
)
{
fin
.
close
();
string
my_pred
;
fin
.
open
(
NEIGHBOURS
);
getline
(
fin
,
my_pred
);
getline
(
fin
,
my_pred
);
fin
.
close
();
int
my_pred_id
;
if
(
my_pred
==
"-1"
)
my_pred_id
=-
1
;
else
my_pred_id
=
stoi
(
my_pred
.
substr
(
my_pred
.
find
(
':'
)
+
1
));
if
(
my_pred_id
!=-
1
&&
my_pred_id
<
key_id
&&
my_id
>
key_id
)
{
node
=
my_pred_id
;
next
=
my_id
;
}
else
if
(
nums
>
0
&&
fingers
[
nums
-
1
]
<
key_id
&&
my_id
>=
key_id
)
{
node
=
fingers
[
nums
-
1
];
next
=
my_id
;
}
...
...
@@ -918,7 +977,23 @@ public:
bool
fl
=
false
;
int
next
=-
1
;
int
my_id
=
stoi
(
params
.
find
(
"LISTENING_PORT"
)
->
second
);
if
(
nums
>
0
&&
fingers
[
nums
-
1
]
<
key_id
&&
my_id
>=
key_id
)
{
fin
.
close
();
string
my_pred
;
fin
.
open
(
NEIGHBOURS
);
getline
(
fin
,
my_pred
);
getline
(
fin
,
my_pred
);
fin
.
close
();
int
my_pred_id
;
if
(
my_pred
==
"-1"
)
my_pred_id
=-
1
;
else
my_pred_id
=
stoi
(
my_pred
.
substr
(
my_pred
.
find
(
':'
)
+
1
));
if
(
my_pred_id
!=-
1
&&
my_pred_id
<
key_id
&&
my_id
>
key_id
)
{
node
=
my_pred_id
;
next
=
my_id
;
}
else
if
(
nums
>
0
&&
fingers
[
nums
-
1
]
<
key_id
&&
my_id
>=
key_id
)
{
node
=
fingers
[
nums
-
1
];
next
=
my_id
;
}
...
...
@@ -1102,7 +1177,23 @@ public:
bool
fl
=
false
;
int
next
=-
1
;
int
my_id
=
stoi
(
params
.
find
(
"LISTENING_PORT"
)
->
second
);
if
(
nums
>
0
&&
fingers
[
nums
-
1
]
<
key_id
&&
my_id
>=
key_id
)
{
fin
.
close
();
string
my_pred
;
fin
.
open
(
NEIGHBOURS
);
getline
(
fin
,
my_pred
);
getline
(
fin
,
my_pred
);
fin
.
close
();
int
my_pred_id
;
if
(
my_pred
==
"-1"
)
my_pred_id
=-
1
;
else
my_pred_id
=
stoi
(
my_pred
.
substr
(
my_pred
.
find
(
':'
)
+
1
));
if
(
my_pred_id
!=-
1
&&
my_pred_id
<
key_id
&&
my_id
>
key_id
)
{
node
=
my_pred_id
;
next
=
my_id
;
}
else
if
(
nums
>
0
&&
fingers
[
nums
-
1
]
<
key_id
&&
my_id
>=
key_id
)
{
node
=
fingers
[
nums
-
1
];
next
=
my_id
;
}
...
...
@@ -1288,7 +1379,23 @@ public:
bool
fl
=
false
;
int
next
=-
1
;
int
my_id
=
stoi
(
params
.
find
(
"LISTENING_PORT"
)
->
second
);
if
(
nums
>
0
&&
fingers
[
nums
-
1
]
<
key_id
&&
my_id
>=
key_id
)
{
fin
.
close
();
string
my_pred
;
fin
.
open
(
NEIGHBOURS
);
getline
(
fin
,
my_pred
);
getline
(
fin
,
my_pred
);
fin
.
close
();
int
my_pred_id
;
if
(
my_pred
==
"-1"
)
my_pred_id
=-
1
;
else
my_pred_id
=
stoi
(
my_pred
.
substr
(
my_pred
.
find
(
':'
)
+
1
));
if
(
my_pred_id
!=-
1
&&
my_pred_id
<
key_id
&&
my_id
>
key_id
)
{
node
=
my_pred_id
;
next
=
my_id
;
}
else
if
(
nums
>
0
&&
fingers
[
nums
-
1
]
<
key_id
&&
my_id
>=
key_id
)
{
node
=
fingers
[
nums
-
1
];
next
=
my_id
;
}
...
...
@@ -1468,7 +1575,22 @@ public:
bool
fl
=
false
;
int
next
=-
1
;
int
my_id
=
stoi
(
params
.
find
(
"LISTENING_PORT"
)
->
second
);
if
(
nums
>
0
&&
fingers
[
nums
-
1
]
<
key_id
&&
my_id
>=
key_id
)
{
fin
.
close
();
string
my_pred
;
fin
.
open
(
NEIGHBOURS
);
getline
(
fin
,
my_pred
);
getline
(
fin
,
my_pred
);
fin
.
close
();
int
my_pred_id
;
if
(
my_pred
==
"-1"
)
my_pred_id
=-
1
;
else
my_pred_id
=
stoi
(
my_pred
.
substr
(
my_pred
.
find
(
':'
)
+
1
));
if
(
my_pred_id
!=-
1
&&
my_pred_id
<
key_id
&&
my_id
>
key_id
)
{
node
=
my_pred_id
;
next
=
my_id
;
}
else
if
(
nums
>
0
&&
fingers
[
nums
-
1
]
<
key_id
&&
my_id
>=
key_id
)
{
node
=
fingers
[
nums
-
1
];
next
=
my_id
;
}
...
...
@@ -1665,7 +1787,23 @@ public:
bool
fl
=
false
;
int
next
=-
1
;
int
my_id
=
stoi
(
params
.
find
(
"LISTENING_PORT"
)
->
second
);
if
(
nums
>
0
&&
fingers
[
nums
-
1
]
<
key_id
&&
my_id
>=
key_id
)
{
fin
.
close
();
string
my_pred
;
fin
.
open
(
NEIGHBOURS
);
getline
(
fin
,
my_pred
);
getline
(
fin
,
my_pred
);
fin
.
close
();
int
my_pred_id
;
if
(
my_pred
==
"-1"
)
my_pred_id
=-
1
;
else
my_pred_id
=
stoi
(
my_pred
.
substr
(
my_pred
.
find
(
':'
)
+
1
));
if
(
my_pred_id
!=-
1
&&
my_pred_id
<
key_id
&&
my_id
>
key_id
)
{
node
=
my_pred_id
;
next
=
my_id
;
}
else
if
(
nums
>
0
&&
fingers
[
nums
-
1
]
<
key_id
&&
my_id
>=
key_id
)
{
node
=
fingers
[
nums
-
1
];
next
=
my_id
;
}
...
...
@@ -2023,6 +2161,7 @@ void register_server_DNS(string my_address) {
status
=
stub
->
INFORMSUCCESSOR
(
&
context2
,
info
,
&
keyValues
);
string
keys
=
keyValues
.
keys
();
string
values
=
keyValues
.
values
();
if
(
keys
!=
"null"
)
{
while
(
true
)
{
cout
<<
"Keys: "
<<
keys
<<
endl
;
cout
<<
"Values: "
<<
values
<<
endl
;
...
...
@@ -2036,6 +2175,7 @@ void register_server_DNS(string my_address) {
keys
=
keys
.
substr
(
keys
.
find
(
';'
)
+
1
);
values
=
values
.
substr
(
values
.
find
(
';'
)
+
1
);
}
}
channel
=
grpc
::
CreateChannel
(
predecessor
,
grpc
::
InsecureChannelCredentials
());
stub
=
KeyValueServices
::
NewStub
(
channel
);
info
.
set_address
(
my_address
);
...
...
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