/[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.16 - (show annotations)
Tue Nov 11 00:28:05 2025 UTC (4 months ago) by sysadm
Branch: MAIN
Changes since 1.15: +4 -0 lines
Content type: text/x-csrc
Use config.h

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 "log.h"
14 #include "trie_dict.h"
15 #include <errno.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <time.h>
20 #include <unistd.h>
21 #include <sys/ipc.h>
22 #include <sys/shm.h>
23
24 struct trie_node_pool_t
25 {
26 int shmid;
27 int node_count_limit;
28 int node_count;
29 TRIE_NODE *p_node_free_list;
30 };
31 typedef struct trie_node_pool_t TRIE_NODE_POOL;
32
33 static TRIE_NODE_POOL *p_trie_node_pool;
34
35 int trie_dict_init(const char *filename, int node_count_limit)
36 {
37 int shmid;
38 int proj_id;
39 key_t key;
40 size_t size;
41 void *p_shm;
42 TRIE_NODE *p_trie_nodes;
43 int i;
44
45 if (p_trie_node_pool != NULL)
46 {
47 log_error("trie_dict_pool already initialized\n");
48 return -1;
49 }
50
51 if (node_count_limit <= 0 || node_count_limit > TRIE_NODE_PER_POOL)
52 {
53 log_error("trie_dict_init(%d) error: invalid node_count_limit\n", node_count_limit);
54 return -1;
55 }
56
57 // Allocate shared memory
58 proj_id = (int)(time(NULL) % getpid());
59 key = ftok(filename, proj_id);
60 if (key == -1)
61 {
62 log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno);
63 return -2;
64 }
65
66 size = sizeof(TRIE_NODE_POOL) + sizeof(TRIE_NODE) * (size_t)node_count_limit;
67 shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
68 if (shmid == -1)
69 {
70 log_error("shmget(size = %d) error (%d)\n", size, errno);
71 return -3;
72 }
73 p_shm = shmat(shmid, NULL, 0);
74 if (p_shm == (void *)-1)
75 {
76 log_error("shmat(shmid = %d) error (%d)\n", shmid, errno);
77 return -3;
78 }
79
80 p_trie_node_pool = p_shm;
81 p_trie_node_pool->shmid = shmid;
82 p_trie_node_pool->node_count_limit = node_count_limit;
83 p_trie_node_pool->node_count = 0;
84
85 p_trie_nodes = (TRIE_NODE *)((char *)p_shm + sizeof(TRIE_NODE_POOL));
86 p_trie_node_pool->p_node_free_list = &(p_trie_nodes[0]);
87 for (i = 0; i < node_count_limit - 1; i++)
88 {
89 p_trie_nodes[i].p_nodes[0] = &(p_trie_nodes[i + 1]);
90 }
91 p_trie_nodes[node_count_limit - 1].p_nodes[0] = NULL;
92
93 return 0;
94 }
95
96 void trie_dict_cleanup(void)
97 {
98 int shmid;
99
100 if (p_trie_node_pool == NULL)
101 {
102 return;
103 }
104
105 shmid = p_trie_node_pool->shmid;
106
107 detach_trie_dict_shm();
108
109 if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1)
110 {
111 log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
112 }
113 }
114
115 int set_trie_dict_shm_readonly(void)
116 {
117 int shmid;
118 void *p_shm;
119
120 if (p_trie_node_pool == NULL)
121 {
122 log_error("trie_dict_pool not initialized\n");
123 return -1;
124 }
125
126 shmid = p_trie_node_pool->shmid;
127
128 // Remap shared memory in read-only mode
129 p_shm = shmat(shmid, p_trie_node_pool, SHM_RDONLY | SHM_REMAP);
130 if (p_shm == (void *)-1)
131 {
132 log_error("shmat(trie_node_pool shmid=%d) error (%d)\n", shmid, errno);
133 return -1;
134 }
135
136 p_trie_node_pool = p_shm;
137
138 return 0;
139 }
140
141 int detach_trie_dict_shm(void)
142 {
143 if (p_trie_node_pool != NULL && shmdt(p_trie_node_pool) == -1)
144 {
145 log_error("shmdt(trie_node_pool) error (%d)\n", errno);
146 return -1;
147 }
148
149 p_trie_node_pool = NULL;
150
151 return 0;
152 }
153
154 TRIE_NODE *trie_dict_create(void)
155 {
156 TRIE_NODE *p_dict = NULL;
157
158 if (p_trie_node_pool != NULL && p_trie_node_pool->p_node_free_list != NULL)
159 {
160 p_dict = p_trie_node_pool->p_node_free_list;
161 p_trie_node_pool->p_node_free_list = p_dict->p_nodes[0];
162
163 memset(p_dict, 0, sizeof(*p_dict));
164
165 p_trie_node_pool->node_count++;
166 }
167 else if (p_trie_node_pool != NULL)
168 {
169 log_error("trie_dict_create() error: node depleted %d >= %d\n", p_trie_node_pool->node_count, p_trie_node_pool->node_count_limit);
170 }
171
172 return p_dict;
173 }
174
175 void trie_dict_destroy(TRIE_NODE *p_dict)
176 {
177 if (p_trie_node_pool == NULL || p_dict == NULL)
178 {
179 return;
180 }
181
182 for (int i = 0; i < TRIE_CHILDREN; i++)
183 {
184 if (p_dict->p_nodes[i] != NULL)
185 {
186 trie_dict_destroy(p_dict->p_nodes[i]);
187 }
188 }
189
190 memset(p_dict, 0, sizeof(*p_dict));
191
192 p_dict->p_nodes[0] = p_trie_node_pool->p_node_free_list;
193 p_trie_node_pool->p_node_free_list = p_dict;
194
195 p_trie_node_pool->node_count--;
196 }
197
198 int trie_dict_set(TRIE_NODE *p_dict, const char *key, int64_t value)
199 {
200 int offset;
201
202 if (p_dict == NULL)
203 {
204 return -1;
205 }
206
207 while (key != NULL && *key != '\0')
208 {
209 offset = (256 + *key) % 256;
210 if (offset < 0 || offset >= TRIE_CHILDREN) // incorrect key character
211 {
212 return -1;
213 }
214
215 if (*(key + 1) == '\0')
216 {
217 if (p_dict->flags[offset] == 0 || p_dict->values[offset] != value)
218 {
219 p_dict->values[offset] = value;
220 p_dict->flags[offset] = 1;
221 return 1; // Set to new value
222 }
223 return 0; // Unchanged
224 }
225 else
226 {
227 if (p_dict->p_nodes[offset] == NULL)
228 {
229 if ((p_dict->p_nodes[offset] = trie_dict_create()) == NULL)
230 {
231 return -2; // OOM
232 }
233 }
234 p_dict = p_dict->p_nodes[offset];
235 key++;
236 }
237 }
238
239 return -1; // NULL key
240 }
241
242 int trie_dict_get(TRIE_NODE *p_dict, const char *key, int64_t *p_value)
243 {
244 int offset;
245
246 if (p_dict == NULL)
247 {
248 return -1;
249 }
250
251 while (key != NULL && *key != '\0')
252 {
253 offset = (256 + *key) % 256;
254 if (offset < 0 || offset >= TRIE_CHILDREN) // incorrect key character
255 {
256 return -1;
257 }
258
259 if (*(key + 1) == '\0')
260 {
261 if (p_dict->flags[offset] != 0)
262 {
263 *p_value = p_dict->values[offset];
264 return 1; // Found
265 }
266 else
267 {
268 return 0; // Not exist
269 }
270 }
271 else if (p_dict->p_nodes[offset] == NULL)
272 {
273 return 0; // Not exist
274 }
275 else
276 {
277 p_dict = p_dict->p_nodes[offset];
278 key++;
279 }
280 }
281
282 return -1; // NULL key
283 }
284
285 int trie_dict_del(TRIE_NODE *p_dict, const char *key)
286 {
287 int offset;
288
289 if (p_dict == NULL)
290 {
291 return -1;
292 }
293
294 while (key != NULL && *key != '\0')
295 {
296 offset = (256 + *key) % 256;
297 if (offset < 0 || offset >= TRIE_CHILDREN) // incorrect key character
298 {
299 return -1;
300 }
301
302 if (*(key + 1) == '\0')
303 {
304 if (p_dict->flags[offset] != 0)
305 {
306 p_dict->flags[offset] = 0;
307 p_dict->values[offset] = 0;
308 return 1; // Done
309 }
310 else
311 {
312 return 0; // Not exist
313 }
314 }
315 else if (p_dict->p_nodes[offset] == NULL)
316 {
317 return 0; // Not exist
318 }
319 else
320 {
321 p_dict = p_dict->p_nodes[offset];
322 key++;
323 }
324 }
325
326 return -1; // NULL key
327 }
328
329 static void _trie_dict_traverse(TRIE_NODE *p_dict, trie_dict_traverse_cb cb, char *key, int depth)
330 {
331 if (p_dict == NULL || depth >= TRIE_MAX_KEY_LEN)
332 {
333 return;
334 }
335
336 for (int i = 0; i < TRIE_CHILDREN; i++)
337 {
338 if (p_dict->flags[i] != 0)
339 {
340 key[depth] = (char)i;
341 key[depth + 1] = '\0';
342 (*cb)(key, p_dict->values[i]);
343 }
344
345 if (p_dict->p_nodes[i] != NULL && depth + 1 < TRIE_MAX_KEY_LEN)
346 {
347 key[depth] = (char)i;
348 _trie_dict_traverse(p_dict->p_nodes[i], cb, key, depth + 1);
349 }
350 }
351 }
352
353 void trie_dict_traverse(TRIE_NODE *p_dict, trie_dict_traverse_cb cb)
354 {
355 char key[TRIE_MAX_KEY_LEN + 1];
356
357 if (p_dict == NULL)
358 {
359 return;
360 }
361
362 _trie_dict_traverse(p_dict, cb, key, 0);
363 }
364
365 int trie_dict_used_nodes(void)
366 {
367 return (p_trie_node_pool == NULL ? -1 : p_trie_node_pool->node_count);
368 }

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