summaryrefslogtreecommitdiff
path: root/lib/libalpm/pkghash.c
blob: 3ed6453fe1d44c25a9af4b1cc3dcb9075c4a68c8 (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
/*
 *  pkghash.c
 *
 *  Copyright (c) 2011 Pacman Development Team <pacman-dev@archlinux.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "pkghash.h"
#include "util.h"
#include "log.h"

/* List of primes for possible sizes of hash tables.
 *
 * The maximum table size is the last prime under 1,000,000.  That is
 * more than an order of magnitude greater than the number of packages
 * in any Linux distribution.
 */
static const size_t prime_list[] =
{
        11ul13ul17ul19ul23ul29ul31ul37ul41ul43ul47ul,
        53ul59ul61ul67ul71ul73ul79ul83ul89ul97ul103ul,
        109ul113ul127ul137ul139ul149ul157ul167ul179ul193ul,
        199ul211ul227ul241ul257ul277ul293ul313ul337ul359ul,
        383ul409ul439ul467ul503ul541ul577ul619ul661ul709ul,
        761ul823ul887ul953ul1031ul1109ul1193ul1289ul1381ul,
        1493ul1613ul1741ul1879ul2029ul2179ul2357ul2549ul,
        2753ul2971ul3209ul3469ul3739ul4027ul4349ul4703ul,
        5087ul5503ul5953ul6427ul6949ul7517ul8123ul8783ul,
        9497ul10273ul11113ul12011ul12983ul14033ul15173ul,
        16411ul17749ul19183ul20753ul22447ul24281ul26267ul,
        28411ul30727ul33223ul35933ul38873ul42043ul45481ul,
        49201ul53201ul57557ul62233ul67307ul72817ul78779ul,
        85229ul92203ul99733ul107897ul116731ul126271ul136607ul,
        147793ul159871ul172933ul187091ul202409ul218971ul236897ul,
        256279ul277261ul299951ul324503ul351061ul379787ul410857ul,
        444487ul480881ul520241ul562841ul608903ul658753ul712697ul,
        771049ul834181ul902483ul976369ul
};

/* Allocate a hash table with at least "size" buckets */
pmpkghash_t *_alpm_pkghash_create(size_t size)
{
        pmpkghash_t *hash = NULL;
        size_t i, loopsize;

        MALLOC(hash, sizeof(pmpkghash_t), RET_ERR(PM_ERR_MEMORY, NULL));

        hash->list = NULL;
        hash->entries = 0;

        loopsize = sizeof(prime_list) / sizeof(*prime_list);
        for(i = 0; i < loopsize; i++) {
                if(prime_list[i] > size) {
                        hash->buckets = prime_list[i];
                        break;
                }
        }

        CALLOC(hash->hash_table, hash->buckets, sizeof(alpm_list_t*), \
                                free(hash); RET_ERR(PM_ERR_MEMORY, NULL));

        return(hash);
}

/* Expand the hash table size to the next increment and rebin the entries */
static pmpkghash_t *rehash(pmpkghash_t *oldhash)
{
        //pmpkghash_t *newhash;

        /* TODO - calculate size of newhash */
        /* Hash tables will need resized in two cases:
         *  - adding packages to the local database
         *  - poor estimation of the number of packages in sync database
         *
         * For small hash tables sizes (<500) the increase in size is by a
         * minimum of a factor of 2 for optimal rehash efficiency.  For
         * larger database sizes, this increase is reduced to avoid excess
         * memory allocation as both scenarios requiring a rehash should not
         * require a table size increase that large. */

        //newhash = _alpm_pkghash_create(oldhash->buckets);

        /* TODO - rebin entries */

        //_alpm_pkghash_free(oldhash);

        //return newhash;

        printf("rehash needed!!!\n");
        return(oldhash);
}

pmpkghash_t *_alpm_pkghash_add(pmpkghash_t *hash, pmpkg_t *pkg)
{
        alpm_list_t *ptr;
        size_t position;

        if(pkg == NULL || hash == NULL) {
                return(hash);
        }

        if((hash->entries + 1) / MAX_HASH_LOAD > hash->buckets) {
                hash = rehash(hash);
        }

        position = pkg->name_hash % hash->buckets;

        /* collision resolution using open addressing with linear probing */
        while((ptr = hash->hash_table[position]) != NULL) {
                position = (position + 1) % hash->buckets;
        }

        ptr = calloc(1sizeof(alpm_list_t));
        if(ptr == NULL) {
                return(hash);
        }

        ptr->data = pkg;
        ptr->next = NULL;
        ptr->prev = ptr;

        hash->hash_table[position] = ptr;
        hash->list = alpm_list_join(hash->list, ptr);
        hash->entries += 1;

        return(hash);
}

pmpkghash_t *_alpm_pkghash_add_sorted(pmpkghash_t *hash, pmpkg_t *pkg)
{
        if(!hash) {
                return(_alpm_pkghash_add(hash, pkg));
        }

        alpm_list_t *ptr;
        size_t position;

        if((hash->entries + 1) / MAX_HASH_LOAD > hash->buckets) {
                hash = rehash(hash);
        }

        position = pkg->name_hash % hash->buckets;

        /* collision resolution using open addressing with linear probing */
        while((ptr = hash->hash_table[position]) != NULL) {
                position = (position + 1) % hash->buckets;
        }

        ptr = calloc(1sizeof(alpm_list_t));
        if(ptr == NULL) {
                return(hash);
        }

        ptr->data = pkg;
        ptr->next = NULL;
        ptr->prev = ptr;

        hash->hash_table[position] = ptr;
        hash->list = alpm_list_mmerge(hash->list, ptr, _alpm_pkg_cmp);
        hash->entries += 1;

        return(hash);
}

/**
 * @brief Remove a package from a pkghash.
 *
 * @param hash     the hash to remove the package from
 * @param pkg      the package we are removing
 * @param data     output parameter containing the removed item
 *
 * @return the resultant hash
 */
pmpkghash_t *_alpm_pkghash_remove(pmpkghash_t *hash, pmpkg_t *pkg, pmpkg_t *data)
{
        alpm_list_t *i;
        size_t position;

        if(pkg == NULL || hash == NULL) {
                return(hash);
        }

        position = pkg->name_hash % hash->buckets;
        while((i = hash->hash_table[position]) != NULL) {
                pmpkg_t *info = i->data;

                if(info->name_hash == pkg->name_hash &&
                                        strcmp(info->name, pkg->name) == 0) {

#if 0  /* Actually removing items is not a good idea with linear probing...  */
                        /* remove from list */
                        /* TODO - refactor with alpm_list_remove */
                        if(i == hash->list) {
                                /* Special case: removing the head node which has a back reference to
                                 * the tail node */
                                hash->list = i->next;
                                if(hash->list) {
                                        hash->list->prev = i->prev;
                                }
                                i->prev = NULL;
                        } else if(i == hash->list->prev) {
                                /* Special case: removing the tail node, so we need to fix the back
                                 * reference on the head node. We also know tail != head. */
                                if(i->prev) {
                                        /* i->next should always be null */
                                        i->prev->next = i->next;
                                        hash->list->prev = i->prev;
                                        i->prev = NULL;
                                }
                        } else {
                                /* Normal case, non-head and non-tail node */
                                if(i->next) {
                                        i->next->prev = i->prev;
                                }
                                if(i->prev) {
                                        i->prev->next = i->next;
                                }
                        }

                        /* remove from hash */
                        data = info;
                        info = NULL;
                        free(i);
                        i = NULL;

                        hash->entries -= 1;
#endif

                        /* fake removal - pkgname can not be blank so 0 hash is impossible */
                        info->name_hash = 0;

                        return(hash);
                }

                position = (position + 1) % hash->buckets;
        }

        return(hash);
}

void _alpm_pkghash_free(pmpkghash_t *hash)
{
        size_t i;
        if(hash != NULL) {
                for(i = 0; i < hash->buckets; i++) {
                        free(hash->hash_table[i]);
                }
                free(hash->hash_table);
        }
        free(hash);
}

pmpkg_t *_alpm_pkghash_find(pmpkghash_t *hash, const char *name)
{
        alpm_list_t *lp;
        unsigned long name_hash;
        size_t position;

        ALPM_LOG_FUNC;

        if(name == NULL || hash == NULL) {
                return(NULL);
        }

        name_hash = _alpm_hash_sdbm(name);

        position = name_hash % hash->buckets;

        while((lp = hash->hash_table[position]) != NULL) {
                pmpkg_t *info = lp->data;

                if(info->name_hash == name_hash && strcmp(info->name, name) == 0) {
                        return(info);
                }

                position = (position + 1) % hash->buckets;
        }

        return(NULL);
}