| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #include "context/v1/context.pb.h" |
| 4 | |
| 5 | #include "libnetdata/libnetdata.h" |
| 6 | |
| 7 | #include "schema_wrapper_utils.h" |
| 8 | |
| 9 | #include "rrdcontext-context.h" |
| 10 | |
| 11 | using namespace context::v1; |
| 12 | |
| 13 | // ContextsSnapshot |
| 14 | contexts_snapshot_t contexts_snapshot_new(const char *claim_id, const char *node_id, uint64_t version) |
| 15 | { |
| 16 | ContextsSnapshot *ctxs_snap = new ContextsSnapshot; |
| 17 | |
| 18 | if (ctxs_snap == NULL) |
| 19 | fatal("Cannot allocate ContextsSnapshot object. OOM"); |
| 20 | |
| 21 | ctxs_snap->set_claim_id(claim_id); |
| 22 | ctxs_snap->set_node_id(node_id); |
| 23 | ctxs_snap->set_version(version); |
| 24 | |
| 25 | return ctxs_snap; |
| 26 | } |
| 27 | |
| 28 | void contexts_snapshot_delete(contexts_snapshot_t snapshot) |
| 29 | { |
| 30 | delete (ContextsSnapshot *)snapshot; |
| 31 | } |
| 32 | |
| 33 | void contexts_snapshot_set_version(contexts_snapshot_t ctxs_snapshot, uint64_t version) |
| 34 | { |
| 35 | ((ContextsSnapshot *)ctxs_snapshot)->set_version(version); |
| 36 | } |
| 37 | |
| 38 | static void fill_ctx_updated(ContextUpdated *ctx, struct context_updated *c_ctx) |
| 39 | { |
| 40 | ctx->set_id(c_ctx->id); |
| 41 | ctx->set_version(c_ctx->version); |
| 42 | ctx->set_first_entry(c_ctx->first_entry); |
| 43 | ctx->set_last_entry(c_ctx->last_entry); |
| 44 | ctx->set_deleted(c_ctx->deleted); |
| 45 | ctx->set_title(c_ctx->title); |
| 46 | ctx->set_priority(c_ctx->priority); |
| 47 | ctx->set_chart_type(c_ctx->chart_type); |
| 48 | ctx->set_units(c_ctx->units); |
| 49 | ctx->set_family(c_ctx->family); |
| 50 | } |
| 51 | |
| 52 | void contexts_snapshot_add_ctx_update(contexts_snapshot_t ctxs_snapshot, struct context_updated *ctx_update) |
| 53 | { |
| 54 | ContextsSnapshot *ctxs_snap = (ContextsSnapshot *)ctxs_snapshot; |
| 55 | ContextUpdated *ctx = ctxs_snap->add_contexts(); |
| 56 | |
| 57 | fill_ctx_updated(ctx, ctx_update); |
| 58 | } |
| 59 | |
| 60 | char *contexts_snapshot_2bin(contexts_snapshot_t ctxs_snapshot, size_t *len) |
| 61 | { |
| 62 | ContextsSnapshot *ctxs_snap = (ContextsSnapshot *)ctxs_snapshot; |
| 63 | *len = PROTO_COMPAT_MSG_SIZE_PTR(ctxs_snap); |
| 64 | char *bin = (char*)mallocz(*len); |
| 65 | if (!ctxs_snap->SerializeToArray(bin, *len)) { |
| 66 | freez(bin); |
| 67 | delete ctxs_snap; |
| 68 | return NULL; |
| 69 | } |
| 70 | |
| 71 | delete ctxs_snap; |
| 72 | return bin; |
| 73 | } |
| 74 | |
| 75 | // ContextsUpdated |
| 76 | contexts_updated_t contexts_updated_new(const char *claim_id, const char *node_id, uint64_t version_hash, uint64_t created_at) |
| 77 | { |
| 78 | ContextsUpdated *ctxs_updated = new ContextsUpdated; |
| 79 | |
| 80 | if (ctxs_updated == NULL) |
| 81 | fatal("Cannot allocate ContextsUpdated object. OOM"); |
| 82 | |
| 83 | ctxs_updated->set_claim_id(claim_id); |
| 84 | ctxs_updated->set_node_id(node_id); |
| 85 | ctxs_updated->set_version_hash(version_hash); |
| 86 | ctxs_updated->set_created_at(created_at); |
| 87 | |
| 88 | return ctxs_updated; |
| 89 | } |
| 90 | |
| 91 | void contexts_updated_delete(contexts_updated_t ctxs_updated) |
| 92 | { |
| 93 | delete (ContextsUpdated *)ctxs_updated; |
| 94 | } |
| 95 | |
| 96 | void contexts_updated_update_version_hash(contexts_updated_t ctxs_updated, uint64_t version_hash) |
| 97 | { |
| 98 | ((ContextsUpdated *)ctxs_updated)->set_version_hash(version_hash); |
| 99 | } |
| 100 | |
| 101 | void contexts_updated_add_ctx_update(contexts_updated_t ctxs_updated, struct context_updated *ctx_update) |
| 102 | { |
| 103 | ContextsUpdated *ctxs_update = (ContextsUpdated *)ctxs_updated; |
| 104 | ContextUpdated *ctx = ctxs_update->add_contextupdates(); |
| 105 | |
| 106 | if (ctx == NULL) |
| 107 | fatal("Cannot allocate ContextUpdated object. OOM"); |
| 108 | |
| 109 | fill_ctx_updated(ctx, ctx_update); |
| 110 | } |
| 111 | |
| 112 | char *contexts_updated_2bin(contexts_updated_t ctxs_updated, size_t *len) |
| 113 | { |
| 114 | ContextsUpdated *ctxs_update = (ContextsUpdated *)ctxs_updated; |
| 115 | *len = PROTO_COMPAT_MSG_SIZE_PTR(ctxs_update); |
| 116 | char *bin = (char*)mallocz(*len); |
| 117 | if (!ctxs_update->SerializeToArray(bin, *len)) { |
| 118 | freez(bin); |
| 119 | delete ctxs_update; |
| 120 | return NULL; |
| 121 | } |
| 122 | |
| 123 | delete ctxs_update; |
| 124 | return bin; |
| 125 | } |