Commit d893258e authored by Samarth Joshi's avatar Samarth Joshi

Updated modulus

parent ea8ae751
File added
1:test
2:test
3:test
4:test
5:test
6:test
7:test
8:test
9:test
10:tet
11:test
......@@ -93,7 +93,8 @@ void *worker(void *args) {
/* Parse the actual message from client */
struct message *requestMessage= malloc(sizeof(struct message));
int readlength=read(events[i].data.fd , requestMessage, sizeof(struct message));
memset(requestMessage->key, 0, 256);
memset(requestMessage->value, 0, 256);
if DEBUG printf("[%s][EVENT][EPOLLIN] \n", name);
switch(requestMessage->status) {
case STATUS_GET:
......@@ -121,10 +122,10 @@ void *worker(void *args) {
if DEBUG printf("[%s] DEL \n", name);
status = cache_del(requestMessage->key);
file_del(requestMessage->key);
if(status == 0) {
requestMessage->status = 240;
} else {
if(status) {
requestMessage->status = 200;
} else {
requestMessage->status = 240;
}
break;
}
......@@ -149,12 +150,12 @@ int main (int argc, int argv) {
int i;
int next_thread_to_assign = 0;
pthread_t *worker_threads;
int pool_thread_size = 10; // TODO: get pool thread size from config file
int pool_thread_size = 1; // TODO: get pool thread size from config file
int sockfd, newsockfd, portno, clilen, n;
struct sockaddr_in serv_addr, cli_addr;
//int *pipes = (int*) malloc(pool_thread_size * 2 * sizeof(pipes));
int pipes[10][2]; // TODO: initialize pipes dynamically
int pipes[1][2]; // TODO: initialize pipes dynamically
worker_threads = malloc(pool_thread_size * sizeof(pthread_t));
for( i=0; i < pool_thread_size; i++ ) {
......
No preview for this file type
......@@ -24,44 +24,50 @@ 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;
}
int file_search(char *key, char *value, int index) {
lseek(fds[index], 0, SEEK_SET);
int offset = 0;
int size;
char fkey[256];
char fval[256];
do {
size = read(fds[index], fkey, sizeof(fkey));
if( size <= 0 )
break;
size = read(fds[index], fval, sizeof(fval));
if( size <= 0 )
break;
if( strcmp(fkey, key)==0 ) {
if(value)
memcpy(value, fval, sizeof(fval));
return offset;
}
offset += sizeof(fkey) + sizeof(fval);
} while (1);
return -1;
}
void file_del( char *key)
{
int offset;
int index=modulus(key,256,setSize);
fflush(stdout);
int length=0;
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));
break;
}
position=ftell(fp);
offset = file_search(key, NULL, index);
if(offset >= 0) {
lseek(fds[index], offset, SEEK_SET);
write(fds[index], 0, 256);
write(fds[index], 0, 256);
}
printf("length %d",length);
sem_post(&mutex[index]);
}
......@@ -70,28 +76,17 @@ int file_get(char *key, char *value)
/* Gets the value stored at offset */
/* Does not depend on key argument */
int found =0;
int index=modulus(key,256,setSize);
lseek(fds[index],0,SEEK_SET);
sem_wait(&readerLocks[index]);
readCounters[index]+=1;
if(readCounters[index]==1)
sem_wait(&mutex[index]);
sem_post(&readerLocks[index]);
char *line;
size_t len=0;
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;
}
if(file_search(key, value, index)>=0) {
found = 1;
}
sem_wait(&readerLocks[index]);
readCounters[index]-=1;
if(readCounters[index]==0)
......@@ -102,55 +97,26 @@ int file_get(char *key, char *value)
return found;
}
off_t file_put(char *key,char *value)
{
void 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 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];
size_t len=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, strlen(key)+strlen(value)+2,"%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");
int offset;
int index=modulus(key,256,setSize);
sem_wait(&mutex[index]);
offset = file_search(key, NULL, index);
if(offset < 0) {
lseek(fds[index], 0, SEEK_END);
} else {
lseek(fds[index], offset, SEEK_SET);
}
write(fds[index], key, 256);
write(fds[index], value, 256);
// sem_post(&mutex[index]);
sem_post(&mutex[index]);
return position;
}
int storage_init()
{
......@@ -179,5 +145,5 @@ int storage_init()
sem_init(&mutex[i],0,1);
readCounters[i]=0;
}
return 0;
}
......@@ -4,7 +4,7 @@ int storage_init();
void file_del( char *key);
off_t file_put(char *key,char *value);
void file_put(char *key,char *value);
int file_get(char *key, char *value);
......
......@@ -30,38 +30,40 @@ unsigned modulus( unsigned char *num, size_t size, unsigned divisor) {
}
return rem;
}
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)
{
int offset;
int index=modulus(key,256,setSize);
fflush(stdout);
int length=0;
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));
break;
}
position=ftell(fp);
offset = file_search(key, value, index)
if(offset >= 0) {
lseek(fds[index], offset, SEEK_SET);
write(fds[index], 0, 256);
write(fds[index], 0, 256);
}
printf("length %d",length);
sem_post(&mutex[index]);
}
......@@ -70,28 +72,17 @@ int file_get(char *key, char *value)
/* Gets the value stored at offset */
/* Does not depend on key argument */
int found =0;
int index=modulus(key,strlen(key),setSize);
lseek(fds[index],0,SEEK_SET);
int index = modulus(key,strlen(key),setSize);
sem_wait(&readerLocks[index]);
readCounters[index]+=1;
if(readCounters[index]==1)
sem_wait(&mutex[index]);
sem_post(&readerLocks[index]);
char *line;
size_t len=0;
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;
}
if(file_search(key, value, index)>=0) {
found = 1;
}
sem_wait(&readerLocks[index]);
readCounters[index]-=1;
if(readCounters[index]==0)
......@@ -102,58 +93,29 @@ int file_get(char *key, char *value)
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 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];
size_t len=0;
FILE *fp=fdopen(fds[index],"rb+");
position=ftell(fp);
while ((getline(&line, &len, fp)) != -1)
{
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;
}
}
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");
int offset;
int index = modulus(key,strlen(key),setSize);
sem_wait(&mutex[index]);
offset = file_search(key, value, index)
if(offset < 0) {
lseek(fds[index], 0, SEEK_END);
} else {
lseek(fds[index], offset, SEEK_SET);
}
write(fds[index], key, sizeof(key));
write(fds[index], value, sizeof(value));
// sem_post(&mutex[index]);
sem_post(&mutex[index]);
return position;
}
int main()
int storage_init()
{
/*
define the array of file descriptors depending on the prefix
......@@ -162,13 +124,6 @@ int main()
GET would use read lock
each write should return the line number
*/
int n2=0;
char prevkey[256]="24";
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);
readCounters=(int *)malloc(sizeof(int)*setSize);
readerLocks=(sem_t *)malloc(sizeof(sem_t)*setSize);
......@@ -187,16 +142,5 @@ int main()
sem_init(&mutex[i],0,1);
readCounters[i]=0;
}
//offset = file_put("24", "value245");
offset = file_put("24", "value1");
file_put("29","update2");
file_put("28","update2");
//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);
return 0;
}
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