Files
ec3/src/create.c

336 lines
6.4 KiB
C
Raw Normal View History

#if 0
#include "b-tree.h"
2025-01-30 18:10:38 +00:00
#include "bin.h"
2024-11-02 11:17:36 +00:00
#include "commands.h"
#include <blue/cmd.h>
2024-12-18 20:51:01 +00:00
#include <stdio.h>
#include <stdlib.h>
2025-01-30 18:10:38 +00:00
#include <zstd.h>
2024-11-02 11:17:36 +00:00
#define INVALID_NODE_PTR 0xFFFF
2025-01-31 21:39:29 +00:00
#define ORDER 6
#define MAX (ORDER - 1)
#define MIN (MAX / 2)
2025-01-31 21:39:29 +00:00
2024-11-02 11:17:36 +00:00
enum {
2024-12-18 20:51:01 +00:00
ARG_INPATH,
2024-11-02 11:17:36 +00:00
OPT_OUTPATH,
OPT_OUTPATH_PATH,
};
struct entry {
2025-01-31 21:39:29 +00:00
uint32_t key;
uint32_t value;
};
struct node {
uint16_t nr_entries;
struct entry entries[MAX];
uint16_t children[ORDER];
};
struct data_tree {
struct b_tree base;
FILE *fp;
};
static int node_init(struct node *n)
{
memset(n, 0x0, sizeof *n);
2025-01-31 21:39:29 +00:00
for (int i = 0; i < ORDER; i++) {
n->children[i] = INVALID_NODE_PTR;
}
return 0;
}
static int tree_get_node(struct b_tree *p, unsigned long id, b_tree_node *n)
{
struct data_tree *tree = (struct data_tree *)p;
size_t offset = (id * sizeof(struct node));
fseek(tree->fp, offset, SEEK_SET);
size_t r = fread(n, sizeof(struct node), 1, tree->fp);
return r == 1 ? 0 : -1;
}
static int tree_put_node(
struct b_tree *p,
unsigned long id,
const b_tree_node *n)
2025-01-31 21:39:29 +00:00
{
struct data_tree *tree = (struct data_tree *)p;
size_t offset = (id * sizeof(struct node));
fseek(tree->fp, offset, SEEK_SET);
size_t r = fwrite(n, sizeof(struct node), 1, tree->fp);
return r == 1 ? 0 : -1;
}
static long tree_alloc_node(struct b_tree *p)
{
struct data_tree *tree = (struct data_tree *)p;
size_t pos = ftell(tree->fp);
fseek(tree->fp, 0, SEEK_END);
size_t len = ftell(tree->fp);
struct node n;
node_init(&n);
fwrite(&n, sizeof n, 1, tree->fp);
fseek(tree->fp, pos, SEEK_SET);
len /= sizeof(struct node);
return (long)len;
}
static unsigned long node_get_nr_entries(b_tree_node *n)
{
struct node *node = (struct node *)n;
return node->nr_entries;
}
static void node_set_nr_entries(b_tree_node *n, unsigned long val)
{
struct node *node = (struct node *)n;
node->nr_entries = val;
}
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[index];
}
static void node_set_entry(
b_tree_node *n,
unsigned long index,
const b_tree_node_entry *entry)
2025-01-31 21:39:29 +00:00
{
struct node *node = (struct node *)n;
memmove(&node->entries[index], entry, sizeof(struct entry));
2025-01-31 21:39:29 +00:00
}
static unsigned long node_get_child(b_tree_node *n, unsigned long index)
2025-01-31 21:39:29 +00:00
{
struct node *node = (struct node *)n;
uint16_t child = node->children[index];
return child == INVALID_NODE_PTR ? B_TREE_INVALID_PTR : child;
2025-01-31 21:39:29 +00:00
}
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)
2025-01-31 21:39:29 +00:00
{
struct entry *a = (struct entry *)e0, *b = (struct entry *)e1;
if (a->key < b->key) {
return -1;
}
if (a->key > b->key) {
return 1;
}
return 0;
}
static const struct b_tree_ops ops = {
.tree_get_node = tree_get_node,
.tree_put_node = tree_put_node,
.tree_alloc_node = tree_alloc_node,
.node_get_nr_entries = node_get_nr_entries,
.node_set_nr_entries = node_set_nr_entries,
.node_get_entry = node_get_entry,
.node_set_entry = node_set_entry,
.node_get_child = node_get_child,
.node_set_child = node_set_child,
2025-01-31 21:39:29 +00:00
.entry_compare = entry_compare,
};
void indent(int depth)
{
for (int i = 0; i < depth; i++) {
fputs(" ", stdout);
}
}
int node_print(struct b_tree *tree, struct node *n, int depth)
{
int c_before = (n->nr_entries + 1) / 2;
int c_after = n->nr_entries + 1 - c_before;
int i_before = 0;
int i_after = i_before + c_before;
int err = 0;
for (unsigned short i = 0; i < c_before; i++) {
struct node child;
int index = i_before + i;
if (n->children[index] == INVALID_NODE_PTR) {
// indent(depth + 1);
continue;
}
err = tree_get_node(
tree,
n->children[index],
(b_tree_node *)&child);
2025-01-31 21:39:29 +00:00
if (err != 0) {
printf("ERR: failed to read node %u\n",
n->children[index]);
2025-01-31 21:39:29 +00:00
continue;
}
node_print(tree, &child, depth + 1);
}
indent(depth);
printf("[");
for (unsigned short i = 0; i < n->nr_entries; i++) {
if (i > 0) {
printf(" ");
}
printf("%d", n->entries[i].key);
}
printf("]\n");
for (unsigned short i = 0; i < c_after; i++) {
struct node child;
int index = i_after + i;
if (n->children[index] == INVALID_NODE_PTR) {
// indent(depth + 1);
continue;
}
err = tree_get_node(
tree,
n->children[index],
(b_tree_node *)&child);
2025-01-31 21:39:29 +00:00
if (err != 0) {
printf("ERR: failed to read node %u\n",
n->children[index]);
2025-01-31 21:39:29 +00:00
continue;
}
node_print(tree, &child, depth + 1);
}
return 0;
}
int tree_print(struct b_tree *tree)
{
struct node root;
tree_get_node(tree, 0, (b_tree_node *)&root);
return node_print(tree, &root, 0);
}
2024-11-02 11:17:36 +00:00
static int create(
const b_command *self,
const b_arglist *opt,
const b_array *args)
{
printf("%zu\n", sizeof(struct ec3_cluster_group));
return 0;
2024-12-18 20:51:01 +00:00
const char *in_path = NULL, *out_path = NULL;
2025-01-30 18:10:38 +00:00
b_arglist_get_string(
opt,
B_COMMAND_INVALID_ID,
ARG_INPATH,
0,
&in_path);
2024-12-18 20:51:01 +00:00
b_arglist_get_string(opt, OPT_OUTPATH, OPT_OUTPATH_PATH, 0, &out_path);
printf("in path: %s\n", in_path);
printf("out path: %s\n", out_path);
2025-01-31 21:39:29 +00:00
FILE *fp = fopen(out_path, "w+b");
2024-12-18 20:51:01 +00:00
2025-01-31 21:39:29 +00:00
struct node root;
node_init(&root);
fwrite(&root, sizeof root, 1, fp);
2024-12-18 20:51:01 +00:00
2025-01-31 21:39:29 +00:00
struct data_tree tree;
b_tree_init(
&tree.base,
&ops,
sizeof(struct node),
sizeof(struct entry),
ORDER);
2025-01-31 21:39:29 +00:00
tree.fp = fp;
2024-12-18 20:51:01 +00:00
2025-01-31 21:39:29 +00:00
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};
2025-01-31 21:39:29 +00:00
b_tree_put(&tree.base, (b_tree_node_entry *)&e);
2024-12-18 20:51:01 +00:00
}
2025-01-31 21:39:29 +00:00
tree_print(&tree.base);
2025-01-31 21:39:29 +00:00
fclose(fp);
2024-12-18 20:51:01 +00:00
2024-11-02 11:17:36 +00:00
return 0;
}
B_COMMAND(CMD_CREATE, CMD_ROOT)
{
B_COMMAND_NAME("create");
B_COMMAND_SHORT_NAME('C');
B_COMMAND_DESC("create an ec3 file");
B_COMMAND_FLAGS(B_COMMAND_SHOW_HELP_BY_DEFAULT);
B_COMMAND_FUNCTION(create);
B_COMMAND_HELP_OPTION();
B_COMMAND_OPTION(OPT_OUTPATH)
{
B_OPTION_SHORT_NAME('o');
B_OPTION_LONG_NAME("out");
B_OPTION_DESC("the path to save the new file to");
B_OPTION_ARG(OPT_OUTPATH_PATH)
{
B_ARG_NAME("path");
B_ARG_NR_VALUES(1);
}
}
2024-12-18 20:51:01 +00:00
2025-01-30 18:10:38 +00:00
B_COMMAND_ARG(ARG_INPATH)
{
2024-12-18 20:51:01 +00:00
B_ARG_NAME("input file");
B_ARG_NR_VALUES(1);
}
2025-01-30 18:10:38 +00:00
B_COMMAND_USAGE()
{
2024-12-18 20:51:01 +00:00
B_COMMAND_USAGE_ARG(ARG_INPATH);
B_COMMAND_USAGE_OPT(OPT_OUTPATH);
}
2024-11-02 11:17:36 +00:00
}
#endif