Commit 293f6818 authored by AXEL JAMES's avatar AXEL JAMES

stage1

parent b47089aa
...@@ -47,6 +47,7 @@ bool dumpToFile(char* key, char* val){ ...@@ -47,6 +47,7 @@ bool dumpToFile(char* key, char* val){
/////////////////add///////////////// /////////////////add/////////////////
int setNo = getFileId(key); int setNo = getFileId(key);
printf("ch1 %d",setNo);
/////////////////add end///////////////// /////////////////add end/////////////////
char fileName[10]; char fileName[10];
...@@ -60,23 +61,52 @@ bool dumpToFile(char* key, char* val){ ...@@ -60,23 +61,52 @@ bool dumpToFile(char* key, char* val){
pthread_mutex_lock(&(store.locks[setNo])); pthread_mutex_lock(&(store.locks[setNo]));
/////////////////add end///////////////// /////////////////add end/////////////////
FILE *fp = fopen(fileName,"a"); FILE *fp = fopen(fileName,"r");
FILE *newFp = fopen(tempfileName,"w"); FILE *newFp = fopen(tempfileName,"w");
char* storedKey = malloc(sizeof(char)*257); char* storedKey = malloc(sizeof(char)*257);
char* storedVal = malloc(sizeof(char)*256*1024+1); char* storedVal = malloc(sizeof(char)*256*1024+1);
while(1){ // char* storedVal;
if(fscanf(fp,"%[^,],%[^\n]\n",storedKey,storedVal) == EOF) int count=0;
break; // while((getc(fp))!=EOF)
if(!strcmp(key,storedKey)) { // {
keyExists = true; // // char buff[1024];
fprintf(newFp,"%s,%s\n",key,val); // 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)) {
keyExists = true;
fprintf(newFp,"%s,%s\n",key,val);
}
else
fprintf(newFp,"%s,%s\n",storedKey,storedVal);
} }
else
fprintf(newFp,"%s,%s\n",storedKey,storedVal);
} }
if(!keyExists) if(!keyExists)
fprintf(newFp,"%s,%s\n",key,val); fprintf(newFp,"%s,%s\n",key,val);
printf("ch3");
remove(fileName); remove(fileName);
// free(fp); // free(fp);
rename(tempfileName,fileName); rename(tempfileName,fileName);
...@@ -111,18 +141,22 @@ bool deleteStore(char* key) { ...@@ -111,18 +141,22 @@ bool deleteStore(char* key) {
pthread_mutex_lock(&(store.locks[setNo])); pthread_mutex_lock(&(store.locks[setNo]));
/////////////////add end///////////////// /////////////////add end/////////////////
FILE *fp = fopen(fileName,"a"); FILE *fp = fopen(fileName,"r");
FILE *newFp = fopen(tempfileName,"w"); FILE *newFp = fopen(tempfileName,"w");
char* storedKey = malloc(sizeof(char)*256); char* storedKey = malloc(sizeof(char)*256);
char* storedVal = malloc(sizeof(char)*256*1024+1); char* storedVal = malloc(sizeof(char)*256*1024+1);
while(1){ if (fp!=NULL)
if(fscanf(fp,"%[^,],%[^\n]\n",storedKey,storedVal) == EOF) {
break; while(1){
if(!strcmp(key,storedKey)) { if(fscanf(fp,"%[^,],%[^\n]\n",storedKey,storedVal) == EOF)
keyExists = true; break;
}else if(!strcmp(key,storedKey)) {
fprintf(newFp,"%s,%s\n",storedKey,storedVal); keyExists = true;
}else
fprintf(newFp,"%s,%s\n",storedKey,storedVal);
}
} }
remove(fileName); remove(fileName);
// free(fp); // free(fp);
...@@ -222,3 +256,13 @@ int iniit() { ...@@ -222,3 +256,13 @@ int iniit() {
printf("Not found"); printf("Not found");
return 0; 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