draft for removing weak nodes
[protos/libecoli.git] / lib / ecoli_node.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <stdint.h>
8 #include <string.h>
9 #include <assert.h>
10 #include <errno.h>
11
12 #include <ecoli_malloc.h>
13 #include <ecoli_string.h>
14 #include <ecoli_strvec.h>
15 #include <ecoli_keyval.h>
16 #include <ecoli_log.h>
17 #include <ecoli_config.h>
18 #include <ecoli_test.h>
19 #include <ecoli_node.h>
20
21 #include <ecoli_node_str.h>
22 #include <ecoli_node_seq.h>
23
24 EC_LOG_TYPE_REGISTER(node);
25
26 static struct ec_node_type_list node_type_list =
27         TAILQ_HEAD_INITIALIZER(node_type_list);
28
29 const struct ec_node_type *
30 ec_node_type_lookup(const char *name)
31 {
32         struct ec_node_type *type;
33
34         TAILQ_FOREACH(type, &node_type_list, next) {
35                 if (!strcmp(name, type->name))
36                         return type;
37         }
38
39         errno = ENOENT;
40         return NULL;
41 }
42
43 int ec_node_type_register(struct ec_node_type *type)
44 {
45         EC_CHECK_ARG(type->size >= sizeof(struct ec_node), -1, EINVAL);
46
47         if (ec_node_type_lookup(type->name) != NULL) {
48                 errno = EEXIST;
49                 return -1;
50         }
51
52         TAILQ_INSERT_TAIL(&node_type_list, type, next);
53
54         return 0;
55 }
56
57 void ec_node_type_dump(FILE *out)
58 {
59         struct ec_node_type *type;
60
61         TAILQ_FOREACH(type, &node_type_list, next)
62                 fprintf(out, "%s\n", type->name);
63 }
64
65 struct ec_node *__ec_node(const struct ec_node_type *type, const char *id)
66 {
67         struct ec_node *node = NULL;
68
69         EC_LOG(EC_LOG_DEBUG, "create node type=%s id=%s\n",
70                 type->name, id);
71         if (id == NULL) {
72                 errno = EINVAL;
73                 goto fail;
74         }
75
76         node = ec_calloc(1, type->size);
77         if (node == NULL)
78                 goto fail;
79
80         node->type = type;
81         node->refcnt = 1;
82
83         node->id = ec_strdup(id);
84         if (node->id == NULL)
85                 goto fail;
86
87         if (ec_asprintf(&node->desc, "<%s>", type->name) < 0)
88                 goto fail;
89
90         node->attrs = ec_keyval();
91         if (node->attrs == NULL)
92                 goto fail;
93
94         if (type->init_priv != NULL) {
95                 if (type->init_priv(node) < 0)
96                         goto fail;
97         }
98
99         return node;
100
101  fail:
102         if (node != NULL) {
103                 ec_keyval_free(node->attrs);
104                 ec_free(node->desc);
105                 ec_free(node->id);
106         }
107         ec_free(node);
108
109         return NULL;
110 }
111
112 struct ec_node *ec_node(const char *typename, const char *id)
113 {
114         const struct ec_node_type *type;
115
116         type = ec_node_type_lookup(typename);
117         if (type == NULL) {
118                 EC_LOG(EC_LOG_ERR, "type=%s does not exist\n",
119                         typename);
120                 return NULL;
121         }
122
123         return __ec_node(type, id);
124 }
125
126 static void traverse_tree1(const struct ec_node *node)
127 {
128         const struct ec_node *child;
129         size_t i, n;
130
131         if (node->free.state == 1) {
132                 node->free.refcnt++;
133                 return;
134         }
135         node->free.refcnt = 1;
136         node->free.state = 1; // traversed
137         n = node->n_children;
138         for (i = 0; i < n; i++) {
139                 child = node->children[i];
140                 traverse_tree1(child);
141         }
142 }
143
144 void ec_node_free(struct ec_node *node)
145 {
146         struct ec_node *child;
147         bool free_it = false;
148         size_t i, n;
149
150         if (node == NULL)
151                 return;
152
153         assert(node->refcnt > 0);
154
155         // calc free refcnt
156         if (node->free.state == 0)
157                 traverse_tree1(node);
158
159         node->refcnt--;
160         if (node->free.state == 2)
161                 return;
162
163         node->free.state = 2; // beeing freed
164         if (node->refcnt < node->free.refcnt) {
165                 for (i = 0; i < n; i++) {
166                         child = node->children[i];
167                         ec_node_free(child);
168                 }
169         }
170
171         if (node->refcnt > 0) {
172                 node->free.refcnt = 0;
173                 node->free.state = 0;
174                 return;
175         }
176
177         if (node->type != NULL && node->type->free_priv != NULL)
178                 node->type->free_priv(node);
179         ec_free(node->id);
180         ec_free(node->desc);
181         ec_keyval_free(node->attrs);
182         ec_config_free(node->config);
183         ec_free(node);
184 }
185
186 struct ec_node *ec_node_clone(struct ec_node *node)
187 {
188         if (node != NULL)
189                 node->refcnt++;
190         return node;
191 }
192
193 size_t ec_node_get_children_count(const struct ec_node *node)
194 {
195         if (node->type->get_children_count == NULL)
196                 return 0;
197         return node->type->get_children_count(node);
198 }
199
200 struct ec_node *
201 ec_node_get_child(const struct ec_node *node, size_t i)
202 {
203         if (node->type->get_child == NULL)
204                 return NULL;
205         return node->type->get_child(node, i);
206 }
207
208 int
209 ec_node_set_config(struct ec_node *node, struct ec_config *config)
210 {
211         if (node->type->schema == NULL) {
212                 errno = EINVAL;
213                 goto fail;
214         }
215         if (ec_config_validate(config, node->type->schema,
216                                 node->type->schema_len) < 0)
217                 goto fail;
218         if (node->type->set_config != NULL) {
219                 if (node->type->set_config(node, config) < 0)
220                         goto fail;
221         }
222
223         ec_config_free(node->config);
224         node->config = config;
225
226         return 0;
227
228 fail:
229         ec_config_free(config);
230         return -1;
231 }
232
233 const struct ec_config *ec_node_get_config(struct ec_node *node)
234 {
235         return node->config;
236 }
237
238 struct ec_node *ec_node_find(struct ec_node *node, const char *id)
239 {
240         struct ec_node *child, *ret;
241         const char *node_id = ec_node_id(node);
242         size_t i, n;
243
244         if (id != NULL && node_id != NULL && !strcmp(node_id, id))
245                 return node;
246
247         n = ec_node_get_children_count(node);
248         for (i = 0; i < n; i++) {
249                 child = ec_node_get_child(node, i);
250                 ret = ec_node_find(child, id);
251                 if (ret != NULL)
252                         return ret;
253         }
254
255         return NULL;
256 }
257
258 const struct ec_node_type *ec_node_type(const struct ec_node *node)
259 {
260         return node->type;
261 }
262
263 struct ec_keyval *ec_node_attrs(const struct ec_node *node)
264 {
265         return node->attrs;
266 }
267
268 const char *ec_node_id(const struct ec_node *node)
269 {
270         return node->id;
271 }
272
273 static void __ec_node_dump(FILE *out,
274         const struct ec_node *node, size_t indent)
275 {
276         const char *id, *typename;
277         struct ec_node *child;
278         size_t i, n;
279
280         id = ec_node_id(node);
281         typename = node->type->name;
282
283         fprintf(out, "%*s" "type=%s id=%s %p free=(%d,%d)\n",
284                 (int)indent * 4, "", typename, id, node,
285                 node->free.state, node->free.refcnt);
286         n = ec_node_get_children_count(node);
287         for (i = 0; i < n; i++) {
288                 child = ec_node_get_child(node, i);
289                 __ec_node_dump(out, child, indent + 1);
290         }
291 }
292
293 void ec_node_dump(FILE *out, const struct ec_node *node)
294 {
295         fprintf(out, "------------------- node dump:\n");
296
297         if (node == NULL) {
298                 fprintf(out, "node is NULL\n");
299                 return;
300         }
301
302         __ec_node_dump(out, node, 0);
303 }
304
305 const char *ec_node_desc(const struct ec_node *node)
306 {
307         if (node->type->desc != NULL)
308                 return node->type->desc(node);
309
310         return node->desc;
311 }
312
313 int ec_node_check_type(const struct ec_node *node,
314                 const struct ec_node_type *type)
315 {
316         if (strcmp(node->type->name, type->name)) {
317                 errno = EINVAL;
318                 return -1;
319         }
320
321         return 0;
322 }
323
324 /* LCOV_EXCL_START */
325 static int ec_node_testcase(void)
326 {
327         struct ec_node *node = NULL;
328         const struct ec_node *child;
329         const struct ec_node_type *type;
330         FILE *f = NULL;
331         char *buf = NULL;
332         size_t buflen = 0;
333         int testres = 0;
334         int ret;
335
336         node = EC_NODE_SEQ(EC_NO_ID,
337                         ec_node_str("id_x", "x"),
338                         ec_node_str("id_y", "y"));
339         if (node == NULL)
340                 goto fail;
341
342         ec_node_clone(node);
343         ec_node_free(node);
344
345         f = open_memstream(&buf, &buflen);
346         if (f == NULL)
347                 goto fail;
348         ec_node_dump(f, node);
349         ec_node_type_dump(f);
350         ec_node_dump(f, NULL);
351         fclose(f);
352         f = NULL;
353
354         testres |= EC_TEST_CHECK(
355                 strstr(buf, "type=seq id=no-id"), "bad dump\n");
356         testres |= EC_TEST_CHECK(
357                 strstr(buf, "type=str id=id_x") &&
358                 strstr(strstr(buf, "type=str id=id_x") + 1,
359                         "type=str id=id_y"),
360                 "bad dump\n");
361         free(buf);
362         buf = NULL;
363
364         testres |= EC_TEST_CHECK(
365                 !strcmp(ec_node_type(node)->name, "seq") &&
366                 !strcmp(ec_node_id(node), EC_NO_ID) &&
367                 !strcmp(ec_node_desc(node), "<seq>"),
368                 "bad child 0");
369
370         testres |= EC_TEST_CHECK(
371                 ec_node_get_children_count(node) == 2,
372                 "bad children count\n");
373         child = ec_node_get_child(node, 0);
374         testres |= EC_TEST_CHECK(child != NULL &&
375                 !strcmp(ec_node_type(child)->name, "str") &&
376                 !strcmp(ec_node_id(child), "id_x"),
377                 "bad child 0");
378         child = ec_node_get_child(node, 1);
379         testres |= EC_TEST_CHECK(child != NULL &&
380                 !strcmp(ec_node_type(child)->name, "str") &&
381                 !strcmp(ec_node_id(child), "id_y"),
382                 "bad child 1");
383         child = ec_node_get_child(node, 2);
384         testres |= EC_TEST_CHECK(child == NULL,
385                 "child 2 should be NULL");
386
387         child = ec_node_find(node, "id_x");
388         testres |= EC_TEST_CHECK(child != NULL &&
389                 !strcmp(ec_node_type(child)->name, "str") &&
390                 !strcmp(ec_node_id(child), "id_x") &&
391                 !strcmp(ec_node_desc(child), "x"),
392                 "bad child id_x");
393         child = ec_node_find(node, "id_dezdex");
394         testres |= EC_TEST_CHECK(child == NULL,
395                 "child with wrong id should be NULL");
396
397         ret = ec_keyval_set(ec_node_attrs(node), "key", "val", NULL);
398         testres |= EC_TEST_CHECK(ret == 0,
399                 "cannot set node attribute\n");
400
401         type = ec_node_type_lookup("seq");
402         testres |= EC_TEST_CHECK(type != NULL &&
403                 ec_node_check_type(node, type) == 0,
404                 "cannot get seq node type");
405         type = ec_node_type_lookup("str");
406         testres |= EC_TEST_CHECK(type != NULL &&
407                 ec_node_check_type(node, type) < 0,
408                 "node type should not be str");
409
410         ec_node_free(node);
411         node = NULL;
412
413         node = ec_node("deznuindez", EC_NO_ID);
414         testres |= EC_TEST_CHECK(node == NULL,
415                         "should not be able to create node\n");
416
417         return testres;
418
419 fail:
420         ec_node_free(node);
421         if (f != NULL)
422                 fclose(f);
423         free(buf);
424
425         assert(errno != 0);
426         return -1;
427 }
428 /* LCOV_EXCL_STOP */
429
430 static struct ec_test ec_node_test = {
431         .name = "node",
432         .test = ec_node_testcase,
433 };
434
435 EC_TEST_REGISTER(ec_node_test);