[11b9ad7] | 1 | /*
|
---|
| 2 | * Copyright (c) 2012 Sean Bartell
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - 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 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup bithenge
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
| 34 | * Trees and nodes.
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <errno.h>
|
---|
| 38 | #include <stdlib.h>
|
---|
[8fc0f47c] | 39 | #include <bithenge/blob.h>
|
---|
| 40 | #include <bithenge/tree.h>
|
---|
[6cd10ac] | 41 | #include "common.h"
|
---|
[11b9ad7] | 42 |
|
---|
[978ccaf1] | 43 | static void blob_destroy(bithenge_node_t *base)
|
---|
[5c925ce] | 44 | {
|
---|
[978ccaf1] | 45 | bithenge_blob_t *self = bithenge_node_as_blob(base);
|
---|
| 46 | assert(self->base.blob_ops);
|
---|
| 47 | self->base.blob_ops->destroy(self);
|
---|
[5c925ce] | 48 | }
|
---|
| 49 |
|
---|
[978ccaf1] | 50 | static void node_destroy(bithenge_node_t *self)
|
---|
[11b9ad7] | 51 | {
|
---|
[978ccaf1] | 52 | switch (bithenge_node_type(self)) {
|
---|
[5c925ce] | 53 | case BITHENGE_NODE_BLOB:
|
---|
[978ccaf1] | 54 | blob_destroy(self);
|
---|
| 55 | return;
|
---|
[11b9ad7] | 56 | case BITHENGE_NODE_STRING:
|
---|
[978ccaf1] | 57 | if (self->string_value.needs_free)
|
---|
| 58 | free((void *)self->string_value.ptr);
|
---|
[11b9ad7] | 59 | break;
|
---|
| 60 | case BITHENGE_NODE_INTERNAL:
|
---|
[978ccaf1] | 61 | self->internal_ops->destroy(self);
|
---|
| 62 | return;
|
---|
[11b9ad7] | 63 | case BITHENGE_NODE_BOOLEAN:
|
---|
[978ccaf1] | 64 | return; /* The boolean nodes are allocated statically below. */
|
---|
[11b9ad7] | 65 | case BITHENGE_NODE_INTEGER: /* pass-through */
|
---|
| 66 | break;
|
---|
| 67 | }
|
---|
[978ccaf1] | 68 | free(self);
|
---|
[11b9ad7] | 69 | }
|
---|
| 70 |
|
---|
[f2da0bb] | 71 | /** Decrement a node's reference count and free it if appropriate.
|
---|
| 72 | * @memberof bithenge_node_t
|
---|
[978ccaf1] | 73 | * @param node The node to dereference, or NULL. */
|
---|
| 74 | void bithenge_node_dec_ref(bithenge_node_t *node)
|
---|
[f2da0bb] | 75 | {
|
---|
| 76 | if (!node)
|
---|
[978ccaf1] | 77 | return;
|
---|
[681a985] | 78 | assert (node->refs > 0);
|
---|
[f2da0bb] | 79 | if (--node->refs == 0)
|
---|
[978ccaf1] | 80 | node_destroy(node);
|
---|
[f2da0bb] | 81 | }
|
---|
| 82 |
|
---|
[e8e31d9] | 83 | typedef struct {
|
---|
| 84 | bithenge_node_t *key;
|
---|
| 85 | bithenge_node_t **out;
|
---|
| 86 | } get_for_each_data_t;
|
---|
| 87 |
|
---|
[b7fd2a0] | 88 | static errno_t get_for_each_func(bithenge_node_t *key, bithenge_node_t *value,
|
---|
[e8e31d9] | 89 | void *raw_data)
|
---|
| 90 | {
|
---|
| 91 | get_for_each_data_t *data = (get_for_each_data_t *)raw_data;
|
---|
[a42d7d8] | 92 | bool equal;
|
---|
[b7fd2a0] | 93 | errno_t rc = bithenge_node_equal(&equal, key, data->key);
|
---|
[e8e31d9] | 94 | bithenge_node_dec_ref(key);
|
---|
[a42d7d8] | 95 | if (rc != EOK)
|
---|
| 96 | return rc;
|
---|
[e8e31d9] | 97 | if (equal) {
|
---|
| 98 | *data->out = value;
|
---|
| 99 | return EEXIST;
|
---|
| 100 | }
|
---|
| 101 | bithenge_node_dec_ref(value);
|
---|
| 102 | return EOK;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | /** Get a child of a node. Takes ownership of the key. If the node does not
|
---|
| 106 | * provide this function, for_each will be used as an alternative, which may be
|
---|
[1c79996] | 107 | * very slow. Also works for blob nodes to find the byte value at a given
|
---|
| 108 | * index.
|
---|
[e8e31d9] | 109 | * @memberof bithenge_node_t
|
---|
[1c79996] | 110 | * @param self The internal/blob node to find a child of.
|
---|
[e8e31d9] | 111 | * @param key The key to search for.
|
---|
| 112 | * @param[out] out Holds the found node.
|
---|
| 113 | * @return EOK on success, ENOENT if not found, or another error code from
|
---|
| 114 | * errno.h. */
|
---|
[b7fd2a0] | 115 | errno_t bithenge_node_get(bithenge_node_t *self, bithenge_node_t *key,
|
---|
[e8e31d9] | 116 | bithenge_node_t **out)
|
---|
| 117 | {
|
---|
[1c79996] | 118 | if (self->type == BITHENGE_NODE_BLOB) {
|
---|
| 119 | if (bithenge_node_type(key) != BITHENGE_NODE_INTEGER) {
|
---|
| 120 | bithenge_node_dec_ref(key);
|
---|
| 121 | return ENOENT;
|
---|
| 122 | }
|
---|
| 123 | bithenge_int_t offset = bithenge_integer_node_value(key);
|
---|
| 124 | bithenge_node_dec_ref(key);
|
---|
| 125 | uint8_t byte;
|
---|
| 126 | aoff64_t size = 1;
|
---|
[b7fd2a0] | 127 | errno_t rc = bithenge_blob_read(bithenge_node_as_blob(self),
|
---|
[1c79996] | 128 | offset, (char *)&byte, &size);
|
---|
| 129 | if (rc != EOK)
|
---|
| 130 | return rc;
|
---|
| 131 | if (size != 1)
|
---|
| 132 | return ENOENT;
|
---|
| 133 |
|
---|
| 134 | return bithenge_new_integer_node(out, byte);
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[e8e31d9] | 137 | assert(self->type == BITHENGE_NODE_INTERNAL);
|
---|
| 138 | if (self->internal_ops->get)
|
---|
| 139 | return self->internal_ops->get(self, key, out);
|
---|
| 140 | *out = NULL;
|
---|
[1433ecda] | 141 | get_for_each_data_t data = { key, out };
|
---|
[b7fd2a0] | 142 | errno_t rc = bithenge_node_for_each(self, get_for_each_func, &data);
|
---|
[e8e31d9] | 143 | bithenge_node_dec_ref(key);
|
---|
| 144 | if (rc == EEXIST && *out)
|
---|
| 145 | return EOK;
|
---|
| 146 | if (rc == EOK)
|
---|
| 147 | rc = ENOENT;
|
---|
| 148 | bithenge_node_dec_ref(*out);
|
---|
| 149 | return rc;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
[10334c2e] | 152 | /** Initialize an internal node.
|
---|
| 153 | * @memberof bithenge_node_t
|
---|
| 154 | * @param[out] self The node.
|
---|
| 155 | * @param[in] ops The operations provided.
|
---|
| 156 | * @return EOK on success or an error code from errno.h. */
|
---|
[b7fd2a0] | 157 | errno_t bithenge_init_internal_node(bithenge_node_t *self,
|
---|
[10334c2e] | 158 | const bithenge_internal_node_ops_t *ops)
|
---|
| 159 | {
|
---|
| 160 | self->type = BITHENGE_NODE_INTERNAL;
|
---|
| 161 | self->refs = 1;
|
---|
| 162 | self->internal_ops = ops;
|
---|
| 163 | return EOK;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | static void internal_node_indestructible(bithenge_node_t *self)
|
---|
| 167 | {
|
---|
| 168 | assert(false);
|
---|
| 169 | }
|
---|
| 170 |
|
---|
[b7fd2a0] | 171 | static errno_t empty_internal_node_for_each(bithenge_node_t *base,
|
---|
[10334c2e] | 172 | bithenge_for_each_func_t func, void *data)
|
---|
| 173 | {
|
---|
| 174 | return EOK;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
[b7fd2a0] | 177 | static errno_t empty_internal_node_get(bithenge_node_t *self, bithenge_node_t *key,
|
---|
[10334c2e] | 178 | bithenge_node_t **out)
|
---|
| 179 | {
|
---|
| 180 | return ENOENT;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | static const bithenge_internal_node_ops_t empty_internal_node_ops = {
|
---|
| 184 | .for_each = empty_internal_node_for_each,
|
---|
| 185 | .get = empty_internal_node_get,
|
---|
| 186 | .destroy = internal_node_indestructible,
|
---|
| 187 | };
|
---|
| 188 |
|
---|
| 189 | static bithenge_node_t empty_internal_node = {
|
---|
| 190 | BITHENGE_NODE_INTERNAL,
|
---|
| 191 | 1,
|
---|
| 192 | { .internal_ops = &empty_internal_node_ops },
|
---|
| 193 | };
|
---|
| 194 |
|
---|
| 195 | /** Create an empty internal node.
|
---|
| 196 | * @param[out] out Holds the created node.
|
---|
| 197 | * @return EOK on success or an error code from errno.h. */
|
---|
[b7fd2a0] | 198 | errno_t bithenge_new_empty_internal_node(bithenge_node_t **out)
|
---|
[10334c2e] | 199 | {
|
---|
[1a3b953] | 200 | if (bithenge_should_fail())
|
---|
| 201 | return ENOMEM;
|
---|
[10334c2e] | 202 | bithenge_node_inc_ref(&empty_internal_node);
|
---|
| 203 | *out = &empty_internal_node;
|
---|
| 204 | return EOK;
|
---|
| 205 | }
|
---|
| 206 |
|
---|
[1433ecda] | 207 | typedef struct {
|
---|
[5f679702] | 208 | bithenge_node_t base;
|
---|
[11b9ad7] | 209 | bithenge_node_t **nodes;
|
---|
| 210 | bithenge_int_t len;
|
---|
| 211 | bool needs_free;
|
---|
| 212 | } simple_internal_node_t;
|
---|
| 213 |
|
---|
[5f679702] | 214 | static simple_internal_node_t *node_as_simple(bithenge_node_t *node)
|
---|
[11b9ad7] | 215 | {
|
---|
| 216 | return (simple_internal_node_t *)node;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
[f2da0bb] | 219 | static bithenge_node_t *simple_as_node(simple_internal_node_t *node)
|
---|
| 220 | {
|
---|
| 221 | return &node->base;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
[b7fd2a0] | 224 | static errno_t simple_internal_node_for_each(bithenge_node_t *base,
|
---|
[8375d0eb] | 225 | bithenge_for_each_func_t func, void *data)
|
---|
[11b9ad7] | 226 | {
|
---|
[b7fd2a0] | 227 | errno_t rc;
|
---|
[978ccaf1] | 228 | simple_internal_node_t *self = node_as_simple(base);
|
---|
| 229 | for (bithenge_int_t i = 0; i < self->len; i++) {
|
---|
[1433ecda] | 230 | bithenge_node_inc_ref(self->nodes[2 * i + 0]);
|
---|
| 231 | bithenge_node_inc_ref(self->nodes[2 * i + 1]);
|
---|
| 232 | rc = func(self->nodes[2 * i + 0], self->nodes[2 * i + 1], data);
|
---|
[11b9ad7] | 233 | if (rc != EOK)
|
---|
| 234 | return rc;
|
---|
| 235 | }
|
---|
| 236 | return EOK;
|
---|
| 237 | }
|
---|
| 238 |
|
---|
[978ccaf1] | 239 | static void simple_internal_node_destroy(bithenge_node_t *base)
|
---|
[5c925ce] | 240 | {
|
---|
[978ccaf1] | 241 | simple_internal_node_t *self = node_as_simple(base);
|
---|
| 242 | for (bithenge_int_t i = 0; i < 2 * self->len; i++)
|
---|
| 243 | bithenge_node_dec_ref(self->nodes[i]);
|
---|
| 244 | if (self->needs_free)
|
---|
| 245 | free(self->nodes);
|
---|
| 246 | free(self);
|
---|
[5c925ce] | 247 | }
|
---|
| 248 |
|
---|
[11b9ad7] | 249 | static bithenge_internal_node_ops_t simple_internal_node_ops = {
|
---|
[5c925ce] | 250 | .for_each = simple_internal_node_for_each,
|
---|
| 251 | .destroy = simple_internal_node_destroy,
|
---|
[11b9ad7] | 252 | };
|
---|
| 253 |
|
---|
[f2da0bb] | 254 | /** Create an internal node from a set of keys and values. This function takes
|
---|
| 255 | * ownership of a reference to the key and value nodes, and optionally the
|
---|
| 256 | * array @a nodes.
|
---|
[8375d0eb] | 257 | * @memberof bithenge_node_t
|
---|
| 258 | * @param[out] out Stores the created internal node.
|
---|
| 259 | * @param nodes The array of key-value pairs. Keys are stored at even indices
|
---|
| 260 | * and values are stored at odd indices.
|
---|
| 261 | * @param len The number of key-value pairs in the node array.
|
---|
| 262 | * @param needs_free If true, when the internal node is destroyed it will free
|
---|
[f2da0bb] | 263 | * the nodes array rather than just dereferencing each node inside it.
|
---|
[8375d0eb] | 264 | * @return EOK on success or an error code from errno.h. */
|
---|
[b7fd2a0] | 265 | errno_t bithenge_new_simple_internal_node(bithenge_node_t **out,
|
---|
[8375d0eb] | 266 | bithenge_node_t **nodes, bithenge_int_t len, bool needs_free)
|
---|
[11b9ad7] | 267 | {
|
---|
[b7fd2a0] | 268 | errno_t rc;
|
---|
[11b9ad7] | 269 | assert(out);
|
---|
[978ccaf1] | 270 | simple_internal_node_t *self = malloc(sizeof(*self));
|
---|
| 271 | if (!self) {
|
---|
[04a7435f] | 272 | rc = ENOMEM;
|
---|
| 273 | goto error;
|
---|
[f2da0bb] | 274 | }
|
---|
[978ccaf1] | 275 | rc = bithenge_init_internal_node(simple_as_node(self),
|
---|
[04a7435f] | 276 | &simple_internal_node_ops);
|
---|
| 277 | if (rc != EOK)
|
---|
| 278 | goto error;
|
---|
[978ccaf1] | 279 | self->nodes = nodes;
|
---|
| 280 | self->len = len;
|
---|
| 281 | self->needs_free = needs_free;
|
---|
| 282 | *out = simple_as_node(self);
|
---|
[11b9ad7] | 283 | return EOK;
|
---|
[04a7435f] | 284 | error:
|
---|
| 285 | for (bithenge_int_t i = 0; i < 2 * len; i++)
|
---|
| 286 | bithenge_node_dec_ref(nodes[i]);
|
---|
| 287 | if (needs_free)
|
---|
| 288 | free(nodes);
|
---|
[978ccaf1] | 289 | free(self);
|
---|
[04a7435f] | 290 | return rc;
|
---|
[11b9ad7] | 291 | }
|
---|
| 292 |
|
---|
[f2da0bb] | 293 | static bithenge_node_t false_node = { BITHENGE_NODE_BOOLEAN, 1, .boolean_value = false };
|
---|
| 294 | static bithenge_node_t true_node = { BITHENGE_NODE_BOOLEAN, 1, .boolean_value = true };
|
---|
[11b9ad7] | 295 |
|
---|
[f2da0bb] | 296 | /** Create a boolean node.
|
---|
[8375d0eb] | 297 | * @memberof bithenge_node_t
|
---|
| 298 | * @param[out] out Stores the created boolean node.
|
---|
| 299 | * @param value The value for the node to hold.
|
---|
| 300 | * @return EOK on success or an error code from errno.h. */
|
---|
[b7fd2a0] | 301 | errno_t bithenge_new_boolean_node(bithenge_node_t **out, bool value)
|
---|
[11b9ad7] | 302 | {
|
---|
| 303 | assert(out);
|
---|
[1a3b953] | 304 | if (bithenge_should_fail())
|
---|
| 305 | return ENOMEM;
|
---|
[5f679702] | 306 | *out = value ? &true_node : &false_node;
|
---|
[f2da0bb] | 307 | (*out)->refs++;
|
---|
[11b9ad7] | 308 | return EOK;
|
---|
| 309 | }
|
---|
| 310 |
|
---|
[f2da0bb] | 311 | /** Create an integer node.
|
---|
[8375d0eb] | 312 | * @memberof bithenge_node_t
|
---|
| 313 | * @param[out] out Stores the created integer node.
|
---|
| 314 | * @param value The value for the node to hold.
|
---|
| 315 | * @return EOK on success or an error code from errno.h. */
|
---|
[b7fd2a0] | 316 | errno_t bithenge_new_integer_node(bithenge_node_t **out, bithenge_int_t value)
|
---|
[11b9ad7] | 317 | {
|
---|
| 318 | assert(out);
|
---|
[978ccaf1] | 319 | bithenge_node_t *self = malloc(sizeof(*self));
|
---|
| 320 | if (!self)
|
---|
[11b9ad7] | 321 | return ENOMEM;
|
---|
[978ccaf1] | 322 | self->type = BITHENGE_NODE_INTEGER;
|
---|
| 323 | self->refs = 1;
|
---|
| 324 | self->integer_value = value;
|
---|
| 325 | *out = self;
|
---|
[11b9ad7] | 326 | return EOK;
|
---|
| 327 | }
|
---|
| 328 |
|
---|
[f2da0bb] | 329 | /** Create a string node.
|
---|
[8375d0eb] | 330 | * @memberof bithenge_node_t
|
---|
[04a7435f] | 331 | * @param[out] out Stores the created string node. On error, this is unchanged.
|
---|
[8375d0eb] | 332 | * @param value The value for the node to hold.
|
---|
| 333 | * @param needs_free Whether the string should be freed when the node is
|
---|
| 334 | * destroyed.
|
---|
| 335 | * @return EOK on success or an error code from errno.h. */
|
---|
[b7fd2a0] | 336 | errno_t bithenge_new_string_node(bithenge_node_t **out, const char *value, bool needs_free)
|
---|
[11b9ad7] | 337 | {
|
---|
| 338 | assert(out);
|
---|
[978ccaf1] | 339 | bithenge_node_t *self = malloc(sizeof(*self));
|
---|
[600f5d1] | 340 | if (!self) {
|
---|
| 341 | if (needs_free)
|
---|
| 342 | free((void *)value);
|
---|
[11b9ad7] | 343 | return ENOMEM;
|
---|
[600f5d1] | 344 | }
|
---|
[978ccaf1] | 345 | self->type = BITHENGE_NODE_STRING;
|
---|
| 346 | self->refs = 1;
|
---|
| 347 | self->string_value.ptr = value;
|
---|
| 348 | self->string_value.needs_free = needs_free;
|
---|
| 349 | *out = self;
|
---|
[11b9ad7] | 350 | return EOK;
|
---|
| 351 | }
|
---|
[8375d0eb] | 352 |
|
---|
[d5070ef] | 353 | /** Check whether the contents of two nodes are equal. Does not yet work for
|
---|
[78d3a00] | 354 | * internal nodes. Takes ownership of nothing.
|
---|
[d5070ef] | 355 | * @memberof bithenge_node_t
|
---|
[a42d7d8] | 356 | * @param[out] out Holds whether the nodes are equal.
|
---|
[d5070ef] | 357 | * @param a, b Nodes to compare.
|
---|
[a42d7d8] | 358 | * @return EOK on success or an error code from errno.h.
|
---|
[0784869] | 359 | * @todo Add support for internal nodes. */
|
---|
[b7fd2a0] | 360 | errno_t bithenge_node_equal(bool *out, bithenge_node_t *a, bithenge_node_t *b)
|
---|
[d5070ef] | 361 | {
|
---|
[a42d7d8] | 362 | if (a->type != b->type) {
|
---|
| 363 | *out = false;
|
---|
| 364 | return EOK;
|
---|
| 365 | }
|
---|
[d5070ef] | 366 | switch (a->type) {
|
---|
| 367 | case BITHENGE_NODE_INTERNAL:
|
---|
[a42d7d8] | 368 | *out = false;
|
---|
| 369 | return EOK;
|
---|
[d5070ef] | 370 | case BITHENGE_NODE_BOOLEAN:
|
---|
[a42d7d8] | 371 | *out = a->boolean_value == b->boolean_value;
|
---|
| 372 | return EOK;
|
---|
[d5070ef] | 373 | case BITHENGE_NODE_INTEGER:
|
---|
[a42d7d8] | 374 | *out = a->integer_value == b->integer_value;
|
---|
| 375 | return EOK;
|
---|
[d5070ef] | 376 | case BITHENGE_NODE_STRING:
|
---|
[a42d7d8] | 377 | *out = !str_cmp(a->string_value.ptr, b->string_value.ptr);
|
---|
| 378 | return EOK;
|
---|
[d5070ef] | 379 | case BITHENGE_NODE_BLOB:
|
---|
[a42d7d8] | 380 | return bithenge_blob_equal(out, bithenge_node_as_blob(a),
|
---|
[d5070ef] | 381 | bithenge_node_as_blob(b));
|
---|
| 382 | }
|
---|
[a42d7d8] | 383 | return EINVAL;
|
---|
[d5070ef] | 384 | }
|
---|
| 385 |
|
---|
[8375d0eb] | 386 | /** @}
|
---|
| 387 | */
|
---|