aboutsummaryrefslogtreecommitdiffstats
path: root/src/anydb.c
blob: ef41af77347b506b65d8059b3caaf54958441b48 (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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
/*
 * Copyright (C) 2018 "IoT.bzh"
 * Author José Bollo <jose.bollo@iot.bzh>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/******************************************************************************/
/******************************************************************************/
/* HIGH LEVEL ABSTRACTION OF THE DATABASES                                    */
/******************************************************************************/
/******************************************************************************/

#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <time.h>
#include <string.h>
#include <errno.h>

#include "data.h"
#include "anydb.h"


#define CLIENT_MATCH_SCORE	1
#define SESSION_MATCH_SCORE	1
#define USER_MATCH_SCORE	1
#define PERMISSION_MATCH_SCORE	1

/**
 * helper for searching items
 */
union searchkey {
	struct {
		/** client id */
		anydb_idx_t idxcli;

		/** session id */
		anydb_idx_t idxses;

		/** user id */
		anydb_idx_t idxusr;

		/** permission string */
		const char *strperm;
	};
	anydb_key_t key;
};
typedef union searchkey searchkey_t;

/******************************************************************************/
/******************************************************************************/
/*** UTILITIES                                                              ***/
/******************************************************************************/
/******************************************************************************/

/**
 * Check whether the 'text' fit String_Any, NULL or ""
 * @param text the text to check
 * @return true if text matches ANY possible values
 */
static
bool
is_any(
	const char *text
) {
	return text == NULL || text[0] == 0 || (!text[1] && text[0] == Data_Any_Char);
}

/**
 * check whether the 'text' fit String_Any, String_Wide, NULL or ""
 * @param text the text to check
 * @return true if text matches ANY or WIDE possible values
 */
static
bool
is_any_or_wide(
	const char *text
) {
	return text == NULL || text[0] == 0
		|| (!text[1] && (text[0] == Data_Any_Char || text[0] == Data_Wide_Char));
}

/**
 * Get the name stored for index
 * @param db the anydb database to query
 * @param idx the index of the string to get
 * @return the name or NULL if doesn't exist
 */
static
const char*
string(
	anydb_t *db,
	anydb_idx_t idx
) {
	if (idx == AnyIdx_Any)
		return Data_Any_String;
	if (idx == AnyIdx_Wide)
		return Data_Wide_String;
	return db->itf.string(db->clodb, idx);
}

/**
 * Search the index of 'name' and create it if 'create'
 * @param db the anydb database to query
 * @param idx where to store the result if needed
 * @param name name to search and/or create
 * @param create if not nul, the name is created if it doesn't exist
 * @return 0 in case of success of -errno
 */
static
int
idx(
	anydb_t *db,
	anydb_idx_t *idx,
	const char *name,
	bool create
) {
	/* handle special names */
	if (!name || !name[0]) {
		/* no name or empty name means ANY */
		*idx = AnyIdx_Any;
		return 0;
	}
	/* handle special names of one character  */
	if (!name[1]) {
		if (name[0] == Data_Any_Char) {
			/* Single char for ANY */
			*idx = AnyIdx_Any;
			return 0;
		}
		if (name[0] == Data_Wide_Char) {
			/* Single char for WIDE */
			*idx = AnyIdx_Wide;
			return 0;
		}
	}

	/* other case: ask the database backend */
	return db->itf.index(db->clodb, idx, name, create);
}

/**
 * Search the index of 'name' and create it if 'create'
 * Return the index for WIDE if name matches ANY or WIDE
 * @param db the backend database
 * @param idx where to store the found index
 * @param name the name to search or create
 * @param create not nul for authorizing creation of the index for name
 * @return 0 on success
 */
static
int
idx_but_any(
	anydb_t *db,
	anydb_idx_t *idx,
	const char *name,
	bool create
) {
	if (is_any_or_wide(name)) {
		*idx = AnyIdx_Wide;
		return 0;
	}

	/* other case */
	return db->itf.index(db->clodb, idx, name, create);
}

/**
 * Return the index of 'name' in the database 'db'. In option 'create' it.
 * If the name encode ANY or WIDE returns WIDE.
 * @param db the backend database
 * @param name the name to search or create
 * @param create not nul for authorizing creation of the index for name
 * @return the found index or AnyIdx_None if not found.
 */
static
anydb_idx_t
idx_or_none_but_any(
	anydb_t *db,
	const char *name,
	bool create
) {
	anydb_idx_t idx;

	if (idx_but_any(db, &idx, name, create))
		idx = AnyIdx_None;
	return idx;
}

/******************************************************************************/
/******************************************************************************/
/*** EXPIRATION                                                             ***/
/******************************************************************************/
/******************************************************************************/

static
bool
expired(
	time_t expire,
	time_t now
) {
	if (expire < 0)
		expire = -(expire + 1);
	return expire && expire <= now;
}

/******************************************************************************/
/******************************************************************************/
/*** SEARCH KEYS                                                            ***/
/******************************************************************************/
/******************************************************************************/

static
bool
searchkey_prepare_match(anydb_t *db,
	const data_key_t *key,
	searchkey_t *skey,
	bool create
) {
	if (idx(db, &skey->idxcli, key->client, create)
	 || idx(db, &skey->idxses, key->session, create)
	 || idx(db, &skey->idxusr, key->user, create))
		return false; /* one of the idx doesn't exist */
	skey->strperm = is_any(key->permission) ? NULL : key->permission;

	return true;
}

static
bool
searchkey_match(
	anydb_t *db,
	const anydb_key_t *key,
	const searchkey_t *skey
) {
	return (skey->idxcli == AnyIdx_Any || skey->idxcli == key->client)
	    && (skey->idxses == AnyIdx_Any || skey->idxses == key->session)
	    && (skey->idxusr == AnyIdx_Any || skey->idxusr == key->user)
	    && (!skey->strperm || !strcasecmp(skey->strperm, string(db, key->permission)));
}

static
int
searchkey_prepare_is(
	anydb_t *db,
	const data_key_t *key,
	searchkey_t *skey,
	bool create
) {
	int rc;

	rc = idx_but_any(db, &skey->idxcli, key->client, create);
	if (!rc) {
		rc = idx_but_any(db, &skey->idxses, key->session, create);
		if (!rc) {
			rc = idx_but_any(db, &skey->idxusr, key->user, create);
			if (!rc)
				skey->strperm = key->permission;
		}
	}
	return rc;
}

static
bool
searchkey_is(
	anydb_t *db,
	const anydb_key_t *key,
	const searchkey_t *skey
) {
	return skey->idxcli == key->client
	    && skey->idxses == key->session
	    && skey->idxusr == key->user
	    && !strcasecmp(skey->strperm, string(db, key->permission));
}

static
void
searchkey_prepare_test(
	anydb_t *db,
	const data_key_t *key,
	searchkey_t *skey,
	bool create
) {
	skey->idxcli = idx_or_none_but_any(db, key->client, create);
	skey->idxses = idx_or_none_but_any(db, key->session, create);
	skey->idxusr = idx_or_none_but_any(db, key->user, create);
	skey->strperm = key->permission;
}

static
unsigned
searchkey_test(
	anydb_t *db,
	const anydb_key_t *key,
	const searchkey_t *skey
) {
	unsigned sc;

	if ((key->client  != AnyIdx_Wide && skey->idxcli != key->client)
	 || (key->session != AnyIdx_Wide && skey->idxses != key->session)
	 || (key->user    != AnyIdx_Wide && skey->idxusr != key->user)
	 || (key->permission != AnyIdx_Wide
	        && strcasecmp(skey->strperm, string(db, key->permission)))) {
		sc = 0;
	} else {
		sc = 1;
		if (key->client != AnyIdx_Wide)
			sc += CLIENT_MATCH_SCORE;
		if (key->session != AnyIdx_Wide)
			sc += SESSION_MATCH_SCORE;
		if (key->user != AnyIdx_Wide)
			sc += USER_MATCH_SCORE;
		if (key->permission != AnyIdx_Wide)
			sc += PERMISSION_MATCH_SCORE;
	}
	return sc;
}

/******************************************************************************/
/******************************************************************************/
/*** FOR ALL                                                                ***/
/******************************************************************************/
/******************************************************************************/

/* see anydb.h */
int
anydb_transaction(
	anydb_t *db,
	anydb_transaction_t oper
) {
	if (db->itf.transaction)
		return db->itf.transaction(db->clodb, oper);
	return -ENOTSUP;
}

/******************************************************************************/
/******************************************************************************/
/*** FOR ALL                                                                ***/
/******************************************************************************/
/******************************************************************************/

struct for_all_s
{
	anydb_t *db;          /* targeted database */
	time_t now;           /* also drop expired items */
	searchkey_t skey;
	void *closure;
	void (*callback)(
		void *closure,
		const data_key_t *key,
		const data_value_t *value);
};

static
anydb_action_t
for_all_cb(
	void *closure,
	const anydb_key_t *key,
	anydb_value_t *value
) {
	struct for_all_s *s = closure;
	data_key_t k;
	data_value_t v;

	/* drop expired items */
	if (expired(value->expire, s->now))
		return Anydb_Action_Remove_And_Continue;

	if (searchkey_match(s->db, key, &s->skey)) {
		k.client = string(s->db, key->client);
		k.session = string(s->db, key->session);
		k.user = string(s->db, key->user);
		k.permission = string(s->db, key->permission);
		v.value = string(s->db, value->value);
		v.expire = value->expire;
		s->callback(s->closure, &k, &v);
	}
	return Anydb_Action_Continue;
}

/* see anydb.h */
void
anydb_for_all(
	anydb_t *db,
	void (*callback)(
		void *closure,
		const data_key_t *key,
		const data_value_t *value),
	void *closure,
	const data_key_t *key
) {
	struct for_all_s s;

	if (!searchkey_prepare_match(db, key, &s.skey, false))
		return; /* nothing to do! because one of the idx doesn't exist */

	s.db = db;
	s.closure = closure;
	s.callback = callback;
	s.now = time(NULL);
	db->itf.apply(db->clodb, for_all_cb, &s);
}

/******************************************************************************/
/******************************************************************************/
/*** DROP                                                                   ***/
/******************************************************************************/
/******************************************************************************/

/* structure for dropping items */
struct drop_s
{
	anydb_t *db;          /* targeted database */
	time_t now;           /* also drop expired items */
	searchkey_t skey;     /* the search key */
};

/* callback for dropping items */
static
anydb_action_t
drop_cb(
	void *closure,
	const anydb_key_t *key,
	anydb_value_t *value
) {
	struct drop_s *s = closure;

	/* drop expired items */
	if (expired(value->expire, s->now))
		return Anydb_Action_Remove_And_Continue;

	/* remove if matches the key */
	if (searchkey_match(s->db, key, &s->skey))
		return Anydb_Action_Remove_And_Continue;

	/* continue to next */
	return Anydb_Action_Continue;
}

/* see anydb.h */
void
anydb_drop(
	anydb_t *db,
	const data_key_t *key
) {
	struct drop_s s;

	if (!searchkey_prepare_match(db, key, &s.skey, false))
		return; /* nothing to do! because one of the idx doesn't exist */

	s.db = db;
	s.now = time(NULL);
	db->itf.apply(db->clodb, drop_cb, &s);
}

/******************************************************************************/
/******************************************************************************/
/*** SET                                                                    ***/
/******************************************************************************/
/******************************************************************************/

/* structure for setting values */
struct set_s
{
	anydb_t *db;          /* targeted database */
	time_t now;           /* also drop expired items */
	searchkey_t skey;     /* searching key */
	anydb_value_t value;  /* value to set */
};

/* callback for setting values */
static
anydb_action_t
set_cb(
	void *closure,
	const anydb_key_t *key,
	anydb_value_t *value
) {
	struct set_s *s = closure;

	/* drop expired items */
	if (expired(value->expire, s->now))
		return Anydb_Action_Remove_And_Continue;

	if (searchkey_is(s->db, key, &s->skey)) {
		value->value = s->value.value;
		value->expire = s->value.expire;
		s->db = NULL; /* indicates that is found */
		return Anydb_Action_Update_And_Stop;
	}

	return Anydb_Action_Continue;
}

/* see anydb.h */
int
anydb_set(
	anydb_t *db,
	const data_key_t *key,
	const data_value_t *value
) {
	int rc;
	struct set_s s;

	rc = searchkey_prepare_is(db, key, &s.skey, true);
	if (rc)
		goto error;

	rc = idx(db, &s.value.value, value->value, true);
	if (rc)
		goto error;

	s.db = db;
	s.value.expire = value->expire;
	s.now = time(NULL);
	db->itf.apply(db->clodb, set_cb, &s);
	if (s.db) {
		/* no item to alter so must be added */
		rc = idx(db, &s.skey.key.permission, key->permission, true);
		if (!rc)
			rc = db->itf.add(db->clodb, &s.skey.key, &s.value);
	}
error:
	return rc;
}

/******************************************************************************/
/******************************************************************************/
/*** TEST                                                                   ***/
/******************************************************************************/
/******************************************************************************/

/* structure for testing rule */
struct test_s
{
	anydb_t *db;          /* targeted database */
	time_t now;
	unsigned score;
	searchkey_t skey;
	anydb_value_t value;
};

/* callback for testing rule */
static
anydb_action_t
test_cb(
	void *closure,
	const anydb_key_t *key,
	anydb_value_t *value
) {
	struct test_s *s = closure;
	unsigned sc;

	/* drop expired items */
	if (expired(value->expire, s->now))
		return Anydb_Action_Remove_And_Continue;

	sc = searchkey_test(s->db, key, &s->skey);
	if (sc > s->score) {
		s->score = sc;
		s->value = *value;
	}
	return Anydb_Action_Continue;
}

/* see anydb.h */
unsigned
anydb_test(
	anydb_t *db,
	const data_key_t *key,
	data_value_t *value
) {
	struct test_s s;

	searchkey_prepare_test(db, key, &s.skey, true);

	s.db = db;
	s.now = time(NULL);
	s.score = 0;
	db->itf.apply(db->clodb, test_cb, &s);
	if (s.score) {
		value->value = string(db, s.value.value);
		value->expire = s.value.expire;
	}
	return s.score;
}


/******************************************************************************/
/******************************************************************************/
/*** IS EMPTY                                                               ***/
/******************************************************************************/
/******************************************************************************/

/* structure for testing emptyness */
struct empty_s
{
	bool empty;
	time_t now;
};

/* callback for computing if empty */
static
anydb_action_t
is_empty_cb(
	void *closure,
	const anydb_key_t *key,
	anydb_value_t *value
) {
	struct empty_s *s = closure;

	/* drop expired items */
	if (expired(value->expire, s->now))
		return Anydb_Action_Remove_And_Continue;

	s->empty = false;
	return Anydb_Action_Stop;
}

/* see anydb.h */
bool
anydb_is_empty(
	anydb_t *db
) {
	struct empty_s s;

	s.empty = true;
	s.now = time(NULL);
	db->itf.apply(db->clodb, is_empty_cb, &s);
	return s.empty;
}

/******************************************************************************/
/******************************************************************************/
/*** CLEANUP                                                                ***/
/******************************************************************************/
/******************************************************************************/

/* cleanup callback */
static
anydb_action_t
cleanup_cb(
	void *closure,
	const anydb_key_t *key,
	anydb_value_t *value
) {
	return expired(value->expire, *(time_t*)closure)
		? Anydb_Action_Remove_And_Continue : Anydb_Action_Continue;
}

/* see anydb.h */
void
anydb_cleanup(
	anydb_t *db
) {
	time_t t;

	t = time(NULL);
	db->itf.apply(db->clodb, cleanup_cb, &t);
	db->itf.gc(db->clodb);
}

/******************************************************************************/
/******************************************************************************/
/*** SYNCHRONIZE                                                            ***/
/******************************************************************************/
/******************************************************************************/

/* see anydb.h */
int
anydb_sync(
	anydb_t *db
) {
	return db->itf.sync ? db->itf.sync(db->clodb) : 0;
}

/******************************************************************************/
/******************************************************************************/
/*** DESTROY                                                                ***/
/******************************************************************************/
/******************************************************************************/

/* see anydb.h */
void
anydb_destroy(
	anydb_t *db
) {
	db->itf.destroy(db->clodb);
}