summaryrefslogtreecommitdiff
path: root/src/util/virhash.c
blob: 8365f51eb3672919e5e4a1904dfb7aa8c71c996d (plain)
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/*
 * virhash.c: chained hash tables
 *
 * Reference: Your favorite introductory book on algorithms
 *
 * Copyright (C) 2005-2014 Red Hat, Inc.
 * Copyright (C) 2000 Bjorn Reese and Daniel Veillard.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS AND
 * CONTRIBUTORS ACCEPT NO RESPONSIBILITY IN ANY CONCEIVABLE MANNER.
 */

#include <config.h>


#include "virerror.h"
#include "virhash.h"
#include "virlog.h"
#include "virhashcode.h"
#include "virrandom.h"
#include "virobject.h"

#define VIR_FROM_THIS VIR_FROM_NONE

VIR_LOG_INIT("util.hash");


struct _virHashAtomic {
    virObjectLockable parent;
    GHashTable *hash;
};

static virClass *virHashAtomicClass;
static void virHashAtomicDispose(void *obj);

static int virHashAtomicOnceInit(void)
{
    if (!VIR_CLASS_NEW(virHashAtomic, virClassForObjectLockable()))
        return -1;

    return 0;
}

VIR_ONCE_GLOBAL_INIT(virHashAtomic);


/**
 * Our hash function uses a random seed to provide uncertainty from run to run
 * to prevent pre-crafting of colliding hash keys.
 */
static uint32_t virHashTableSeed;

static int virHashTableSeedOnceInit(void)
{
    virHashTableSeed = virRandomBits(32);
    return 0;
}

VIR_ONCE_GLOBAL_INIT(virHashTableSeed);


static unsigned int
virHashTableStringKey(const void *vkey)
{
    const char *key = vkey;

    return virHashCodeGen(key, strlen(key), virHashTableSeed);
}


/**
 * virHashNew:
 * @dataFree: callback to free data
 *
 * Create a new GHashTable * for use with string-based keys.
 *
 * Returns the newly created object.
 */
GHashTable *
virHashNew(GDestroyNotify dataFree)
{
    ignore_value(virHashTableSeedInitialize());

    return g_hash_table_new_full(virHashTableStringKey, g_str_equal, g_free, dataFree);
}


virHashAtomic *
virHashAtomicNew(GDestroyNotify dataFree)
{
    virHashAtomic *hash;

    if (virHashAtomicInitialize() < 0)
        return NULL;

    if (!(hash = virObjectLockableNew(virHashAtomicClass)))
        return NULL;

    hash->hash = virHashNew(dataFree);
    return hash;
}


static void
virHashAtomicDispose(void *obj)
{
    virHashAtomic *hash = obj;

    g_clear_pointer(&hash->hash, g_hash_table_unref);
}


/**
 * virHashAddEntry:
 * @table: the hash table
 * @name: the name of the userdata
 * @userdata: a pointer to the userdata
 *
 * Add the @userdata to the hash @table. This can later be retrieved
 * by using @name. Duplicate entries generate errors.
 *
 * Deprecated: Consider using g_hash_table_insert instead. Note that
 * g_hash_table_insert doesn't fail if entry exists. Also note that
 * g_hash_table_insert doesn't copy the key.
 *
 * Returns 0 the addition succeeded and -1 in case of error.
 */
int
virHashAddEntry(GHashTable *table, const char *name, void *userdata)
{
    if (!table || !name)
        return -1;

    if (g_hash_table_contains(table, name)) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Duplicate hash table key '%1$s'"), name);
        return -1;
    }

    g_hash_table_insert(table, g_strdup(name), userdata);

    return 0;
}

/**
 * virHashUpdateEntry:
 * @table: the hash table
 * @name: the name of the userdata
 * @userdata: a pointer to the userdata
 *
 * Add the @userdata to the hash @table. This can later be retrieved
 * by using @name. Existing entry for this tuple
 * will be removed and freed with @f if found.
 *
 * Deprecated: consider using g_hash_table_insert insert. Note that
 * g_hash_table_insert doesn't copy the key.
 *
 * Returns 0 the addition succeeded and -1 in case of error.
 */
int
virHashUpdateEntry(GHashTable *table, const char *name,
                   void *userdata)
{
    if (!table || !name)
        return -1;

    g_hash_table_insert(table, g_strdup(name), userdata);

    return 0;
}

int
virHashAtomicUpdate(virHashAtomic *table,
                    const char *name,
                    void *userdata)
{
    int ret;

    virObjectLock(table);
    ret = virHashUpdateEntry(table->hash, name, userdata);
    virObjectUnlock(table);

    return ret;
}


/**
 * virHashLookup:
 * @table: the hash table
 * @name: the name of the userdata
 *
 * Find the userdata specified by @name
 *
 * Deprecated: consider using g_hash_table_lookup instead
 *
 * Returns a pointer to the userdata
 */
void *
virHashLookup(GHashTable *table,
              const char *name)
{
    if (!table || !name)
        return NULL;

    return g_hash_table_lookup(table, name);
}


/**
 * virHashHasEntry:
 * @table: the hash table
 * @name: the name of the userdata
 *
 * Find whether entry specified by @name exists.
 *
 * Deprecated: consider using g_hash_table_contains instead
 *
 * Returns true if the entry exists and false otherwise
 */
bool
virHashHasEntry(GHashTable *table,
                const char *name)
{
    if (!table || !name)
        return false;

    return g_hash_table_contains(table, name);
}


/**
 * virHashSteal:
 * @table: the hash table
 * @name: the name of the userdata
 *
 * Find the userdata specified by @name
 * and remove it from the hash without freeing it.
 *
 * Deprecated: consider using g_hash_table_steal_extended instead
 *
 * Returns a pointer to the userdata
 */
void *virHashSteal(GHashTable *table, const char *name)
{
    g_autofree void *orig_name = NULL; /* the original key needs to be freed */
    void *val = NULL;

    if (!table || !name)
        return NULL;

    g_hash_table_steal_extended(table, name, &orig_name, &val);

    return val;
}

void *
virHashAtomicSteal(virHashAtomic *table,
                   const char *name)
{
    void *data;

    virObjectLock(table);
    data = virHashSteal(table->hash, name);
    virObjectUnlock(table);

    return data;
}


/**
 * virHashSize:
 * @table: the hash table
 *
 * Query the number of elements installed in the hash @table.
 *
 * Deprecated: consider using g_hash_table_size instead
 *
 * Returns the number of elements in the hash table or
 * -1 in case of error
 */
ssize_t
virHashSize(GHashTable *table)
{
    if (table == NULL)
        return -1;

    return g_hash_table_size(table);
}


/**
 * virHashRemoveEntry:
 * @table: the hash table
 * @name: the name of the userdata
 *
 * Find the userdata specified by the @name and remove
 * it from the hash @table. Existing userdata for this tuple will be removed
 * and freed with @f.
 *
 * Deprecated: consider using g_hash_table_remove
 *
 * Returns 0 if the removal succeeded and -1 in case of error or not found.
 */
int
virHashRemoveEntry(GHashTable *table,
                   const char *name)
{
    if (!table || !name)
        return -1;

    if (g_hash_table_remove(table, name))
        return 0;

    return -1;
}


/**
 * virHashForEach, virHashForEachSorted, virHashForEachSafe
 * @table: the hash table to process
 * @iter: callback to process each element
 * @opaque: opaque data to pass to the iterator
 *
 * Iterates over every element in the hash table, invoking the 'iter' callback.
 *
 * The elements are iterated in arbitrary order.
 *
 * virHashForEach prohibits @iter from modifying @table
 *
 * virHashForEachSafe allows the callback to remove the current
 * element using virHashRemoveEntry but calling other virHash* functions is
 * prohibited. Note that removing the entry invalidates @key and @payload in
 * the callback.
 *
 * virHashForEachSorted iterates the elements in order by sorted key.
 *
 * virHashForEachSorted and virHashForEachSafe are more computationally
 * expensive than virHashForEach.
 *
 * If @iter fails and returns a negative value, the evaluation is stopped and -1
 * is returned.
 *
 * Deprecated: Consider using g_hash_table_foreach as replacement for
 * virHashForEach, rewrite your code if it would require virHashForEachSafe.
 *
 * Returns 0 on success or -1 on failure.
 */
int
virHashForEach(GHashTable *table, virHashIterator iter, void *opaque)
{
    GHashTableIter htitr;
    void *key;
    void *value;

    if (!table || !iter)
        return -1;

    g_hash_table_iter_init(&htitr, table);

    while (g_hash_table_iter_next(&htitr, &key, &value)) {
        if (iter(value, key, opaque) < 0)
            return -1;
    }

    return 0;
}


int
virHashForEachSafe(GHashTable *table,
                   virHashIterator iter,
                   void *opaque)
{
    g_autofree virHashKeyValuePair *items = virHashGetItems(table, NULL, false);
    size_t i;

    if (!items)
        return -1;

    for (i = 0; items[i].key; i++) {
        if (iter((void *)items[i].value, items[i].key, opaque) < 0)
            return -1;
    }

    return 0;
}


int
virHashForEachSorted(GHashTable *table,
                     virHashIterator iter,
                     void *opaque)
{
    g_autofree virHashKeyValuePair *items = virHashGetItems(table, NULL, true);
    size_t i;

    if (!items)
        return -1;

    for (i = 0; items[i].key; i++) {
        if (iter((void *)items[i].value, items[i].key, opaque) < 0)
            return -1;
    }

    return 0;
}


struct virHashSearcherWrapFuncData {
    virHashSearcher iter;
    const void *opaque;
    const char *name;
};

static gboolean
virHashSearcherWrapFunc(gpointer key,
                        gpointer value,
                        gpointer opaque)
{
    struct virHashSearcherWrapFuncData *data = opaque;

    data->name = key;

    return !!(data->iter(value, key, data->opaque));
}

/**
 * virHashRemoveSet
 * @table: the hash table to process
 * @iter: callback to identify elements for removal
 * @opaque: opaque data to pass to the iterator
 *
 * Iterates over all elements in the hash table, invoking the 'iter'
 * callback. If the callback returns a non-zero value, the element
 * will be removed from the hash table & its payload passed to the
 * data freer callback registered at creation.
 *
 * Deprecated: consider using g_hash_table_foreach_remove instead
 *
 * Returns number of items removed on success, -1 on failure
 */
ssize_t
virHashRemoveSet(GHashTable *table,
                 virHashSearcher iter,
                 const void *opaque)
{
    struct virHashSearcherWrapFuncData data = { iter, opaque, NULL };

    if (table == NULL || iter == NULL)
        return -1;

    return g_hash_table_foreach_remove(table, virHashSearcherWrapFunc, &data);
}

/**
 * virHashRemoveAll
 * @table: the hash table to clear
 *
 * Free the hash @table's contents. The userdata is
 * deallocated with the function provided at creation time.
 *
 * Deprecated: consider using g_hash_table_remove_all instead
 */
void
virHashRemoveAll(GHashTable *table)
{
    if (!table)
        return;

    g_hash_table_remove_all(table);
}

/**
 * virHashSearch:
 * @table: the hash table to search
 * @iter: an iterator to identify the desired element
 * @opaque: extra opaque information passed to the iter
 * @name: the name of found user data, pass NULL to ignore
 *
 * Iterates over the hash table calling the 'iter' callback
 * for each element. The first element for which the iter
 * returns non-zero will be returned by this function.
 * The elements are processed in a undefined order. Caller is
 * responsible for freeing the @name.
 *
 * Deprecated: consider using g_hash_table_find instead
 */
void *virHashSearch(GHashTable *table,
                    virHashSearcher iter,
                    const void *opaque,
                    char **name)
{
    struct virHashSearcherWrapFuncData data = { iter, opaque, NULL };
    void *ret;

    if (!table || !iter)
        return NULL;

    if (!(ret = g_hash_table_find(table, virHashSearcherWrapFunc, &data)))
        return NULL;

    if (name)
        *name = g_strdup(data.name);

    return ret;
}


static int
virHashGetItemsKeySorter(const void *va,
                         const void *vb)
{
    const virHashKeyValuePair *a = va;
    const virHashKeyValuePair *b = vb;

    return strcmp(a->key, b->key);
}


virHashKeyValuePair *
virHashGetItems(GHashTable *table,
                size_t *nitems,
                bool sortKeys)
{
    virHashKeyValuePair *items;
    size_t dummy;
    GHashTableIter htitr;
    void *key;
    void *value;
    size_t i = 0;

    if (!nitems)
        nitems = &dummy;

    if (!table)
        return NULL;

    *nitems = g_hash_table_size(table);
    items = g_new0(virHashKeyValuePair, *nitems + 1);

    g_hash_table_iter_init(&htitr, table);

    while (g_hash_table_iter_next(&htitr, &key, &value)) {
        items[i].key = key;
        items[i].value = value;
        i++;
    }

    if (sortKeys)
        qsort(items, *nitems, sizeof(*items), virHashGetItemsKeySorter);

    return items;
}


struct virHashEqualData
{
    bool equal;
    GHashTable *table2;
    virHashValueComparator compar;
};

static int virHashEqualSearcher(const void *payload, const char *name,
                                const void *opaque)
{
    struct virHashEqualData *vhed = (void *)opaque;
    const void *value;

    value = virHashLookup(vhed->table2, name);
    if (!value ||
        vhed->compar(value, payload) != 0) {
        /* key is missing in 2nd table or values are different */
        vhed->equal = false;
        /* stop 'iteration' */
        return 1;
    }
    return 0;
}

bool virHashEqual(GHashTable *table1,
                  GHashTable *table2,
                  virHashValueComparator compar)
{
    struct virHashEqualData data = {
        .equal = true,
        .table2 = table2,
        .compar = compar,
    };

    if (table1 == table2)
        return true;

    if (!table1 || !table2 ||
        virHashSize(table1) != virHashSize(table2))
        return false;

    virHashSearch(table1, virHashEqualSearcher, &data, NULL);

    return data.equal;
}