Back to home page

Redis cross reference

 
 

    


0001 /* Hash Tables Implementation.
0002  *
0003  * This file implements in-memory hash tables with insert/del/replace/find/
0004  * get-random-element operations. Hash tables will auto-resize if needed
0005  * tables of power of two in size are used, collisions are handled by
0006  * chaining. See the source code for more information... :)
0007  *
0008  * Copyright (c) 2006-2012, Salvatore Sanfilippo <antirez at gmail dot com>
0009  * All rights reserved.
0010  *
0011  * Redistribution and use in source and binary forms, with or without
0012  * modification, are permitted provided that the following conditions are met:
0013  *
0014  *   * Redistributions of source code must retain the above copyright notice,
0015  *     this list of conditions and the following disclaimer.
0016  *   * Redistributions in binary form must reproduce the above copyright
0017  *     notice, this list of conditions and the following disclaimer in the
0018  *     documentation and/or other materials provided with the distribution.
0019  *   * Neither the name of Redis nor the names of its contributors may be used
0020  *     to endorse or promote products derived from this software without
0021  *     specific prior written permission.
0022  *
0023  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0024  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0025  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0026  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0027  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0028  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0029  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0030  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0031  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0032  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0033  * POSSIBILITY OF SUCH DAMAGE.
0034  */
0035 
0036 #include <stdint.h>
0037 
0038 #ifndef __DICT_H
0039 #define __DICT_H
0040 
0041 #define DICT_OK 0
0042 #define DICT_ERR 1
0043 
0044 /* Unused arguments generate annoying warnings... */
0045 #define DICT_NOTUSED(V) ((void) V)
0046 
0047 typedef struct dictEntry {
0048     void *key;
0049     union {
0050         void *val;
0051         uint64_t u64;
0052         int64_t s64;
0053     } v;
0054     struct dictEntry *next;
0055 } dictEntry;
0056 
0057 typedef struct dictType {
0058     unsigned int (*hashFunction)(const void *key);
0059     void *(*keyDup)(void *privdata, const void *key);
0060     void *(*valDup)(void *privdata, const void *obj);
0061     int (*keyCompare)(void *privdata, const void *key1, const void *key2);
0062     void (*keyDestructor)(void *privdata, void *key);
0063     void (*valDestructor)(void *privdata, void *obj);
0064 } dictType;
0065 
0066 /* This is our hash table structure. Every dictionary has two of this as we
0067  * implement incremental rehashing, for the old to the new table. */
0068 typedef struct dictht {
0069     dictEntry **table;
0070     unsigned long size;
0071     unsigned long sizemask;
0072     unsigned long used;
0073 } dictht;
0074 
0075 typedef struct dict {
0076     dictType *type;
0077     void *privdata;
0078     dictht ht[2];
0079     int rehashidx; /* rehashing not in progress if rehashidx == -1 */
0080     int iterators; /* number of iterators currently running */
0081 } dict;
0082 
0083 /* If safe is set to 1 this is a safe iterator, that means, you can call
0084  * dictAdd, dictFind, and other functions against the dictionary even while
0085  * iterating. Otherwise it is a non safe iterator, and only dictNext()
0086  * should be called while iterating. */
0087 typedef struct dictIterator {
0088     dict *d;
0089     int table, index, safe;
0090     dictEntry *entry, *nextEntry;
0091 } dictIterator;
0092 
0093 /* This is the initial size of every hash table */
0094 #define DICT_HT_INITIAL_SIZE     4
0095 
0096 /* ------------------------------- Macros ------------------------------------*/
0097 #define dictFreeVal(d, entry) \
0098     if ((d)->type->valDestructor) \
0099         (d)->type->valDestructor((d)->privdata, (entry)->v.val)
0100 
0101 #define dictSetVal(d, entry, _val_) do { \
0102     if ((d)->type->valDup) \
0103         entry->v.val = (d)->type->valDup((d)->privdata, _val_); \
0104     else \
0105         entry->v.val = (_val_); \
0106 } while(0)
0107 
0108 #define dictSetSignedIntegerVal(entry, _val_) \
0109     do { entry->v.s64 = _val_; } while(0)
0110 
0111 #define dictSetUnsignedIntegerVal(entry, _val_) \
0112     do { entry->v.u64 = _val_; } while(0)
0113 
0114 #define dictFreeKey(d, entry) \
0115     if ((d)->type->keyDestructor) \
0116         (d)->type->keyDestructor((d)->privdata, (entry)->key)
0117 
0118 #define dictSetKey(d, entry, _key_) do { \
0119     if ((d)->type->keyDup) \
0120         entry->key = (d)->type->keyDup((d)->privdata, _key_); \
0121     else \
0122         entry->key = (_key_); \
0123 } while(0)
0124 
0125 #define dictCompareKeys(d, key1, key2) \
0126     (((d)->type->keyCompare) ? \
0127         (d)->type->keyCompare((d)->privdata, key1, key2) : \
0128         (key1) == (key2))
0129 
0130 #define dictHashKey(d, key) (d)->type->hashFunction(key)
0131 #define dictGetKey(he) ((he)->key)
0132 #define dictGetVal(he) ((he)->v.val)
0133 #define dictGetSignedIntegerVal(he) ((he)->v.s64)
0134 #define dictGetUnsignedIntegerVal(he) ((he)->v.u64)
0135 #define dictSlots(d) ((d)->ht[0].size+(d)->ht[1].size)
0136 #define dictSize(d) ((d)->ht[0].used+(d)->ht[1].used)
0137 #define dictIsRehashing(ht) ((ht)->rehashidx != -1)
0138 
0139 /* API */
0140 dict *dictCreate(dictType *type, void *privDataPtr);
0141 int dictExpand(dict *d, unsigned long size);
0142 int dictAdd(dict *d, void *key, void *val);
0143 dictEntry *dictAddRaw(dict *d, void *key);
0144 int dictReplace(dict *d, void *key, void *val);
0145 dictEntry *dictReplaceRaw(dict *d, void *key);
0146 int dictDelete(dict *d, const void *key);
0147 int dictDeleteNoFree(dict *d, const void *key);
0148 void dictRelease(dict *d);
0149 dictEntry * dictFind(dict *d, const void *key);
0150 void *dictFetchValue(dict *d, const void *key);
0151 int dictResize(dict *d);
0152 dictIterator *dictGetIterator(dict *d);
0153 dictIterator *dictGetSafeIterator(dict *d);
0154 dictEntry *dictNext(dictIterator *iter);
0155 void dictReleaseIterator(dictIterator *iter);
0156 dictEntry *dictGetRandomKey(dict *d);
0157 void dictPrintStats(dict *d);
0158 unsigned int dictGenHashFunction(const void *key, int len);
0159 unsigned int dictGenCaseHashFunction(const unsigned char *buf, int len);
0160 void dictEmpty(dict *d);
0161 void dictEnableResize(void);
0162 void dictDisableResize(void);
0163 int dictRehash(dict *d, int n);
0164 int dictRehashMilliseconds(dict *d, int ms);
0165 void dictSetHashFunctionSeed(unsigned int initval);
0166 unsigned int dictGetHashFunctionSeed(void);
0167 
0168 /* Hash table types */
0169 extern dictType dictTypeHeapStringCopyKey;
0170 extern dictType dictTypeHeapStrings;
0171 extern dictType dictTypeHeapStringCopyKeyValue;
0172 
0173 #endif /* __DICT_H */