Commit ead88885 authored by Roshan Rabinarayan's avatar Roshan Rabinarayan

final change modulus

parent 53e5ef7a
...@@ -23,47 +23,47 @@ int readercount = 0; ...@@ -23,47 +23,47 @@ 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;
int i=0;
while(size>0) while(i<divisor)
{ {
rem+=num[size--]%divisor; rem+=num[i]%divisor;
i++;
} }
return rem%divisor; return rem%divisor;
} }
int file_search(char *key, cahr *value, int index) {
lseek(fds[index], 0, SEEK_SET);
int offset = 0;
char fkey[256];
char fval[256];
do {
if( read(fds[index], fkey, sizeof(fkey)) < 0 )
break;
if( read(fds[index], fval, sizeof(fval)) < 0 )
break;
if( strcmp(fkey, key)==0 ) {
memcpy(value, fval, sizeof(fval));
return offset;
}
offset += sizeof(fkey) + sizeof(fval);
} while (1);
return -1;
}
void file_del( char *key) void file_del( char *key)
{ {
int offset;
int index=modulus(key,256,setSize); int index=modulus(key,256,setSize);
fflush(stdout);
int length=0;
sem_wait(&mutex[index]); sem_wait(&mutex[index]);
char blankspace[512];
char ch;
int k=-1;
lseek(fds[index], 0, SEEK_SET);
memset(blankspace,0,512);
char *line;
char temp[514];
size_t len=0;
off_t position=0;
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));
offset = file_search(key, value, index) break;
if(offset >= 0) {
lseek(fds[index], offset, SEEK_SET);
write(fds[index], 0, 256);
write(fds[index], 0, 256);
} }
position=ftell(fp);
}
printf("length %d",length);
sem_post(&mutex[index]); sem_post(&mutex[index]);
} }
...@@ -72,17 +72,28 @@ int file_get(char *key, char *value) ...@@ -72,17 +72,28 @@ 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 found =0;
int index = modulus(key,strlen(key),setSize);
int index=modulus(key,strlen(key),setSize);
lseek(fds[index],0,SEEK_SET);
sem_wait(&readerLocks[index]); sem_wait(&readerLocks[index]);
readCounters[index]+=1; readCounters[index]+=1;
if(readCounters[index]==1) if(readCounters[index]==1)
sem_wait(&mutex[index]); sem_wait(&mutex[index]);
sem_post(&readerLocks[index]); sem_post(&readerLocks[index]);
char *line;
if(file_search(key, value, index)>=0) { size_t len=0;
found = 1; FILE *fp=fdopen(fds[index],"rb+");
while ((getline(&line, &len, fp)) != -1)
{
char *fkey=strtok(line,":");
char *fvalue=strtok(NULL,":");
if(strcmp(key,fkey)==0){
memcpy(value,fvalue,strlen(fvalue)-1);
found=1;
break;
} }
}
sem_wait(&readerLocks[index]); sem_wait(&readerLocks[index]);
readCounters[index]-=1; readCounters[index]-=1;
if(readCounters[index]==0) if(readCounters[index]==0)
...@@ -93,7 +104,8 @@ int file_get(char *key, char *value) ...@@ -93,7 +104,8 @@ int file_get(char *key, char *value)
return found; return found;
} }
off_t file_put(char *key,char *value) { 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!
...@@ -128,7 +140,7 @@ off_t file_put(char *key,char *value) { ...@@ -128,7 +140,7 @@ off_t file_put(char *key,char *value) {
//sem_post(&mutex[index]); //sem_post(&mutex[index]);
return position; return position;
} }
int storage_init() int main()
{ {
/* /*
define the array of file descriptors depending on the prefix define the array of file descriptors depending on the prefix
...@@ -137,6 +149,20 @@ int storage_init() ...@@ -137,6 +149,20 @@ int storage_init()
GET would use read lock GET would use read lock
each write should return the line number each write should return the line number
*/ */
int n2=0;
char prevkey[256];
memset(prevkey,0,sizeof(prevkey));
memcpy(prevkey,"w",1);
char newkey[256];
memset(newkey,0,sizeof(newkey));
memcpy(newkey,"x",1);
printf("key:%s\n",prevkey);
char key[256]="25";
char value[256]="value2";
off_t offset;
bzero(key+strlen(key),sizeof(key)-strlen(key));
bzero(value+strlen(value),sizeof(value)-strlen(value));
fds=(int *)malloc(sizeof(int)*setSize); fds=(int *)malloc(sizeof(int)*setSize);
readCounters=(int *)malloc(sizeof(int)*setSize); readCounters=(int *)malloc(sizeof(int)*setSize);
readerLocks=(sem_t *)malloc(sizeof(sem_t)*setSize); readerLocks=(sem_t *)malloc(sizeof(sem_t)*setSize);
...@@ -155,7 +181,12 @@ int storage_init() ...@@ -155,7 +181,12 @@ int storage_init()
sem_init(&mutex[i],0,1); sem_init(&mutex[i],0,1);
readCounters[i]=0; readCounters[i]=0;
} }
printf("%u",modulus("99",256,setSize));
i=0;
printf("%d",modulus(prevkey,256,2));
printf("%d",modulus(newkey,256,2));
//offset = file_put("24", "value245"); //offset = file_put("24", "value245");
//offset = file_put("24", "value1"); //offset = file_put("24", "value1");
......
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