-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.c
264 lines (245 loc) · 6.85 KB
/
index.c
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ldns/ldns.h>
#include "uthash.h"
#include "proto.h"
#pragma GCC optimize ("O0")
typedef int (*comparefunction)(const void *, const void *);
typedef int (*acceptfunction)(dictionary newitem, dictionary currentitem, int* cmp);
struct names_index_struct {
const char* keyname;
ldns_rbtree_t* tree;
acceptfunction acceptfunc;
};
struct names_iterator_struct {
int (*iterate)(names_iterator*iter, void**);
int (*advance)(names_iterator*iter, void**);
int (*end)(names_iterator*iter);
ldns_rbnode_t* current;
};
int
names_indexcreate(names_index_type* index, const char* keyname)
{
comparefunction comparfunc;
acceptfunction acceptfunc;
*index = malloc(sizeof(struct names_index_struct));
names_recordindexfunction(keyname, &acceptfunc, &comparfunc);
assert(acceptfunc);
assert(comparfunc);
(*index)->keyname = strdup(keyname);
(*index)->acceptfunc = acceptfunc;
(*index)->tree = ldns_rbtree_create(comparfunc);
return 0;
}
struct destroyinfo {
void (*free)(void* arg, void* key, void* val);
void* arg;
};
static void
disposenode(ldns_rbnode_t* node, void* cargo)
{
struct destroyinfo* user = cargo;
if(user && user->free) {
user->free(user->arg, (void*)node->key, (void*)node->data);
}
free(node);
}
void
names_indexdestroy(names_index_type index, void (*userfunc)(void* arg, void* key, void* val), void* userarg)
{
struct destroyinfo cargo;
cargo.free = userfunc;
cargo.arg = userarg;
ldns_traverse_postorder(index->tree, disposenode, (userfunc?&cargo:NULL));
ldns_rbtree_free(index->tree);
free((void*)index->keyname);
free(index);
}
int
names_indexaccept(names_index_type index, dictionary record)
{
int cmp;
if(index->acceptfunc) {
return index->acceptfunc(record, NULL, &cmp);
} else
return 1;
}
int
names_indexinsert(names_index_type index, dictionary d)
{
int cmp;
ldns_rbnode_t* node;
if(index->acceptfunc(d, NULL, NULL)) {
node = malloc(sizeof(ldns_rbnode_t));
assert(d);
node->key = d;
node->data = d;
if(!ldns_rbtree_insert(index->tree, node)) {
free(node);
node = ldns_rbtree_search(index->tree, d);
assert(node);
switch(index->acceptfunc(d, (dictionary)node->data, &cmp)) {
case 0:
return 0;
case 1:
if(names_recordhasmarker(d)) {
ldns_rbtree_delete(index->tree, node->key);
} else {
node->key = d;
node->data = d;
}
return 1;
case 2:
ldns_rbtree_delete(index->tree, node->key);
return 0;
default:
abort(); // FIXME
}
} else {
return 1;
}
} else {
node = ldns_rbtree_search(index->tree, d);
if(node != NULL) {
if(names_recordhasmarker(d) ||
index->acceptfunc(d, (dictionary)node->data, &cmp) == 0 ||
(cmp == 0 && node->key == d)) {
ldns_rbtree_delete(index->tree, node->key);
}
}
return 0;
}
}
dictionary
names_indexlookupkey(names_index_type index, const char* keyvalue)
{
dictionary find;
dictionary found;
find = names_recordcreate(NULL);
getset(find,index->keyname,NULL,&keyvalue); // FIXME realisticly we only lookup on name
found = names_indexlookup(index, find);
dispose(find);
return found;
}
dictionary
names_indexlookup(names_index_type index, dictionary find)
{
ldns_rbnode_t* node;
node = ldns_rbtree_search(index->tree, find);
return node ? (dictionary) node->data : NULL;
}
int
names_indexremove(names_index_type index, dictionary d)
{
const char *value;
if(getset(d, index->keyname, &value, NULL)) {
return names_indexremovekey(index, value);
} else
return 0;
}
int
names_indexremovekey(names_index_type index, const char* keyvalue)
{
dictionary find;
ldns_rbnode_t* node;
find = names_recordcreate(NULL);
getset(find, index->keyname, NULL, &keyvalue);
node = ldns_rbtree_delete(index->tree, find);
dispose(find);
if(node) {
free(node);
return 1;
} else
return 0;
}
static int
iterateimpl(names_iterator* i, void** item)
{
struct names_iterator_struct** iter = i;
if (item)
*item = NULL;
if (*iter) {
if ((*iter)->current != NULL && (*iter)->current != LDNS_RBTREE_NULL) {
if (item)
*item = (void*) (*iter)->current->data;
return 1;
} else {
free(*iter);
*iter = NULL;
}
}
return 0;
}
static int
advanceimpl(names_iterator*i, void** item)
{
struct names_iterator_struct** iter = i;
if (item)
*item = NULL;
if (*iter) {
if((*iter)->current != NULL && (*iter)->current != LDNS_RBTREE_NULL) {
(*iter)->current = ldns_rbtree_next((*iter)->current);
if((*iter)->current != NULL && (*iter)->current != LDNS_RBTREE_NULL) {
if(item)
*item = (void*) (*iter)->current->data;
return 1;
}
}
free(*iter);
*iter = NULL;
}
return 0;
}
static int
endimpl(names_iterator*iter)
{
if(*iter) free(*iter);
*iter = NULL;
return 0;
}
names_iterator
names_indexiterator(names_index_type index)
{
names_iterator iter;
iter = malloc(sizeof(struct names_iterator_struct));
iter->iterate = iterateimpl;
iter->advance = advanceimpl;
iter->end = endimpl;
iter->current = (index->tree != NULL ? ldns_rbtree_first(index->tree) : NULL);
return iter;
}
names_iterator
names_indexrange(names_index_type index, const char* selection, ...)
{
va_list ap;
const char* find;
const char* found;
int findlen;
dictionary record;
ldns_rbnode_t* node;
names_iterator iter;
va_start(ap, selection);
iter = names_iterator_create(0);
if (index->tree) {
find = va_arg(ap, char*);
findlen = strlen(find);
record = names_recordcreate(NULL);
getset(record, "name", NULL, &find);
(void)ldns_rbtree_find_less_equal(index->tree, record, &node);
names_recorddestroy(record);
while(node && node != LDNS_RBTREE_NULL) {
record = (dictionary)node->key;
getset(record,"name",&found,NULL);
if(!strncmp(find,found,findlen) && (found[findlen-1]=='\0' || found[findlen-1]=='.')) {
names_iterator_add(iter, record);
} else {
break;
}
node = ldns_rbtree_previous(node);
}
}
va_end(ap);
return iter;
}