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){ ...@@ -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,11 +61,36 @@ bool dumpToFile(char* key, char* val){ ...@@ -60,11 +61,36 @@ 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;
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) if(fscanf(fp,"%[^,],%[^\n]\n",storedKey,storedVal) == EOF)
break; break;
if(!strcmp(key,storedKey)) { if(!strcmp(key,storedKey)) {
...@@ -74,9 +100,13 @@ bool dumpToFile(char* key, char* val){ ...@@ -74,9 +100,13 @@ bool dumpToFile(char* key, char* val){
else else
fprintf(newFp,"%s,%s\n",storedKey,storedVal); 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,10 +141,12 @@ bool deleteStore(char* key) { ...@@ -111,10 +141,12 @@ 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);
if (fp!=NULL)
{
while(1){ while(1){
if(fscanf(fp,"%[^,],%[^\n]\n",storedKey,storedVal) == EOF) if(fscanf(fp,"%[^,],%[^\n]\n",storedKey,storedVal) == EOF)
break; break;
...@@ -123,6 +155,8 @@ bool deleteStore(char* key) { ...@@ -123,6 +155,8 @@ bool deleteStore(char* key) {
}else }else
fprintf(newFp,"%s,%s\n",storedKey,storedVal); 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