Code

Merged branch 'master' of git://git.tokkee.org/sysdb.
[sysdb.git] / src / core / store-private.h
1 /*
2  * SysDB - src/core/store-private.h
3  * Copyright (C) 2012-2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 /*
29  * private data structures used by the store module
30  */
32 #ifndef SDB_CORE_STORE_PRIVATE_H
33 #define SDB_CORE_STORE_PRIVATE_H 1
35 #include "core/store.h"
37 #include <sys/types.h>
38 #include <regex.h>
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
44 struct sdb_store_obj {
45         sdb_object_t super;
47         /* object type */
48         int type;
50         /* common meta information */
51         sdb_time_t last_update;
52         sdb_time_t interval; /* moving average */
53         char **backends;
54         size_t backends_num;
55         sdb_store_obj_t *parent;
56 };
57 #define STORE_OBJ(obj) ((sdb_store_obj_t *)(obj))
58 #define STORE_CONST_OBJ(obj) ((const sdb_store_obj_t *)(obj))
60 typedef struct {
61         sdb_store_obj_t super;
63         sdb_data_t value;
64 } sdb_attribute_t;
65 #define ATTR(obj) ((sdb_attribute_t *)(obj))
66 #define CONST_ATTR(obj) ((const sdb_attribute_t *)(obj))
68 typedef struct {
69         sdb_store_obj_t super;
71         sdb_llist_t *attributes;
72 } sdb_service_t;
73 #define SVC(obj) ((sdb_service_t *)(obj))
74 #define CONST_SVC(obj) ((const sdb_service_t *)(obj))
76 typedef struct {
77         sdb_store_obj_t super;
79         sdb_llist_t *services;
80         sdb_llist_t *attributes;
81 } sdb_host_t;
82 #define HOST(obj) ((sdb_host_t *)(obj))
83 #define CONST_HOST(obj) ((const sdb_host_t *)(obj))
85 /* shortcuts for accessing service/host attributes */
86 #define _last_update super.last_update
87 #define _interval super.interval
89 /*
90  * conditionals
91  */
93 /* compares a host using the specified conditional */
94 typedef int (*cmp_cb)(sdb_host_t *, sdb_store_cond_t *);
96 struct sdb_store_cond {
97         sdb_object_t super;
98         cmp_cb cmp;
99 };
101 typedef struct {
102         sdb_store_cond_t super;
103         char *name;
104         sdb_data_t value;
105 } attr_cond_t;
106 #define ATTR_C(obj) ((attr_cond_t *)(obj))
108 /*
109  * matchers
110  */
112 /* when adding to this, also update 'matchers' and 'matchers_tostring'
113  * in store_lookup.c */
114 enum {
115         MATCHER_OR,
116         MATCHER_AND,
117         MATCHER_NOT,
118         MATCHER_NAME,
119         MATCHER_ATTR,
120         MATCHER_LT,
121         MATCHER_LE,
122         MATCHER_EQ,
123         MATCHER_GE,
124         MATCHER_GT,
125 };
127 #define MATCHER_SYM(t) \
128         (((t) == MATCHER_OR) ? "OR" \
129                 : ((t) == MATCHER_AND) ? "AND" \
130                 : ((t) == MATCHER_NOT) ? "NOT" \
131                 : ((t) == MATCHER_NAME) ? "NAME" \
132                 : ((t) == MATCHER_ATTR) ? "ATTR" \
133                 : ((t) == MATCHER_LT) ? "<" \
134                 : ((t) == MATCHER_LE) ? "<=" \
135                 : ((t) == MATCHER_EQ) ? "=" \
136                 : ((t) == MATCHER_GE) ? ">=" \
137                 : ((t) == MATCHER_GT) ? ">" \
138                 : "UNKNOWN")
140 /* match the name of something */
141 typedef struct {
142         char    *name;
143         regex_t *name_re;
144 } string_matcher_t;
146 /* matcher base type */
147 struct sdb_store_matcher {
148         sdb_object_t super;
149         /* type of the matcher */
150         int type;
151 };
152 #define M(m) ((sdb_store_matcher_t *)(m))
154 /* infix operator matcher */
155 typedef struct {
156         sdb_store_matcher_t super;
158         /* left and right hand operands */
159         sdb_store_matcher_t *left;
160         sdb_store_matcher_t *right;
161 } op_matcher_t;
162 #define OP_M(m) ((op_matcher_t *)(m))
164 /* unary operator matcher */
165 typedef struct {
166         sdb_store_matcher_t super;
168         /* operand */
169         sdb_store_matcher_t *op;
170 } uop_matcher_t;
171 #define UOP_M(m) ((uop_matcher_t *)(m))
173 /* match any type of object by it's name */
174 typedef struct {
175         sdb_store_matcher_t super;
177         int obj_type;
178         string_matcher_t name;
179 } name_matcher_t;
180 #define NAME_M(m) ((name_matcher_t *)(m))
182 /* match attributes */
183 typedef struct {
184         sdb_store_matcher_t super;
185         char *name;
186         string_matcher_t value;
187 } attr_matcher_t;
188 #define ATTR_M(m) ((attr_matcher_t *)(m))
190 /* match using conditionals */
191 typedef struct {
192         sdb_store_matcher_t super;
193         sdb_store_cond_t *cond;
194 } cond_matcher_t;
195 #define COND_M(m) ((cond_matcher_t *)(m))
197 #ifdef __cplusplus
198 } /* extern "C" */
199 #endif
201 #endif /* ! SDB_CORE_STORE_H */
203 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */