/[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.23 - (show annotations)
Thu Nov 20 03:22:35 2025 UTC (3 months, 3 weeks ago) by sysadm
Branch: MAIN
Changes since 1.22: +3 -5 lines
Content type: text/x-csrc
Refine

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_NAME_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 void trie_dict_cleanup(void)
121 {
122 if (p_trie_node_pool == NULL)
123 {
124 return;
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
137 int get_trie_dict_shm_readonly(void)
138 {
139 int fd;
140 struct stat sb;
141 size_t size;
142 void *p_shm;
143
144 if ((fd = shm_open(trie_node_shm_name, O_RDONLY, 0600)) == -1)
145 {
146 log_error("shm_open(%s) error (%d)\n", trie_node_shm_name, errno);
147 return -2;
148 }
149
150 if (fstat(fd, &sb) < 0)
151 {
152 log_error("fstat(fd) error (%d)\n", errno);
153 close(fd);
154 return -2;
155 }
156
157 size = (size_t)sb.st_size;
158
159 p_shm = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0L);
160 if (p_shm == MAP_FAILED)
161 {
162 log_error("mmap() error (%d)\n", errno);
163 close(fd);
164 return -2;
165 }
166
167 if (close(fd) < 0)
168 {
169 log_error("close(fd) error (%d)\n", errno);
170 return -1;
171 }
172
173 if (p_trie_node_pool->shm_size != size)
174 {
175 log_error("Shared memory size mismatch (%ld != %ld)\n", p_trie_node_pool->shm_size, size);
176 munmap(p_shm, size);
177 return -3;
178 }
179
180 p_trie_node_pool = p_shm;
181
182 return 0;
183 }
184
185 int set_trie_dict_shm_readonly(void)
186 {
187 if (p_trie_node_pool != NULL && mprotect(p_trie_node_pool, p_trie_node_pool->shm_size, PROT_READ) < 0)
188 {
189 log_error("mprotect() error (%d)\n", errno);
190 return -1;
191 }
192
193 return 0;
194 }
195
196 int detach_trie_dict_shm(void)
197 {
198 if (p_trie_node_pool != NULL && munmap(p_trie_node_pool, p_trie_node_pool->shm_size) < 0)
199 {
200 log_error("munmap() error (%d)\n", errno);
201 return -1;
202 }
203
204 p_trie_node_pool = NULL;
205
206 return 0;
207 }
208
209 TRIE_NODE *trie_dict_create(void)
210 {
211 TRIE_NODE *p_dict = NULL;
212
213 if (p_trie_node_pool != NULL && p_trie_node_pool->p_node_free_list != NULL)
214 {
215 p_dict = p_trie_node_pool->p_node_free_list;
216 p_trie_node_pool->p_node_free_list = p_dict->p_nodes[0];
217
218 memset(p_dict, 0, sizeof(*p_dict));
219
220 p_trie_node_pool->node_count++;
221 }
222 else if (p_trie_node_pool != NULL)
223 {
224 log_error("trie_dict_create() error: node depleted %d >= %d\n", p_trie_node_pool->node_count, p_trie_node_pool->node_count_limit);
225 }
226
227 return p_dict;
228 }
229
230 void trie_dict_destroy(TRIE_NODE *p_dict)
231 {
232 if (p_trie_node_pool == NULL || p_dict == NULL)
233 {
234 return;
235 }
236
237 for (int i = 0; i < TRIE_CHILDREN; i++)
238 {
239 if (p_dict->p_nodes[i] != NULL)
240 {
241 trie_dict_destroy(p_dict->p_nodes[i]);
242 }
243 }
244
245 memset(p_dict, 0, sizeof(*p_dict));
246
247 p_dict->p_nodes[0] = p_trie_node_pool->p_node_free_list;
248 p_trie_node_pool->p_node_free_list = p_dict;
249
250 p_trie_node_pool->node_count--;
251 }
252
253 int trie_dict_set(TRIE_NODE *p_dict, const char *key, int64_t value)
254 {
255 int offset;
256
257 if (p_dict == NULL)
258 {
259 return -1;
260 }
261
262 while (key != NULL && *key != '\0')
263 {
264 offset = (256 + *key) % 256;
265 if (offset < 0 || offset >= TRIE_CHILDREN) // incorrect key character
266 {
267 return -1;
268 }
269
270 if (*(key + 1) == '\0')
271 {
272 if (p_dict->flags[offset] == 0 || p_dict->values[offset] != value)
273 {
274 p_dict->values[offset] = value;
275 p_dict->flags[offset] = 1;
276 return 1; // Set to new value
277 }
278 return 0; // Unchanged
279 }
280 else
281 {
282 if (p_dict->p_nodes[offset] == NULL)
283 {
284 if ((p_dict->p_nodes[offset] = trie_dict_create()) == NULL)
285 {
286 return -2; // OOM
287 }
288 }
289 p_dict = p_dict->p_nodes[offset];
290 key++;
291 }
292 }
293
294 return -1; // NULL key
295 }
296
297 int trie_dict_get(TRIE_NODE *p_dict, const char *key, int64_t *p_value)
298 {
299 int offset;
300
301 if (p_dict == NULL)
302 {
303 return -1;
304 }
305
306 while (key != NULL && *key != '\0')
307 {
308 offset = (256 + *key) % 256;
309 if (offset < 0 || offset >= TRIE_CHILDREN) // incorrect key character
310 {
311 return -1;
312 }
313
314 if (*(key + 1) == '\0')
315 {
316 if (p_dict->flags[offset] != 0)
317 {
318 *p_value = p_dict->values[offset];
319 return 1; // Found
320 }
321 else
322 {
323 return 0; // Not exist
324 }
325 }
326 else if (p_dict->p_nodes[offset] == NULL)
327 {
328 return 0; // Not exist
329 }
330 else
331 {
332 p_dict = p_dict->p_nodes[offset];
333 key++;
334 }
335 }
336
337 return -1; // NULL key
338 }
339
340 int trie_dict_del(TRIE_NODE *p_dict, const char *key)
341 {
342 int offset;
343
344 if (p_dict == NULL)
345 {
346 return -1;
347 }
348
349 while (key != NULL && *key != '\0')
350 {
351 offset = (256 + *key) % 256;
352 if (offset < 0 || offset >= TRIE_CHILDREN) // incorrect key character
353 {
354 return -1;
355 }
356
357 if (*(key + 1) == '\0')
358 {
359 if (p_dict->flags[offset] != 0)
360 {
361 p_dict->flags[offset] = 0;
362 p_dict->values[offset] = 0;
363 return 1; // Done
364 }
365 else
366 {
367 return 0; // Not exist
368 }
369 }
370 else if (p_dict->p_nodes[offset] == NULL)
371 {
372 return 0; // Not exist
373 }
374 else
375 {
376 p_dict = p_dict->p_nodes[offset];
377 key++;
378 }
379 }
380
381 return -1; // NULL key
382 }
383
384 static void _trie_dict_traverse(TRIE_NODE *p_dict, trie_dict_traverse_cb cb, char *key, int depth)
385 {
386 if (p_dict == NULL || depth >= TRIE_MAX_KEY_LEN)
387 {
388 return;
389 }
390
391 for (int i = 0; i < TRIE_CHILDREN; i++)
392 {
393 if (p_dict->flags[i] != 0)
394 {
395 key[depth] = (char)i;
396 key[depth + 1] = '\0';
397 (*cb)(key, p_dict->values[i]);
398 }
399
400 if (p_dict->p_nodes[i] != NULL && depth + 1 < TRIE_MAX_KEY_LEN)
401 {
402 key[depth] = (char)i;
403 _trie_dict_traverse(p_dict->p_nodes[i], cb, key, depth + 1);
404 }
405 }
406 }
407
408 void trie_dict_traverse(TRIE_NODE *p_dict, trie_dict_traverse_cb cb)
409 {
410 char key[TRIE_MAX_KEY_LEN + 1];
411
412 if (p_dict == NULL)
413 {
414 return;
415 }
416
417 _trie_dict_traverse(p_dict, cb, key, 0);
418 }
419
420 int trie_dict_used_nodes(void)
421 {
422 return (p_trie_node_pool == NULL ? -1 : p_trie_node_pool->node_count);
423 }

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