create cluster table while writing tag data to image

This commit is contained in:
2025-02-02 15:20:11 +00:00
parent 8ca50e4888
commit c5d8b4e881
6 changed files with 569 additions and 198 deletions

View File

@@ -1,17 +1,17 @@
#include "b-tree.h"
#include "bin.h"
#include "commands.h"
#include "b-tree.h"
#include <blue/cmd.h>
#include <stdio.h>
#include <stdlib.h>
#include <zstd.h>
#define INVALID_NODE_PTR 0xFFFF
#define INVALID_NODE_PTR 0xFFFF
#define ORDER 6
#define MAX (ORDER - 1)
#define MIN (MAX / 2)
#define ORDER 6
#define MAX (ORDER - 1)
#define MIN (MAX / 2)
enum {
ARG_INPATH,
@@ -19,8 +19,7 @@ enum {
OPT_OUTPATH_PATH,
};
struct entry
{
struct entry {
uint32_t key;
uint32_t value;
};
@@ -38,7 +37,7 @@ struct data_tree {
static int node_init(struct node *n)
{
memset(n, 0x0, sizeof * n);
memset(n, 0x0, sizeof *n);
for (int i = 0; i < ORDER; i++) {
n->children[i] = INVALID_NODE_PTR;
@@ -56,7 +55,10 @@ static int tree_get_node(struct b_tree *p, unsigned long id, b_tree_node *n)
return r == 1 ? 0 : -1;
}
static int tree_put_node(struct b_tree *p, unsigned long id, const b_tree_node *n)
static int tree_put_node(
struct b_tree *p,
unsigned long id,
const b_tree_node *n)
{
struct data_tree *tree = (struct data_tree *)p;
size_t offset = (id * sizeof(struct node));
@@ -96,19 +98,42 @@ static void node_set_nr_entries(b_tree_node *n, unsigned long val)
node->nr_entries = val;
}
static b_tree_node_entry *node_get_entries(b_tree_node *n)
static b_tree_node_entry *node_get_entry(b_tree_node *n, unsigned long index)
{
struct node *node = (struct node *)n;
return (b_tree_node_entry *)node->entries;
return (b_tree_node_entry *)&node->entries[index];
}
static uint16_t *node_get_children(b_tree_node *n)
static void node_set_entry(
b_tree_node *n,
unsigned long index,
const b_tree_node_entry *entry)
{
struct node *node = (struct node *)n;
return node->children;
memmove(&node->entries[index], entry, sizeof(struct entry));
}
static int entry_compare(const b_tree_node_entry *e0, const b_tree_node_entry *e1)
static unsigned long node_get_child(b_tree_node *n, unsigned long index)
{
struct node *node = (struct node *)n;
uint16_t child = node->children[index];
return child == INVALID_NODE_PTR ? B_TREE_INVALID_PTR : child;
}
static void node_set_child(
b_tree_node *n,
unsigned long index,
unsigned long ptr)
{
struct node *node = (struct node *)n;
uint16_t child
= ptr == B_TREE_INVALID_PTR ? INVALID_NODE_PTR : (uint16_t)ptr;
node->children[index] = child;
}
static int entry_compare(
const b_tree_node_entry *e0,
const b_tree_node_entry *e1)
{
struct entry *a = (struct entry *)e0, *b = (struct entry *)e1;
@@ -130,8 +155,10 @@ static const struct b_tree_ops ops = {
.node_get_nr_entries = node_get_nr_entries,
.node_set_nr_entries = node_set_nr_entries,
.node_get_entries = node_get_entries,
.node_get_children = node_get_children,
.node_get_entry = node_get_entry,
.node_set_entry = node_set_entry,
.node_get_child = node_get_child,
.node_set_child = node_set_child,
.entry_compare = entry_compare,
};
@@ -161,10 +188,13 @@ int node_print(struct b_tree *tree, struct node *n, int depth)
continue;
}
err = tree_get_node(tree, n->children[index], (b_tree_node *)&child);
err = tree_get_node(
tree,
n->children[index],
(b_tree_node *)&child);
if (err != 0) {
printf("ERR: failed to read node %u\n",
n->children[index]);
n->children[index]);
continue;
}
@@ -193,10 +223,13 @@ int node_print(struct b_tree *tree, struct node *n, int depth)
continue;
}
err = tree_get_node(tree, n->children[index], (b_tree_node *)&child);
err = tree_get_node(
tree,
n->children[index],
(b_tree_node *)&child);
if (err != 0) {
printf("ERR: failed to read node %u\n",
n->children[index]);
n->children[index]);
continue;
}
@@ -240,20 +273,24 @@ static int create(
fwrite(&root, sizeof root, 1, fp);
struct data_tree tree;
b_tree_init(&tree.base, &ops, sizeof(struct node), sizeof(struct entry), ORDER);
b_tree_init(
&tree.base,
&ops,
sizeof(struct node),
sizeof(struct entry),
ORDER);
tree.fp = fp;
for (int i = 1; i <= 32; i++) {
int key = rand() % 65535;
//int key = i;
// printf("%d: adding [%d]\n", i, key);
struct entry e = { .key = key, .value = key * 2 };
// int key = i;
// printf("%d: adding [%d]\n", i, key);
struct entry e = {.key = key, .value = key * 2};
b_tree_put(&tree.base, (b_tree_node_entry *)&e);
}
tree_print(&tree.base);
fclose(fp);
return 0;