-
Notifications
You must be signed in to change notification settings - Fork 9
/
store.c
500 lines (429 loc) · 12.2 KB
/
store.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
// store.c
// hosts2zones
//
// Created by Dr. Rolf Jansen on 2014-08-01.
// Copyright (c) 2014 projectworld.net. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <syslog.h>
#include <sys/stat.h>
#include "binutils.h"
#include "store.h"
#pragma mark ••• Value Data householding •••
static inline void releaseValue(Value *value)
{
switch (-value->kind) // dynamic data, has to be released
{
case Simple:
case Data:
deallocate(VPR(value->p), false);
break;
case String:
deallocate(VPR(value->s), false);
break;
case Dictionary:
releaseTable((Node **)value->p);
break;
}
}
#pragma mark ••• AVL Tree •••
static int balanceNode(Node **node)
{
int change = 0;
Node *o = *node;
Node *p, *q;
if (o->B == -2)
{
if (p = o->L) // make the static analyzer happy
if (p->B == +1)
{
change = 1; // double left-right rotation
q = p->R; // left rotation
p->R = q->L;
q->L = p;
o->L = q->R; // right rotation
q->R = o;
o->B = +(q->B < 0);
p->B = -(q->B > 0);
q->B = 0;
*node = q;
}
else
{
change = p->B; // single right rotation
o->L = p->R;
p->R = o;
o->B = -(++p->B);
*node = p;
}
}
else if (o->B == +2)
{
if (q = o->R) // make the static analyzer happy
if (q->B == -1)
{
change = 1; // double right-left rotation
p = q->L; // right rotation
q->L = p->R;
p->R = q;
o->R = p->L; // left rotation
p->L = o;
o->B = -(p->B > 0);
q->B = +(p->B < 0);
p->B = 0;
*node = p;
}
else
{
change = q->B; // single left rotation
o->R = q->L;
q->L = o;
o->B = -(--q->B);
*node = q;
}
}
return change != 0;
}
static int pickPrevNode(Node **node, Node **exch)
{ // *exch on entry = parent node
Node *o = *node; // *exch on exit = picked previous value node
if (o->R)
{
*exch = o;
int change = -pickPrevNode(&o->R, exch);
if (change)
if (abs(o->B += change) > 1)
return balanceNode(node);
else
return o->B == 0;
else
return 0;
}
else if (o->L)
{
Node *p = o->L;
o->L = NULL;
(*exch)->R = p;
*exch = o;
return p->B == 0;
}
else
{
(*exch)->R = NULL;
*exch = o;
return 1;
}
}
static int pickNextNode(Node **node, Node **exch)
{ // *exch on entry = parent node
Node *o = *node; // *exch on exit = picked next value node
if (o->L)
{
*exch = o;
int change = +pickNextNode(&o->L, exch);
if (change)
if (abs(o->B += change) > 1)
return balanceNode(node);
else
return o->B == 0;
else
return 0;
}
else if (o->R)
{
Node *q = o->R;
o->R = NULL;
(*exch)->L = q;
*exch = o;
return q->B == 0;
}
else
{
(*exch)->L = NULL;
*exch = o;
return 1;
}
}
// CAUTION: The following recursive functions must not be called with name == NULL.
// For performance reasons no extra error checking is done.
Node *findTreeNode(const char *name, Node *node)
{
if (node)
{
int ord = strcmp(name, node->name);
if (ord == 0)
return node;
else if (ord < 0)
return findTreeNode(name, node->L);
else // (ord > 0)
return findTreeNode(name, node->R);
}
else
return NULL;
}
int addTreeNode(const char *name, ssize_t naml, Value *value, Node **node, Node **passed)
{
static const Value zeros = {0, {.i = 0}};
Node *o = *node;
if (o == NULL) // if the hash/name is not in the tree
{ // then add it into a new leaf
if (o = allocate(sizeof(Node), true))
if (o->name = allocate(naml+1, false))
{
strcpy(o->name, name);
o->naml = naml;
if (value)
o->value = *value;
*node = *passed = o; // report back the new node into which the new value has been entered
return 1; // add the weight of 1 leaf onto the balance
}
else
deallocate(VPR(o), false);
*passed = NULL;
return 0; // Out of Memory situation, nothing changed
}
else
{
int change;
int ord = strcmp(name, o->name);
if (ord == 0) // if the name is already in the tree then
{
releaseValue(&o->value); // release the old value - if kind is empty then releaseValue() does nothing
o->value = (value) ? *value // either store the new value or
: zeros; // zero-out the value struct
*passed = o; // report back the node in which the value was modified
return 0;
}
else if (ord < 0)
change = -addTreeNode(name, naml, value, &o->L, passed);
else // (ord > 0)
change = +addTreeNode(name, naml, value, &o->R, passed);
if (change)
if (abs(o->B += change) > 1)
return 1 - balanceNode(node);
else
return o->B != 0;
else
return 0;
}
}
int removeTreeNode(const char *name, Node **node)
{
Node *o = *node;
if (o == NULL)
return 0; // not found -> recursively do nothing
else
{
int change;
int ord = strcmp(name, o->name);
if (ord == 0)
{
int b = o->B;
Node *p = o->L;
Node *q = o->R;
if (!p || !q)
{
releaseValue(&(*node)->value);
deallocate_batch(false, VPR((*node)->name),
VPR(*node), NULL);
*node = (p > q) ? p : q;
return 1; // remove the weight of 1 leaf from the balance
}
else
{
if (b == -1)
{
if (!p->R)
{
change = +1;
o = p;
o->R = q;
}
else
{
change = +pickPrevNode(&p, &o);
o->L = p;
o->R = q;
}
}
else
{
if (!q->L)
{
change = -1;
o = q;
o->L = p;
}
else
{
change = -pickNextNode(&q, &o);
o->L = p;
o->R = q;
}
}
o->B = b;
releaseValue(&(*node)->value);
deallocate_batch(false, VPR((*node)->name),
VPR(*node), NULL);
*node = o;
}
}
else if (ord < 0)
change = +removeTreeNode(name, &o->L);
else // (ord > 0)
change = -removeTreeNode(name, &o->R);
if (change)
if (abs(o->B += change) > 1)
return balanceNode(node);
else
return o->B == 0;
else
return 0;
}
}
void releaseTree(Node *node)
{
if (node)
{
if (node->L)
releaseTree(node->L);
if (node->R)
releaseTree(node->R);
releaseValue(&node->value);
deallocate_batch(false, VPR(node->name),
VPR(node), NULL);
}
}
#pragma mark ••• Hash Table •••
// Essence of MurmurHash3_x86_32()
//
// Original at: https://github.com/aappleby/smhasher/blob/master/src/MurmurHash3.cpp
//
// Quote from the Source:
// "MurmurHash3 was written by Austin Appleby, and is placed in the public
// domain. The author hereby disclaims copyright to this source code."
//
// Many thanks to Austin!
static inline uint mmh3(const char *name, ssize_t naml)
{
int i, n = (int)(naml/4);
uint k1, h1 = (uint)naml; // quite tiny (0.2 %) better distribution by seeding the name length
uint *quads = (uint *)(name + n*4);
uchar *tail = (uchar *)quads;
for (i = -n; i; i++)
{
k1 = quads[i];
k1 *= 0xCC9E2D51;
k1 = (k1<<15)|(k1>>17);
k1 *= 0x1B873593;
h1 ^= k1;
h1 = (h1<<13)|(h1>>19);
h1 = h1*5 + 0xE6546B64;
}
k1 = 0;
switch (naml & 3)
{
case 3: k1 ^= (uint)(tail[2] << 16);
case 2: k1 ^= (uint)(tail[1] << 8);
case 1: k1 ^= (uint)(tail[0]);
k1 *= 0xCC9E2D51;
k1 = (k1<<15)|(k1>>17);
k1 *= 0x1B873593;
h1 ^= k1;
};
h1 ^= naml;
h1 ^= h1 >> 16;
h1 *= 0x85EBCA6B;
h1 ^= h1 >> 13;
h1 *= 0xC2B2AE35;
h1 ^= h1 >> 16;
return h1;
}
// Table creation and release
Node **createTable(uint n)
{
Node **table = allocate((n+1)*sizeof(Node *), true);
if (table)
*(uint *)table = n;
return table;
}
void releaseTable(Node *table[])
{
if (table)
{
uint i, n = *(uint *)table;
for (i = 1; i <= n; i++)
releaseTree(table[i]);
deallocate(VPR(table), false);
}
}
// Storing and retrieving values by name
Node *findName(Node *table[], const char *name, ssize_t naml)
{
if (name && *name)
{
if (naml < 0)
naml = strvlen(name);
uint n = *(uint *)table;
return findTreeNode(name, table[mmh3(name, naml)%n + 1]);
}
else
return NULL;
}
Node *storeName(Node *table[], const char *name, ssize_t naml, Value *value)
{
if (name && *name)
{
if (naml < 0)
naml = strvlen(name);
uint n = *(uint *)table;
Node *passed;
addTreeNode(name, naml, value, &table[mmh3(name, naml)%n + 1], &passed);
return passed;
}
else
return NULL;
}
void removeName(Node *table[], const char *name, ssize_t naml)
{
if (name && *name)
{
if (naml < 0)
naml = strvlen(name);
uint tidx = mmh3(name, naml) % *(uint*)table + 1;
Node *node = table[tidx];
if (node)
if (!node->L && !node->R)
{
releaseValue(&node->value);
deallocate_batch(false, VPR(node->name),
VPR(table[tidx]), NULL);
}
else
removeTreeNode(name, &table[tidx]);
}
}