upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/main/negentropy_adapter.c
blob: 29392894021b2186a3fb6f4c06607c09eefe9d52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "negentropy_adapter.h"
#include "storage_engine.h"
#include <stdlib.h>
#include <string.h>
#include "esp_log.h"

static const char *TAG = "negentropy_adapter";

struct negentropy_adapter {
    void *storage;
    negentropy_item_t *items;
    size_t count;
    size_t capacity;
};

negentropy_adapter_t *negentropy_adapter_from_storage(void *storage_engine)
{
    if (!storage_engine) return NULL;

    negentropy_adapter_t *adapter = calloc(1, sizeof(negentropy_adapter_t));
    if (!adapter) return NULL;

    adapter->storage = storage_engine;
    adapter->items = NULL;
    adapter->count = 0;
    adapter->capacity = 0;

    return adapter;
}

esp_err_t negentropy_adapter_get_items(negentropy_adapter_t *adapter,
                                       negentropy_item_t **items,
                                       size_t *count)
{
    if (!adapter || !items || !count) return ESP_ERR_INVALID_ARG;

    if (adapter->items) {
        free(adapter->items);
        adapter->items = NULL;
    }
    adapter->count = 0;
    adapter->capacity = 0;

    *items = adapter->items;
    *count = adapter->count;

    ESP_LOGI(TAG, "Adapter has %zu items", adapter->count);
    return ESP_OK;
}

esp_err_t negentropy_adapter_insert_item(negentropy_adapter_t *adapter,
                                          uint64_t created_at,
                                          const uint8_t *id)
{
    if (!adapter || !id) return ESP_ERR_INVALID_ARG;

    if (adapter->count >= adapter->capacity) {
        size_t new_cap = adapter->capacity == 0 ? 64 : adapter->capacity * 2;
        negentropy_item_t *new_items = realloc(adapter->items, new_cap * sizeof(negentropy_item_t));
        if (!new_items) return ESP_ERR_NO_MEM;
        adapter->items = new_items;
        adapter->capacity = new_cap;
    }

    negentropy_item_t *item = &adapter->items[adapter->count];
    item->created_at = created_at;
    memcpy(item->id, id, 32);
    adapter->count++;

    return ESP_OK;
}

void negentropy_adapter_destroy(negentropy_adapter_t *adapter)
{
    if (!adapter) return;
    if (adapter->items) free(adapter->items);
    free(adapter);
}