aboutsummaryrefslogtreecommitdiffstats
path: root/src/db.c
blob: 09151a153e9a1358181af598beabfcd5a068f42a (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
/*
 * 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.
 */
/******************************************************************************/
/******************************************************************************/
/* INTERNAL DATABASE IMPLEMENTATION                                           */
/******************************************************************************/
/******************************************************************************/

#include <assert.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdalign.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <errno.h>

#include "data.h"
#include "anydb.h"
#include "filedb.h"
#include "memdb.h"
#include "db.h"

static anydb_t *memdb;
static anydb_t *filedb;
static bool modifiable;

/**
 * check whether the 'text' fit String_Any, String_Wide, NULL or ""
 * @param text the text to check
 * @return true if ANY or WIDE
 */
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));
}


/* see db.h */
int
db_open(
	const char *directory
) {
	int rc;

	rc = memdb_create(&memdb);
	if (!rc) {
		rc = filedb_create(&filedb, directory, NULL);
		if (rc)
			anydb_destroy(memdb);
	}
	return rc;
}

/* see db.h */
void
db_close(
) {
	anydb_destroy(filedb);
	anydb_destroy(memdb);
}

/* see db.h */
bool
db_is_empty(
) {
	return anydb_is_empty(filedb);
}

/* see db.h */
int
db_transaction_begin(
) {
	int rc1, rc2, rc;

	if (modifiable)
		return -EALREADY;

	rc1 = anydb_transaction(filedb, Anydb_Transaction_Start);
	rc2 = anydb_transaction(memdb, Anydb_Transaction_Start);

	rc = rc1 ?: rc2;
	modifiable = !rc;

	return rc;
}

/* see db.h */
int
db_transaction_end(
	bool commit
) {
	int rc1, rc2, rc3, rc4;

	if (!modifiable)
		return -EALREADY;

	if (commit) {
		rc1 = anydb_transaction(filedb, Anydb_Transaction_Commit);
		rc2 = anydb_transaction(memdb, Anydb_Transaction_Commit);
		rc3 = db_cleanup();
	} else {
		rc1 = anydb_transaction(filedb, Anydb_Transaction_Cancel);
		rc2 = anydb_transaction(memdb, Anydb_Transaction_Cancel);
		rc3 = 0;
	}
	rc4 = db_sync();
	modifiable = false;

	return rc1 ?: rc2 ?: rc3 ?: rc4;
}


/* see db.h */
void
db_for_all(
	void (*callback)(
		void *closure,
		const data_key_t *key,
		const data_value_t *value),
	void *closure,
	const data_key_t *key
) {
	anydb_for_all(filedb, callback, closure, key);
	anydb_for_all(memdb, callback, closure, key);
}

/* see db.h */
int
db_drop(
	const data_key_t *key
) {
	if (!modifiable)
		return -EACCES;

	anydb_drop(filedb, key);
	anydb_drop(memdb, key);
	return 0;
}

/* see db.h */
int
db_set(
	const data_key_t *key,
	const data_value_t *value
) {
	anydb_t *db;

	if (!modifiable)
		return -EACCES;

	db = is_any_or_wide(key->session) ? filedb : memdb;
	return anydb_set(db, key, value);
}

/* see db.h */
unsigned
db_test(
	const data_key_t *key,
	data_value_t *value
) {
	unsigned s1, s2;
	data_value_t v1, v2;

	s1 = anydb_test(memdb, key, &v1);
	s2 = anydb_test(filedb, key, &v2);
	if (s2 > s1) {
		*value = v2;
		return s2;
	}
	if (s1)
		*value = v1;
	return s1;
}

/* see db.h */
int
db_cleanup(
) {
	anydb_cleanup(filedb);
	anydb_cleanup(memdb);
	return 0;
}

/* see db.h */
int
db_sync(
) {
	int rc1 = anydb_sync(filedb);
	int rc2 = anydb_sync(memdb);
	return rc1 ?: rc2;
}