Code

store: Added a generic JSON formatter.
[sysdb.git] / src / include / core / store.h
index d5a76941c06255085215a77215c2cd17b0b89f3c..6dd7d1ade1ca98b952a4ce05a8b1a4f6bc21fa49 100644 (file)
@@ -64,22 +64,63 @@ enum {
 struct sdb_store_obj;
 typedef struct sdb_store_obj sdb_store_obj_t;
 
+/*
+ * Expressions represent arithmetic expressions based on stored objects and
+ * their various attributes.
+ *
+ * An expression object inherits from sdb_object_t and, thus, may safely be
+ * cast to a generic object.
+ */
+struct sdb_store_expr;
+typedef struct sdb_store_expr sdb_store_expr_t;
+#define SDB_STORE_EXPR(obj) ((sdb_store_expr_t *)(obj))
+
+/*
+ * Store matchers may be used to lookup hosts from the store based on their
+ * various attributes. Service and attribute matchers are applied to a host's
+ * services and attributes and evaluate to true if *any* service or attribute
+ * matches.
+ *
+ * A store matcher object inherits from sdb_object_t and, thus, may safely be
+ * cast to a generic object.
+ */
+struct sdb_store_matcher;
+typedef struct sdb_store_matcher sdb_store_matcher_t;
+#define SDB_STORE_MATCHER(obj) ((sdb_store_matcher_t *)(obj))
+
+/*
+ * A JSON formatter converts a stored object into the JSON format.
+ * See http://www.ietf.org/rfc/rfc4627.txt
+ */
+struct sdb_store_json_formatter;
+typedef struct sdb_store_json_formatter sdb_store_json_formatter_t;
+
 /*
  * Queryable fields of a stored object.
  */
 enum {
-       SDB_FIELD_LAST_UPDATE = 1, /* datetime */
-       SDB_FIELD_AGE,             /* datetime */
-       SDB_FIELD_INTERVAL,        /* datetime */
-       SDB_FIELD_BACKEND,         /* string */
+       SDB_FIELD_NAME = 1,    /* string */
+       SDB_FIELD_LAST_UPDATE, /* datetime */
+       SDB_FIELD_AGE,         /* datetime */
+       SDB_FIELD_INTERVAL,    /* datetime */
+       SDB_FIELD_BACKEND,     /* string */
 };
 
 #define SDB_FIELD_TO_NAME(f) \
-       (((f) == SDB_FIELD_LAST_UPDATE) ? "last-update" \
+       (((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" : "unknown")
 
+#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) \
+               : -1)
+
 /*
  * sdb_store_clear:
  * Clear the entire store and remove all stored objects.
@@ -239,7 +280,8 @@ sdb_store_fetch_timeseries(const char *hostname, const char *metric,
  * sdb_store_get_field:
  * Get the value of a stored object's queryable field. The caller is
  * responsible for freeing any dynamically allocated memory possibly stored in
- * the returned value.
+ * the returned value. If 'res' is NULL, the function will return whether the
+ * field exists.
  *
  * Note: Retrieving the backend this way is not currently supported.
  *
@@ -251,14 +293,20 @@ int
 sdb_store_get_field(sdb_store_obj_t *obj, int field, sdb_data_t *res);
 
 /*
- * Expressions specify arithmetic expressions.
+ * sdb_store_get_attr:
+ * Get the value of a stored object's attribute. The caller is responsible for
+ * freeing any dynamically allocated memory possibly stored in the returned
+ * value. If 'res' is NULL, the function will return whether the attribute
+ * exists. If specified, only attributes matching the filter will be
+ * considered.
  *
- * A expression object inherits from sdb_object_t and, thus, may safely be
- * cast to a generic object.
+ * Returns:
+ *  - 0 if the attribute exists
+ *  - a negative value else
  */
-struct sdb_store_expr;
-typedef struct sdb_store_expr sdb_store_expr_t;
-#define SDB_STORE_EXPR(obj) ((sdb_store_expr_t *)(obj))
+int
+sdb_store_get_attr(sdb_store_obj_t *obj, const char *name, sdb_data_t *res,
+               sdb_store_matcher_t *filter);
 
 /*
  * sdb_store_expr_create:
@@ -284,6 +332,19 @@ sdb_store_expr_create(int op, sdb_store_expr_t *left, sdb_store_expr_t *right);
 sdb_store_expr_t *
 sdb_store_expr_fieldvalue(int field);
 
+/*
+ * sdb_store_expr_attrvalue:
+ * Creates an expression which evaluates to the value of the specified
+ * attribute of a stored object. Evaluates to a NULL value if the attribute
+ * does not exist.
+ *
+ * Returns:
+ *  - an expression object on success
+ *  - NULL else
+ */
+sdb_store_expr_t *
+sdb_store_expr_attrvalue(const char *name);
+
 /*
  * sdb_store_expr_constvalue:
  * Creates an expression which evaluates to the specified constant value.
@@ -301,7 +362,8 @@ sdb_store_expr_constvalue(const sdb_data_t *value);
  * result in 'res'. The result's value will be allocated dynamically if
  * necessary and, thus, should be free'd by the caller (e.g. using
  * sdb_data_free_datum). The object may be NULL, in which case the expression
- * needs to evaluate to a constant value.
+ * needs to evaluate to a constant value. If specified, only objects matching
+ * the filter will be used during the evaluation.
  *
  * Returns:
  *  - 0 on success
@@ -309,140 +371,138 @@ sdb_store_expr_constvalue(const sdb_data_t *value);
  */
 int
 sdb_store_expr_eval(sdb_store_expr_t *expr, sdb_store_obj_t *obj,
-               sdb_data_t *res);
+               sdb_data_t *res, sdb_store_matcher_t *filter);
 
 /*
- * Conditionals may be used to lookup hosts from the store based on a
- * conditional expression.
- *
- * A conditional object inherits from sdb_object_t and, thus, may safely be
- * cast to a generic object.
+ * sdb_store_isnull_matcher:
+ * Creates a matcher matching NULL values.
  */
-struct sdb_store_cond;
-typedef struct sdb_store_cond sdb_store_cond_t;
-#define SDB_STORE_COND(obj) ((sdb_store_cond_t *)(obj))
+sdb_store_matcher_t *
+sdb_store_isnull_matcher(sdb_store_expr_t *expr);
 
 /*
- * sdb_store_attr_cond:
- * Creates a conditional based on attribute values. The value of stored
- * attributes is compared against the value the expression evaluates to. See
- * sdb_data_cmp for details about the comparison.
+ * sdb_store_isnnull_matcher:
+ * Creates a matcher matching non-NULL values.
  */
-sdb_store_cond_t *
-sdb_store_attr_cond(const char *name, sdb_store_expr_t *expr);
+sdb_store_matcher_t *
+sdb_store_isnnull_matcher(sdb_store_expr_t *expr);
 
 /*
- * sdb_store_obj_cond:
- * Creates a conditional based on queryable object fields. The respective
- * field of *any* object type is compared against the value the expression
- * evaluates to.
+ * sdb_store_any_matcher:
+ * Creates a matcher iterating over objects of the specified type. It matches
+ * if *any* of those objects match 'm'.
  */
-sdb_store_cond_t *
-sdb_store_obj_cond(int field, sdb_store_expr_t *expr);
+sdb_store_matcher_t *
+sdb_store_any_matcher(int type, sdb_store_matcher_t *m);
 
 /*
- * Store matchers may be used to lookup hosts from the store based on their
- * various attributes. Service and attribute matchers are applied to a host's
- * services and attributes and evaluate to true if *any* service or attribute
- * matches.
- *
- * A store matcher object inherits from sdb_object_t and, thus, may safely be
- * cast to a generic object.
+ * sdb_store_all_matcher:
+ * Creates a matcher iterating over objects of the specified type. It matches
+ * if *all* of those objects match 'm'.
  */
-struct sdb_store_matcher;
-typedef struct sdb_store_matcher sdb_store_matcher_t;
-#define SDB_STORE_MATCHER(obj) ((sdb_store_matcher_t *)(obj))
+sdb_store_matcher_t *
+sdb_store_all_matcher(int type, sdb_store_matcher_t *m);
 
 /*
- * sdb_store_name_matcher:
- * Creates a matcher matching by the specified object type's name. If 're' is
- * true, the specified name is treated as a POSIX extended regular expression.
- * Else, the exact name has to match (case-insensitive).
+ * sdb_store_lt_matcher, sdb_store_le_matcher, sdb_store_eq_matcher,
+ * sdb_store_ge_matcher, sdb_store_gt_matcher:
+ * Create conditional matchers comparing the values of two expressions. The
+ * matcher matches if the left expression compres less than, less or equal
+ * than, equal to, not equal to, greater or equal than, or greater than the
+ * right expression.
  */
 sdb_store_matcher_t *
-sdb_store_name_matcher(int type, const char *name, _Bool re);
+sdb_store_lt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
+sdb_store_matcher_t *
+sdb_store_le_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
+sdb_store_matcher_t *
+sdb_store_eq_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
+sdb_store_matcher_t *
+sdb_store_ne_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
+sdb_store_matcher_t *
+sdb_store_ge_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
+sdb_store_matcher_t *
+sdb_store_gt_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
 
 /*
- * sdb_store_attr_matcher:
- * Creates a matcher matching attributes based on their value. If 're' is
- * true, the specified name is treated as a POSIX extended regular expression.
- * Else, the exact name has to match (case-insensitive).
+ * sdb_store_in_matcher:
+ * Creates a matcher which matches if the right value evaluates to an array
+ * value and the left value is included in that array. See sdb_data_inarray
+ * for more details.
  */
 sdb_store_matcher_t *
-sdb_store_attr_matcher(const char *name, const char *value, _Bool re);
+sdb_store_in_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
 
 /*
- * sdb_store_isnull_matcher:
- * Creates a matcher matching "missing" attributes.
+ * sdb_store_regex_matcher:
+ * Creates a matcher which matches the string value the left expression
+ * evaluates to against the regular expression the right expression evaluates
+ * to. The right expression may either be a constant value regular expression
+ * or string or a dynamic value evaluating to a string. In the latter case,
+ * the string is compiled to a regex every time the matcher is executed.
  */
 sdb_store_matcher_t *
-sdb_store_isnull_matcher(const char *attr_name);
+sdb_store_regex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
 
 /*
- * sdb_store_lt_matcher, sdb_store_le_matcher, sdb_store_eq_matcher,
- * sdb_store_ge_matcher, sdb_store_gt_matcher:
- * Creates a matcher based on a conditional. The matcher matches objects for
- * which the conditional evaluates the object to compare less than, less or
- * equal, equal, greater or equal, or greater than the conditional's value
- * repsectively.
+ * sdb_store_nregex_matcher:
+ * Creates a regex matcher just like sdb_store_regex_matcher except that it
+ * matches in case the regular expression does not match.
  */
 sdb_store_matcher_t *
-sdb_store_lt_matcher(sdb_store_cond_t *cond);
-sdb_store_matcher_t *
-sdb_store_le_matcher(sdb_store_cond_t *cond);
-sdb_store_matcher_t *
-sdb_store_eq_matcher(sdb_store_cond_t *cond);
-sdb_store_matcher_t *
-sdb_store_ge_matcher(sdb_store_cond_t *cond);
-sdb_store_matcher_t *
-sdb_store_gt_matcher(sdb_store_cond_t *cond);
+sdb_store_nregex_matcher(sdb_store_expr_t *left, sdb_store_expr_t *right);
 
 /*
- * sdb_store_parse_object_type_plural:
- * Parse the type name (plural) of a stored object.
+ * sdb_store_matcher_op_cb:
+ * Callback constructing a matcher operator.
+ */
+typedef sdb_store_matcher_t *(*sdb_store_matcher_op_cb)
+       (sdb_store_expr_t *, sdb_store_expr_t *);
+
+/*
+ * sdb_store_parse_matcher_op:
+ * Parse a matcher operator and return a constructor for the respective
+ * matcher.
  *
  * Returns:
- *  - the object type on success
- *  - a negative value else
+ *  - matcher operator constructor on success
+ *  - NULL else
  */
-int
-sdb_store_parse_object_type_plural(const char *name);
+sdb_store_matcher_op_cb
+sdb_store_parse_matcher_op(const char *op);
 
 /*
- * sdb_store_parse_field_name:
- * Parse the name of a stored object's queryable field.
+ * sdb_store_parse_object_type:
+ * Parse the type name of a stored object.
  *
  * Returns:
- *  - the field id on success
+ *  - the object type on success
  *  - a negative value else
  */
 int
-sdb_store_parse_field_name(const char *name);
+sdb_store_parse_object_type(const char *name);
 
 /*
- * sdb_store_matcher_parse_cmp:
- * Parse a simple compare expression (<obj_type>.<attr> <op> <expression>).
+ * sdb_store_parse_object_type_plural:
+ * Parse the type name (plural) of a stored object.
  *
  * Returns:
- *  - a matcher object on success
- *  - NULL else
+ *  - the object type on success
+ *  - a negative value else
  */
-sdb_store_matcher_t *
-sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
-               const char *op, sdb_store_expr_t *expr);
+int
+sdb_store_parse_object_type_plural(const char *name);
 
 /*
- * sdb_store_matcher_parse_field_cmp:
- * Parse a simple compare expression for queryable object fields (<field> <op>
- * <expression>).
+ * sdb_store_parse_field_name:
+ * Parse the name of a stored object's queryable field.
  *
  * Returns:
- *  - a matcher object on success
- *  - NULL else
+ *  - the field id on success
+ *  - a negative value else
  */
-sdb_store_matcher_t *
-sdb_store_matcher_parse_field_cmp(const char *name, const char *op,
-               sdb_store_expr_t *expr);
+int
+sdb_store_parse_field_name(const char *name);
 
 /*
  * sdb_store_dis_matcher:
@@ -484,35 +544,29 @@ int
 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
                sdb_store_matcher_t *filter);
 
-/*
- * sdb_store_matcher_tostring:
- * Format a matcher object as string. This is meant for logging or debugging
- * purposes.
- */
-char *
-sdb_store_matcher_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen);
-
 /*
  * 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.
+ * in the store passing on the lookup filter and the specified user-data. The
+ * lookup aborts early if the callback returns non-zero.
  */
-typedef int (*sdb_store_lookup_cb)(sdb_store_obj_t *obj, void *user_data);
+typedef int (*sdb_store_lookup_cb)(sdb_store_obj_t *obj,
+               sdb_store_matcher_t *filter, void *user_data);
 
 /*
  * sdb_store_scan:
- * Look up objects in the store. The specified callback function is called for
- * each object in the store matching 'm'. The function performs a full scan of
- * all hosts stored in the database. If specified, the filter will be used to
- * preselect objects for further evaluation. See the description of
- * 'sdb_store_matcher_matches' for details.
+ * Look up objects of the specified type in the store. The specified callback
+ * function is called for each object in the store matching 'm'. The function
+ * performs a full scan of all objects stored in the database. If specified,
+ * the filter will be used to preselect objects for further evaluation. See
+ * the description of 'sdb_store_matcher_matches' for details.
  *
  * Returns:
  *  - 0 on success
  *  - a negative value else
  */
 int
-sdb_store_scan(sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
+sdb_store_scan(int type, sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
                sdb_store_lookup_cb cb, void *user_data);
 
 /*
@@ -566,22 +620,56 @@ sdb_store_host_tojson(sdb_store_obj_t *host, sdb_strbuf_t *buf,
                sdb_store_matcher_t *filter, int flags);
 
 /*
- * sdb_store_iter_cb:
- * Store iterator callback. Iteration stops if the callback returns non-zero.
+ * sdb_store_json_formatter:
+ * Create a JSON formatter writing to the specified buffer.
  */
-typedef int (*sdb_store_iter_cb)(sdb_store_obj_t *obj, void *user_data);
+sdb_store_json_formatter_t *
+sdb_store_json_formatter(sdb_strbuf_t *buf);
 
 /*
- * 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.
+ * sdb_store_json_emit:
+ * Serialize a single object to JSON adding it to the string buffer associated
+ * with the formatter object. The serialized object will not include
+ * attributes or any child objects. Instead, call the function again for each
+ * of those objects. All attributes have to be emitted before any other
+ * children types. Use sdb_store_json_emit_full() to emit a full (filtered)
+ * object.
+ *
+ * Note that the output might not be valid JSON before calling
+ * sdb_store_json_finish().
  *
  * Returns:
  *  - 0 on success
  *  - a negative value else
  */
 int
-sdb_store_iterate(sdb_store_iter_cb cb, void *user_data);
+sdb_store_json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj);
+
+/*
+ * sdb_store_json_emit_full:
+ * Serialize a single object including it's attributes and all children to
+ * JSON, adding it to the string buffer associated with the formatter object.
+ * The filter, if specified, is applied to each attribute and child object.
+ * Only matching objects will be included in the output.
+ *
+ * Note that the output might not be valid JSON before calling
+ * sdb_store_json_finish().
+ *
+ * Returns:
+ *  - 0 on success
+ *  - a negative value else
+ */
+int
+sdb_store_json_emit_full(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj,
+               sdb_store_matcher_t *filter);
+
+/*
+ * sdb_store_json_finish:
+ * Finish the JSON output. This function has to be called once after emiting
+ * all objects.
+ */
+int
+sdb_store_json_finish(sdb_store_json_formatter_t *f);
 
 #ifdef __cplusplus
 } /* extern "C" */