/[LeafOK_CVS]/lbbs/src/trie_dict.c
ViewVC logotype

Contents of /lbbs/src/trie_dict.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.22 - (show annotations)
Thu Nov 20 02:56:46 2025 UTC (3 months, 3 weeks ago) by sysadm
Branch: MAIN
Changes since 1.21: +0 -1 lines
Content type: text/x-csrc
Update

1 /* SPDX-License-Identifier: GPL-3.0-or-later */
2 /*
3 * trie_dict
4 * - trie-tree based dict feature
5 *
6 * Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com>
7 */
8
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif
12
13 #include "common.h"
14 #include "log.h"
15 #include "trie_dict.h"
16 #include <errno.h>
17 #include <fcntl.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <time.h>
22 #include <unistd.h>
23 #include <sys/mman.h>
24 #include <sys/stat.h>
25
26 struct trie_node_pool_t
27 {
28 size_t shm_size;
29 int node_count_limit;
30 int node_count;
31 TRIE_NODE *p_node_free_list;
32 };
33 typedef struct trie_node_pool_t TRIE_NODE_POOL;
34
35 static char trie_node_shm_name[FILE_PATH_LEN];
36 static TRIE_NODE_POOL *p_trie_node_pool;
37
38 int trie_dict_init(const char *filename, int node_count_limit)
39 {
40 char filepath[FILE_PATH_LEN];
41 int fd;
42 size_t size;
43 void *p_shm;
44 TRIE_NODE *p_trie_nodes;
45 int i;
46
47 if (filename == NULL)
48 {
49 log_error("NULL pointer error\n");
50 return -1;
51 }
52
53 if (p_trie_node_pool != NULL)
54 {
55 log_error("trie_dict_pool already initialized\n");
56 return -1;
57 }
58
59 if (node_count_limit <= 0 || node_count_limit > TRIE_NODE_PER_POOL)
60 {
61 log_error("trie_dict_init(%d) error: invalid node_count_limit\n", node_count_limit);
62 return -1;
63 }
64
65 // Allocate shared memory
66 size = sizeof(TRIE_NODE_POOL) + sizeof(TRIE_NODE) * (size_t)node_count_limit;
67
68 strncpy(filepath, filename, sizeof(filepath) - 1);
69 filepath[sizeof(filepath) - 1] = '\0';
70 snprintf(trie_node_shm_name, sizeof(trie_node_shm_name), "/TRIE_DICT_SHM_%s", basename(filepath));
71
72 if (shm_unlink(trie_node_shm_name) == -1 && errno != ENOENT)
73 {
74 log_error("shm_unlink(%s) error (%d)\n", trie_node_shm_name, errno);
75 return -2;
76 }
77
78 if ((fd = shm_open(trie_node_shm_name, O_CREAT | O_EXCL | O_RDWR, 0600)) == -1)
79 {
80 log_error("shm_open(%s) error (%d)\n", trie_node_shm_name, errno);
81 return -2;
82 }
83 if (ftruncate(fd, (off_t)size) == -1)
84 {
85 log_error("ftruncate(size=%d) error (%d)\n", size, errno);
86 close(fd);
87 return -2;
88 }
89
90 p_shm = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0L);
91 if (p_shm == MAP_FAILED)
92 {
93 log_error("mmap() error (%d)\n", errno);
94 close(fd);
95 return -2;
96 }
97
98 if (close(fd) < 0)
99 {
100 log_error("close(fd) error (%d)\n", errno);
101 return -1;
102 }
103
104 p_trie_node_pool = p_shm;
105 p_trie_node_pool->shm_size = size;
106 p_trie_node_pool->node_count_limit = node_count_limit;
107 p_trie_node_pool->node_count = 0;
108
109 p_trie_nodes = (TRIE_NODE *)((char *)p_shm + sizeof(TRIE_NODE_POOL));
110 p_trie_node_pool->p_node_free_list = &(p_trie_nodes[0]);
111 for (i = 0; i < node_count_limit - 1; i++)
112 {
113 p_trie_nodes[i].p_nodes[0] = &(p_trie_nodes[i + 1]);
114 }
115 p_trie_nodes[node_count_limit - 1].p_nodes[0] = NULL;
116
117 return 0;
118 }
119
120 int trie_dict_cleanup(void)
121 {
122 if (p_trie_node_pool == NULL)
123 {
124 return -1;
125 }
126
127 detach_trie_dict_shm();
128
129 if (shm_unlink(trie_node_shm_name) == -1 && errno != ENOENT)
130 {
131 log_error("shm_unlink(%s) error (%d)\n", trie_node_shm_name, errno);
132 }
133
134 trie_node_shm_name[0] = '\0';
135
136 return 0;
137 }
138
139 int get_trie_dict_shm_readonly(void)
140 {
141 int fd;
142 struct stat sb;
143 size_t size;
144 void *p_shm;
145
146 if ((fd = shm_open(trie_node_shm_name, O_RDONLY, 0600)) == -1)
147 {
148 log_error("shm_open(%s) error (%d)\n", trie_node_shm_name, errno);
149 return -2;
150 }
151
152 if (fstat(fd, &sb) < 0)
153 {
154 log_error("fstat(fd) error (%d)\n", errno);
155 close(fd);
156 return -2;
157 }
158
159 size = (size_t)sb.st_size;
160
161 p_shm = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0L);
162 if (p_shm == MAP_FAILED)
163 {
164 log_error("mmap() error (%d)\n", errno);
165 close(fd);
166 return -2;
167 }
168
169 if (close(fd) < 0)
170 {
171 log_error("close(fd) error (%d)\n", errno);
172 return -1;
173 }
174
175 if (p_trie_node_pool->shm_size != size)
176 {
177 log_error("Shared memory size mismatch (%ld != %ld)\n", p_trie_node_pool->shm_size, size);
178 munmap(p_shm, size);
179 return -3;
180 }
181
182 p_trie_node_pool = p_shm;
183
184 return 0;
185 }
186
187 int set_trie_dict_shm_readonly(void)
188 {
189 if (p_trie_node_pool != NULL && mprotect(p_trie_node_pool, p_trie_node_pool->shm_size, PROT_READ) < 0)
190 {
191 log_error("mprotect() error (%d)\n", errno);
192 return -1;
193 }
194
195 return 0;
196 }
197
198 int detach_trie_dict_shm(void)
199 {
200 if (p_trie_node_pool != NULL && munmap(p_trie_node_pool, p_trie_node_pool->shm_size) < 0)
201 {
202 log_error("munmap() error (%d)\n", errno);
203 return -1;
204 }
205
206 p_trie_node_pool = NULL;
207
208 return 0;
209 }
210
211 TRIE_NODE *trie_dict_create(void)
212 {
213 TRIE_NODE *p_dict = NULL;
214
215 if (p_trie_node_pool != NULL && p_trie_node_pool->p_node_free_list != NULL)
216 {
217 p_dict = p_trie_node_pool->p_node_free_list;
218 p_trie_node_pool->p_node_free_list = p_dict->p_nodes[0];
219
220 memset(p_dict, 0, sizeof(*p_dict));
221
222 p_trie_node_pool->node_count++;
223 }
224 else if (p_trie_node_pool != NULL)
225 {
226 log_error("trie_dict_create() error: node depleted %d >= %d\n", p_trie_node_pool->node_count, p_trie_node_pool->node_count_limit);
227 }
228
229 return p_dict;
230 }
231
232 void trie_dict_destroy(TRIE_NODE *p_dict)
233 {
234 if (p_trie_node_pool == NULL || p_dict == NULL)
235 {
236 return;
237 }
238
239 for (int i = 0; i < TRIE_CHILDREN; i++)
240 {
241 if (p_dict->p_nodes[i] != NULL)
242 {
243 trie_dict_destroy(p_dict->p_nodes[i]);
244 }
245 }
246
247 memset(p_dict, 0, sizeof(*p_dict));
248
249 p_dict->p_nodes[0] = p_trie_node_pool->p_node_free_list;
250 p_trie_node_pool->p_node_free_list = p_dict;
251
252 p_trie_node_pool->node_count--;
253 }
254
255 int trie_dict_set(TRIE_NODE *p_dict, const char *key, int64_t value)
256 {
257 int offset;
258
259 if (p_dict == NULL)
260 {
261 return -1;
262 }
263
264 while (key != NULL && *key != '\0')
265 {
266 offset = (256 + *key) % 256;
267 if (offset < 0 || offset >= TRIE_CHILDREN) // incorrect key character
268 {
269 return -1;
270 }
271
272 if (*(key + 1) == '\0')
273 {
274 if (p_dict->flags[offset] == 0 || p_dict->values[offset] != value)
275 {
276 p_dict->values[offset] = value;
277 p_dict->flags[offset] = 1;
278 return 1; // Set to new value
279 }
280 return 0; // Unchanged
281 }
282 else
283 {
284 if (p_dict->p_nodes[offset] == NULL)
285 {
286 if ((p_dict->p_nodes[offset] = trie_dict_create()) == NULL)
287 {
288 return -2; // OOM
289 }
290 }
291 p_dict = p_dict->p_nodes[offset];
292 key++;
293 }
294 }
295
296 return -1; // NULL key
297 }
298
299 int trie_dict_get(TRIE_NODE *p_dict, const char *key, int64_t *p_value)
300 {
301 int offset;
302
303 if (p_dict == NULL)
304 {
305 return -1;
306 }
307
308 while (key != NULL && *key != '\0')
309 {
310 offset = (256 + *key) % 256;
311 if (offset < 0 || offset >= TRIE_CHILDREN) // incorrect key character
312 {
313 return -1;
314 }
315
316 if (*(key + 1) == '\0')
317 {
318 if (p_dict->flags[offset] != 0)
319 {
320 *p_value = p_dict->values[offset];
321 return 1; // Found
322 }
323 else
324 {
325 return 0; // Not exist
326 }
327 }
328 else if (p_dict->p_nodes[offset] == NULL)
329 {
330 return 0; // Not exist
331 }
332 else
333 {
334 p_dict = p_dict->p_nodes[offset];
335 key++;
336 }
337 }
338
339 return -1; // NULL key
340 }
341
342 int trie_dict_del(TRIE_NODE *p_dict, const char *key)
343 {
344 int offset;
345
346 if (p_dict == NULL)
347 {
348 return -1;
349 }
350
351 while (key != NULL && *key != '\0')
352 {
353 offset = (256 + *key) % 256;
354 if (offset < 0 || offset >= TRIE_CHILDREN) // incorrect key character
355 {
356 return -1;
357 }
358
359 if (*(key + 1) == '\0')
360 {
361 if (p_dict->flags[offset] != 0)
362 {
363 p_dict->flags[offset] = 0;
364 p_dict->values[offset] = 0;
365 return 1; // Done
366 }
367 else
368 {
369 return 0; // Not exist
370 }
371 }
372 else if (p_dict->p_nodes[offset] == NULL)
373 {
374 return 0; // Not exist
375 }
376 else
377 {
378 p_dict = p_dict->p_nodes[offset];
379 key++;
380 }
381 }
382
383 return -1; // NULL key
384 }
385
386 static void _trie_dict_traverse(TRIE_NODE *p_dict, trie_dict_traverse_cb cb, char *key, int depth)
387 {
388 if (p_dict == NULL || depth >= TRIE_MAX_KEY_LEN)
389 {
390 return;
391 }
392
393 for (int i = 0; i < TRIE_CHILDREN; i++)
394 {
395 if (p_dict->flags[i] != 0)
396 {
397 key[depth] = (char)i;
398 key[depth + 1] = '\0';
399 (*cb)(key, p_dict->values[i]);
400 }
401
402 if (p_dict->p_nodes[i] != NULL && depth + 1 < TRIE_MAX_KEY_LEN)
403 {
404 key[depth] = (char)i;
405 _trie_dict_traverse(p_dict->p_nodes[i], cb, key, depth + 1);
406 }
407 }
408 }
409
410 void trie_dict_traverse(TRIE_NODE *p_dict, trie_dict_traverse_cb cb)
411 {
412 char key[TRIE_MAX_KEY_LEN + 1];
413
414 if (p_dict == NULL)
415 {
416 return;
417 }
418
419 _trie_dict_traverse(p_dict, cb, key, 0);
420 }
421
422 int trie_dict_used_nodes(void)
423 {
424 return (p_trie_node_pool == NULL ? -1 : p_trie_node_pool->node_count);
425 }

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1