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

Annotation of /lbbs/src/trie_dict.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.16 - (hide 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 sysadm 1.13 /* SPDX-License-Identifier: GPL-3.0-or-later */
2     /*
3     * trie_dict
4     * - trie-tree based dict feature
5     *
6 sysadm 1.15 * Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com>
7 sysadm 1.13 */
8 sysadm 1.1
9 sysadm 1.16 #ifdef HAVE_CONFIG_H
10     #include "config.h"
11     #endif
12    
13 sysadm 1.11 #include "log.h"
14 sysadm 1.1 #include "trie_dict.h"
15 sysadm 1.7 #include <errno.h>
16 sysadm 1.10 #include <stdio.h>
17 sysadm 1.1 #include <stdlib.h>
18 sysadm 1.10 #include <string.h>
19 sysadm 1.7 #include <time.h>
20     #include <unistd.h>
21 sysadm 1.11 #include <sys/ipc.h>
22 sysadm 1.7 #include <sys/shm.h>
23    
24     struct trie_node_pool_t
25     {
26     int shmid;
27 sysadm 1.8 int node_count_limit;
28     int node_count;
29 sysadm 1.7 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 sysadm 1.8 int trie_dict_init(const char *filename, int node_count_limit)
36 sysadm 1.7 {
37     int shmid;
38     int proj_id;
39     key_t key;
40     size_t size;
41     void *p_shm;
42 sysadm 1.8 TRIE_NODE *p_trie_nodes;
43 sysadm 1.7 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 sysadm 1.8 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 sysadm 1.7 // 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 sysadm 1.8 size = sizeof(TRIE_NODE_POOL) + sizeof(TRIE_NODE) * (size_t)node_count_limit;
67 sysadm 1.7 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 sysadm 1.8 p_trie_node_pool->node_count_limit = node_count_limit;
83 sysadm 1.7 p_trie_node_pool->node_count = 0;
84    
85 sysadm 1.12 p_trie_nodes = (TRIE_NODE *)((char *)p_shm + sizeof(TRIE_NODE_POOL));
86 sysadm 1.8 p_trie_node_pool->p_node_free_list = &(p_trie_nodes[0]);
87     for (i = 0; i < node_count_limit - 1; i++)
88 sysadm 1.7 {
89 sysadm 1.8 p_trie_nodes[i].p_nodes[0] = &(p_trie_nodes[i + 1]);
90 sysadm 1.7 }
91 sysadm 1.8 p_trie_nodes[node_count_limit - 1].p_nodes[0] = NULL;
92 sysadm 1.7
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 sysadm 1.14 if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1)
110 sysadm 1.7 {
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 sysadm 1.9 log_error("shmat(trie_node_pool shmid=%d) error (%d)\n", shmid, errno);
133 sysadm 1.7 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 sysadm 1.9 log_error("shmdt(trie_node_pool) error (%d)\n", errno);
146 sysadm 1.7 return -1;
147     }
148 sysadm 1.8
149 sysadm 1.7 p_trie_node_pool = NULL;
150    
151     return 0;
152     }
153 sysadm 1.1
154     TRIE_NODE *trie_dict_create(void)
155     {
156 sysadm 1.7 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 sysadm 1.1
163 sysadm 1.10 memset(p_dict, 0, sizeof(*p_dict));
164 sysadm 1.8
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 sysadm 1.7 }
171 sysadm 1.1
172     return p_dict;
173     }
174    
175     void trie_dict_destroy(TRIE_NODE *p_dict)
176     {
177 sysadm 1.7 if (p_trie_node_pool == NULL || p_dict == NULL)
178 sysadm 1.1 {
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 sysadm 1.7 }
189 sysadm 1.3
190 sysadm 1.10 memset(p_dict, 0, sizeof(*p_dict));
191 sysadm 1.1
192 sysadm 1.7 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 sysadm 1.8
195     p_trie_node_pool->node_count--;
196 sysadm 1.1 }
197    
198     int trie_dict_set(TRIE_NODE *p_dict, const char *key, int64_t value)
199     {
200     int offset;
201    
202 sysadm 1.5 if (p_dict == NULL)
203     {
204     return -1;
205     }
206    
207 sysadm 1.1 while (key != NULL && *key != '\0')
208     {
209 sysadm 1.6 offset = (256 + *key) % 256;
210 sysadm 1.3 if (offset < 0 || offset >= TRIE_CHILDREN) // incorrect key character
211     {
212     return -1;
213     }
214 sysadm 1.1
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 sysadm 1.5 if (p_dict == NULL)
247     {
248     return -1;
249     }
250    
251 sysadm 1.1 while (key != NULL && *key != '\0')
252     {
253 sysadm 1.6 offset = (256 + *key) % 256;
254 sysadm 1.3 if (offset < 0 || offset >= TRIE_CHILDREN) // incorrect key character
255     {
256     return -1;
257     }
258 sysadm 1.1
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 sysadm 1.5 if (p_dict == NULL)
290     {
291     return -1;
292     }
293    
294 sysadm 1.1 while (key != NULL && *key != '\0')
295     {
296 sysadm 1.6 offset = (256 + *key) % 256;
297 sysadm 1.3 if (offset < 0 || offset >= TRIE_CHILDREN) // incorrect key character
298     {
299     return -1;
300     }
301 sysadm 1.1
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 sysadm 1.2
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 sysadm 1.5 key[depth] = (char)i;
341 sysadm 1.3 key[depth + 1] = '\0';
342 sysadm 1.2 (*cb)(key, p_dict->values[i]);
343     }
344    
345     if (p_dict->p_nodes[i] != NULL && depth + 1 < TRIE_MAX_KEY_LEN)
346     {
347 sysadm 1.5 key[depth] = (char)i;
348 sysadm 1.2 _trie_dict_traverse(p_dict->p_nodes[i], cb, key, depth + 1);
349     }
350 sysadm 1.5 }
351 sysadm 1.2 }
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 sysadm 1.5
357     if (p_dict == NULL)
358     {
359     return;
360     }
361 sysadm 1.2
362     _trie_dict_traverse(p_dict, cb, key, 0);
363     }
364 sysadm 1.8
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