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
a78ebd73
Commit
a78ebd73
authored
Nov 13, 2020
by
Saikumar
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
modified persistent cache
parent
fea09cd8
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
50 additions
and
47 deletions
+50
-47
cache.cpp
cache.cpp
+25
-27
persistentStorage.cpp
persistentStorage.cpp
+25
-20
No files found.
cache.cpp
View file @
a78ebd73
...
...
@@ -35,8 +35,18 @@ using namespace std;
list
<
cacheNode
>
cache
;
unordered_map
<
string
,
list
<
cacheNode
>
::
iterator
>
hashTable
;
// pthread_mutex_t cLock, pLock;
pthread_mutex_t
cLock
;
void
initializeCache
()
{
pthread_mutex_init
(
&
cLock
,
NULL
);
// pthread_mutex_init(&pLock, NULL);
initializeStorageVariables
();
}
/*
* Returns 1 if key-value pair is present in cache and 0 otherwise
* If key is present, brings correspoding node to front of the cache(To identify recently used element)
...
...
@@ -93,34 +103,29 @@ void insert(string key, string value) {
}
string
GET
(
string
key
)
{
//
printf("GET function\n");
printf
(
"GET function
\n
"
);
pthread_mutex_lock
(
&
cLock
);
string
ans
=
search
(
key
);
pthread_mutex_unlock
(
&
cLock
);
/*need to add ravi code*/
if
(
ans
==
""
)
{
// char * new_key = new char[key.size() + 1];
// copy(key.begin(), key.end(), new_key);
// new_key[key.size()] = '\0';
// string value = GETT(new_key);
// if(value[0] != (char)240) {
// // cLock
// pthread_mutex_lock(&cLock);
// insert(key, value);
// pthread_mutex_unlock(&cLock);
// //uncLock
// }
// return value;
return
"ERROR"
;
}
char
*
new_key
=
new
char
[
key
.
size
()
+
1
];
copy
(
key
.
begin
(),
key
.
end
(),
new_key
);
new_key
[
key
.
size
()]
=
'\0'
;
string
value
=
GETT
(
new_key
);
if
(
value
[
0
]
!=
(
char
)
240
)
{
pthread_mutex_lock
(
&
cLock
);
insert
(
key
,
value
);
pthread_mutex_unlock
(
&
cLock
);
}
return
value
;
}
return
ans
;
}
string
PUT
(
string
key
,
string
value
)
{
// cout << "PUT started" << endl;
pthread_mutex_lock
(
&
cLock
);
string
ans
=
search
(
key
);
if
(
ans
==
""
)
{
...
...
@@ -131,14 +136,13 @@ string PUT(string key, string value) {
it
->
second
->
value
=
value
;
}
pthread_mutex_unlock
(
&
cLock
);
// uncLock
char
*
new_key
=
new
char
[
key
.
size
()
+
1
];
copy
(
key
.
begin
(),
key
.
end
(),
new_key
);
new_key
[
key
.
size
()]
=
'\0'
;
char
*
new_value
=
new
char
[
value
.
size
()
+
1
];
copy
(
value
.
begin
(),
value
.
end
(),
new_value
);
new_value
[
value
.
size
()]
=
'\0'
;
//
PUTT(new_key, new_value);
PUTT
(
new_key
,
new_value
);
string
s
(
1
,
(
char
)
200
);
string
response
=
s
+
"Put Completed Successfully"
;
return
response
;
...
...
@@ -148,8 +152,6 @@ string PUT(string key, string value) {
* Delete from cache
*/
string
DEL
(
string
key
)
{
/*Need to add ravi code*/
//cLock
pthread_mutex_lock
(
&
cLock
);
string
ans
=
search
(
key
);
if
(
ans
!=
""
)
{
...
...
@@ -157,16 +159,12 @@ string DEL(string key) {
list
<
cacheNode
>
::
iterator
cnode_it
=
it
->
second
;
cache
.
erase
(
cnode_it
);
hashTable
.
erase
(
key
);
pthread_mutex_unlock
(
&
cLock
);
return
"SUCCESS"
;
}
//uncLock
pthread_mutex_unlock
(
&
cLock
);
char
*
new_key
=
new
char
[
key
.
size
()
+
1
];
copy
(
key
.
begin
(),
key
.
end
(),
new_key
);
new_key
[
key
.
size
()]
=
'\0'
;
// string response=DELL(new_key);
string
response
=
"ERROR"
;
string
response
=
DELL
(
new_key
);
return
response
;
}
...
...
persistentStorage.cpp
View file @
a78ebd73
...
...
@@ -40,19 +40,34 @@ struct n2
struct
n1
*
h1
=
NULL
;
struct
n2
*
h2
=
NULL
;
unsigned
int
nooffiles
=
0
;
void
initializeStorageVariables
()
{
pthread_mutex_init
(
&
m
,
NULL
);
sem_init
(
&
w
,
0
,
1
);
}
struct
n2
*
check
(
struct
n2
*
h2
){
while
(
1
)
{
if
(
h2
==
NULL
){
return
NULL
;
}
else
{
if
((
h2
->
nooffreeline
)
>
0
){
return
h2
;}
else
{
h2
=
h2
->
pointer
;
if
(
h2
==
NULL
)
{
return
NULL
;
}
else
{
if
((
h2
->
nooffreeline
)
>
0
)
{
return
h2
;
}
else
{
h2
=
h2
->
pointer
;
}
}
}
}
...
...
@@ -642,7 +657,7 @@ string GETT(char * key){
void
PUTT
(
char
*
key
,
char
*
value
){
sem_wait
(
&
w
);
printf
(
"key = %s
\n
value=%s"
,
key
,
value
);
//
printf("key = %s\nvalue=%s",key, value);
h1
=
insert
(
h1
,
key
);
char
filename2
[
10
];
strcpy
(
filename2
,
add
->
filename
);
...
...
@@ -800,16 +815,6 @@ void pp2(struct n1*r){
// int main()
// {
// printf("Saikumar\n");
// 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