Commit 7f1f4d66 authored by AXEL JAMES's avatar AXEL JAMES Committed by Bhavesh Yadav

cleaned code

parent 748bdb91
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
#include<unistd.h> #include<unistd.h>
#include<stdbool.h> #include<stdbool.h>
/////////////////add/////////////////
#define NUMFILES 10 #define NUMFILES 10
typedef struct Store typedef struct Store
{ {
...@@ -22,6 +21,7 @@ int getFileId(char * key){ ...@@ -22,6 +21,7 @@ int getFileId(char * key){
} }
return s%(NUMFILES); return s%(NUMFILES);
} }
void initStore() { void initStore() {
//creating locks //creating locks
store.locks = (pthread_mutex_t *) malloc(NUMFILES * sizeof(pthread_mutex_t)); store.locks = (pthread_mutex_t *) malloc(NUMFILES * sizeof(pthread_mutex_t));
...@@ -41,14 +41,11 @@ void initStore() { ...@@ -41,14 +41,11 @@ void initStore() {
return; return;
} }
/////////////////add end/////////////////
bool dumpToFile(char* key, char* val){ bool dumpToFile(char* key, char* val){
/////////////////add/////////////////
int setNo = getFileId(key); int setNo = getFileId(key);
printf("ch1 %d",setNo);
/////////////////add end/////////////////
char fileName[10]; char fileName[10];
char tempfileName[10]; char tempfileName[10];
...@@ -57,34 +54,16 @@ bool dumpToFile(char* key, char* val){ ...@@ -57,34 +54,16 @@ bool dumpToFile(char* key, char* val){
sprintf(fileName,"%d",setNo); sprintf(fileName,"%d",setNo);
strcat(fileName,".csv"); strcat(fileName,".csv");
strcat(tempfileName,fileName); strcat(tempfileName,fileName);
/////////////////add/////////////////
pthread_mutex_lock(&(store.locks[setNo])); pthread_mutex_lock(&(store.locks[setNo]));
/////////////////add end/////////////////
FILE *fp = fopen(fileName,"r"); 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);
// char* storedVal;
int count=0; 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) if (fp!=NULL)
{ {
/* code */ /* code */
...@@ -111,9 +90,7 @@ bool dumpToFile(char* key, char* val){ ...@@ -111,9 +90,7 @@ bool dumpToFile(char* key, char* val){
// free(fp); // free(fp);
rename(tempfileName,fileName); rename(tempfileName,fileName);
/////////////////add/////////////////
pthread_mutex_unlock(&(store.locks[setNo])); pthread_mutex_unlock(&(store.locks[setNo]));
/////////////////add end/////////////////
fclose(newFp); fclose(newFp);
free(storedKey); free(storedKey);
...@@ -125,9 +102,7 @@ bool dumpToFile(char* key, char* val){ ...@@ -125,9 +102,7 @@ bool dumpToFile(char* key, char* val){
//TODO:: re-use code of add-key instead of copy-paste //TODO:: re-use code of add-key instead of copy-paste
bool deleteStore(char* key) { bool deleteStore(char* key) {
/////////////////add/////////////////
int setNo = getFileId(key); int setNo = getFileId(key);
/////////////////add end/////////////////
char fileName[10]; char fileName[10];
char tempfileName[10]; char tempfileName[10];
...@@ -137,9 +112,7 @@ bool deleteStore(char* key) { ...@@ -137,9 +112,7 @@ bool deleteStore(char* key) {
strcat(fileName,".csv"); strcat(fileName,".csv");
strcat(tempfileName,fileName); strcat(tempfileName,fileName);
/////////////////add/////////////////
pthread_mutex_lock(&(store.locks[setNo])); pthread_mutex_lock(&(store.locks[setNo]));
/////////////////add end/////////////////
FILE *fp = fopen(fileName,"r"); FILE *fp = fopen(fileName,"r");
FILE *newFp = fopen(tempfileName,"w"); FILE *newFp = fopen(tempfileName,"w");
...@@ -162,9 +135,7 @@ bool deleteStore(char* key) { ...@@ -162,9 +135,7 @@ bool deleteStore(char* key) {
// free(fp); // free(fp);
rename(tempfileName,fileName); rename(tempfileName,fileName);
/////////////////add/////////////////
pthread_mutex_unlock(&(store.locks[setNo])); pthread_mutex_unlock(&(store.locks[setNo]));
/////////////////add end/////////////////
fclose(newFp); fclose(newFp);
free(storedKey); free(storedKey);
...@@ -257,12 +228,3 @@ int iniit() { ...@@ -257,12 +228,3 @@ int iniit() {
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