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

changes modulus

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