Question

Concurrent Key-Value Database Implement a non-persistent, concurrent key-value database using the Reader/Writers algorithm. - Use a...

Concurrent Key-Value Database

Implement a non-persistent, concurrent key-value database using the Reader/Writers algorithm.

- Use a hashmap as the underlying data structure. Inspiration is ok, however the implementation must be yours. The hashmap must be able to grow to fit new elements, but it does not need to reduce its size.

-- Linked lists? Positional arrays? All are fine.

- Keys and values are strings (char *)

- Operations are: get, put

Provided files:

- Implement the contract set in the provided header file (ht_*)

- Run the provided main.c file. It must verify that there are no mismatches.

---------------------------------------------------------------------------------------------------------------------------------------------------

/**

* HashTable

*/

typedef struct {

// Add your implementation here.

} ht_hash_table;

ht_hash_table* ht_new();

void ht_del_hash_table(ht_hash_table* ht);

void ht_insert(ht_hash_table* ht, const char* key, const char* value);

char* ht_search(ht_hash_table* ht, const char* key);

void ht_delete(ht_hash_table* h, const char* key);

---------------------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#include <string.h>

#include <pthread.h>

#include <stdbool.h>

#include "hashtable.h"

#define MAX_INSERT_THREADS 100

#define MAX_COMPARE_THREADS 1

#define KEY_LENGTH 3

#define VALUE_LENGTH 6

typedef struct {

ht_hash_table *ht;

char *key;

char *value;

} insert_item_t;

typedef struct {

ht_hash_table *ht;

char *key;

} search_item_t;

void *insert_worker(void* in)

{

insert_item_t *insert_item = (insert_item_t*)in;

printf("Inserting key: %s, value: %s\n", insert_item->key, insert_item->value);

ht_insert(insert_item->ht, insert_item->key, insert_item->value);

free(insert_item);

return NULL;

}

void *search(void *in)

{

search_item_t *search_item = (search_item_t*)in;

printf("Searching for key: %s\n", search_item->key);

char *value = ht_search(search_item->ht, search_item->key);

return (void*)value;

}

char *random_string(const int size, char **blacklist, size_t blacklist_count)

{

int upper = 90;

int lower = 65;

char *buffer = calloc(size + 1, sizeof(char));

bool found = false;

do {

FILE *f = fopen("/dev/urandom", "r");

fread(buffer, size, sizeof(char), f);

for (int i=0; i < size; i++) {

buffer[i] = (abs(buffer[i]) % (upper - lower + 1)) + lower;

}

fclose(f);

} while (found == true);

return buffer;

}

int main() {

ht_hash_table *ht = ht_new();

pthread_t insert_threads[MAX_INSERT_THREADS];

pthread_t compare_threads[MAX_COMPARE_THREADS];

int mismatch_count = 0;

char *keys[MAX_INSERT_THREADS];

char *values[MAX_INSERT_THREADS];

for (int i=0; i < MAX_INSERT_THREADS; i++) {

keys[i] = random_string(KEY_LENGTH, keys, MAX_INSERT_THREADS);

values[i] = random_string(VALUE_LENGTH, values, MAX_INSERT_THREADS);

insert_item_t *insert_item = malloc(sizeof(insert_item_t));

insert_item->ht = ht;

insert_item->key = keys[i];

insert_item->value = values[i];

pthread_create(&insert_threads[i], NULL, insert_worker, (void*) insert_item);

}

for (int i=0; i < MAX_INSERT_THREADS; i++) {

pthread_join(insert_threads[i], NULL);

}

for (int i=0; i < MAX_INSERT_THREADS;) {

for(int j=0; j < MAX_COMPARE_THREADS && i < MAX_INSERT_THREADS; i++, j++) {

search_item_t search_item = {ht, keys[i]};

pthread_create(&compare_threads[i], NULL, search, &search_item);

char *result;

pthread_join(compare_threads[i], (void**)&result);

if (strcmp(result, values[i]) != 0) {

printf("Mismatch. Expected:%s - Obtained: %s\n", values[i], result);

mismatch_count++;

}

}

for (int i=0; i < MAX_COMPARE_THREADS; i++) {

pthread_join(compare_threads[i], NULL);

}

}

for (int i=0; i < MAX_INSERT_THREADS; i++) {

free(keys[i]);

free(values[i]);

}

printf("Process ended. Mismatches found: %d", mismatch_count);

return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node{
char *key;
char *val;
struct node *next;
};
struct table{
int size;
struct node **list;
};
struct table *createTable(int size);
int hashCode(struct table t,char key);
void put(struct table t,char key,char *val);
char get(struct table t,char *key);

int main(){
struct table *t = createTable(500);
put(t,"key1","firstValue");
put(t,"key2","secondValue");
printf("%s",get(t,"key2"));
return 0;
}
struct table *createTable(int size){
struct table *t = (struct table*)malloc(sizeof(struct table));
t->size = size;
t->list = (struct node**)malloc(sizeof(struct node*)*size);
int i;
for(i=0;i<size;i++)
t->list[i] = NULL;
return t;
}
int hashCode(struct table t,char key){
int code = 0;
for (int i = 0; i < strlen(key); ++i)
{
code = code + key[i];
}
if(code<0)
return -(code%t->size);
return code%t->size;
}
void put(struct table t,char key,char *val){
int pos = hashCode(t,key);
struct node *list = t->list[pos];
struct node *newNode = (struct node*)malloc(sizeof(struct node));
struct node *temp = list;
while(temp){
if(temp->key==key){
temp->val = val;
return;
}
temp = temp->next;
}
newNode->key = key;
newNode->val = val;
newNode->next = list;
t->list[pos] = newNode;
}
char get(struct table t,char *key){
int pos = hashCode(t,key);
struct node *list = t->list[pos];
struct node *temp = list;
while(temp){
if(temp->key==key){
return temp->val;
}
temp = temp->next;
}
return NULL;
}

Add a comment
Know the answer?
Add Answer to:
Concurrent Key-Value Database Implement a non-persistent, concurrent key-value database using the Reader/Writers algorithm. - Use a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hashtable in C Please fix void ht_put(hashtable_t *ht, char *key, void *val) and void free_hashtable(hashtable_t *ht)...

    Hashtable in C Please fix void ht_put(hashtable_t *ht, char *key, void *val) and void free_hashtable(hashtable_t *ht) Implement void ht_del(hashtable_t *ht, char *key) and void ht_rehash(hashtable_t *ht, unsigned long newsize) --------------[hashtable.h]--------------------------------------- -######################################### #ifndef HASHTABLE_T #define HASHTABLE_T typedef struct hashtable hashtable_t; typedef struct bucket bucket_t; struct bucket { char *key; void *val; bucket_t *next; }; struct hashtable { unsigned long size; bucket_t **buckets; }; unsigned long hash(char *str); hashtable_t *make_hashtable(unsigned long size); void ht_put(hashtable_t *ht, char *key, void *val); void *ht_get(hashtable_t *ht,...

  • a multi-threaded producer / consumer program without any locking or signaling. It therefore has synchronization problems....

    a multi-threaded producer / consumer program without any locking or signaling. It therefore has synchronization problems. Add locks and signals so that it works correctly. /* Homework 5.X */ /* Robin Ehrlich */ /* compile: gcc Homework5.c -lpthread */ #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <string.h> struct class {    struct class *next;    int id;    int grade; }; #define SLEEP_TIME 1 #define MAX_PRODUCE 10 static struct class *classHead = NULL; static struct class *classTail = NULL; static...

  • i want to fix the solution and provide an explanation why the pthread_join is not working...

    i want to fix the solution and provide an explanation why the pthread_join is not working as intended? #include <stdio.h> /* standard I/O routines */ #include <pthread.h> /* pthread functions and data structures */ void* PrintHello(void* data) { pthread_t tid = (pthread_t)data; /* data received by thread */ pthread_join(tid, NULL); /* wait for thread tid */ printf("Hello from new thread %u - got %u\n", pthread_self(), data); pthread_exit(NULL); /* terminate the thread */ } /* like any C program, program's execution...

  • Solve the Consumer/Producer problem using semaphores. A skeleton program (Save it as producer_consumer.c) is provided to...

    Solve the Consumer/Producer problem using semaphores. A skeleton program (Save it as producer_consumer.c) is provided to you: The main function creates 2 threads: consumer represents the consumer and executes the consume function, and producer represents the producer and executes the  produce function. You should declare and initialize 3 semaphores. Those semaphores should be used in the consume(..) and produce(...) functions. #include <stdio.h> #include <stdlib.h> #include <pthread.h> //compile and link with -pthread #define BUFFER_SIZE 10 int buffer[BUFFER_SIZE]; int in, out; int num;...

  • In c programming The Consumer Submits processing requests to the producer by supplying a file name, its location and a character. It also outputs the contents of the file provided by the producer...

    In c programming The Consumer Submits processing requests to the producer by supplying a file name, its location and a character. It also outputs the contents of the file provided by the producer to the standard output. The Producer Accepts multiple consumer requests and processes each request by creating the following four threads. The reader thread will read an input file, one line at a time. It will pass each line of input to the character thread through a queue...

  • Modify the client server system program given below so that instead of sendto() and recvfrom(), you...

    Modify the client server system program given below so that instead of sendto() and recvfrom(), you use connect() and un-addresssed write() and read() calls. //Server.c #include #include #include #include #include #include #include #include #include #include # define PortNo 4567 # define BUFFER 1024 int main(int argc, char ** argv) { int ssd; int n; socklen_t len; char msg[BUFFER]; char clientmsg[BUFFER]; struct sockaddr_in server; struct sockaddr_in client; int max_iterations = 0; int count = 0, totalChar = 0, i = 0;...

  • I am supposed to write documentation and report for the code below but I am new...

    I am supposed to write documentation and report for the code below but I am new to operating system concepts I will appreciate if someone can help make a detailed comment on each line of code for better understanding. Thanks #include <pthread.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <ctype.h> #define handle_error_en(en, msg) \ do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0) #define handle_error(msg) \ do { perror(msg); exit(EXIT_FAILURE); } while (0) struct thread_info...

  • //In this assignment, we use multiple threads to calculate the frequencies of the first digits //of...

    //In this assignment, we use multiple threads to calculate the frequencies of the first digits //of the numbers in an array of long integers #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <assert.h> //#define NUM_THREADS 2 #define DIGITS 10 #define MAX 100000000 unsigned long a[MAX]; struct thread_data { int thread_num; int i, j;                           //the staring and ending index unsigned long freq[DIGITS];           // results }; //initialize the array void init_array(unsigned...

  • Please change this 2 thread program to a 4 thread program. /*compile as "gcc thread.c -pthread"...

    Please change this 2 thread program to a 4 thread program. /*compile as "gcc thread.c -pthread" */ #include <pthread.h> #include <stdio.h> #include <time.h> #define SIZE 100000000 //100 M //#define SIZE 10000 #define NUMBER_OF_TIMES 100 float a[SIZE]; typedef struct {        int start;        int end;        double sum;        int pid;    } range; void *thread_function(void *arg) //define a function that will be executed from //within a thread {    range *incoming = (range *) arg;...

  • Pleas help I need to create and array to add to my code something like this...

    Pleas help I need to create and array to add to my code something like this for (i = 0; i < NUM_THREADS; ++i) { thr_data[i].tid = i; if ((rc = pthread_create(&thr[i], NULL, thr_func, &thr_data[i]))) { fprintf(stderr, "error: pthread_create, rc: %d\n", rc); return EXIT_FAILURE; ::::::::: CODE ::::::::::::: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h>    int sharedVar = 0; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; /* thread handler */ void *threadHandler(void *vargp) {    int i = 0;    pthread_mutex_lock(&mutex);   ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT