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
2cff26e1
Commit
2cff26e1
authored
Nov 09, 2020
by
Roshan Rabinarayan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
complete code for del,put,get for file
parent
c5045548
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
43 additions
and
24 deletions
+43
-24
temp.c
temp.c
+43
-24
No files found.
temp.c
View file @
2cff26e1
...
...
@@ -33,20 +33,23 @@ int modulus(char *num, int size, int divisor) {
void
file_del
(
off_t
offset
,
char
*
key
)
{
int
index
=
modulus
(
key
,
256
,
setSize
);
fflush
(
stdout
);
int
length
=
0
;
sem_wait
(
&
mutex
[
index
]);
char
blankspace
[
512
];
memset
(
blankspace
,
0
,
512
);
char
ch
;
int
k
=-
1
;
lseek
(
fds
[
index
],
offset
,
SEEK_SET
);
write
(
fds
[
index
],
blankspace
,
512
);
sem_wait
(
&
readerLocks
[
index
]);
readercount
--
;
if
(
readercount
==
0
)
while
(
read
(
fds
[
index
],
&
ch
,
sizeof
(
ch
))
!=-
1
&&
ch
!=
'\n'
)
{
sem_post
(
&
mutex
[
index
])
;
length
++
;
}
sem_post
(
&
readerLocks
[
index
]);
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
(
off_t
offset
,
char
*
key
,
char
*
value
)
{
...
...
@@ -55,18 +58,33 @@ void file_get(off_t offset, char *key, char *value)
int
index
=
modulus
(
key
,
256
,
setSize
);
sem_wait
(
&
readerLocks
[
index
]);
readCounters
[
index
]
+
+
;
readCounters
[
index
]
+
=
1
;
if
(
readCounters
[
index
]
==
1
)
sem_wait
(
&
mutex
[
index
]);
sem_post
(
&
readerLocks
[
index
]);
lseek
(
fds
[
index
],
offset
,
SEEK_SET
);
read
(
fds
[
index
],
key
,
256
);
read
(
fds
[
index
],
value
,
256
);
char
line
[
10
];
//FILE *fp =fdopen(fds[index],"r+");
// fseek(fp, offset,SEEK_SET);
size_t
len
=
0
;
char
ch
;
int
k
=-
1
;
while
(
read
(
fds
[
index
],
&
ch
,
sizeof
(
ch
))
!=-
1
&&
ch
!=
'\n'
)
{
if
(
k
>=
0
)
{
value
[
k
++
]
=
ch
;
}
if
(
ch
==
':'
)
{
k
=
0
;
}
}
sem_wait
(
&
readerLocks
[
index
]);
read
ercount
--
;
if
(
read
ercount
==
0
)
read
Counters
[
index
]
-=
1
;
if
(
read
Counters
[
index
]
==
0
)
{
sem_post
(
&
mutex
[
index
]);
}
...
...
@@ -82,9 +100,10 @@ off_t file_put(char *key,char *value) {
sem_wait
(
&
mutex
[
index
]);
printf
(
"[Write to File: %d]
\n
"
,
index
);
position
=
lseek
(
fds
[
index
],
0
,
SEEK_END
);
write
(
fds
[
index
],
key
,
256
);
write
(
fds
[
index
],
value
,
256
);
printf
(
"bytes : %d
\n
"
,
bytes
);
char
line
[
514
];
snprintf
(
line
,
sizeof
(
line
),
"%s:%s
\n
"
,
key
,
value
);
if
(
write
(
fds
[
index
],
line
,
strlen
(
line
))
<
0
)
printf
(
"
\n
[unable to perform file_put]
\n
"
);
sem_post
(
&
mutex
[
index
]);
return
position
;
...
...
@@ -101,7 +120,7 @@ int main()
int
n2
=
0
;
char
prevkey
[
256
]
=
"24"
;
char
key
[
256
]
=
"24sadasdasdasdasdsad"
;
char
value
[
256
]
=
"value
asdasdad
"
;
char
value
[
256
]
=
"value
2
"
;
off_t
offset
;
bzero
(
key
+
strlen
(
key
),
sizeof
(
key
)
-
strlen
(
key
));
bzero
(
value
+
strlen
(
value
),
sizeof
(
value
)
-
strlen
(
value
));
...
...
@@ -114,7 +133,7 @@ int main()
for
(
i
=
0
;
i
<
setSize
;
i
++
)
{
snprintf
(
fileName
,
sizeof
(
fileName
),
"File%d.txt"
,
i
);
fds
[
i
]
=
open
(
fileName
,
O_CREAT
|
O_RDWR
|
O_APPEND
,
S_IRWXU
);
fds
[
i
]
=
open
(
fileName
,
O_CREAT
|
O_RDWR
,
S_IRWXU
);
if
(
fds
[
i
]
<
0
)
{
printf
(
"
\n
[Unable to Open File%d.txt]
\n
"
,
i
);
...
...
@@ -125,10 +144,10 @@ int main()
}
offset
=
file_put
(
prevkey
,
value
);
offset
=
file_put
(
prevkey
,
"value1"
);
offset
=
file_put
(
key
,
value
);
file_get
(
0
,
prevkey
,
value
);
// Doesnot depend on key arg, returns key and value at offset 0
file_get
(
9
,
prevkey
,
value
);
// Doesnot depend on key arg, returns key and value at offset 0
printf
(
"%s
\n
"
,
value
);
file_del
(
0
,
prevkey
);
file_del
(
offset
,
prevkey
);
}
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