index 1dfa9ab51005f3b116ce18332b9f6124c95cd833..5f60c8dfb0a4a1091953ab3a82d8d4a36632fefb 100644 (file)
--- a/src/include/core/store.h
+++ b/src/include/core/store.h
/*
* SysDB - src/include/core/store.h
- * Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
+ * Copyright (C) 2012-2015 Sebastian 'tokkee' Harl <sh@tokkee.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
#include "core/object.h"
#include "core/data.h"
#include "core/time.h"
-#include "utils/llist.h"
+#include "parser/ast.h"
#include "utils/strbuf.h"
#include <stdio.h>
#endif
/*
- * sdb_store_base_t represents the super-class of any object stored in the
- * database. It inherits from sdb_object_t and may safely be cast to a generic
- * object to access its name.
+ * Store object types.
*/
-struct sdb_store_base;
-typedef struct sdb_store_base sdb_store_base_t;
+enum {
+ SDB_HOST = 1,
+ SDB_SERVICE,
+ SDB_METRIC,
-/*
- * sdb_store_clear:
- * Clear the entire store and remove all stored objects.
- */
-void
-sdb_store_clear(void);
+ SDB_ATTRIBUTE = 1 << 4,
-/*
- * sdb_store_host:
- * Add/update a host in the store. If the host, identified by its
- * canonicalized name, already exists, it will be updated according to the
- * specified name and timestamp. Else, a new entry will be created in the
- * store. Any memory required for storing the entry will be allocated an
- * managed by the store itself.
- *
- * Returns:
- * - 0 on success
- * - a positive value if the new entry is older than the currently stored
- * entry (in this case, no update will happen)
- * - a negative value on error
- */
-int
-sdb_store_host(const char *name, sdb_time_t last_update);
+ /*
+ * Queryable fields of a stored object.
+ */
+ SDB_FIELD_NAME = 1 << 8, /* type: string */
+ SDB_FIELD_LAST_UPDATE, /* type: datetime */
+ SDB_FIELD_AGE, /* type: datetime */
+ SDB_FIELD_INTERVAL, /* type: datetime */
+ SDB_FIELD_BACKEND, /* type: array of strings */
+ SDB_FIELD_VALUE, /* attributes only; type: type of the value */
+ SDB_FIELD_TIMESERIES, /* metrics only; type: boolean */
+};
+#define SDB_STORE_TYPE_TO_NAME(t) \
+ (((t) == SDB_HOST) ? "host" \
+ : ((t) == SDB_SERVICE) ? "service" \
+ : ((t) == SDB_METRIC) ? "metric" \
+ : ((t) == SDB_ATTRIBUTE) ? "attribute" \
+ : ((t) == (SDB_ATTRIBUTE | SDB_HOST)) ? "host attribute" \
+ : ((t) == (SDB_ATTRIBUTE | SDB_SERVICE)) ? "service attribute" \
+ : ((t) == (SDB_ATTRIBUTE | SDB_METRIC)) ? "metric attribute" \
+ : "unknown")
-/*
- * sdb_store_has_host:
- * sdb_store_get_host:
- * Query the store for a host by its (canonicalized) name.
- *
- * sdb_store_get_host increments the ref count of the host object. The caller
- * needs to deref it when no longer using it.
- */
-_Bool
-sdb_store_has_host(const char *name);
+#define SDB_FIELD_TO_NAME(f) \
+ (((f) == SDB_FIELD_NAME) ? "name" \
+ : ((f) == SDB_FIELD_LAST_UPDATE) ? "last-update" \
+ : ((f) == SDB_FIELD_AGE) ? "age" \
+ : ((f) == SDB_FIELD_INTERVAL) ? "interval" \
+ : ((f) == SDB_FIELD_BACKEND) ? "backend" \
+ : ((f) == SDB_FIELD_VALUE) ? "value" \
+ : ((f) == SDB_FIELD_TIMESERIES) ? "timeseries" \
+ : "unknown")
-sdb_store_base_t *
-sdb_store_get_host(const char *name);
+#define SDB_FIELD_TYPE(f) \
+ (((f) == SDB_FIELD_NAME) ? SDB_TYPE_STRING \
+ : ((f) == SDB_FIELD_LAST_UPDATE) ? SDB_TYPE_DATETIME \
+ : ((f) == SDB_FIELD_AGE) ? SDB_TYPE_DATETIME \
+ : ((f) == SDB_FIELD_INTERVAL) ? SDB_TYPE_DATETIME \
+ : ((f) == SDB_FIELD_BACKEND) ? (SDB_TYPE_ARRAY | SDB_TYPE_STRING) \
+ : ((f) == SDB_FIELD_VALUE) ? -1 /* unknown */ \
+ : ((f) == SDB_FIELD_TIMESERIES) ? SDB_TYPE_BOOLEAN \
+ : -1)
/*
- * sdb_store_attribute:
- * Add/update a host's attribute in the store. If the attribute, identified by
- * its key, already exists for the specified host, it will be updated to the
- * specified values. If the referenced host does not exist, an error will be
- * reported. Else, a new entry will be created in the store. Any memory
- * required for storing the entry will be allocated and managed by the store
- * itself.
- *
- * Returns:
- * - 0 on success
- * - a positive value if the new entry is older than the currently stored
- * entry (in this case, no update will happen)
- * - a negative value on error
+ * sdb_store_host_t represents the meta-data of a stored host object.
*/
-int
-sdb_store_attribute(const char *hostname,
- const char *key, const sdb_data_t *value,
- sdb_time_t last_update);
+typedef struct {
+ const char *name;
-/*
- * sdb_store_service:
- * Add/update a store in the store. If the service, identified by its name,
- * already exists for the specified host, it will be updated according to the
- * specified 'service' object. If the referenced host does not exist, an error
- * will be reported. Else, a new entry will be created in the store. Any
- * memory required for storing the entry will be allocated an managed by the
- * store itself. The specified service-object will not be referenced or
- * further accessed.
- *
- * Returns:
- * - 0 on success
- * - a positive value if the new entry is older than the currently stored
- * entry (in this case, no update will happen)
- * - a negative value on error
- */
-int
-sdb_store_service(const char *hostname, const char *name,
- sdb_time_t last_update);
+ sdb_time_t last_update;
+ sdb_time_t interval;
+ const char * const *backends;
+ size_t backends_num;
+} sdb_store_host_t;
+#define SDB_STORE_HOST_INIT { NULL, 0, 0, NULL, 0 }
/*
- * Store matchers may be used to lookup objects from the host based on their
- * various attributes. Each type of matcher evaluates attributes of the
- * respective object type.
- *
- * For each matcher object, *all* specified attributes have to match.
- *
- * A store matcher object inherits from sdb_object_t and, thus, may safely be
- * cast to a generic object.
+ * sdb_store_service_t represents the meta-data of a stored service object.
*/
-struct sdb_store_matcher;
-typedef struct sdb_store_matcher sdb_store_matcher_t;
-#define SDB_STORE_MATCHER(obj) ((sdb_store_matcher_t *)(obj))
+typedef struct {
+ const char *hostname;
+ const char *name;
-/*
- * sdb_store_attr_matcher:
- * Creates a matcher matching attributes based on their name or value. Either
- * a complete name (which will have to match completely but case-independent)
- * or an extended POSIX regular expression may be specified.
- */
-sdb_store_matcher_t *
-sdb_store_attr_matcher(const char *attr_name, const char *attr_name_re,
- const char *attr_value, const char *attr_value_re);
+ sdb_time_t last_update;
+ sdb_time_t interval;
+ const char * const *backends;
+ size_t backends_num;
+} sdb_store_service_t;
+#define SDB_STORE_SERVICE_INIT { NULL, NULL, 0, 0, NULL, 0 }
/*
- * sdb_store_service_matcher:
- * Creates a matcher matching services based on their name or attributes.
+ * sdb_metric_store_t specifies how to access a metric's data.
*/
-sdb_store_matcher_t *
-sdb_store_service_matcher(const char *service_name, const char *service_name_re,
- sdb_store_matcher_t *attr_matcher);
+typedef struct {
+ const char *type;
+ const char *id;
+} sdb_metric_store_t;
/*
- * sdb_store_host_matcher:
- * Creates a matcher matching hosts based on their name, services assigned to
- * the host, or its attributes.
+ * sdb_store_metric_t represents the meta-data of a stored metric object.
*/
-sdb_store_matcher_t *
-sdb_store_host_matcher(const char *host_name, const char *host_name_re,
- sdb_store_matcher_t *service_matcher,
- sdb_store_matcher_t *attr_matcher);
+typedef struct {
+ const char *hostname;
+ const char *name;
+ struct {
+ const char *type;
+ const char *id;
+ } store;
-/*
- * sdb_store_dis_matcher:
- * Creates a matcher matching the disjunction (logical OR) of two matchers.
- */
-sdb_store_matcher_t *
-sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
+ sdb_time_t last_update;
+ sdb_time_t interval;
+ const char * const *backends;
+ size_t backends_num;
+} sdb_store_metric_t;
+#define SDB_STORE_METRIC_INIT { NULL, NULL, { NULL, NULL }, 0, 0, NULL, 0 }
/*
- * sdb_store_con_matcher:
- * Creates a matcher matching the conjunction (logical AND) of two matchers.
+ * sdb_store_attribute_t represents a stored attribute.
*/
-sdb_store_matcher_t *
-sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
+typedef struct {
+ const char *hostname; /* optional */
+ int parent_type;
+ const char *parent;
+ const char *key;
+ sdb_data_t value;
-/*
- * sdb_store_matcher_matches:
- * Check whether the specified matcher matches the specified store object.
- *
- * Returns:
- * - 1 if the object matches
- * - 0 else
- */
-int
-sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_base_t *obj);
+ sdb_time_t last_update;
+ sdb_time_t interval;
+ const char * const *backends;
+ size_t backends_num;
+} sdb_store_attribute_t;
+#define SDB_STORE_ATTRIBUTE_INIT { NULL, 0, NULL, NULL, SDB_DATA_INIT, 0, 0, NULL, 0 }
/*
- * sdb_store_lookup_cb:
- * Lookup callback. It is called for each matching object when looking up data
- * in the store. The lookup aborts if the callback returns non-zero.
+ * A JSON formatter converts stored objects into the JSON format.
+ * See http://www.ietf.org/rfc/rfc4627.txt
+ *
+ * A JSON formatter object inherits from sdb_object_t and, thus, may safely be
+ * cast to a generic object.
*/
-typedef int (*sdb_store_lookup_cb)(sdb_store_base_t *obj, void *user_data);
+struct sdb_store_json_formatter;
+typedef struct sdb_store_json_formatter sdb_store_json_formatter_t;
/*
- * sdb_store_lookup:
- * Look up objects in the store. The specified callback function is called for
- * each object in the store matching 'm'.
+ * A store writer describes the interface for plugins implementing a store.
*
- * Returns:
+ * Any of the call-back functions shall return:
* - 0 on success
- * - a negative value else
+ * - a positive value if the new entry is older than the currently stored
+ * entry (in this case, no update will happen)
+ * - a negative value on error
*/
-int
-sdb_store_lookup(sdb_store_matcher_t *m, sdb_store_lookup_cb cb,
- void *user_data);
+typedef struct {
+ /*
+ * store_host:
+ * Add/update a host in the store. If the host, identified by its
+ * canonicalized name, already exists, it will be updated according to the
+ * specified name and timestamp. Else, a new entry will be created in the
+ * store.
+ */
+ int (*store_host)(sdb_store_host_t *host, sdb_object_t *user_data);
+
+ /*
+ * store_service:
+ * Add/update a service in the store. If the service, identified by its
+ * name, already exists for the specified host, it will be updated
+ * according to the specified name and timestamp. If the referenced host
+ * does not exist, an error will be reported. Else, a new entry will be
+ * created in the store.
+ */
+ int (*store_service)(sdb_store_service_t *service, sdb_object_t *user_data);
+
+ /*
+ * store_metric:
+ * Add/update a metric in the store. If the metric, identified by its
+ * name, already exists for the specified host, it will be updated
+ * according to the specified attributes. If the referenced host does not
+ * exist, an error will be reported. Else, a new entry will be created in
+ * the store.
+ */
+ int (*store_metric)(sdb_store_metric_t *metric, sdb_object_t *user_data);
+
+ /*
+ * store_attribute:
+ * Add/update a host's attribute in the store. If the attribute,
+ * identified by its key, already exists for the specified host, it will
+ * be updated to the specified values. If the referenced host does not
+ * exist, an error will be reported. Else, a new entry will be created in
+ * the store.
+ */
+ int (*store_attribute)(sdb_store_attribute_t *attr, sdb_object_t *user_data);
+} sdb_store_writer_t;
/*
- * Flags for serialization functions.
- *
- * By default, the full object will be included in the serialized output. When
- * specifying any of the flags, the respective information will be left out.
+ * A store reader describes the interface to query a store implementation.
*/
-enum {
- SDB_SKIP_ATTRIBUTES = 1 << 0,
- SDB_SKIP_SERVICES = 1 << 1,
- SDB_SKIP_SERVICE_ATTRIBUTES = 1 << 2,
+typedef struct {
+ /*
+ * prepare_query:
+ * Prepare the query described by 'ast' for execution.
+ */
+ sdb_object_t *(*prepare_query)(sdb_ast_node_t *ast,
+ sdb_strbuf_t *errbuf, sdb_object_t *user_data);
- SDB_SKIP_ALL = 0xffff,
-};
+ /*
+ * execute_query:
+ * Execute a previously prepared query. The callback may expect that only
+ * queries prepared by its respective prepare callback will be passed to
+ * this function. The query result will be passed back via the specified
+ * store writer.
+ */
+ int (*execute_query)(sdb_object_t *q,
+ sdb_store_writer_t *w, sdb_object_t *wd,
+ sdb_strbuf_t *errbuf, sdb_object_t *user_data);
+} sdb_store_reader_t;
/*
- * sdb_store_tojson:
- * Serialize the entire store to JSON and append the result to the specified
- * buffer.
- *
- * Returns:
- * - 0 on success
- * - a negative value on error
+ * Flags for JSON formatting.
*/
-int
-sdb_store_tojson(sdb_strbuf_t *buf, int flags);
+enum {
+ SDB_WANT_ARRAY = 1 << 0,
+};
/*
- * sdb_store_host_tojson:
- * Serialize a host object to JSON and append the result to the specified
- * buffer.
- *
- * Returns:
- * - 0 on success
- * - a negative value on error
+ * sdb_store_json_formatter:
+ * Create a JSON formatter for the specified object types writing to the
+ * specified buffer.
*/
-int
-sdb_store_host_tojson(sdb_store_base_t *host, sdb_strbuf_t *buf, int flags);
+sdb_store_json_formatter_t *
+sdb_store_json_formatter(sdb_strbuf_t *buf, int type, int flags);
/*
- * sdb_store_iter_cb:
- * Store iterator callback. Iteration stops if the callback returns non-zero.
+ * sdb_store_json_finish:
+ * Finish the JSON output. This function has to be called once after emiting
+ * all objects.
*/
-typedef int (*sdb_store_iter_cb)(sdb_store_base_t *obj, void *user_data);
+int
+sdb_store_json_finish(sdb_store_json_formatter_t *f);
/*
- * sdb_store_iterate:
- * Iterate the entire store, calling the specified callback for each object.
- * The user_data pointer is passed on to each call of the callback.
- *
- * Returns:
- * - 0 on success
- * - a negative value else
+ * sdb_store_json_writer:
+ * A store writer implementation that generates JSON output. It expects a
+ * store JSON formatter as its user-data argument.
*/
-int
-sdb_store_iterate(sdb_store_iter_cb cb, void *user_data);
+extern sdb_store_writer_t sdb_store_json_writer;
#ifdef __cplusplus
} /* extern "C" */