aboutsummaryrefslogtreecommitdiffstats
path: root/src/db.h
blob: 0171c0219526db1553df75dec8dd0c4935e7f821 (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
/*
 * 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.
 */
#pragma once
/******************************************************************************/
/******************************************************************************/
/* INTERNAL DATABASE IMPLEMENTATION                                           */
/******************************************************************************/
/******************************************************************************/

/**
 * Open the database in the directory
 *
 * @param directory the directory containing the database
 * @return 0 in case of success or a negative error code
 *
 * @see db_close
 */
extern
int
db_open(
	const char *directory
);

/**
 * close the database
 */
extern
void
db_close(
);

/**
 * Is the database empty?
 *
 * @return true if empty or else false
 */
extern
bool
db_is_empty(
);

/**
 * Enter atomic mode or cancelable mode
 *
 * @return 0 in case of success or a negative -errno like value
 *
 * @see db_transaction_end, db_drop, db_set
 */
extern
int
db_transaction_begin(
);

/**
 * Leave atomic mode commiting or not the changes
 *
 * @param commit if true changes are commited otherwise, if false cancel them
 * @return 0 in case of success or a negative -errno like value
 *
 * @see db_transaction_begin, db_drop, db_set
 */
extern
int
db_transaction_end(
	bool commit
);

/**
 * Erase rules matching the key
 *
 * @param key the search key for the rules to remove
 * @return 0 in case of success or a negative -errno like value
 *
 * @see db_transaction_begin, db_transaction_end, db_set
 */
extern
int
db_drop(
	const data_key_t *key
);

/**
 * Add the rule of key and value
 *
 * @param key the key of the rule to add
 * @param value the value of the rule
 * @return 0 in case of success or a negative -errno like value
 *
 * @see db_transaction_begin, db_transaction_end, db_drop
 */
extern
int
db_set(
	const data_key_t *key,
	const data_value_t *value
);

/**
 * Iterate over rules matching the key: call the callback for each found item
 *
 * @param callback the callback function to be call for each rule matching key
 * @param closure the closure of the callback
 * @param key the searching key
 */
extern
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
);

/**
 * Get the rule value for the key
 *
 * @param key The key to query
 * @param value Where to store the result if any
 * @return 0 if no rule matched (value unchanged then) or a positive integer
 *  when a value was found for the key
 */
extern
unsigned
db_test(
	const data_key_t *key,
	data_value_t *value
);

/**
 * Cleanup the database by removing expired items
 *
 * @return 0 in case of success or a negative -errno like value
 */
extern
int
db_cleanup(
);

/**
 * Write the database to the file system (synchrnize it)
 *
 * @return 0 in case of success or a negative -errno like value
 */
extern
int
db_sync(
);