forked from 5ujinKang/Key-Value-Store
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_multithread_kvs.c
More file actions
161 lines (131 loc) · 3.99 KB
/
basic_multithread_kvs.c
File metadata and controls
161 lines (131 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* Basic multithread key value store in c
* with test cases.
* */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#define NUM_BUCKETS 1024
#define VALUE_SIZE 32
#define NUM_THREADS 4
#define OPS_PER_THREAD 5
typedef struct Entry {
size_t key;
char value[VALUE_SIZE];
struct Entry* next;
} Entry;
typedef struct {
Entry* buckets[NUM_BUCKETS];
pthread_mutex_t locks[NUM_BUCKETS];
} KVStore;
// ---------------- KV Store Functions ---------------- //
size_t hash(size_t key) {
return key % NUM_BUCKETS;
}
KVStore* kvstore_create() {
KVStore* store = malloc(sizeof(KVStore));
for (size_t i = 0; i < NUM_BUCKETS; i++) {
store->buckets[i] = NULL;
pthread_mutex_init(&store->locks[i], NULL);
}
return store;
}
void kv_set(KVStore* store, size_t key, const char* value) {
size_t idx = hash(key);
pthread_mutex_lock(&store->locks[idx]);
Entry* curr = store->buckets[idx];
while (curr) {
if (curr->key == key) {
strncpy(curr->value, value, VALUE_SIZE);
printf("[SET] Updated key %zu -> %s\n", key, value);
pthread_mutex_unlock(&store->locks[idx]);
return;
}
curr = curr->next;
}
Entry* new_entry = malloc(sizeof(Entry));
new_entry->key = key;
strncpy(new_entry->value, value, VALUE_SIZE);
new_entry->next = store->buckets[idx];
store->buckets[idx] = new_entry;
printf("[SET] Inserted key %zu -> %s\n", key, value);
pthread_mutex_unlock(&store->locks[idx]);
}
int kv_get(KVStore* store, size_t key, char* out_value) {
if (!store) {
printf("[GET] ERROR: KV store is NULL\n");
return 0;
}
size_t idx = hash(key);
pthread_mutex_lock(&store->locks[idx]);
Entry* curr = store->buckets[idx];
while (curr) {
if (curr->key == key) {
strncpy(out_value, curr->value, VALUE_SIZE);
pthread_mutex_unlock(&store->locks[idx]);
printf("[GET] Key %zu -> %s\n", key, out_value);
return 1;
}
curr = curr->next;
}
pthread_mutex_unlock(&store->locks[idx]);
printf("[GET] Key %zu not found\n", key);
return 0;
}
void kvstore_destroy(KVStore** store_ptr) {
if (!store_ptr || !*store_ptr) return;
KVStore* store = *store_ptr;
printf("[DESTROY] Destroying KV store...\n");
for (size_t i = 0; i < NUM_BUCKETS; i++) {
Entry* curr = store->buckets[i];
while (curr) {
Entry* tmp = curr;
curr = curr->next;
free(tmp);
}
pthread_mutex_destroy(&store->locks[i]);
}
free(store);
*store_ptr = NULL;
printf("[DESTROY] KV store destroyed and pointer set to NULL\n");
}
// ---------------- Multi-thread Test ---------------- //
typedef struct {
KVStore* store;
int thread_id;
} ThreadArg;
void* thread_task(void* arg) {
ThreadArg* t = (ThreadArg*)arg;
char value[VALUE_SIZE];
for (size_t i = 0; i < OPS_PER_THREAD; i++) {
size_t key = t->thread_id * OPS_PER_THREAD + i;
char val[VALUE_SIZE];
snprintf(val, VALUE_SIZE, "T%d_val%zu", t->thread_id, i);
kv_set(t->store, key, val);
// Randomly get some keys (could hit same keys across threads)
size_t get_key = rand() % (NUM_THREADS * OPS_PER_THREAD);
kv_get(t->store, get_key, value);
}
return NULL;
}
void test_multithreaded() {
printf("\n=== Test: Multi-threaded KV Store ===\n");
KVStore* store = kvstore_create();
pthread_t threads[NUM_THREADS];
ThreadArg args[NUM_THREADS];
for (int i = 0; i < NUM_THREADS; i++) {
args[i].store = store;
args[i].thread_id = i;
pthread_create(&threads[i], NULL, thread_task, &args[i]);
}
for (int i = 0; i < NUM_THREADS; i++) {
pthread_join(threads[i], NULL);
}
kvstore_destroy(&store);
}
// ---------------- Main ---------------- //
int main() {
test_multithreaded();
printf("\nAll tests done!\n");
return 0;
}