summaryrefslogtreecommitdiff
path: root/src/maat_ex_data.c
blob: d38cb7491c3ef2a7c11ecfd10abcc4b04599e9e6 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
**********************************************************************************************
*	File: maat_ex_data.c
*   Description: ex data
*	Authors: Liu WenTan <[email protected]>
*	Date:    2022-10-31
*   Copyright: (c) Since 2022 Geedge Networks, Ltd. All rights reserved.
***********************************************************************************************
*/

#include <stddef.h>
#include <stdio.h>
#include <assert.h>

#include "uthash/uthash.h"
#include "uthash/utarray.h"
#include "log/log.h"
#include "maat_utils.h"
#include "maat_ex_data.h"

#define MODULE_EX_DATA	 module_name_str("maat.ex_data")

struct ex_data_runtime {
    UT_array *cache_rows;
    size_t cache_row_num;
    size_t cache_size;

    struct rcu_hash_table *htable; // store ex_container
    struct ex_container_schema *ref_container_schema;
    int gc_timeout_s;
	int table_id;

    struct log_handle *logger;
};

void cache_row_free(void *p)
{
    struct ex_data_row *ex_data_row = (struct ex_data_row *)(char*)p;

    if (ex_data_row->row != NULL) {
        FREE(ex_data_row->row);
        ex_data_row->row = NULL;
    }
	free(*(char **)p);
}

void cache_row_copy(void *dst, const void *src)
{
    struct ex_data_row *ex_data_row_src = (struct ex_data_row *)src;
    struct ex_data_row *ex_data_row_dst = (struct ex_data_row *)dst;

    ex_data_row_dst->row = ALLOC(char, strlen(ex_data_row_src->row) + 1);
    strcpy(ex_data_row_dst->row, ex_data_row_src->row);
    ex_data_row_dst->op = ex_data_row_src->op;
}

UT_icd ut_cache_row_icd = {sizeof(struct ex_data_row), NULL, cache_row_copy, cache_row_free};

struct ex_data_runtime *
ex_data_runtime_new(int table_id, int gc_timeout_s, struct log_handle *logger)
{
    if (table_id < 0 || NULL == logger) {
        return NULL;
    }

    struct ex_data_runtime *ex_data_rt = ALLOC(struct ex_data_runtime, 1);

    utarray_new(ex_data_rt->cache_rows, &ut_cache_row_icd);
    ex_data_rt->htable = NULL;
    ex_data_rt->gc_timeout_s = gc_timeout_s;
    ex_data_rt->table_id = table_id;
    ex_data_rt->logger = logger;

    return ex_data_rt;
}

void ex_data_runtime_free(struct ex_data_runtime *ex_data_rt)
{
    if (NULL == ex_data_rt) {
        return;
    }

    if (ex_data_rt->cache_rows != NULL) {
		utarray_free(ex_data_rt->cache_rows);
		ex_data_rt->cache_rows = NULL;
		ex_data_rt->cache_row_num = 0;
	}

    if (ex_data_rt->htable != NULL) {
        rcu_hash_free(ex_data_rt->htable);
        ex_data_rt->htable = NULL;
    }

    FREE(ex_data_rt);
}

void ex_data_runtime_commit(struct ex_data_runtime *ex_data_rt)
{    
    if (NULL == ex_data_rt) {
        return;
    }

    rcu_hash_commit(ex_data_rt->htable);
}

void ex_data_runtime_cache_row_put(struct ex_data_runtime *ex_data_rt, const char *row, enum maat_operation op)
{
    if (NULL == ex_data_rt || NULL == row) {
        return;
    }

	size_t row_len = strlen(row);
    struct ex_data_row ex_data_row;
    ex_data_row.row = ALLOC(char, row_len + 1);

    ex_data_row.op = op;
	memcpy(ex_data_row.row, row, row_len);
	ex_data_rt->cache_size += row_len;
	utarray_push_back(ex_data_rt->cache_rows, &ex_data_row);
	ex_data_rt->cache_row_num++;	

    FREE(ex_data_row.row);
}

const struct ex_data_row *ex_data_runtime_cached_row_get(struct ex_data_runtime *ex_data_rt, size_t index)
{
    if (NULL == ex_data_rt) {
        return NULL;
    }

	return (struct ex_data_row *)utarray_eltptr(ex_data_rt->cache_rows, index);
}

size_t ex_data_runtime_cached_row_count(struct ex_data_runtime *ex_data_rt)
{
    if (NULL == ex_data_rt) {
        return 0;
    }

    return ex_data_rt->cache_row_num;
}

void ex_data_runtime_clear_row_cache(struct ex_data_runtime *ex_data_rt)
{
    if (NULL == ex_data_rt) {
        return;
    }

    if (ex_data_rt->cache_rows != NULL) {
        utarray_free(ex_data_rt->cache_rows);
        ex_data_rt->cache_rows = NULL;
    }
    
    ex_data_rt->cache_row_num = 0;
    ex_data_rt->cache_size = 0;
}

void ex_data_runtime_set_ex_container_schema(struct ex_data_runtime *ex_data_rt, 
                                             struct ex_container_schema *container_schema)
{
    ex_data_rt->ref_container_schema = container_schema;
}

void *ex_data_runtime_row2ex_data(struct ex_data_runtime *ex_data_rt,
                                  const char *table_name, const char *row, 
                                  const char *key, size_t key_len)
{   
    void *ex_data = NULL;
    struct ex_container_schema *container_schema = ex_data_rt->ref_container_schema;
    container_schema->ex_schema.new_func(table_name, key, row, 
                                         &ex_data, container_schema->ex_schema.argl, 
                                         container_schema->ex_schema.argp);
    return ex_data;
}

struct ex_container *ex_container_new(void *ex_data, void *custom_data)
{
    struct ex_container *ex_container = ALLOC(struct ex_container, 1);

    ex_container->ex_data = ex_data;
    ex_container->custom_data = custom_data;

    return ex_container;
}

void ex_container_free(void *ex_data_runtime, void *ex_container)
{
    /* schema is NULL if not call ex_data_runtime_set_ex_container_schema */
    if (NULL == ex_data_runtime || NULL == ex_container) {
        return;
    }

    struct ex_container *container = (struct ex_container *)ex_container;
    struct ex_data_runtime *ex_data_rt = (struct ex_data_runtime *)ex_data_runtime;
    struct ex_container_schema *container_schema = ex_data_rt->ref_container_schema;

    /* free ex_container->custom_data */
    if (container->custom_data != NULL && container_schema->custom_data_free != NULL) {
        container_schema->custom_data_free(container->custom_data);
        container->custom_data = NULL;
    }

    /* free ex_container->ex_data */
    if (container->ex_data != NULL && container_schema->ex_schema.free_func != NULL) {
        container_schema->ex_schema.free_func(container_schema->table_name,
                                              &(container->ex_data),
                                              container_schema->ex_schema.argl,
                                              container_schema->ex_schema.argp);
        container->ex_data = NULL;
    }

    FREE(container);
}

int ex_data_runtime_add_ex_container(struct ex_data_runtime *ex_data_rt,
                                     const char *key, size_t key_len,
                                     struct ex_container *ex_container)
{
    if (NULL == ex_data_rt || NULL == key || 0 == key_len ||
        NULL == ex_container) {
        return -1;
    }

    if (NULL == ex_data_rt->htable) {
        /* ex_data_rt->ref_container_schema has been set */
        assert(ex_data_rt->ref_container_schema != NULL);
        ex_data_rt->htable = rcu_hash_new(ex_container_free, ex_data_rt,
                                          ex_data_rt->gc_timeout_s);
    }

    return rcu_hash_add(ex_data_rt->htable, key, key_len, ex_container);
}

int ex_data_runtime_del_ex_container(struct ex_data_runtime *ex_data_rt,
                                     const char *key, size_t key_len)
{
    if (NULL == ex_data_rt || NULL == key || 0 == key_len ||
        NULL == ex_data_rt->htable) {
        return -1;
    }

    return rcu_hash_del(ex_data_rt->htable, key, key_len);
}

void *ex_data_runtime_get_ex_data_by_key(struct ex_data_runtime *ex_data_rt,
                                         const char *key, size_t key_len)
{
    if (NULL == ex_data_rt || NULL == key || 0 == key_len) {
        return NULL;
    }

    struct ex_container_schema *container_schema = ex_data_rt->ref_container_schema;
    if (NULL == container_schema || (0 == container_schema->set_flag)) {
        return NULL;
    }

    struct ex_container *ex_container = (struct ex_container *)rcu_hash_find(ex_data_rt->htable, 
                                                                             key, key_len);
    if (NULL == ex_container) {
        return NULL;
    }

    void *dup_ex_data = NULL;
    container_schema->ex_schema.dup_func(container_schema->table_name, &dup_ex_data, 
                                         &(ex_container->ex_data), 
                                         container_schema->ex_schema.argl, 
                                         container_schema->ex_schema.argp);
    return dup_ex_data;
}

void *ex_data_runtime_get_ex_data_by_container(struct ex_data_runtime *ex_data_rt,
                                               struct ex_container *ex_container)
{
    if (NULL == ex_data_rt || NULL == ex_container) {
        return NULL;
    }

    struct ex_container_schema *container_schema = ex_data_rt->ref_container_schema;
    if (NULL == container_schema || (0 == container_schema->set_flag)) {
        return NULL;
    }

    void *dup_ex_data = NULL;
    container_schema->ex_schema.dup_func(container_schema->table_name, &dup_ex_data, 
                                         &(ex_container->ex_data),
                                         container_schema->ex_schema.argl, 
                                         container_schema->ex_schema.argp);
    return dup_ex_data;
}

size_t ex_data_runtime_ex_container_count(struct ex_data_runtime *ex_data_rt)
{
    return rcu_hash_count(ex_data_rt->htable);
}

int ex_data_runtime_is_updating(struct ex_data_runtime *ex_data_rt)
{
    return rcu_hash_is_updating(ex_data_rt->htable);
}

size_t ex_data_runtime_list_updating_ex_container(struct ex_data_runtime *ex_data_rt, 
                                                  struct ex_container ***ex_container)
{
    return rcu_updating_hash_list(ex_data_rt->htable, (void ***)ex_container);
}

void ex_data_runtime_garbage_collect_routine(struct ex_data_runtime *ex_data_rt)
{
    if (NULL == ex_data_rt) {
        return;
    }

    if (ex_data_rt->htable != NULL) {
        rcu_hash_garbage_collect_routine(ex_data_rt->htable);
    }
}