[btr] Split btr_del_node() into two functions.
[paraslash.git] / buffer_tree.c
index 084827e3d34c0952588f1a751a20ffe32eb28467..022a4118a76d0191d197b5cda6bc64b77eff924c 100644 (file)
@@ -5,6 +5,7 @@
 #include "list.h"
 #include "string.h"
 #include "buffer_tree.h"
+#include "error.h"
 
 
 struct btr_buffer {
@@ -34,26 +35,37 @@ struct btr_node {
         * used by this btr node.
         */
        struct list_head input_queue;
+       btr_command_handler execute;
+       void *context;
 };
 
 #define FOR_EACH_CHILD(_tn, _btrn) list_for_each_entry((_tn), \
        &((_btrn)->children), node)
+#define FOR_EACH_CHILD_SAFE(_tn, _tmp, _btrn) \
+       list_for_each_entry_safe((_tn), (_tmp), &((_btrn)->children), node)
 
 #define FOR_EACH_BUFFER_REF(_br, _btrn) \
        list_for_each_entry((_br), &(_btrn)->input_queue, node)
 #define FOR_EACH_BUFFER_REF_SAFE(_br, _tmp, _btrn) \
        list_for_each_entry_safe((_br), (_tmp), &(_btrn)->input_queue, node)
 
-struct btr_node *btr_new_node(char *name, struct btr_node *parent)
+struct btr_node *btr_new_node(const char *name, struct btr_node *parent,
+               btr_command_handler handler, void *context)
 {
        struct btr_node *btrn = para_malloc(sizeof(*btrn));
 
        btrn->name = para_strdup(name);
        btrn->parent = parent;
+       btrn->execute = handler;
+       btrn->context = context;
        if (parent)
                list_add_tail(&btrn->node, &parent->children);
        INIT_LIST_HEAD(&btrn->children);
        INIT_LIST_HEAD(&btrn->input_queue);
+       if (parent)
+               PARA_INFO_LOG("added %s as child of %s\n", name, parent->name);
+       else
+               PARA_INFO_LOG("added %s as btr root\n", name);
        return btrn;
 }
 
@@ -79,6 +91,7 @@ static void btr_drop_buffer_reference(struct btr_buffer_reference *br)
 {
        struct btr_buffer *btrb = br->btrb;
 
+       //PARA_CRIT_LOG("dropping buffer reference %p\n", br);
        list_del(&br->node);
        free(br);
        btrb->refcount--;
@@ -88,14 +101,15 @@ static void btr_drop_buffer_reference(struct btr_buffer_reference *br)
        }
 }
 
-static void add_btrb_to_children(struct btr_buffer *btrb, struct btr_node *btrn)
+static void add_btrb_to_children(struct btr_buffer *btrb,
+               struct btr_node *btrn, size_t consumed)
 {
        struct btr_node *ch;
 
        FOR_EACH_CHILD(ch, btrn) {
                struct btr_buffer_reference *br = para_malloc(sizeof(*br));
                br->btrb = btrb;
-               br->consumed = 0;
+               br->consumed = consumed;
                list_add_tail(&br->node, &ch->input_queue);
                btrb->refcount++;
        }
@@ -105,13 +119,18 @@ void btr_add_output(char *buf, size_t size, struct btr_node *btrn)
 {
        struct btr_buffer *btrb;
 
+       assert(size != 0);
+       if (list_empty(&btrn->children)) {
+               free(buf);
+               return;
+       }
        btrb = new_btrb(buf, size);
-       add_btrb_to_children(btrb, btrn);
+       add_btrb_to_children(btrb, btrn, 0);
 }
 
 static void btr_pushdown_br(struct btr_buffer_reference *br, struct btr_node *btrn)
 {
-       add_btrb_to_children(br->btrb, btrn);
+       add_btrb_to_children(br->btrb, btrn, br->consumed);
        btr_drop_buffer_reference(br);
 }
 
@@ -123,8 +142,19 @@ void btr_pushdown(struct btr_node *btrn)
                btr_pushdown_br(br, btrn);
 }
 
+int btr_pushdown_one(struct btr_node *btrn)
+{
+       struct btr_buffer_reference *br;
+
+       if (list_empty(&btrn->input_queue))
+               return 0;
+       br = list_first_entry(&btrn->input_queue, struct btr_buffer_reference, node);
+       btr_pushdown_br(br, btrn);
+       return 1;
+}
+
 /* Return true if this node has no children. */
-bool btr_is_leaf_node(struct btr_node *btrn)
+bool btr_no_children(struct btr_node *btrn)
 {
        return list_empty(&btrn->children);
 }
@@ -141,14 +171,6 @@ bool btr_inplace_ok(struct btr_node *btrn)
        return list_is_singular(&btrn->parent->children);
 }
 
-struct btr_buffer_reference *btr_next_br(struct btr_node *btrn)
-{
-       if (list_empty(&btrn->input_queue))
-               return NULL;
-       return list_first_entry(&btrn->input_queue, struct btr_buffer_reference, node);
-}
-
-
 static inline size_t br_available_bytes(struct btr_buffer_reference *br)
 {
        return br->btrb->size - br->consumed;
@@ -160,9 +182,29 @@ size_t btr_get_buffer_by_reference(struct btr_buffer_reference *br, char **buf)
        return br_available_bytes(br);
 }
 
-void btr_increase_used_bytes(struct btr_buffer_reference *br, size_t consumed)
+/**
+ * \return zero if the input buffer queue is empty.
+ */
+size_t btr_next_buffer(struct btr_node *btrn, char **bufp)
 {
-       br->consumed += consumed;
+       struct btr_buffer_reference *br;
+
+       if (list_empty(&btrn->input_queue)) {
+               *bufp = NULL;
+               return 0;
+       }
+       br = list_first_entry(&btrn->input_queue, struct btr_buffer_reference, node);
+       return btr_get_buffer_by_reference(br, bufp);
+}
+
+void btr_consume(struct btr_node *btrn, size_t numbytes)
+{
+       struct btr_buffer_reference *br;
+
+       assert(!list_empty(&btrn->input_queue));
+       br = list_first_entry(&btrn->input_queue, struct btr_buffer_reference, node);
+       assert(br->consumed + numbytes <= br->btrb->size);
+       br->consumed += numbytes;
        if (br->consumed == br->btrb->size)
                btr_drop_buffer_reference(br);
 }
@@ -174,17 +216,26 @@ static void flush_input_queue(struct btr_node *btrn)
                btr_drop_buffer_reference(br);
 }
 
-void btr_del_node(struct btr_node *btrn)
+void btr_free_node(struct btr_node *btrn)
+{
+       if (!btrn)
+               return;
+       free(btrn->name);
+       free(btrn);
+}
+
+void btr_remove_node(struct btr_node *btrn)
 {
        struct btr_node *ch;
 
+       if (!btrn)
+               return;
+       PARA_NOTICE_LOG("removing btr node %s from buffer tree\n", btrn->name);
        FOR_EACH_CHILD(ch, btrn)
                ch->parent = NULL;
        flush_input_queue(btrn);
        if (btrn->parent)
                list_del(&btrn->node);
-       free(btrn->name);
-       free(btrn);
 }
 
 size_t btr_get_input_queue_size(struct btr_node *btrn)
@@ -192,7 +243,187 @@ size_t btr_get_input_queue_size(struct btr_node *btrn)
        struct btr_buffer_reference *br;
        size_t size = 0;
 
-       FOR_EACH_BUFFER_REF(br, btrn)
+       FOR_EACH_BUFFER_REF(br, btrn) {
+               //PARA_CRIT_LOG("size: %zu\n", size);
                size += br_available_bytes(br);
+       }
        return size;
 }
+
+void btr_splice_out_node(struct btr_node *btrn)
+{
+       struct btr_node *ch, *tmp;
+
+       assert(btrn);
+       PARA_NOTICE_LOG("splicing out %s\n", btrn->name);
+       btr_pushdown(btrn);
+       if (btrn->parent)
+               list_del(&btrn->node);
+       FOR_EACH_CHILD_SAFE(ch, tmp, btrn) {
+               PARA_INFO_LOG("parent(%s): %s\n", ch->name,
+                       btrn->parent? btrn->parent->name : "NULL");
+               ch->parent = btrn->parent;
+               if (btrn->parent)
+                       list_move(&ch->node, &btrn->parent->children);
+       }
+       assert(list_empty(&btrn->children));
+}
+
+/**
+ * Return the size of the largest input queue.
+ *
+ * Iterates over all children of the given node.
+ */
+size_t btr_bytes_pending(struct btr_node *btrn)
+{
+       size_t max_size = 0;
+       struct btr_node *ch;
+
+       FOR_EACH_CHILD(ch, btrn) {
+               size_t size = btr_get_input_queue_size(ch);
+               max_size = PARA_MAX(max_size, size);
+       }
+       return max_size;
+}
+
+int btr_exec(struct btr_node *btrn, const char *command, char **value_result)
+{
+       if (!btrn)
+               return -ERRNO_TO_PARA_ERROR(EINVAL);
+       if (!btrn->execute)
+               return -ERRNO_TO_PARA_ERROR(ENOTSUP);
+       return btrn->execute(btrn, command, value_result);
+}
+
+int btr_exec_up(struct btr_node *btrn, const char *command, char **value_result)
+{
+       int ret;
+
+       for (; btrn; btrn = btrn->parent) {
+               struct btr_node *parent = btrn->parent;
+               if (!parent)
+                       return -ERRNO_TO_PARA_ERROR(ENOTSUP);
+               if (!parent->execute)
+                       continue;
+               PARA_INFO_LOG("parent: %s, cmd: %s\n", parent->name, command);
+               ret = parent->execute(parent, command, value_result);
+               if (ret == -ERRNO_TO_PARA_ERROR(ENOTSUP))
+                       continue;
+               if (ret < 0)
+                       return ret;
+               if (value_result && *value_result)
+                       PARA_NOTICE_LOG("%s(%s): %s\n", command, parent->name,
+                               *value_result);
+               return 1;
+       }
+       return -ERRNO_TO_PARA_ERROR(ENOTSUP);
+}
+
+void *btr_context(struct btr_node *btrn)
+{
+       return btrn->context;
+}
+
+/**
+ * Merge the first two input buffers into one.
+ *
+ * This is a quite expensive operation.
+ *
+ * \return The number of buffers that have been available (zero, one or two).
+ */
+static int merge_input(struct btr_node *btrn)
+{
+       struct btr_buffer_reference *brs[2], *br;
+       char *bufs[2], *buf;
+       size_t szs[2], sz;
+       int i;
+
+       if (list_empty(&btrn->input_queue))
+               return 0;
+       if (list_is_singular(&btrn->input_queue))
+               return 1;
+       i = 0;
+       /* get references to the first two buffers */
+       FOR_EACH_BUFFER_REF(br, btrn) {
+               brs[i] = br;
+               szs[i] = btr_get_buffer_by_reference(brs[i], bufs + i);
+               i++;
+               if (i == 2)
+                       break;
+       }
+       /* make a new btrb that combines the two buffers and a br to it. */
+       sz = szs[0] + szs[1];
+       //PARA_CRIT_LOG("merging input buffers: (%zu, %zu) -> %zu\n",
+       //      szs[0], szs[1], sz);
+       buf = para_malloc(sz);
+       /* TODO: Avoid this memcopy by introducing btr buffer pool. */
+       memcpy(buf, bufs[0], szs[0]);
+       memcpy(buf + szs[0], bufs[1], szs[1]);
+
+       br = para_malloc(sizeof(*br));
+       br->btrb = new_btrb(buf, sz);
+       br->btrb->refcount = 1;
+       br->consumed = 0;
+
+       /* replace the first two refs by the new one */
+       btr_drop_buffer_reference(brs[0]);
+       btr_drop_buffer_reference(brs[1]);
+       para_list_add(&br->node, &btrn->input_queue);
+       return 2;
+}
+
+void btr_merge(struct btr_node *btrn, size_t dest_size)
+{
+       for (;;) {
+               char *buf;
+               size_t len = btr_next_buffer(btrn, &buf);
+               if (len >= dest_size)
+                       return;
+               if (merge_input(btrn) < 2)
+                       return;
+       }
+}
+
+bool btr_eof(struct btr_node *btrn)
+{
+       char *buf;
+       size_t len = btr_next_buffer(btrn, &buf);
+
+       return (len == 0 && btr_no_parent(btrn));
+}
+
+void log_tree_recursively(struct btr_node *btrn, int loglevel, int depth)
+{
+       struct btr_node *ch;
+       const char spaces[] = "                 ", *space = spaces + 16 - depth;
+
+       if (depth > 16)
+               return;
+       para_log(loglevel, "%s%s\n", space, btrn->name);
+       FOR_EACH_CHILD(ch, btrn)
+               log_tree_recursively(ch, loglevel, depth + 1);
+}
+
+void btr_log_tree(struct btr_node *btrn, int loglevel)
+{
+       return log_tree_recursively(btrn, loglevel, 0);
+}
+
+/** 640K ought to be enough for everybody ;) */
+#define BTRN_MAX_PENDING (640 * 1024)
+
+int btr_node_status(struct btr_node *btrn, size_t min_iqs)
+{
+       size_t iqs;
+
+       if (btr_eof(btrn))
+               return -E_BTR_EOF;
+       if (btr_bytes_pending(btrn) > BTRN_MAX_PENDING)
+               return 0;
+       iqs = btr_get_input_queue_size(btrn);
+       if (iqs == 0) /* we have a parent, because not eof */
+               return 0;
+       if (iqs < min_iqs && !btr_no_parent(btrn))
+               return 0;
+       return 1;
+}