Commit 748bdb91 authored by AXEL JAMES's avatar AXEL JAMES Committed by Bhavesh Yadav

stage1

parent 97ecdd95
......@@ -47,6 +47,7 @@ bool dumpToFile(char* key, char* val){
/////////////////add/////////////////
int setNo = getFileId(key);
printf("ch1 %d",setNo);
/////////////////add end/////////////////
char fileName[10];
......@@ -60,11 +61,36 @@ bool dumpToFile(char* key, char* val){
pthread_mutex_lock(&(store.locks[setNo]));
/////////////////add end/////////////////
FILE *fp = fopen(fileName,"a");
FILE *fp = fopen(fileName,"r");
FILE *newFp = fopen(tempfileName,"w");
char* storedKey = malloc(sizeof(char)*257);
char* storedVal = malloc(sizeof(char)*256*1024+1);
while(1){
// char* storedVal;
int count=0;
// while((getc(fp))!=EOF)
// {
// // char buff[1024];
// fgets(stored, 256*1024+1, (FILE*)fp);
// puts(stored);
// storedKey = strtok(storedVal,",");
// if(!strcmp(key,storedKey)) {
// keyExists = true;
// fprintf(newFp,"%s,%s\n",key,val);
// }
// storedVal = strtok(NULL,",");
// // count++;
// // if(count != 1)
// // {
// // // getData(buff);
// // }
// getch();
// }
if (fp!=NULL)
{
/* code */
while(!feof(fp)){
printf("ch2");
if(fscanf(fp,"%[^,],%[^\n]\n",storedKey,storedVal) == EOF)
break;
if(!strcmp(key,storedKey)) {
......@@ -74,9 +100,13 @@ bool dumpToFile(char* key, char* val){
else
fprintf(newFp,"%s,%s\n",storedKey,storedVal);
}
}
if(!keyExists)
fprintf(newFp,"%s,%s\n",key,val);
printf("ch3");
remove(fileName);
// free(fp);
rename(tempfileName,fileName);
......@@ -111,10 +141,12 @@ bool deleteStore(char* key) {
pthread_mutex_lock(&(store.locks[setNo]));
/////////////////add end/////////////////
FILE *fp = fopen(fileName,"a");
FILE *fp = fopen(fileName,"r");
FILE *newFp = fopen(tempfileName,"w");
char* storedKey = malloc(sizeof(char)*256);
char* storedVal = malloc(sizeof(char)*256*1024+1);
if (fp!=NULL)
{
while(1){
if(fscanf(fp,"%[^,],%[^\n]\n",storedKey,storedVal) == EOF)
break;
......@@ -123,6 +155,8 @@ bool deleteStore(char* key) {
}else
fprintf(newFp,"%s,%s\n",storedKey,storedVal);
}
}
remove(fileName);
// free(fp);
......@@ -222,3 +256,13 @@ int iniit() {
printf("Not found");
return 0;
}
// int main(){
// initStore();
// dumpToFile("sundaram111","shivam4");
// dumpToFile("sundaram112","shivam4");
// dumpToFile("key1","val");
// printf("%s\n",restoreFromFile("sundaram111"));
// deleteStore("key1");
// }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment