Commit 53e5ef7a authored by Samarth Joshi's avatar Samarth Joshi

Fixed conflicts temp.c

parents d893258e 8cacda00
...@@ -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;
} }
...@@ -98,21 +98,34 @@ off_t file_put(char *key,char *value) { ...@@ -98,21 +98,34 @@ off_t file_put(char *key,char *value) {
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 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! if not present then search for empty line and insert there!
*/ */
int offset; unsigned int index=modulus(key,256,setSize);
int index = modulus(key,strlen(key),setSize); off_t position;
sem_wait(&mutex[index]); //sem_wait(&mutex[index]);
printf("[Write to File: %d]\n",index);
offset = file_search(key, value, index) char *line;
if(offset < 0) { char lin[514];
lseek(fds[index], 0, SEEK_END); char blankspace[513];
} else { memset(blankspace, 0, sizeof(blankspace));
lseek(fds[index], offset, SEEK_SET); size_t len=0;
FILE *fp=fdopen(fds[index],"rb+");
while ((getline(&line, &len, fp)) != -1)
{
int temp =strlen(line);
char *fkey=strtok(line,":");
if(strcmp(key,fkey)==0)
{
fseek(fp,position,SEEK_SET);
fwrite(blankspace,sizeof(blankspace),temp,fp);
break;
}
position=ftell(fp);
} }
write(fds[index], key, sizeof(key));
write(fds[index], value, sizeof(value)); snprintf(lin,sizeof(lin),"%s:%s\n",key,value);
fwrite(lin,sizeof(lin),strlen(lin),fp);
sem_post(&mutex[index]);
//sem_post(&mutex[index]);
return position; return position;
} }
int storage_init() int storage_init()
...@@ -142,5 +155,18 @@ int storage_init() ...@@ -142,5 +155,18 @@ int storage_init()
sem_init(&mutex[i],0,1); sem_init(&mutex[i],0,1);
readCounters[i]=0; readCounters[i]=0;
} }
return 0; printf("%u",modulus("99",256,setSize));
//offset = file_put("24", "value245");
//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");
//file_get("24",value);
// printf("(%s)\n", value);
//file_del("21");
//printf("(%s)\n", value);
} }
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