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
481b3be8
Commit
481b3be8
authored
Oct 27, 2020
by
Shivaji
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added sample LRU
parent
4174ea50
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
186 additions
and
0 deletions
+186
-0
LRU.cpp
LRU.cpp
+186
-0
No files found.
LRU.cpp
0 → 100644
View file @
481b3be8
#include <iostream>
#include <string>
#include <vector>
#include <pthread.h>
#include <map>
#include <bits/stdc++.h>
struct
KV
{
char
*
key
;
char
*
value
;
bool
valid
;
bool
modified
;
};
typedef
struct
KV
KV
;
using
namespace
std
;
int
MAX_SIZE
=
10
;
map
<
char
*
,
int
>
mp
;
vector
<
KV
*>
stor
;
deque
<
char
*>
dq
;
void
remove_element_from_deque
(
char
*
key
)
{
for
(
auto
it
=
dq
.
begin
();
it
!=
dq
.
end
();
it
++
)
{
if
(
*
it
==
key
)
{
dq
.
erase
(
it
);
break
;
}
}
}
int
find_empty_location
()
{
for
(
int
i
=
0
;
i
<
MAX_SIZE
;
i
++
)
{
if
(
stor
[
i
]
->
valid
==
0
)
return
i
;
}
}
void
delet
(
char
*
key
)
{
remove_element_from_deque
(
key
);
int
indx
=
mp
[
key
];
mp
.
erase
(
key
);
stor
[
indx
]
->
valid
=
0
;
//TODO remove key from file also
}
void
put
(
char
*
key
,
char
*
value
)
{
int
indx
=-
1
;
if
(
mp
.
find
(
key
)
!=
mp
.
end
())
{
indx
=
mp
[
key
];
remove_element_from_deque
(
key
);
}
else
{
indx
=
find_empty_location
();
mp
[
key
]
=
indx
;
// TODO should write to file if modified is true
// replacment from cache
}
stor
[
indx
]
->
key
=
key
;
stor
[
indx
]
->
value
=
value
;
stor
[
indx
]
->
valid
=
1
;
stor
[
indx
]
->
modified
=
1
;
dq
.
push_front
(
key
);
}
char
*
get
(
char
*
key
)
{
if
(
mp
.
find
(
key
)
!=
mp
.
end
())
{
int
indx
=
mp
[
key
];
remove_element_from_deque
(
key
);
dq
.
push_front
(
key
);
return
stor
[
indx
]
->
value
;
}
return
(
char
*
)
"NOT FOUND"
;
}
int
main
()
{
// stor.resize(10);
stor
.
resize
(
10
);
int
i
=
0
;
for
(
int
j
=
0
;
j
<
MAX_SIZE
;
j
++
)
{
stor
[
j
]
=
(
KV
*
)
malloc
(
sizeof
(
KV
));
stor
[
j
]
->
valid
=
false
;
}
char
key1
[
250
]
=
"Hello world1"
;
char
value1
[
256
]
=
"THIS IS Vlaue1"
;
char
key2
[
250
]
=
"Hello world2"
;
char
value2
[
256
]
=
"THIS IS Vlaue2"
;
char
key3
[
250
]
=
"Hello world3"
;
char
value3
[
256
]
=
"THIS IS Vlaue3"
;
char
key4
[
250
]
=
"Hello world4"
;
char
value4
[
256
]
=
"THIS IS Vlaue4"
;
char
key5
[
250
]
=
"Hello world5"
;
char
value5
[
256
]
=
"THIS IS Vlaue5"
;
char
key6
[
250
]
=
"Hello world6"
;
char
value6
[
256
]
=
"THIS IS Vlaue6"
;
KV
*
temp1
=
(
KV
*
)
malloc
(
sizeof
(
KV
));
KV
*
temp2
=
(
KV
*
)
malloc
(
sizeof
(
KV
));
KV
*
temp3
=
(
KV
*
)
malloc
(
sizeof
(
KV
));
KV
*
temp4
=
(
KV
*
)
malloc
(
sizeof
(
KV
));
KV
*
temp5
=
(
KV
*
)
malloc
(
sizeof
(
KV
));
KV
*
temp6
=
(
KV
*
)
malloc
(
sizeof
(
KV
));
temp1
->
key
=
key1
;
temp1
->
value
=
value1
;
temp1
->
valid
=
1
;
temp2
->
key
=
key2
;
temp2
->
value
=
value2
;
temp2
->
valid
=
1
;
temp3
->
key
=
key3
;
temp3
->
value
=
value3
;
temp3
->
valid
=
1
;
temp4
->
key
=
key4
;
temp4
->
value
=
value4
;
temp4
->
valid
=
1
;
temp5
->
key
=
key5
;
temp5
->
value
=
value5
;
temp5
->
valid
=
1
;
temp6
->
key
=
key6
;
temp6
->
value
=
value6
;
temp6
->
valid
=
1
;
stor
[
i
++
]
=
(
temp1
);
stor
[
i
++
]
=
(
temp2
);
stor
[
i
++
]
=
(
temp3
);
stor
[
i
++
]
=
(
temp4
);
stor
[
i
++
]
=
(
temp5
);
stor
[
i
++
]
=
(
temp6
);
mp
.
insert
({
key6
,
5
});
mp
.
insert
({
key1
,
0
});
mp
.
insert
({
key2
,
1
});
mp
.
insert
({
key3
,
2
});
mp
.
insert
({
key4
,
3
});
mp
.
insert
({
key5
,
4
});
cout
<<
mp
.
size
()
<<
endl
;
cout
<<
stor
[
0
]
->
key
<<
" "
<<
stor
[
0
]
->
value
<<
endl
;
cout
<<
stor
[
1
]
->
key
<<
" "
<<
stor
[
1
]
->
value
<<
endl
;
cout
<<
stor
[
2
]
->
key
<<
" "
<<
stor
[
2
]
->
value
<<
endl
;
cout
<<
stor
[
3
]
->
key
<<
" "
<<
stor
[
3
]
->
value
<<
endl
;
cout
<<
stor
[
4
]
->
key
<<
" "
<<
stor
[
4
]
->
value
<<
endl
;
cout
<<
stor
[
5
]
->
key
<<
" "
<<
stor
[
5
]
->
value
<<
endl
;
cout
<<
get
(
stor
[
5
]
->
key
)
<<
endl
;
put
(
stor
[
5
]
->
key
,
(
char
*
)
"This is a good"
);
cout
<<
get
(
stor
[
5
]
->
key
)
<<
endl
;
delet
(
stor
[
5
]
->
key
);
delet
(
stor
[
0
]
->
key
);
put
((
char
*
)
"Hello World7"
,
(
char
*
)
"THis is vaue7"
);
cout
<<
stor
[
0
]
->
key
<<
" "
<<
stor
[
0
]
->
value
<<
"
\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