Commit 8cacda00 authored by Roshan Rabinarayan's avatar Roshan Rabinarayan

changes modulus

parent ea8ae751
...@@ -24,11 +24,11 @@ int readercount = 0; ...@@ -24,11 +24,11 @@ int readercount = 0;
unsigned modulus( unsigned char *num, size_t size, unsigned divisor) { unsigned modulus( unsigned char *num, size_t size, unsigned divisor) {
unsigned rem = 0; unsigned rem = 0;
while (size-- > 0) { while(size>0)
rem = ((UCHAR_MAX + 1ULL)*rem + *num) % divisor; {
num++; rem+=num[size--]%divisor;
} }
return rem; return rem%divisor;
} }
void file_del( char *key) void file_del( char *key)
{ {
...@@ -109,48 +109,33 @@ off_t file_put(char *key,char *value) ...@@ -109,48 +109,33 @@ off_t file_put(char *key,char *value)
if not present then search for empty line and insert there! if not present then search for empty line and insert there!
*/ */
unsigned int index=modulus(key,256,setSize); unsigned int index=modulus(key,256,setSize);
lseek(fds[index],0,SEEK_SET);
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);
char *line; char *line;
char lin[514]; char lin[514];
char blankspace[513];
memset(blankspace, 0, sizeof(blankspace));
size_t len=0; size_t len=0;
FILE *fp=fdopen(fds[index],"rb+"); FILE *fp=fdopen(fds[index],"rb+");
position=ftell(fp);
while ((getline(&line, &len, fp)) != -1) while ((getline(&line, &len, fp)) != -1)
{ {
int temp =strlen(line);
char *fkey=strtok(line,":"); char *fkey=strtok(line,":");
char *fvalue=strtok(NULL,":"); if(strcmp(key,fkey)==0)
printf("key:(%s)(%s)\n",fkey,key); {
if(strcmp(key,fkey)==0){ fseek(fp,position,SEEK_SET);
if(strcmp(value,strtok(fvalue,"\n"))==0) fwrite(blankspace,sizeof(blankspace),temp,fp);
{ break;
printf("(%s)(%s)\n",fkey,key);
///everything same then why insert?
return 0;
}
else
{
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); 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");
snprintf(lin,sizeof(lin),"%s:%s\n",key,value);
// sem_post(&mutex[index]); fwrite(lin,sizeof(lin),strlen(lin),fp);
//sem_post(&mutex[index]);
return position; return position;
} }
int main() int main()
...@@ -187,10 +172,12 @@ int main() ...@@ -187,10 +172,12 @@ int main()
sem_init(&mutex[i],0,1); sem_init(&mutex[i],0,1);
readCounters[i]=0; readCounters[i]=0;
} }
printf("%u",modulus("99",256,setSize));
//offset = file_put("24", "value245"); //offset = file_put("24", "value245");
offset = file_put("24", "value1"); //offset = file_put("24", "value1");
file_put("29","update2"); //file_put("23","update1");
file_put("28","update2"); //file_put("24","update6969");
//file_get(prevkey, value); // Doesnot depend on key arg, returns key and value at offset 0 //file_get(prevkey, value); // Doesnot depend on key arg, returns key and value at offset 0
//printf("(%s)", value); //printf("(%s)", value);
//file_del("29"); //file_del("29");
......
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