nostrdb

an unfairly fast embedded nostr database backed by lmdb
git clone git://jb55.com/nostrdb
Log | Files | Refs | Submodules | README | LICENSE

value_set.c (1388B)


      1  /* Note: only one hash table can be implemented a single file. */
      2 #include "../symbols.h"
      3 #include "hash/ht_hash_function.h"
      4 
      5 static size_t value_hash_function(const void *key, size_t key_len)
      6 {
      7     const fb_value_t *value = key;
      8 
      9     (void)key_len;
     10 
     11     switch (value->type) {
     12     case vt_int:
     13         return ht_int_hash_function((void *)(size_t)(value->i ^ value->type), sizeof(value->i));
     14     case vt_uint:
     15         return ht_int_hash_function((void *)(size_t)(value->u ^ value->type), sizeof(value->u));
     16     case vt_bool:
     17         return ht_int_hash_function((void *)(size_t)(value->b ^ value->type), sizeof(value->b));
     18     default:
     19         return 0;
     20     }
     21 }
     22 
     23 #define HT_HASH_FUNCTION value_hash_function
     24 
     25 #include "hash/hash_table_def.h"
     26 DEFINE_HASH_TABLE(fb_value_set)
     27 #include "hash/hash_table_impl.h"
     28 
     29 static inline int ht_match(const void *key, size_t len, fb_value_t *item)
     30 {
     31     const fb_value_t *value = key;
     32 
     33     (void)len;
     34 
     35     if (value->type != item->type) {
     36         return 0;
     37     }
     38     switch (value->type) {
     39     case vt_int:
     40         return value->i == item->i;
     41     case vt_uint:
     42         return value->u == item->u;
     43     case vt_bool:
     44         return value->b == item->b;
     45     default:
     46         return 0;
     47     }
     48 }
     49 
     50 static inline const void *ht_key(fb_value_t *value)
     51 {
     52     return value;
     53 }
     54 
     55 static inline size_t ht_key_len(fb_value_t *value)
     56 {
     57     (void)value;
     58 
     59     return 0;
     60 }