summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/batch.c184
-rw-r--r--src/lib/delkeys.c95
-rw-r--r--src/lib/getkeys.c16
-rw-r--r--src/lib/keytab.c119
-rw-r--r--src/lib/setkeys.c132
-rw-r--r--src/lib/utils.c78
-rw-r--r--src/lib/utils.h19
7 files changed, 339 insertions, 304 deletions
diff --git a/src/lib/batch.c b/src/lib/batch.c
deleted file mode 100644
index 0534daf..0000000
--- a/src/lib/batch.c
+++ /dev/null
@@ -1,184 +0,0 @@
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <threads.h>
-
-#include "mdb.h"
-
-// TODO: move these functions into seperate files
-
-static int setkey_helper(void *thr_data);
-static int delkey_helper(void *thr_data);
-static int *clone(int n);
-static tablist_t *copytab(tablist_t *dst, tablist_t *src);
-static void dellist(tablist_t *list);
-
-extern int setkey(tablist_t **, int, char *);
-extern int delkey(tablist_t *, int, char *);
-
-tablist_t *setkey_copy;
-char *setkey_str;
-
-mtx_t setkey_mtx;
-int setkeys(tablist_t **list, int id, char **pairs, int len)
-{
- if (id < -1 || pairs == NULL || len <= 0)
- return -1;
- if (mtx_init(&setkey_mtx, mtx_plain) != thrd_success)
- return -2;
- int rc = 0;
- setkey_copy = calloc((*list)[0].len, sizeof(tablist_t));
- copytab(setkey_copy, *list);
-
- for (int i = 0; i < len; ++i) {
- setkey_str = pairs[i];
- if (id == -1) {
- thrd_t *thrds = calloc((*list)[0].len, sizeof(thrd_t));
- for (int i = 0; i < setkey_copy[0].len; ++i)
- thrd_create(&thrds[i], setkey_helper, clone(i));
- for (int i = 0; i < setkey_copy[0].len; ++i) {
- if (rc)
- thrd_join(thrds[i], NULL);
- else
- thrd_join(thrds[i], &rc);
- }
- free(thrds);
- }
- else
- rc = setkey_helper(clone(id));
- }
-
- if (!rc) {
- if (setkey_copy[0].len > (*list)[0].len)
- *list = realloc(*list, setkey_copy[0].len * sizeof(tablist_t));
- dellist(*list);
- copytab(*list, setkey_copy);
- }
- mtx_destroy(&setkey_mtx);
- dellist(setkey_copy);
- free(setkey_copy);
- return rc;
-}
-
-tablist_t *delkey_copy;
-char **delkeys_keys;
-int keys_len;
-
-mtx_t delkey_mtx;
-int delkeys(tablist_t *list, int id, char **keys, int len)
-{
- if (id < -1 || len < 0)
- return -1;
- if (mtx_init(&delkey_mtx, mtx_plain) != thrd_success)
- return -2;
- int rc = 0;
- delkey_copy = calloc(list[0].len, sizeof(tablist_t));
- copytab(delkey_copy, list);
- delkeys_keys = keys;
- keys_len = len;
-
- if (id == -1) {
- thrd_t *thrds = calloc(list[0].len, sizeof(thrd_t));
- for (int i = 0; i < delkey_copy[0].len; ++i)
- thrd_create(&thrds[i], delkey_helper, clone(i));
- for (int i = 0; i < delkey_copy[0].len; ++i) {
- if (rc)
- thrd_join(thrds[i], NULL);
- else
- thrd_join(thrds[i], &rc);
- }
- free(thrds);
- } else
- rc = delkey_helper(clone(id));
-
- if (!rc) {
- dellist(list);
- memmove(list, delkey_copy, delkey_copy[0].len * sizeof(tablist_t));
- } else
- dellist(delkey_copy);
- mtx_destroy(&delkey_mtx);
- free(delkey_copy);
- return rc;
-}
-
-static int setkey_helper(void *thr_data)
-{
- int rc;
- int *id = (int *) thr_data;
-
- mtx_lock(&setkey_mtx);
- rc = setkey(&setkey_copy, *id, setkey_str);
- mtx_unlock(&setkey_mtx);
-
- free(id);
- return rc;
-}
-
-static int delkey_helper(void *thr_data)
-{
- int rc = 0;
- int *id = (int *) thr_data;
-
- mtx_lock(&delkey_mtx);
- if (keys_len > 0 && delkeys_keys != NULL)
- for (int i = 0; i < keys_len; ++i) {
- if (delkeys_keys[i] == NULL)
- return -3;
- rc = delkey(delkey_copy, *id, delkeys_keys[i]);
- }
- else {
- tablist_t *indexes = getkeys(delkey_copy, *id, NULL, 0);
- for (int i = 0; indexes[0].tab[i].flag; ++i)
- rc = delkey(delkey_copy, *id, indexes[0].tab[i].key);
- free(indexes);
- }
- mtx_unlock(&delkey_mtx);
-
- free(id);
- return rc;
-}
-
-static int *clone(int n)
-{
- int *c = calloc(1, sizeof(int));
- *c = n;
- return c;
-}
-
-static tablist_t *copytab(tablist_t *dst, tablist_t *src)
-{
- if (dst == NULL)
- return NULL;
- dst[0].len = src[0].len;
- for (int i = 0; i < src[0].len; ++i) {
- for (int j = 0; j < TABLEN; ++j) {
- dst[i].tab[j] = (tabidx_t) { NULL, 0, { 0 } };
- if (src[i].tab[j].flag) {
- switch (src[i].tab[j].flag) {
- case 3:
- dst[i].tab[j].value.str =
- calloc(strlen(src[i].tab[j].value.str) + 1, sizeof(char));
- strcpy(dst[i].tab[j].value.str, src[i].tab[j].value.str);
- break;
- default:
- dst[i].tab[j].value = src[i].tab[j].value;
- break;
- }
- dst[i].tab[j].flag = src[i].tab[j].flag;
- dst[i].tab[j].key = calloc(strlen(src[i].tab[j].key) + 1, sizeof(char));
- strcpy(dst[i].tab[j].key, src[i].tab[j].key);
- }
- }
- }
- return dst;
-}
-
-static void dellist(tablist_t *list)
-{
- for (int i = 0; i < list[0].len; ++i) {
- tablist_t *indexes = getkeys(list, i, NULL, 0);
- for (int j = 0; indexes[0].tab[j].flag; ++j)
- delkey(list, i, indexes[0].tab[j].key);
- free(indexes);
- }
-}
diff --git a/src/lib/delkeys.c b/src/lib/delkeys.c
index e69de29..b1ee83a 100644
--- a/src/lib/delkeys.c
+++ b/src/lib/delkeys.c
@@ -0,0 +1,95 @@
+#include <threads.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "mdb.h"
+#include "utils.h"
+
+static int delkey_helper(void *thr_data);
+static int delkey(tablist_t *, int, char *);
+
+tablist_t *delkey_copy;
+char **delkeys_keys;
+int keys_len;
+
+mtx_t delkey_mtx;
+
+
+int delkeys(tablist_t *list, int id, char **keys, int len)
+{
+ if (id < -1 || len < 0)
+ return -1;
+ if (mtx_init(&delkey_mtx, mtx_plain) != thrd_success)
+ return -2;
+ int rc = 0;
+ delkey_copy = calloc(list[0].len, sizeof(tablist_t));
+ copytab(delkey_copy, list);
+ delkeys_keys = keys;
+ keys_len = len;
+
+ if (id == -1) {
+ thrd_t *thrds = calloc(list[0].len, sizeof(thrd_t));
+ for (int i = 0; i < delkey_copy[0].len; ++i)
+ thrd_create(&thrds[i], delkey_helper, clone(i));
+ for (int i = 0; i < delkey_copy[0].len; ++i) {
+ if (rc)
+ thrd_join(thrds[i], NULL);
+ else
+ thrd_join(thrds[i], &rc);
+ }
+ free(thrds);
+ } else
+ rc = delkey_helper(clone(id));
+
+ if (!rc) {
+ dellist(list);
+ memmove(list, delkey_copy, delkey_copy[0].len * sizeof(tablist_t));
+ } else
+ dellist(delkey_copy);
+ mtx_destroy(&delkey_mtx);
+ free(delkey_copy);
+ return rc;
+}
+
+static int delkey_helper(void *thr_data)
+{
+ int rc = 0;
+ int *id = (int *) thr_data;
+
+ mtx_lock(&delkey_mtx);
+ if (keys_len > 0 && delkeys_keys != NULL)
+ for (int i = 0; i < keys_len; ++i) {
+ if (delkeys_keys[i] == NULL)
+ return -3;
+ rc = delkey(delkey_copy, *id, delkeys_keys[i]);
+ }
+ else {
+ tablist_t *indexes = getkeys(delkey_copy, *id, NULL, 0);
+ for (int i = 0; indexes[0].tab[i].flag; ++i)
+ rc = delkey(delkey_copy, *id, indexes[0].tab[i].key);
+ free(indexes);
+ }
+ mtx_unlock(&delkey_mtx);
+
+ free(id);
+ return rc;
+}
+
+static int delkey(tablist_t *list, int id, char *key)
+{
+ int idx = hash(key);
+ if (list[id].tab[idx].key == NULL)
+ return 1;
+ while (strcmp(list[id].tab[idx].key, key) && idx < TABLEN)
+ idx++;
+ if (idx >= TABLEN)
+ return 2;
+ free(list[id].tab[idx].key);
+ list[id].tab[idx].key = NULL;
+ if (list[id].tab[idx].flag == 3) {
+ free(list[id].tab[idx].value.str);
+ list[id].tab[idx].value.str = NULL;
+ }
+ list[id].tab[idx].flag = 0;
+ return 0;
+}
diff --git a/src/lib/getkeys.c b/src/lib/getkeys.c
index 98442a3..cdba202 100644
--- a/src/lib/getkeys.c
+++ b/src/lib/getkeys.c
@@ -4,8 +4,9 @@
#include <threads.h>
#include "mdb.h"
+#include "utils.h"
-extern tabidx_t getkey(tablist_t *list, int id, char *key);
+static tabidx_t getkey(tablist_t *list, int id, char *key);
static int getkeys_helper(void *data);
@@ -89,3 +90,16 @@ static struct params *pass(tablist_t *list, tablist_t *ret, char **keys, int len
p->pid = pid;
return p;
}
+
+static tabidx_t getkey(tablist_t *list, int id, char *key)
+{
+ int idx = hash(key);
+ if (list[id].tab[idx].key == NULL)
+ return (tabidx_t) { .key = NULL, .flag = 0, .value = { 0 } };
+
+ while (strcmp(list[id].tab[idx].key, key) && idx < TABLEN)
+ idx++;
+ if (idx >= TABLEN)
+ return (tabidx_t) { .key = NULL, .flag = 0, .value = { 0 } };
+ return list[id].tab[idx];
+}
diff --git a/src/lib/keytab.c b/src/lib/keytab.c
deleted file mode 100644
index e9f744a..0000000
--- a/src/lib/keytab.c
+++ /dev/null
@@ -1,119 +0,0 @@
-#include <stdlib.h>
-#include <string.h>
-#include <ctype.h>
-
-#include "mdb.h"
-
-// TODO: move all these functions into seperate files
-
-static int hash(char *key);
-static char **getkv(char *pair);
-
-// getkey - gets a single key from a keytable
-// if it can't find the key it will return an empty table index
-tabidx_t getkey(tablist_t *list, int id, char *key)
-{
- int idx = hash(key);
- if (list[id].tab[idx].key == NULL)
- return (tabidx_t) { .key = NULL, .flag = 0, .value = { 0 } };
-
- while (strcmp(list[id].tab[idx].key, key) && idx < TABLEN)
- idx++;
- if (idx >= TABLEN)
- return (tabidx_t) { .key = NULL, .flag = 0, .value = { 0 } };
- return list[id].tab[idx];
-}
-
-// setkey - adds the given pair to the given key table
-// if setkey fails it will return 1 otherwise 0
-int setkey(tablist_t **list, int id, char *pair)
-{
- if (pair == NULL)
- return 1;
- if (id >= (*list)[0].len) {
- *list = realloc(*list, (id + 1) * sizeof(tablist_t));
- for (int i = (*list)[0].len; i <= id; ++i) {
- for (int j = 0; j < TABLEN; ++j)
- (*list)[i].tab[j] = (tabidx_t) { NULL, 0, { 0 } };
- }
- (*list)[0].len = id + 1;
- }
- char **kv = getkv(pair);
- if (kv == NULL)
- return 2;
-
- int idx = hash(kv[0]);
- while ((*list)[id].tab[idx].key != NULL &&
- strcmp((*list)[id].tab[idx].key, kv[0]) &&
- idx < TABLEN) idx++;
- if (idx >= TABLEN)
- return 2;
- if (!(*list)[id].tab[idx].key)
- (*list)[id].tab[idx].key = kv[0];
- else {
- free(kv[0]);
- if ((*list)[id].tab[idx].flag == 3)
- free((*list)[id].tab[idx].value.str);
- }
- char *end;
- double num = strtod(kv[1], &end);
- if (*end == '\0') {
- (*list)[id].tab[idx].flag = 1;
- (*list)[id].tab[idx].value.num = num;
- } else if (!strcmp(kv[1], "true") || !strcmp(kv[1], "false")) {
- (*list)[id].tab[idx].flag = 2;
- (*list)[id].tab[idx].value.boolean = !strcmp(kv[1], "true");
- } else {
- (*list)[id].tab[idx].flag = 3;
- (*list)[id].tab[idx].value.str = calloc(strlen(kv[1]) + 1, sizeof(char));
- strcpy((*list)[id].tab[idx].value.str, kv[1]);
- }
- free(kv[1]);
- free(kv);
- return 0;
-}
-
-// delkey - removes the given key from the given key table
-int delkey(tablist_t *list, int id, char *key)
-{
- int idx = hash(key);
- if (list[id].tab[idx].key == NULL)
- return 1;
- while (strcmp(list[id].tab[idx].key, key) && idx < TABLEN)
- idx++;
- if (idx >= TABLEN)
- return 2;
- free(list[id].tab[idx].key);
- list[id].tab[idx].key = NULL;
- if (list[id].tab[idx].flag == 3) {
- free(list[id].tab[idx].value.str);
- list[id].tab[idx].value.str = NULL;
- }
- list[id].tab[idx].flag = 0;
- return 0;
-}
-
-static int hash(char *key)
-{
- unsigned long h = 5381;
- for (int i = 0; i < strlen(key); ++i)
- h = ((h << 5) + h) + key[i];
- return h % TABLEN;
-}
-
-static char **getkv(char *pair)
-{
- char **kv = calloc(2, sizeof(char *));
- int i = 0;
- while (pair[i] != ':' && i < strlen(pair))
- i++;
- if (i >= strlen(pair)) {
- free(kv);
- return NULL;
- }
- kv[0] = calloc(i + 1, sizeof(char));
- strncpy(kv[0], pair, i);
- kv[1] = calloc(strlen(pair) - i, sizeof(char));
- strcpy(kv[1], pair + i + 1);
- return kv;
-}
diff --git a/src/lib/setkeys.c b/src/lib/setkeys.c
index e69de29..7e9ef09 100644
--- a/src/lib/setkeys.c
+++ b/src/lib/setkeys.c
@@ -0,0 +1,132 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <threads.h>
+
+#include "mdb.h"
+#include "utils.h"
+
+static int setkey_helper(void *thr_data);
+static int setkey(tablist_t **, int, char *);
+static char **getkv(char *pair);
+
+tablist_t *setkey_copy;
+char *setkey_str;
+
+mtx_t setkey_mtx;
+int setkeys(tablist_t **list, int id, char **pairs, int len)
+{
+ if (id < -1 || pairs == NULL || len <= 0)
+ return -1;
+ if (mtx_init(&setkey_mtx, mtx_plain) != thrd_success)
+ return -2;
+ int rc = 0;
+ setkey_copy = calloc((*list)[0].len, sizeof(tablist_t));
+ copytab(setkey_copy, *list);
+
+ for (int i = 0; i < len; ++i) {
+ setkey_str = pairs[i];
+ if (id == -1) {
+ thrd_t *thrds = calloc((*list)[0].len, sizeof(thrd_t));
+ for (int i = 0; i < setkey_copy[0].len; ++i)
+ thrd_create(&thrds[i], setkey_helper, clone(i));
+ for (int i = 0; i < setkey_copy[0].len; ++i) {
+ if (rc)
+ thrd_join(thrds[i], NULL);
+ else
+ thrd_join(thrds[i], &rc);
+ }
+ free(thrds);
+ }
+ else
+ rc = setkey_helper(clone(id));
+ }
+
+ if (!rc) {
+ if (setkey_copy[0].len > (*list)[0].len)
+ *list = realloc(*list, setkey_copy[0].len * sizeof(tablist_t));
+ dellist(*list);
+ copytab(*list, setkey_copy);
+ }
+ mtx_destroy(&setkey_mtx);
+ dellist(setkey_copy);
+ free(setkey_copy);
+ return rc;
+}
+
+static int setkey_helper(void *thr_data)
+{
+ int rc;
+ int *id = (int *) thr_data;
+
+ mtx_lock(&setkey_mtx);
+ rc = setkey(&setkey_copy, *id, setkey_str);
+ mtx_unlock(&setkey_mtx);
+
+ free(id);
+ return rc;
+}
+
+static int setkey(tablist_t **list, int id, char *pair)
+{
+ if (pair == NULL)
+ return 1;
+ if (id >= (*list)[0].len) {
+ *list = realloc(*list, (id + 1) * sizeof(tablist_t));
+ for (int i = (*list)[0].len; i <= id; ++i) {
+ for (int j = 0; j < TABLEN; ++j)
+ (*list)[i].tab[j] = (tabidx_t) { NULL, 0, { 0 } };
+ }
+ (*list)[0].len = id + 1;
+ }
+ char **kv = getkv(pair);
+ if (kv == NULL)
+ return 2;
+
+ int idx = hash(kv[0]);
+ while ((*list)[id].tab[idx].key != NULL &&
+ strcmp((*list)[id].tab[idx].key, kv[0]) &&
+ idx < TABLEN) idx++;
+ if (idx >= TABLEN)
+ return 2;
+ if (!(*list)[id].tab[idx].key)
+ (*list)[id].tab[idx].key = kv[0];
+ else {
+ free(kv[0]);
+ if ((*list)[id].tab[idx].flag == 3)
+ free((*list)[id].tab[idx].value.str);
+ }
+ char *end;
+ double num = strtod(kv[1], &end);
+ if (*end == '\0') {
+ (*list)[id].tab[idx].flag = 1;
+ (*list)[id].tab[idx].value.num = num;
+ } else if (!strcmp(kv[1], "true") || !strcmp(kv[1], "false")) {
+ (*list)[id].tab[idx].flag = 2;
+ (*list)[id].tab[idx].value.boolean = !strcmp(kv[1], "true");
+ } else {
+ (*list)[id].tab[idx].flag = 3;
+ (*list)[id].tab[idx].value.str = calloc(strlen(kv[1]) + 1, sizeof(char));
+ strcpy((*list)[id].tab[idx].value.str, kv[1]);
+ }
+ free(kv[1]);
+ free(kv);
+ return 0;
+}
+
+static char **getkv(char *pair)
+{
+ char **kv = calloc(2, sizeof(char *));
+ int i = 0;
+ while (pair[i] != ':' && i < strlen(pair))
+ i++;
+ if (i >= strlen(pair)) {
+ free(kv);
+ return NULL;
+ }
+ kv[0] = calloc(i + 1, sizeof(char));
+ strncpy(kv[0], pair, i);
+ kv[1] = calloc(strlen(pair) - i, sizeof(char));
+ strcpy(kv[1], pair + i + 1);
+ return kv;
+}
diff --git a/src/lib/utils.c b/src/lib/utils.c
new file mode 100644
index 0000000..9961c11
--- /dev/null
+++ b/src/lib/utils.c
@@ -0,0 +1,78 @@
+#include <stdlib.h>
+#include <string.h>
+
+#include "utils.h"
+
+static int delkey(tablist_t *, int, char *);
+
+void dellist(tablist_t *list)
+{
+ for (int i = 0; i < list[0].len; ++i) {
+ tablist_t *indexes = getkeys(list, i, NULL, 0);
+ for (int j = 0; indexes[0].tab[j].flag; ++j)
+ delkey(list, i, indexes[0].tab[j].key);
+ free(indexes);
+ }
+}
+
+int hash(char *key)
+{
+ unsigned long h = 5381;
+ for (int i = 0; i < strlen(key); ++i)
+ h = ((h << 5) + h) + key[i];
+ return h % TABLEN;
+}
+
+int *clone(int n)
+{
+ int *c = calloc(1, sizeof(int));
+ *c = n;
+ return c;
+}
+
+tablist_t *copytab(tablist_t *dst, tablist_t *src)
+{
+ if (dst == NULL)
+ return NULL;
+ dst[0].len = src[0].len;
+ for (int i = 0; i < src[0].len; ++i) {
+ for (int j = 0; j < TABLEN; ++j) {
+ dst[i].tab[j] = (tabidx_t) { NULL, 0, { 0 } };
+ if (src[i].tab[j].flag) {
+ switch (src[i].tab[j].flag) {
+ case 3:
+ dst[i].tab[j].value.str =
+ calloc(strlen(src[i].tab[j].value.str) + 1, sizeof(char));
+ strcpy(dst[i].tab[j].value.str, src[i].tab[j].value.str);
+ break;
+ default:
+ dst[i].tab[j].value = src[i].tab[j].value;
+ break;
+ }
+ dst[i].tab[j].flag = src[i].tab[j].flag;
+ dst[i].tab[j].key = calloc(strlen(src[i].tab[j].key) + 1, sizeof(char));
+ strcpy(dst[i].tab[j].key, src[i].tab[j].key);
+ }
+ }
+ }
+ return dst;
+}
+
+static int delkey(tablist_t *list, int id, char *key)
+{
+ int idx = hash(key);
+ if (list[id].tab[idx].key == NULL)
+ return 1;
+ while (strcmp(list[id].tab[idx].key, key) && idx < TABLEN)
+ idx++;
+ if (idx >= TABLEN)
+ return 2;
+ free(list[id].tab[idx].key);
+ list[id].tab[idx].key = NULL;
+ if (list[id].tab[idx].flag == 3) {
+ free(list[id].tab[idx].value.str);
+ list[id].tab[idx].value.str = NULL;
+ }
+ list[id].tab[idx].flag = 0;
+ return 0;
+}
diff --git a/src/lib/utils.h b/src/lib/utils.h
new file mode 100644
index 0000000..64e2787
--- /dev/null
+++ b/src/lib/utils.h
@@ -0,0 +1,19 @@
+#ifndef UTILS_H
+#define UTILS_H
+
+#include "mdb.h"
+
+// dellist: deletes the provided table
+void dellist(tablist_t *list);
+
+/* hash: calculates the DJB2 hash of a string,
+ * and returns that mod TABLEN */
+int hash(char *str);
+
+// clone: clones and returns a pointer to int
+int *clone(int n);
+
+// copytab: copys table src into table dst
+tablist_t *copytab(tablist_t *dst, tablist_t *src);
+
+#endif