Commit 2ac11a25 authored by Roshan Rabinarayan's avatar Roshan Rabinarayan

complete persistensce storage

parent 3353b49b
...@@ -21,41 +21,51 @@ pthread_t tid; ...@@ -21,41 +21,51 @@ pthread_t tid;
pthread_t writerthreads[100],readerthreads[100]; pthread_t writerthreads[100],readerthreads[100];
int readercount = 0; int readercount = 0;
int modulus(char *num, int size, int divisor) { unsigned modulus( unsigned char *num, size_t size, unsigned divisor) {
int rem = 0; unsigned rem = 0;
while (size-- > 0) { while (size-- > 0) {
rem = ((UCHAR_MAX + 1)*rem + *num) % divisor; rem = ((UCHAR_MAX + 1ULL)*rem + *num) % divisor;
num++; num++;
} }
return rem; return rem;
} }
void file_del(off_t offset, char *key) void file_del( char *key)
{ {
int index=modulus(key,256,setSize); int index=modulus(key,256,setSize);
fflush(stdout); fflush(stdout);
int length=0; int length=0;
sem_wait(&mutex[index]); sem_wait(&mutex[index]);
char blankspace[512]; char blankspace[512];
char ch; memset(blankspace,0,512);
int k=-1; char *line;
lseek(fds[index], offset, SEEK_SET); char temp[514];
while(read(fds[index], &ch,sizeof(ch))!=-1 && ch!='\n') size_t len=0;
{ off_t position=0;
length++; FILE *fp=fdopen(fds[index],"rb+");
while ((getline(&line, &len, fp)) != -1)
{
memcpy(temp,line,strlen(line)-1);
char *fkey=strtok(line,":");
char *fvalue=strtok(NULL,":");
if(strcmp(key,fkey)==0){
lseek(fds[index],position,SEEK_SET);
write(fds[index],blankspace,strlen(temp));
puts(temp);
break;
}
position=ftell(fp);
} }
printf("length %d",length);
memset(blankspace, 0, length);
lseek(fds[index], offset, SEEK_SET);
write(fds[index], blankspace, length);
sem_post(&mutex[index]); sem_post(&mutex[index]);
} }
void file_get(char *key, char *value) int file_get(char *key, char *value)
{ {
/* Gets the value stored at offset */ /* Gets the value stored at offset */
/* Does not depend on key argument */ /* Does not depend on key argument */
int found =0;
int index=modulus(key,256,setSize); int index=modulus(key,256,setSize);
sem_wait(&readerLocks[index]); sem_wait(&readerLocks[index]);
readCounters[index]+=1; readCounters[index]+=1;
...@@ -63,20 +73,15 @@ void file_get(char *key, char *value) ...@@ -63,20 +73,15 @@ void file_get(char *key, char *value)
sem_wait(&mutex[index]); sem_wait(&mutex[index]);
sem_post(&readerLocks[index]); sem_post(&readerLocks[index]);
char *line; char *line;
//FILE *fp =fdopen(fds[index],"r+");
// fseek(fp, offset,SEEK_SET);
size_t len=0; size_t len=0;
FILE *fp=fdopen(fds[index],"rb+");
char ch;
int k=-1;
FILE *fp=fdopen(fds[index],"r+");
while ((getline(&line, &len, fp)) != -1) while ((getline(&line, &len, fp)) != -1)
{ {
char *fkey=strtok(line,":"); char *fkey=strtok(line,":");
char *fvalue=strtok(NULL,":"); char *fvalue=strtok(NULL,":");
if(strcmp(key,fkey)==0){ if(strcmp(key,fkey)==0){
memcpy(value,fvalue,strlen(fvalue)); memcpy(value,fvalue,strlen(fvalue)-1);
found=1;
break; break;
} }
...@@ -88,31 +93,57 @@ void file_get(char *key, char *value) ...@@ -88,31 +93,57 @@ void file_get(char *key, char *value)
sem_post(&mutex[index]); sem_post(&mutex[index]);
} }
sem_post(&readerLocks[index]); sem_post(&readerLocks[index]);
return found;
} }
off_t file_put(char *key,char *value) { off_t file_put(char *key,char *value)
{
int index=modulus(key,256,setSize); /*
int bytes, rembytes, i; if found then ask the offset where it is present and if the value noy matches with the present value ,update the given line
if not present then search for empty line and insert there!
*/
unsigned int index=modulus(key,256,setSize);
off_t position; off_t position;
//sem_wait(&mutex[index]);
sem_wait(&mutex[index]);
printf("[Write to File: %d]\n",index); printf("[Write to File: %d]\n",index);
position = lseek(fds[index], 0, SEEK_END); char *line;
char line [514]; char lin[514];
snprintf(line, sizeof(line),"%s:%s\n",key,value); size_t len=0;
if(write(fds[index], line, strlen(line))<0) FILE *fp=fdopen(fds[index],"rb+");
position=ftell(fp);
while ((getline(&line, &len, fp)) != -1)
{
char *fkey=strtok(line,":");
char *fvalue=strtok(NULL,":");
if(strcmp(key,fkey)==0){
if(strcmp(value,strtok(fvalue,"\n"))==0)
{
printf("(%s)(%s)\n",fkey,key);
///everything same then why insert?
return 0;
}
else
{
printf("key:(%s)(%s)\n",fkey,key);
printf("value:(%s)(%s)\n",fvalue,value);
fflush(stdout);
fseek(fp,position,SEEK_SET);
snprintf(lin, sizeof(lin),"%s:%s\n",key,value);
if(fputs(lin,fp)<0)
printf("\n[unable to perform file_put]\n");
return 0;
}
}
position=ftell(fp);
}
snprintf(lin, sizeof(lin),"%s:%s\n",key,value);
if(write(fds[index], lin, strlen(lin))<0)
printf("\n[unable to perform file_put]\n"); printf("\n[unable to perform file_put]\n");
sem_post(&mutex[index]);
return position;
}
void file_search(char *key,char *value)
{
int index=modulus(key,256,setSize);
// sem_post(&mutex[index]);
return position;
} }
int main() int main()
{ {
...@@ -125,7 +156,7 @@ int main() ...@@ -125,7 +156,7 @@ int main()
*/ */
int n2=0; int n2=0;
char prevkey[256]="24"; char prevkey[256]="24";
char key[256]="24sadasdasdasdasdsad"; char key[256]="25";
char value[256]="value2"; char value[256]="value2";
off_t offset; off_t offset;
bzero(key+strlen(key),sizeof(key)-strlen(key)); bzero(key+strlen(key),sizeof(key)-strlen(key));
...@@ -148,12 +179,10 @@ int main() ...@@ -148,12 +179,10 @@ int main()
sem_init(&mutex[i],0,1); sem_init(&mutex[i],0,1);
readCounters[i]=0; readCounters[i]=0;
} }
//offset = file_put("24", "value245");
//offset = file_put("40", "value28");
offset = file_put(prevkey, "value1"); //file_get(prevkey, value); // Doesnot depend on key arg, returns key and value at offset 0
offset = file_put(key, value); //printf("(%s)", value);
file_get(prevkey, value); // Doesnot depend on key arg, returns key and value at offset 0 file_del("29");
printf("%s\n", value);
//file_del(offset, prevkey);
} }
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