summaryrefslogtreecommitdiffstats
path: root/src/aclk/schema-wrappers/context.cc
blob: b04c9d20cc26532b12463e9ac2952c04cf9e2fd0 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// SPDX-License-Identifier: GPL-3.0-or-later

#include "proto/context/v1/context.pb.h"

#include "libnetdata/libnetdata.h"

#include "schema_wrapper_utils.h"

#include "context.h"

using namespace context::v1;

// ContextsSnapshot
contexts_snapshot_t contexts_snapshot_new(const char *claim_id, const char *node_id, uint64_t version)
{
    ContextsSnapshot *ctxs_snap = new ContextsSnapshot;

    if (ctxs_snap == NULL)
        fatal("Cannot allocate ContextsSnapshot object. OOM");

    ctxs_snap->set_claim_id(claim_id);
    ctxs_snap->set_node_id(node_id);
    ctxs_snap->set_version(version);

    return ctxs_snap;
}

void contexts_snapshot_delete(contexts_snapshot_t snapshot)
{
    delete (ContextsSnapshot *)snapshot;
}

void contexts_snapshot_set_version(contexts_snapshot_t ctxs_snapshot, uint64_t version)
{
    ((ContextsSnapshot *)ctxs_snapshot)->set_version(version);
}

static void fill_ctx_updated(ContextUpdated *ctx, struct context_updated *c_ctx)
{
    ctx->set_id(c_ctx->id);
    ctx->set_version(c_ctx->version);
    ctx->set_first_entry(c_ctx->first_entry);
    ctx->set_last_entry(c_ctx->last_entry);
    ctx->set_deleted(c_ctx->deleted);
    ctx->set_title(c_ctx->title);
    ctx->set_priority(c_ctx->priority);
    ctx->set_chart_type(c_ctx->chart_type);
    ctx->set_units(c_ctx->units);
    ctx->set_family(c_ctx->family);
}

void contexts_snapshot_add_ctx_update(contexts_snapshot_t ctxs_snapshot, struct context_updated *ctx_update)
{
    ContextsSnapshot *ctxs_snap = (ContextsSnapshot *)ctxs_snapshot;
    ContextUpdated *ctx = ctxs_snap->add_contexts();

    fill_ctx_updated(ctx, ctx_update);
}

char *contexts_snapshot_2bin(contexts_snapshot_t ctxs_snapshot, size_t *len)
{
    ContextsSnapshot *ctxs_snap = (ContextsSnapshot *)ctxs_snapshot;
    *len = PROTO_COMPAT_MSG_SIZE_PTR(ctxs_snap);
    char *bin = (char*)mallocz(*len);
    if (!ctxs_snap->SerializeToArray(bin, *len)) {
        freez(bin);
        delete ctxs_snap;
        return NULL;
    }

    delete ctxs_snap;
    return bin;
}

// ContextsUpdated
contexts_updated_t contexts_updated_new(const char *claim_id, const char *node_id, uint64_t version_hash, uint64_t created_at)
{
    ContextsUpdated *ctxs_updated = new ContextsUpdated;

    if (ctxs_updated == NULL)
        fatal("Cannot allocate ContextsUpdated object. OOM");

    ctxs_updated->set_claim_id(claim_id);
    ctxs_updated->set_node_id(node_id);
    ctxs_updated->set_version_hash(version_hash);
    ctxs_updated->set_created_at(created_at);

    return ctxs_updated;
}

void contexts_updated_delete(contexts_updated_t ctxs_updated)
{
    delete (ContextsUpdated *)ctxs_updated;
}

void contexts_updated_update_version_hash(contexts_updated_t ctxs_updated, uint64_t version_hash)
{
    ((ContextsUpdated *)ctxs_updated)->set_version_hash(version_hash);
}

void contexts_updated_add_ctx_update(contexts_updated_t ctxs_updated, struct context_updated *ctx_update)
{
    ContextsUpdated *ctxs_update = (ContextsUpdated *)ctxs_updated;
    ContextUpdated *ctx = ctxs_update->add_contextupdates();

    if (ctx == NULL)
        fatal("Cannot allocate ContextUpdated object. OOM");

    fill_ctx_updated(ctx, ctx_update);
}

char *contexts_updated_2bin(contexts_updated_t ctxs_updated, size_t *len)
{
    ContextsUpdated *ctxs_update = (ContextsUpdated *)ctxs_updated;
    *len = PROTO_COMPAT_MSG_SIZE_PTR(ctxs_update);
    char *bin = (char*)mallocz(*len);
    if (!ctxs_update->SerializeToArray(bin, *len)) {
        freez(bin);
        delete ctxs_update;
        return NULL;
    }

    delete ctxs_update;
    return bin;
}