summaryrefslogtreecommitdiff
path: root/src/include/engine
diff options
context:
space:
mode:
Diffstat (limited to 'src/include/engine')
-rw-r--r--src/include/engine/delkeys.c174
-rw-r--r--src/include/engine/engine.h23
-rw-r--r--src/include/engine/file.c273
-rw-r--r--src/include/engine/getkeys.c162
-rw-r--r--src/include/engine/setkeys.c232
-rw-r--r--src/include/engine/utils.c111
-rw-r--r--src/include/engine/utils.h2
7 files changed, 482 insertions, 495 deletions
diff --git a/src/include/engine/delkeys.c b/src/include/engine/delkeys.c
index ad9c228..70e668c 100644
--- a/src/include/engine/delkeys.c
+++ b/src/include/engine/delkeys.c
@@ -1,111 +1,109 @@
-#include <threads.h>
#include <stdlib.h>
#include <string.h>
+#include <threads.h>
#include "engine.h"
#include "utils.h"
static int delkey_helper(void *thr_data);
static int delkey(tablist_t *, int, char *);
-static struct params *pass(mtx_t *mtx, tablist_t *list, char **keys, int len, int id);
+static struct params *pass(mtx_t *mtx, tablist_t *list, char **keys, int len,
+ int id);
struct params {
- mtx_t *mtx;
- tablist_t *copy;
- char **keys;
- int len;
- int id;
+ mtx_t *mtx;
+ tablist_t *copy;
+ char **keys;
+ int len;
+ int id;
};
+int delkeys(tablist_t *list, int id, char **keys, int len) {
+ mtx_t mtx;
+ if (id >= list[0].len || id < -1 || len < 0)
+ return -1;
+ if (mtx_init(&mtx, mtx_plain) != thrd_success)
+ return -2;
+ int rc = 0;
+ tablist_t *copy = calloc(list[0].len, sizeof(tablist_t));
+ copytab(copy, list);
-int delkeys(tablist_t *list, int id, char **keys, int len)
-{
- mtx_t mtx;
- if (id >= list[0].len || id < -1 || len < 0)
- return -1;
- if (mtx_init(&mtx, mtx_plain) != thrd_success)
- return -2;
- int rc = 0;
- tablist_t *copy = calloc(list[0].len, sizeof(tablist_t));
- copytab(copy, list);
-
- if (id == -1) {
- thrd_t *thrds = calloc(list[0].len, sizeof(thrd_t));
- for (int i = 0; i < copy[0].len; ++i)
- thrd_create(&thrds[i], delkey_helper, pass(&mtx, copy, keys, len, i));
- for (int i = 0; i < copy[0].len; ++i) {
- if (rc)
- thrd_join(thrds[i], NULL);
- else
- thrd_join(thrds[i], &rc);
- }
- free(thrds);
- } else
- rc = delkey_helper(pass(&mtx, copy, keys, len, id));
+ if (id == -1) {
+ thrd_t *thrds = calloc(list[0].len, sizeof(thrd_t));
+ for (int i = 0; i < copy[0].len; ++i)
+ thrd_create(&thrds[i], delkey_helper,
+ pass(&mtx, copy, keys, len, i));
+ for (int i = 0; i < copy[0].len; ++i) {
+ if (rc)
+ thrd_join(thrds[i], NULL);
+ else
+ thrd_join(thrds[i], &rc);
+ }
+ free(thrds);
+ } else
+ rc = delkey_helper(pass(&mtx, copy, keys, len, id));
- if (!rc) {
- dellist(list);
- memmove(list, copy, copy[0].len * sizeof(tablist_t));
- } else
- dellist(copy);
- mtx_destroy(&mtx);
- free(copy);
- return rc;
+ if (!rc) {
+ dellist(list);
+ memmove(list, copy, copy[0].len * sizeof(tablist_t));
+ } else
+ dellist(copy);
+ mtx_destroy(&mtx);
+ free(copy);
+ return rc;
}
-static int delkey_helper(void *thr_data)
-{
- int rc = 0;
- struct params *p = (struct params *) thr_data;
+static int delkey_helper(void *thr_data) {
+ int rc = 0;
+ struct params *p = (struct params *)thr_data;
- mtx_lock(p->mtx);
- if (p->len > 0 && p->keys != NULL)
- for (int i = 0; i < p->len; ++i) {
- if (p->keys[i] == NULL) {
- mtx_unlock(p->mtx);
- free(p);
- return -3;
- }
- rc = delkey(p->copy, p->id, p->keys[i]);
- }
- else {
- tablist_t *indexes = getkeys(p->copy, p->id, NULL, 0);
- for (int i = 0; indexes[0].tab[i].flag; ++i)
- rc = delkey(p->copy, p->id, indexes[0].tab[i].key);
- free(indexes);
- }
- mtx_unlock(p->mtx);
+ mtx_lock(p->mtx);
+ if (p->len > 0 && p->keys != NULL)
+ for (int i = 0; i < p->len; ++i) {
+ if (p->keys[i] == NULL) {
+ mtx_unlock(p->mtx);
+ free(p);
+ return -3;
+ }
+ rc = delkey(p->copy, p->id, p->keys[i]);
+ }
+ else {
+ tablist_t *indexes = getkeys(p->copy, p->id, NULL, 0);
+ for (int i = 0; indexes[0].tab[i].flag; ++i)
+ rc = delkey(p->copy, p->id, indexes[0].tab[i].key);
+ free(indexes);
+ }
+ mtx_unlock(p->mtx);
- free(p);
- return rc;
+ free(p);
+ 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;
+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;
}
-static struct params *pass(mtx_t *mtx, tablist_t *list, char **keys, int len, int id)
-{
- struct params *p = calloc(1, sizeof(struct params));
- p->mtx = mtx;
- p->copy = list;
- p->keys = keys;
- p->len = len;
- p->id = id;
- return p;
+static struct params *pass(mtx_t *mtx, tablist_t *list, char **keys, int len,
+ int id) {
+ struct params *p = calloc(1, sizeof(struct params));
+ p->mtx = mtx;
+ p->copy = list;
+ p->keys = keys;
+ p->len = len;
+ p->id = id;
+ return p;
}
diff --git a/src/include/engine/engine.h b/src/include/engine/engine.h
index d233bdd..5d9a3ea 100644
--- a/src/include/engine/engine.h
+++ b/src/include/engine/engine.h
@@ -4,18 +4,18 @@
#define TABLEN 1024
typedef struct {
- char *key;
- int flag;
- union {
- char *str;
- double num;
- unsigned int boolean : 1;
- } value;
+ char *key;
+ int flag;
+ union {
+ char *str;
+ double num;
+ unsigned int boolean : 1;
+ } value;
} tabidx_t;
typedef struct {
- int len;
- tabidx_t tab[TABLEN];
+ int len;
+ tabidx_t tab[TABLEN];
} tablist_t;
/* getkeys: gets the provided keys from the provided id,
@@ -32,6 +32,11 @@ int setkeys(tablist_t **list, int id, char **pairs, int len);
* if keys is NULL it, will delete every key. */
int delkeys(tablist_t *list, int id, char **keys, int len);
+// TODO: make readdb and writedb rely on helpers
+// to make them both more private
+
+// TODO: reimplement binary formatting, again
+
/* readdb: reads the provided db file,
* if the filename is NULL, it will return an empty table list,
* if the file format is invalid, it will return NULL. */
diff --git a/src/include/engine/file.c b/src/include/engine/file.c
index 13d6d3e..ab77010 100644
--- a/src/include/engine/file.c
+++ b/src/include/engine/file.c
@@ -11,166 +11,157 @@ static long getsz(FILE *fp);
static int checkmark(char *buf, int pos);
static char *readidx(char *buf, int *i, char ***known, int *len, int *pos);
-tablist_t *readdb(char *filename)
-{
- FILE *fp;
- int len = 2;
- tablist_t *list = calloc(len, sizeof(tablist_t));
- list[0].len = len;
- if (filename == NULL || (fp = fopen(filename, "rb")) == NULL)
- return list;
- long sz = getsz(fp);
- char *buf = calloc(sz, sizeof(char));
- fread(buf, sizeof(char), sz, fp);
- fclose(fp);
+tablist_t *readdb(char *filename) {
+ FILE *fp;
+ int len = 2;
+ tablist_t *list = calloc(len, sizeof(tablist_t));
+ list[0].len = len;
+ if (filename == NULL || (fp = fopen(filename, "rb")) == NULL)
+ return list;
+ long sz = getsz(fp);
+ char *buf = calloc(sz, sizeof(char));
+ fread(buf, sizeof(char), sz, fp);
+ fclose(fp);
- int tlen, vlen, t = 0, v = 0, l = -1;
- char **ktags = calloc((tlen = 2), sizeof(char *));
- char **kvalues = calloc((vlen = 2), sizeof (char *));
- char *key, *value;
- for (int i = 0; i < sz; ++i) {
- if (buf[i] == ':') {
- ++i;
- value = readidx(buf, &i, &kvalues, &vlen, &v);
- char *pair = calloc(strlen(key) + strlen(value) + 2, sizeof(char));
- sprintf(pair, "%s:%s", key, value);
- setkeys(&list, l, &pair, 1);
- free(pair);
- key = value = NULL;
- }
- else {
- key = readidx(buf, &i, &ktags, &tlen, &t);
- if (!strcmp(key, "document"))
- ++l;
- }
- }
- freeknown(&ktags, t);
- freeknown(&kvalues, v);
- free(buf);
- return list;
+ int tlen, vlen, t = 0, v = 0, l = -1;
+ char **ktags = calloc((tlen = 2), sizeof(char *));
+ char **kvalues = calloc((vlen = 2), sizeof(char *));
+ char *key, *value;
+ for (int i = 0; i < sz; ++i) {
+ if (buf[i] == ':') {
+ ++i;
+ value = readidx(buf, &i, &kvalues, &vlen, &v);
+ char *pair = calloc(strlen(key) + strlen(value) + 2, sizeof(char));
+ sprintf(pair, "%s:%s", key, value);
+ setkeys(&list, l, &pair, 1);
+ free(pair);
+ key = value = NULL;
+ } else {
+ key = readidx(buf, &i, &ktags, &tlen, &t);
+ if (!strcmp(key, "document"))
+ ++l;
+ }
+ }
+ freeknown(&ktags, t);
+ freeknown(&kvalues, v);
+ free(buf);
+ return list;
}
-static char *readidx(char *buf, int *i, char ***known, int *len, int *pos)
-{
- char *r = NULL;
- if (checkmark(buf, *i)) {
- if (*len >= *pos) {
- *len *= 2;
- *known = realloc(*known, *len * sizeof(char *));
- }
- *i += 4;
- int nlen;
- memcpy(&nlen, buf + *i, sizeof(int));
- *i += sizeof(int);
- (*known)[*pos] = calloc(nlen + 1, sizeof(char));
- strncpy((*known)[(*pos)++], buf + *i, nlen);
- r = (*known)[(*pos) - 1];
- *i += nlen - 1;
- } else {
- int idx;
- memcpy(&idx, buf + *i, sizeof(int));
- *i += sizeof(int) - 1;
- if (idx > *pos)
- return NULL;
- r = (*known)[idx];
- }
- return r;
+static char *readidx(char *buf, int *i, char ***known, int *len, int *pos) {
+ char *r = NULL;
+ if (checkmark(buf, *i)) {
+ if (*len >= *pos) {
+ *len *= 2;
+ *known = realloc(*known, *len * sizeof(char *));
+ }
+ *i += 4;
+ int nlen;
+ memcpy(&nlen, buf + *i, sizeof(int));
+ *i += sizeof(int);
+ (*known)[*pos] = calloc(nlen + 1, sizeof(char));
+ strncpy((*known)[(*pos)++], buf + *i, nlen);
+ r = (*known)[(*pos) - 1];
+ *i += nlen - 1;
+ } else {
+ int idx;
+ memcpy(&idx, buf + *i, sizeof(int));
+ *i += sizeof(int) - 1;
+ if (idx > *pos)
+ return NULL;
+ r = (*known)[idx];
+ }
+ return r;
}
-static int checkmark(char *buf, int pos)
-{
- int marker = 0xffffffff;
- return !memcmp(buf + pos, &marker, sizeof(int));
+static int checkmark(char *buf, int pos) {
+ int marker = 0xffffffff;
+ return !memcmp(buf + pos, &marker, sizeof(int));
}
-static long getsz(FILE *fp)
-{
- fseek(fp, 0, SEEK_END);
- long sz = ftell(fp);
- fseek(fp, 0, SEEK_SET);
- return sz;
+static long getsz(FILE *fp) {
+ fseek(fp, 0, SEEK_END);
+ long sz = ftell(fp);
+ fseek(fp, 0, SEEK_SET);
+ return sz;
}
static void newtag(char *tag, FILE *fp);
static void writeidx(char ***known, int *len, int *pos, char *str, FILE *fp);
-void writedb(char *filename, tablist_t *list)
-{
- FILE *fp = fopen(filename, "wb");
- if (fp == NULL)
- return;
- int tlen, vlen, t = 0, v = 0;
- char **ktags = calloc((tlen = 2), sizeof(char *));
- writeidx(&ktags, &tlen, &t, "documents", fp);
- writeidx(&ktags, &tlen, &t, "document", fp);
+void writedb(char *filename, tablist_t *list) {
+ FILE *fp = fopen(filename, "wb");
+ if (fp == NULL)
+ return;
+ int tlen, vlen, t = 0, v = 0;
+ char **ktags = calloc((tlen = 2), sizeof(char *));
+ writeidx(&ktags, &tlen, &t, "documents", fp);
+ writeidx(&ktags, &tlen, &t, "document", fp);
- char buf[64];
- char **kvalues = calloc((vlen = 2), sizeof(char *));
- tablist_t *indexes = getkeys(list, -1, NULL, 0);
- for (int i = 0; i < indexes[0].len; ++i) {
- if (i > 0)
- writeidx(&ktags, &tlen, &t, "document", fp);
- for (int j = 0; indexes[i].tab[j].flag; ++j) {
- writeidx(&ktags, &tlen, &t, indexes[i].tab[j].key, fp);
- fputc(':', fp);
- switch (indexes[i].tab[j].flag) {
- case 1:
- snprintf(buf, 64, "%g", indexes[i].tab[j].value.num);
- writeidx(&kvalues, &vlen, &v, buf, fp);
- break;
- case 2:
- writeidx(&kvalues, &vlen, &v,
- indexes[i].tab[j].value.boolean ? "true" : "false", fp);
- break;
- case 3:
- writeidx(&kvalues, &vlen, &v, indexes[i].tab[j].value.str, fp);
- break;
- }
- }
- }
- freeknown(&ktags, t);
- freeknown(&kvalues, v);
- free(indexes);
- fclose(fp);
+ char buf[64];
+ char **kvalues = calloc((vlen = 2), sizeof(char *));
+ tablist_t *indexes = getkeys(list, -1, NULL, 0);
+ for (int i = 0; i < indexes[0].len; ++i) {
+ if (i > 0)
+ writeidx(&ktags, &tlen, &t, "document", fp);
+ for (int j = 0; indexes[i].tab[j].flag; ++j) {
+ writeidx(&ktags, &tlen, &t, indexes[i].tab[j].key, fp);
+ fputc(':', fp);
+ switch (indexes[i].tab[j].flag) {
+ case 1:
+ snprintf(buf, 64, "%g", indexes[i].tab[j].value.num);
+ writeidx(&kvalues, &vlen, &v, buf, fp);
+ break;
+ case 2:
+ writeidx(&kvalues, &vlen, &v,
+ indexes[i].tab[j].value.boolean ? "true" : "false",
+ fp);
+ break;
+ case 3:
+ writeidx(&kvalues, &vlen, &v, indexes[i].tab[j].value.str, fp);
+ break;
+ }
+ }
+ }
+ freeknown(&ktags, t);
+ freeknown(&kvalues, v);
+ free(indexes);
+ fclose(fp);
}
-static void newtag(char *tag, FILE *fp)
-{
- int len = strlen(tag);
- int marker = 0xffffffff;
- fwrite(&marker, sizeof(int), 1, fp);
- fwrite(&len, sizeof(int), 1, fp);
- fwrite(tag, sizeof(char), len, fp);
+static void newtag(char *tag, FILE *fp) {
+ int len = strlen(tag);
+ int marker = 0xffffffff;
+ fwrite(&marker, sizeof(int), 1, fp);
+ fwrite(&len, sizeof(int), 1, fp);
+ fwrite(tag, sizeof(char), len, fp);
}
-static void writeidx(char ***known, int *len, int *pos, char *str, FILE *fp)
-{
- int n;
- if ((n = check(*known, *pos, str)) >= 0)
- fwrite(&n, sizeof(int), 1, fp);
- else {
- if (*pos >= *len) {
- *len *= 2;
- *known = realloc(*known, *len * sizeof(char *));
- }
- (*known)[*pos] = calloc(strlen(str) + 1, sizeof(char));
- strcpy((*known)[*pos], str);
- newtag((*known)[(*pos)++], fp);
- }
+static void writeidx(char ***known, int *len, int *pos, char *str, FILE *fp) {
+ int n;
+ if ((n = check(*known, *pos, str)) >= 0)
+ fwrite(&n, sizeof(int), 1, fp);
+ else {
+ if (*pos >= *len) {
+ *len *= 2;
+ *known = realloc(*known, *len * sizeof(char *));
+ }
+ (*known)[*pos] = calloc(strlen(str) + 1, sizeof(char));
+ strcpy((*known)[*pos], str);
+ newtag((*known)[(*pos)++], fp);
+ }
}
-static void freeknown(char ***known, int pos)
-{
- for (int i = 0; i < pos; ++i)
- free((*known)[i]);
- free(*known);
+static void freeknown(char ***known, int pos) {
+ for (int i = 0; i < pos; ++i)
+ free((*known)[i]);
+ free(*known);
}
-static int check(char **known, int len, char *str)
-{
- for (int i = 0; i < len; ++i) {
- if (!strcmp(str, known[i]))
- return i;
- }
- return -1;
+static int check(char **known, int len, char *str) {
+ for (int i = 0; i < len; ++i) {
+ if (!strcmp(str, known[i]))
+ return i;
+ }
+ return -1;
}
diff --git a/src/include/engine/getkeys.c b/src/include/engine/getkeys.c
index ae2eb73..3193e6b 100644
--- a/src/include/engine/getkeys.c
+++ b/src/include/engine/getkeys.c
@@ -1,6 +1,6 @@
+#include <ctype.h>
#include <stdlib.h>
#include <string.h>
-#include <ctype.h>
#include <threads.h>
#include "engine.h"
@@ -11,96 +11,96 @@ static tabidx_t getkey(tablist_t *list, int id, char *key);
static int getkeys_helper(void *data);
struct params {
- mtx_t *mtx;
- tablist_t *list;
- tablist_t *ret;
- char **keys;
- int len;
- int lid;
- int pid;
+ mtx_t *mtx;
+ tablist_t *list;
+ tablist_t *ret;
+ char **keys;
+ int len;
+ int lid;
+ int pid;
};
-static struct params *pass(mtx_t *mtx, tablist_t *list, tablist_t *ret, char **keys, int len, int lid, int pid);
+static struct params *pass(mtx_t *mtx, tablist_t *list, tablist_t *ret,
+ char **keys, int len, int lid, int pid);
-tablist_t *getkeys(tablist_t *list, int id, char **keys, int klen)
-{
- mtx_t mtx;
- if (id >= list[0].len || id < -1 || mtx_init(&mtx, mtx_plain) != thrd_success)
- return NULL;
- int rc = 0;
- int len = id == -1 ? list[0].len : 1;
- tablist_t *indexes = calloc(len, sizeof(tablist_t));
- indexes[0].len = len;
- if (id >= 0)
- rc = getkeys_helper(pass(&mtx, list, indexes, keys, klen, id, 0));
- else {
- thrd_t *thrds = calloc(list[0].len, sizeof(thrd_t));
- for (int i = 0; i < list[0].len; ++i)
- thrd_create(&thrds[i], getkeys_helper, pass(&mtx, list, indexes, keys, klen, i, i));
- for (int i = 0; i < list[0].len; ++i) {
- if (rc)
- thrd_join(thrds[i], NULL);
- else
- thrd_join(thrds[i], &rc);
- }
- free(thrds);
- }
+tablist_t *getkeys(tablist_t *list, int id, char **keys, int klen) {
+ mtx_t mtx;
+ if (id >= list[0].len || id < -1 ||
+ mtx_init(&mtx, mtx_plain) != thrd_success)
+ return NULL;
+ int rc = 0;
+ int len = id == -1 ? list[0].len : 1;
+ tablist_t *indexes = calloc(len, sizeof(tablist_t));
+ indexes[0].len = len;
+ if (id >= 0)
+ rc = getkeys_helper(pass(&mtx, list, indexes, keys, klen, id, 0));
+ else {
+ thrd_t *thrds = calloc(list[0].len, sizeof(thrd_t));
+ for (int i = 0; i < list[0].len; ++i)
+ thrd_create(&thrds[i], getkeys_helper,
+ pass(&mtx, list, indexes, keys, klen, i, i));
+ for (int i = 0; i < list[0].len; ++i) {
+ if (rc)
+ thrd_join(thrds[i], NULL);
+ else
+ thrd_join(thrds[i], &rc);
+ }
+ free(thrds);
+ }
- mtx_destroy(&mtx);
- if (!rc)
- return indexes;
- else {
- free(indexes);
- return NULL;
- }
+ mtx_destroy(&mtx);
+ if (!rc)
+ return indexes;
+ else {
+ free(indexes);
+ return NULL;
+ }
}
-static int getkeys_helper(void *data)
-{
- struct params *p = (struct params *) data;
- int rc = 0;
+static int getkeys_helper(void *data) {
+ struct params *p = (struct params *)data;
+ int rc = 0;
- mtx_lock(p->mtx);
- if (p->keys == NULL) {
- for (int i = 0, j = 0; i < TABLEN; ++i)
- if (p->list[p->lid].tab[i].flag)
- p->ret[p->pid].tab[j++] = p->list[p->lid].tab[i];
- } else {
- for (int i = 0, j = 0; i < p->len; ++i) {
- tabidx_t idx = getkey(p->list, p->lid, p->keys[i]);
- if (idx.flag == 0)
- rc = 1;
- else
- p->ret[p->pid].tab[j++] = idx;
- }
- }
- mtx_unlock(p->mtx);
- free(p);
- return rc;
+ mtx_lock(p->mtx);
+ if (p->keys == NULL) {
+ for (int i = 0, j = 0; i < TABLEN; ++i)
+ if (p->list[p->lid].tab[i].flag)
+ p->ret[p->pid].tab[j++] = p->list[p->lid].tab[i];
+ } else {
+ for (int i = 0, j = 0; i < p->len; ++i) {
+ tabidx_t idx = getkey(p->list, p->lid, p->keys[i]);
+ if (idx.flag == 0)
+ rc = 1;
+ else
+ p->ret[p->pid].tab[j++] = idx;
+ }
+ }
+ mtx_unlock(p->mtx);
+ free(p);
+ return rc;
}
-static struct params *pass(mtx_t *mtx, tablist_t *list, tablist_t *ret, char **keys, int len, int lid, int pid)
-{
- struct params *p = calloc(1, sizeof(struct params));
- p->mtx = mtx;
- p->list = list;
- p->ret = ret;
- p->keys = keys;
- p->len = len;
- p->lid = lid;
- p->pid = pid;
- return p;
+static struct params *pass(mtx_t *mtx, tablist_t *list, tablist_t *ret,
+ char **keys, int len, int lid, int pid) {
+ struct params *p = calloc(1, sizeof(struct params));
+ p->mtx = mtx;
+ p->list = list;
+ p->ret = ret;
+ p->keys = keys;
+ p->len = len;
+ p->lid = lid;
+ 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 } };
+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];
+ 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/include/engine/setkeys.c b/src/include/engine/setkeys.c
index 34ef3d2..f8797e6 100644
--- a/src/include/engine/setkeys.c
+++ b/src/include/engine/setkeys.c
@@ -1,6 +1,6 @@
#include <stdio.h>
-#include <string.h>
#include <stdlib.h>
+#include <string.h>
#include <threads.h>
#include "engine.h"
@@ -11,138 +11,134 @@ static int setkey(tablist_t **, int, char *);
static char **getkv(char *pair);
struct params {
- mtx_t *mtx;
- tablist_t **copy;
- char *pair;
- int id;
+ mtx_t *mtx;
+ tablist_t **copy;
+ char *pair;
+ int id;
};
static struct params *pass(mtx_t *mtx, tablist_t **copy, char *pair, int id);
-int setkeys(tablist_t **list, int id, char **pairs, int len)
-{
- mtx_t mtx;
- if (id < -1 || pairs == NULL || len <= 0)
- return -1;
- if (mtx_init(&mtx, mtx_plain) != thrd_success)
- return -2;
- int rc = 0;
- tablist_t *copy = calloc((*list)[0].len, sizeof(tablist_t));
- copytab(copy, *list);
+int setkeys(tablist_t **list, int id, char **pairs, int len) {
+ mtx_t mtx;
+ if (id < -1 || pairs == NULL || len <= 0)
+ return -1;
+ if (mtx_init(&mtx, mtx_plain) != thrd_success)
+ return -2;
+ int rc = 0;
+ tablist_t *copy = calloc((*list)[0].len, sizeof(tablist_t));
+ copytab(copy, *list);
- for (int i = 0; i < len; ++i) {
- char *pair = pairs[i];
- if (id == -1) {
- thrd_t *thrds = calloc((*list)[0].len, sizeof(thrd_t));
- for (int i = 0; i < copy[0].len; ++i)
- thrd_create(&thrds[i], setkey_helper, pass(&mtx, &copy, pair, i));
- for (int i = 0; i < copy[0].len; ++i) {
- if (rc)
- thrd_join(thrds[i], NULL);
- else
- thrd_join(thrds[i], &rc);
- }
- free(thrds);
- }
- else
- rc = setkey_helper(pass(&mtx, &copy, pair, id));
- }
+ for (int i = 0; i < len; ++i) {
+ char *pair = pairs[i];
+ if (id == -1) {
+ thrd_t *thrds = calloc((*list)[0].len, sizeof(thrd_t));
+ for (int i = 0; i < copy[0].len; ++i)
+ thrd_create(&thrds[i], setkey_helper,
+ pass(&mtx, &copy, pair, i));
+ for (int i = 0; i < copy[0].len; ++i) {
+ if (rc)
+ thrd_join(thrds[i], NULL);
+ else
+ thrd_join(thrds[i], &rc);
+ }
+ free(thrds);
+ } else
+ rc = setkey_helper(pass(&mtx, &copy, pair, id));
+ }
- if (!rc) {
- if (copy[0].len > (*list)[0].len)
- *list = realloc(*list, copy[0].len * sizeof(tablist_t));
- dellist(*list);
- copytab(*list, copy);
- }
- mtx_destroy(&mtx);
- dellist(copy);
- free(copy);
- return rc;
+ if (!rc) {
+ if (copy[0].len > (*list)[0].len)
+ *list = realloc(*list, copy[0].len * sizeof(tablist_t));
+ dellist(*list);
+ copytab(*list, copy);
+ }
+ mtx_destroy(&mtx);
+ dellist(copy);
+ free(copy);
+ return rc;
}
-static int setkey_helper(void *thr_data)
-{
- int rc;
- struct params *p = (struct params *) thr_data;
+static int setkey_helper(void *thr_data) {
+ int rc;
+ struct params *p = (struct params *)thr_data;
- mtx_lock(p->mtx);
- rc = setkey(p->copy, p->id, p->pair);
- mtx_unlock(p->mtx);
+ mtx_lock(p->mtx);
+ rc = setkey(p->copy, p->id, p->pair);
+ mtx_unlock(p->mtx);
- free(p);
- return rc;
+ free(p);
+ 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;
+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;
+ 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;
+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;
}
-static struct params *pass(mtx_t *mtx, tablist_t **copy, char *pair, int id)
-{
- struct params *p = calloc(1, sizeof(struct params));
- p->mtx = mtx;
- p->copy = copy;
- p->pair = pair;
- p->id = id;
- return p;
+static struct params *pass(mtx_t *mtx, tablist_t **copy, char *pair, int id) {
+ struct params *p = calloc(1, sizeof(struct params));
+ p->mtx = mtx;
+ p->copy = copy;
+ p->pair = pair;
+ p->id = id;
+ return p;
}
diff --git a/src/include/engine/utils.c b/src/include/engine/utils.c
index 909c123..f8ef7f0 100644
--- a/src/include/engine/utils.c
+++ b/src/include/engine/utils.c
@@ -5,67 +5,64 @@
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);
- }
+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 hash(char *key) {
+ unsigned long h = 5381;
+ for (int i = 0; i < strlen(key); ++i)
+ h = ((h << 5) + h) + key[i];
+ return h % TABLEN;
}
-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;
+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;
+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/include/engine/utils.h b/src/include/engine/utils.h
index 54bfc17..fe6022f 100644
--- a/src/include/engine/utils.h
+++ b/src/include/engine/utils.h
@@ -6,7 +6,7 @@
// dellist: deletes the provided table
void dellist(tablist_t *list);
-/* hash: calculates the DJB2 hash of a string,
+/* hash: calculates the DJB2 hash of a string,
* and returns that mod TABLEN */
int hash(char *str);