diff options
author | bpc2003 <wpesfriendnva@gmail.com> | 2025-03-20 13:23:41 -0400 |
---|---|---|
committer | bpc2003 <wpesfriendnva@gmail.com> | 2025-03-20 13:23:41 -0400 |
commit | 283a433f56fc761a8fe698fcfc71898ebca07e71 (patch) | |
tree | ddad01911cdf9c2bffe2b36887dd729834c20af9 | |
parent | 74856ca4c7a57ac5895c5dd00f2179b9d3586223 (diff) |
Have setkeys partially working
-rw-r--r-- | .gitignore | 4 | ||||
-rw-r--r-- | Makefile | 14 | ||||
-rw-r--r-- | src/lib/batch.c | 61 | ||||
-rw-r--r-- | src/main.c | 3 | ||||
-rw-r--r-- | src/test.c | 26 |
5 files changed, 103 insertions, 5 deletions
@@ -7,3 +7,7 @@ target/ # lsp .ccls-cache + +# debug files +dbg +core @@ -1,19 +1,25 @@ CC = gcc BUILD = target -C_FLAGS = -Wall -lmdb -std=c11 -O2 +C_FLAGS = -Wall -lmdb -std=c11 D_FLAGS = -L$(BUILD) -Wl,-rpath=$(BUILD) L_FLAGS = -c -fPIC -Wall -Werror all: mdb mdb: lib - $(CC) src/*.c $(D_FLAGS) $(C_FLAGS) -o $(BUILD)/mdb.out + $(CC) src/main.c src/cmd.c $(D_FLAGS) -O2 $(C_FLAGS) -o $(BUILD)/mdb.out lib: $(BUILD) $(CC) src/lib/*.c $(L_FLAGS) $(CC) -shared -o $(BUILD)/libmdb.so *.o rm *.o +test: dev_lib + $(CC) src/test.c $(D_FLAGS) -g $(C_FLAGS) -o $(BUILD)/test.out + valgrind --leak-check=full ./$(BUILD)/test.out + valgrind --tool=helgrind ./$(BUILD)/test.out + rm -rf $(BUILD) + dev: dev_lib - $(CC) src/*.c $(D_FLAGS) -g $(C_FLAGS) -o $(BUILD)/devmdb.out + $(CC) src/main.c src/cmd.c $(D_FLAGS) -g $(C_FLAGS) -o $(BUILD)/devmdb.out dev_lib: $(BUILD) $(CC) src/lib/*.c $(L_FLAGS) -g $(CC) -shared -o $(BUILD)/libmdb.so *.o @@ -26,7 +32,7 @@ install: lib cp src/lib/mdb.h /usr/include/mdb.h mv $(BUILD)/libmdb.so /usr/lib/libmdb.so ldconfig - $(CC) src/*.c $(C_FLAGS) -o /usr/bin/mdb + $(CC) src/*.c -O2 $(C_FLAGS) -o /usr/bin/mdb rm -rf $(BUILD) uninstall: diff --git a/src/lib/batch.c b/src/lib/batch.c new file mode 100644 index 0000000..e728557 --- /dev/null +++ b/src/lib/batch.c @@ -0,0 +1,61 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <threads.h> +#include <stdatomic.h> + +#include "mdb.h" + +static int setkey_helper(void *thr_data); +/*static int delkey_helper(void *thr_data);*/ + + +tablist_t *copy; +char *str; + +mtx_t setkey_mtx; +int setkeys(tablist_t **list, char *pair) +{ + int rc; + copy = calloc(list[0]->len, sizeof(tablist_t)); + memcpy(copy, *list, list[0]->len * sizeof(tablist_t)); + str = pair; + + thrd_t *thrds = calloc(list[0]->len, sizeof(thrd_t)); + if (mtx_init(&setkey_mtx, mtx_plain) != thrd_success) + return 1; + + for (int i = 0; i < copy[0].len; ++i) { + int *arg = calloc(1, sizeof(int)); + *arg = i; + thrd_create(&thrds[i], setkey_helper, arg); + } + for (int i = 0; i < copy[0].len; ++i) { + thrd_join(thrds[i], &rc); + if (rc) + break; + } + if (!rc) + memcpy(*list, copy, copy[0].len * sizeof(tablist_t)); + free(thrds); + free(copy); + return rc; +} + +int delkeys(tablist_t *list, char *key) +{ + return 0; +} + +static int setkey_helper(void *thr_data) +{ + int *id = (int *) thr_data; + int rc; + + mtx_lock(&setkey_mtx); + rc = setkey(©, *id, str); + mtx_unlock(&setkey_mtx); + + free(id); + return rc; +} @@ -9,6 +9,7 @@ int getid(char *selector); int printkeys(tablist_t **list, int id, char **keys, int klen); void printkey(tabidx_t idx); +// TODO: rename these functions int setkeys_main(tablist_t **list, int id, char **pairs, int plen); int delkeys_main(tablist_t **list, int id, char **keys, int klen); int exec(int (*tabop)(tablist_t **, int, char **, int), @@ -40,7 +41,7 @@ int main(int argc, char **argv) break; case SET: case DEL: - id = exec(evaled.type == SET ? setkeys : delkeys, + id = exec(evaled.type == SET ? setkeys_main : delkeys_main, &list, id, evaled.params, evaled.plen); if (!id && filename) writedb(filename, list); diff --git a/src/test.c b/src/test.c new file mode 100644 index 0000000..4103c41 --- /dev/null +++ b/src/test.c @@ -0,0 +1,26 @@ +#include <stdio.h> +#include <stdlib.h> + +#include "lib/mdb.h" + +void test_setkeys(void) +{ + tablist_t *list = readdb("dbs/test.db"); + if (setkeys(&list, "name:john")) + fprintf(stderr, "test_setkeys: failed\n"); + for (int i = 0; i < list[0].len; ++i) { + int *indexes = getkeys(list, i); + for (int j = 0; indexes[j]; ++j) { + printf("%s\n", list[i].tab[indexes[j]].key); + delkey(list, i, list[i].tab[indexes[j]].key); + } + free(indexes); + } + free(list); +} + +int main(void) +{ + test_setkeys(); + exit(0); +} |