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
2ac11a25
Commit
2ac11a25
authored
Nov 09, 2020
by
Roshan Rabinarayan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
complete persistensce storage
parent
3353b49b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
80 additions
and
51 deletions
+80
-51
temp.c
temp.c
+80
-51
No files found.
temp.c
View file @
2ac11a25
...
...
@@ -21,41 +21,51 @@ pthread_t tid;
pthread_t
writerthreads
[
100
],
readerthreads
[
100
];
int
readercount
=
0
;
int
modulus
(
char
*
num
,
int
size
,
int
divisor
)
{
int
rem
=
0
;
unsigned
modulus
(
unsigned
char
*
num
,
size_t
size
,
unsigned
divisor
)
{
unsigned
rem
=
0
;
while
(
size
--
>
0
)
{
rem
=
((
UCHAR_MAX
+
1
)
*
rem
+
*
num
)
%
divisor
;
rem
=
((
UCHAR_MAX
+
1
ULL
)
*
rem
+
*
num
)
%
divisor
;
num
++
;
}
return
rem
;
}
void
file_del
(
off_t
offset
,
char
*
key
)
void
file_del
(
char
*
key
)
{
int
index
=
modulus
(
key
,
256
,
setSize
);
fflush
(
stdout
);
int
length
=
0
;
sem_wait
(
&
mutex
[
index
]);
char
blankspace
[
512
];
char
ch
;
int
k
=-
1
;
lseek
(
fds
[
index
],
offset
,
SEEK_SET
);
while
(
read
(
fds
[
index
],
&
ch
,
sizeof
(
ch
))
!=-
1
&&
ch
!=
'\n'
)
{
length
++
;
memset
(
blankspace
,
0
,
512
);
char
*
line
;
char
temp
[
514
];
size_t
len
=
0
;
off_t
position
=
0
;
FILE
*
fp
=
fdopen
(
fds
[
index
],
"rb+"
);
while
((
getline
(
&
line
,
&
len
,
fp
))
!=
-
1
)
{
memcpy
(
temp
,
line
,
strlen
(
line
)
-
1
);
char
*
fkey
=
strtok
(
line
,
":"
);
char
*
fvalue
=
strtok
(
NULL
,
":"
);
if
(
strcmp
(
key
,
fkey
)
==
0
){
lseek
(
fds
[
index
],
position
,
SEEK_SET
);
write
(
fds
[
index
],
blankspace
,
strlen
(
temp
));
puts
(
temp
);
break
;
}
position
=
ftell
(
fp
);
}
printf
(
"length %d"
,
length
);
memset
(
blankspace
,
0
,
length
);
lseek
(
fds
[
index
],
offset
,
SEEK_SET
);
write
(
fds
[
index
],
blankspace
,
length
);
sem_post
(
&
mutex
[
index
]);
}
void
file_get
(
char
*
key
,
char
*
value
)
int
file_get
(
char
*
key
,
char
*
value
)
{
/* Gets the value stored at offset */
/* Does not depend on key argument */
int
found
=
0
;
int
index
=
modulus
(
key
,
256
,
setSize
);
sem_wait
(
&
readerLocks
[
index
]);
readCounters
[
index
]
+=
1
;
...
...
@@ -63,20 +73,15 @@ void file_get(char *key, char *value)
sem_wait
(
&
mutex
[
index
]);
sem_post
(
&
readerLocks
[
index
]);
char
*
line
;
//FILE *fp =fdopen(fds[index],"r+");
// fseek(fp, offset,SEEK_SET);
size_t
len
=
0
;
char
ch
;
int
k
=-
1
;
FILE
*
fp
=
fdopen
(
fds
[
index
],
"r+"
);
FILE
*
fp
=
fdopen
(
fds
[
index
],
"rb+"
);
while
((
getline
(
&
line
,
&
len
,
fp
))
!=
-
1
)
{
char
*
fkey
=
strtok
(
line
,
":"
);
char
*
fvalue
=
strtok
(
NULL
,
":"
);
if
(
strcmp
(
key
,
fkey
)
==
0
){
memcpy
(
value
,
fvalue
,
strlen
(
fvalue
));
memcpy
(
value
,
fvalue
,
strlen
(
fvalue
)
-
1
);
found
=
1
;
break
;
}
...
...
@@ -88,31 +93,57 @@ void file_get(char *key, char *value)
sem_post
(
&
mutex
[
index
]);
}
sem_post
(
&
readerLocks
[
index
]);
return
found
;
}
off_t
file_put
(
char
*
key
,
char
*
value
)
{
int
index
=
modulus
(
key
,
256
,
setSize
);
int
bytes
,
rembytes
,
i
;
off_t
file_put
(
char
*
key
,
char
*
value
)
{
/*
if found then ask the offset where it is present and if the value noy matches with the present value ,update the given line
if not present then search for empty line and insert there!
*/
unsigned
int
index
=
modulus
(
key
,
256
,
setSize
);
off_t
position
;
sem_wait
(
&
mutex
[
index
]);
//sem_wait(&mutex[index]);
printf
(
"[Write to File: %d]
\n
"
,
index
);
position
=
lseek
(
fds
[
index
],
0
,
SEEK_END
);
char
line
[
514
];
snprintf
(
line
,
sizeof
(
line
),
"%s:%s
\n
"
,
key
,
value
);
if
(
write
(
fds
[
index
],
line
,
strlen
(
line
))
<
0
)
char
*
line
;
char
lin
[
514
];
size_t
len
=
0
;
FILE
*
fp
=
fdopen
(
fds
[
index
],
"rb+"
);
position
=
ftell
(
fp
);
while
((
getline
(
&
line
,
&
len
,
fp
))
!=
-
1
)
{
char
*
fkey
=
strtok
(
line
,
":"
);
char
*
fvalue
=
strtok
(
NULL
,
":"
);
if
(
strcmp
(
key
,
fkey
)
==
0
){
if
(
strcmp
(
value
,
strtok
(
fvalue
,
"
\n
"
))
==
0
)
{
printf
(
"(%s)(%s)
\n
"
,
fkey
,
key
);
///everything same then why insert?
return
0
;
}
else
{
printf
(
"key:(%s)(%s)
\n
"
,
fkey
,
key
);
printf
(
"value:(%s)(%s)
\n
"
,
fvalue
,
value
);
fflush
(
stdout
);
fseek
(
fp
,
position
,
SEEK_SET
);
snprintf
(
lin
,
sizeof
(
lin
),
"%s:%s
\n
"
,
key
,
value
);
if
(
fputs
(
lin
,
fp
)
<
0
)
printf
(
"
\n
[unable to perform file_put]
\n
"
);
return
0
;
}
}
position
=
ftell
(
fp
);
}
snprintf
(
lin
,
sizeof
(
lin
),
"%s:%s
\n
"
,
key
,
value
);
if
(
write
(
fds
[
index
],
lin
,
strlen
(
lin
))
<
0
)
printf
(
"
\n
[unable to perform file_put]
\n
"
);
sem_post
(
&
mutex
[
index
]);
return
position
;
}
void
file_search
(
char
*
key
,
char
*
value
)
{
int
index
=
modulus
(
key
,
256
,
setSize
);
// sem_post(&mutex[index]);
return
position
;
}
int
main
()
{
...
...
@@ -125,7 +156,7 @@ int main()
*/
int
n2
=
0
;
char
prevkey
[
256
]
=
"24"
;
char
key
[
256
]
=
"2
4sadasdasdasdasdsad
"
;
char
key
[
256
]
=
"2
5
"
;
char
value
[
256
]
=
"value2"
;
off_t
offset
;
bzero
(
key
+
strlen
(
key
),
sizeof
(
key
)
-
strlen
(
key
));
...
...
@@ -148,12 +179,10 @@ int main()
sem_init
(
&
mutex
[
i
],
0
,
1
);
readCounters
[
i
]
=
0
;
}
offset
=
file_put
(
prevkey
,
"value1"
);
offset
=
file_put
(
key
,
value
);
file_get
(
prevkey
,
value
);
// Doesnot depend on key arg, returns key and value at offset 0
printf
(
"%s
\n
"
,
value
);
//file_del(offset, prevkey);
//offset = file_put("24", "value245");
//offset = file_put("40", "value28");
//file_get(prevkey, value); // Doesnot depend on key arg, returns key and value at offset 0
//printf("(%s)", value);
file_del
(
"29"
);
}
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