summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHolger Eitzenberger <heitzenberger@astaro.com>2009-10-12 13:35:54 +0200
committerHarald Welte <laforge@gnumonks.org>2010-10-21 19:15:38 +0200
commitc748b369e1446ed5745499b3db7b05537c0375e7 (patch)
treed543d649ad282d1be0dc45a2155464e5d8bee225
parentfa2f3550fff88d7bf0bc5c39cea8872a48a410f1 (diff)
db: improve temporal keymap handling
Signed-off-by: Holger Eitzenberger <holger@eitzenberger.org>
-rw-r--r--include/ulogd/db.h13
-rw-r--r--include/ulogd/plugin.h4
-rw-r--r--src/db.c64
3 files changed, 44 insertions, 37 deletions
diff --git a/include/ulogd/db.h b/include/ulogd/db.h
index 4846f30..7eeeeae 100644
--- a/include/ulogd/db.h
+++ b/include/ulogd/db.h
@@ -38,7 +38,13 @@ struct db_row {
struct ulogd_value value[];
};
-struct db_column {
+/*
+ * Map column to a particular ulogd key.
+ *
+ * This mapping is determined during each propagation because the mapping
+ * of ulogd keys to database columns is N:1.
+ */
+struct db_map {
struct ulogd_key *key;
};
@@ -49,7 +55,10 @@ struct db_instance {
const struct db_driver *driver;
- struct db_column *col;
+ /*
+ * Map key to a database column
+ */
+ struct db_map *map;
int num_cols;
/* batching */
diff --git a/include/ulogd/plugin.h b/include/ulogd/plugin.h
index 2ce77bc..a6f7fa7 100644
--- a/include/ulogd/plugin.h
+++ b/include/ulogd/plugin.h
@@ -87,7 +87,7 @@ struct ulogd_value {
int ulogd_value_to_ascii(const struct ulogd_value *, char *, size_t);
-struct db_column;
+struct db_map;
/* structure describing an input / output parameter of a plugin */
struct ulogd_key {
@@ -98,7 +98,7 @@ struct ulogd_key {
/*
* Map to database column
*/
- struct db_column *col;
+ struct db_map *map;
char *name;
diff --git a/src/db.c b/src/db.c
index 49becad..6837850 100644
--- a/src/db.c
+++ b/src/db.c
@@ -167,30 +167,6 @@ err_rollback:
return rows;
}
-static int
-db_alloc_columns(struct db_instance *di, size_t cols)
-{
- if (!di)
- return -1;
-
- di->col = calloc(cols, sizeof(struct db_column));
- if (!di->col) {
- ulogd_log(ULOGD_FATAL, "%s: out of memory\n", __func__);
- return -1;
- }
-
- return 0;
-}
-
-static void
-db_free_columns(struct db_instance *di)
-{
- if (di) {
- free(di->col);
- di->col = NULL;
- }
-}
-
/**
* Periodic database timer, reponsible for committing database rows.
*
@@ -295,6 +271,30 @@ check_driver(struct ulogd_pluginstance *pi)
}
static int
+db_alloc_keymap(struct db_instance *di, size_t cols)
+{
+ if (!di || !cols)
+ return -1;
+
+ di->map = calloc(cols, sizeof(struct db_map));
+ if (!di->map) {
+ ulogd_log(ULOGD_FATAL, "%s: out of memory\n", __func__);
+ return -1;
+ }
+
+ return 0;
+}
+
+static void
+db_free_keymap(struct db_instance *di)
+{
+ if (di) {
+ free(di->map);
+ di->map = NULL;
+ }
+}
+
+static int
keymap_lexer(const char **in)
{
char *out = keymap_lexbuf, *end = keymap_lexbuf + KEYMAP_LEX_LEN - 1;
@@ -428,7 +428,7 @@ keymap_map_keys(const char *str, struct ulogd_keyset *set,
goto err_inval;
BUG_ON(!set->keys[keyno].name);
col = atoi(keymap_lexbuf);
- set->keys[keyno].col = &di->col[col];
+ set->keys[keyno].map = &di->map[col];
ulogd_log(ULOGD_DEBUG, "db: key%d ('%s') maps to col%d\n",
keyno, set->keys[keyno].name, col);
keyno++;
@@ -519,7 +519,7 @@ ulogd_db_map_keys(struct ulogd_pluginstance *pi)
if (ulogd_init_keyset(set, KEYSET_F_ALLOC) < 0)
return -1;
- if (db_alloc_columns(di, di->num_cols) < 0)
+ if (db_alloc_keymap(di, di->num_cols) < 0)
goto err_free;
if (keymap_map_keys(keymap, set, di) < 0)
@@ -532,7 +532,7 @@ ulogd_db_map_keys(struct ulogd_pluginstance *pi)
return 0;
err_free:
- db_free_columns(di);
+ db_free_keymap(di);
ulogd_free_keyset(set);
return -1;
}
@@ -669,8 +669,7 @@ ulogd_db_interp(struct ulogd_pluginstance *pi, unsigned *flags)
if (blackhole_ce(pi))
return ULOGD_IRET_OK;
- /* reset key pointers */
- memset(di->col, 0, di->num_cols * sizeof(di->col[0]));
+ memset(di->map, 0, di->num_cols * sizeof(di->map[0]));
for (i = 0; i < pi->input.num_keys; i++) {
key = &in[i];
@@ -678,9 +677,8 @@ ulogd_db_interp(struct ulogd_pluginstance *pi, unsigned *flags)
if (!key_src_valid(key))
continue;
- BUG_ON(!key->col);
- BUG_ON(key->col->key);
- key->col->key = key_src(key);
+ BUG_ON(!key->map);
+ key->map->key = key_src(key);
}
if ((row = db_row_new(pi)) == NULL)
@@ -691,7 +689,7 @@ ulogd_db_interp(struct ulogd_pluginstance *pi, unsigned *flags)
* everything else.
*/
for (i = 0; i < di->num_cols; i++) {
- key = di->col[i].key;
+ key = di->map[i].key;
if (!key)
memset(&row->value[i], 0, sizeof(struct ulogd_value));