PostgreSQL Source Code  git master
bloomfilter.c File Reference
#include "postgres.h"
#include <math.h>
#include "common/hashfn.h"
#include "lib/bloomfilter.h"
#include "port/pg_bitutils.h"
Include dependency graph for bloomfilter.c:

Go to the source code of this file.

Data Structures

struct  bloom_filter
 

Macros

#define MAX_HASH_FUNCS   10
 

Functions

static int my_bloom_power (uint64 target_bitset_bits)
 
static int optimal_k (uint64 bitset_bits, int64 total_elems)
 
static void k_hashes (bloom_filter *filter, uint32 *hashes, unsigned char *elem, size_t len)
 
static uint32 mod_m (uint32 val, uint64 m)
 
bloom_filterbloom_create (int64 total_elems, int bloom_work_mem, uint64 seed)
 
void bloom_free (bloom_filter *filter)
 
void bloom_add_element (bloom_filter *filter, unsigned char *elem, size_t len)
 
bool bloom_lacks_element (bloom_filter *filter, unsigned char *elem, size_t len)
 
double bloom_prop_bits_set (bloom_filter *filter)
 

Macro Definition Documentation

◆ MAX_HASH_FUNCS

#define MAX_HASH_FUNCS   10

Definition at line 42 of file bloomfilter.c.

Function Documentation

◆ bloom_add_element()

void bloom_add_element ( bloom_filter filter,
unsigned char *  elem,
size_t  len 
)

Definition at line 135 of file bloomfilter.c.

136 {
137  uint32 hashes[MAX_HASH_FUNCS];
138  int i;
139 
140  k_hashes(filter, hashes, elem, len);
141 
142  /* Map a bit-wise address to a byte-wise address + bit offset */
143  for (i = 0; i < filter->k_hash_funcs; i++)
144  {
145  filter->bitset[hashes[i] >> 3] |= 1 << (hashes[i] & 7);
146  }
147 }
#define MAX_HASH_FUNCS
Definition: bloomfilter.c:42
static void k_hashes(bloom_filter *filter, uint32 *hashes, unsigned char *elem, size_t len)
Definition: bloomfilter.c:250
uint32_t uint32
Definition: c.h:485
int i
Definition: isn.c:72
const void size_t len
unsigned char bitset[FLEXIBLE_ARRAY_MEMBER]
Definition: bloomfilter.c:51
int k_hash_funcs
Definition: bloomfilter.c:47

References bloom_filter::bitset, i, bloom_filter::k_hash_funcs, k_hashes(), len, and MAX_HASH_FUNCS.

Referenced by bt_target_page_check(), populate_with_dummy_strings(), and roles_list_append().

◆ bloom_create()

bloom_filter* bloom_create ( int64  total_elems,
int  bloom_work_mem,
uint64  seed 
)

Definition at line 87 of file bloomfilter.c.

88 {
89  bloom_filter *filter;
90  int bloom_power;
91  uint64 bitset_bytes;
92  uint64 bitset_bits;
93 
94  /*
95  * Aim for two bytes per element; this is sufficient to get a false
96  * positive rate below 1%, independent of the size of the bitset or total
97  * number of elements. Also, if rounding down the size of the bitset to
98  * the next lowest power of two turns out to be a significant drop, the
99  * false positive rate still won't exceed 2% in almost all cases.
100  */
101  bitset_bytes = Min(bloom_work_mem * UINT64CONST(1024), total_elems * 2);
102  bitset_bytes = Max(1024 * 1024, bitset_bytes);
103 
104  /*
105  * Size in bits should be the highest power of two <= target. bitset_bits
106  * is uint64 because PG_UINT32_MAX is 2^32 - 1, not 2^32
107  */
108  bloom_power = my_bloom_power(bitset_bytes * BITS_PER_BYTE);
109  bitset_bits = UINT64CONST(1) << bloom_power;
110  bitset_bytes = bitset_bits / BITS_PER_BYTE;
111 
112  /* Allocate bloom filter with unset bitset */
113  filter = palloc0(offsetof(bloom_filter, bitset) +
114  sizeof(unsigned char) * bitset_bytes);
115  filter->k_hash_funcs = optimal_k(bitset_bits, total_elems);
116  filter->seed = seed;
117  filter->m = bitset_bits;
118 
119  return filter;
120 }
static int optimal_k(uint64 bitset_bits, int64 total_elems)
Definition: bloomfilter.c:229
static int my_bloom_power(uint64 target_bitset_bits)
Definition: bloomfilter.c:210
#define Min(x, y)
Definition: c.h:958
#define Max(x, y)
Definition: c.h:952
uint64_t uint64
Definition: c.h:486
#define UINT64CONST(x)
Definition: c.h:500
void * palloc0(Size size)
Definition: mcxt.c:1347
#define BITS_PER_BYTE
uint64 seed
Definition: bloomfilter.c:48

References BITS_PER_BYTE, bloom_filter::k_hash_funcs, bloom_filter::m, Max, Min, my_bloom_power(), optimal_k(), palloc0(), bloom_filter::seed, and UINT64CONST.

Referenced by bt_check_every_level(), create_and_test_bloom(), and roles_list_append().

◆ bloom_free()

void bloom_free ( bloom_filter filter)

Definition at line 126 of file bloomfilter.c.

127 {
128  pfree(filter);
129 }
void pfree(void *pointer)
Definition: mcxt.c:1521

References pfree().

Referenced by bt_check_every_level(), create_and_test_bloom(), and roles_is_member_of().

◆ bloom_lacks_element()

bool bloom_lacks_element ( bloom_filter filter,
unsigned char *  elem,
size_t  len 
)

Definition at line 157 of file bloomfilter.c.

158 {
159  uint32 hashes[MAX_HASH_FUNCS];
160  int i;
161 
162  k_hashes(filter, hashes, elem, len);
163 
164  /* Map a bit-wise address to a byte-wise address + bit offset */
165  for (i = 0; i < filter->k_hash_funcs; i++)
166  {
167  if (!(filter->bitset[hashes[i] >> 3] & (1 << (hashes[i] & 7))))
168  return true;
169  }
170 
171  return false;
172 }

References bloom_filter::bitset, i, bloom_filter::k_hash_funcs, k_hashes(), len, and MAX_HASH_FUNCS.

Referenced by bt_tuple_present_callback(), nfalsepos_for_missing_strings(), and roles_list_append().

◆ bloom_prop_bits_set()

double bloom_prop_bits_set ( bloom_filter filter)

Definition at line 187 of file bloomfilter.c.

188 {
189  int bitset_bytes = filter->m / BITS_PER_BYTE;
190  uint64 bits_set = pg_popcount((char *) filter->bitset, bitset_bytes);
191 
192  return bits_set / (double) filter->m;
193 }
static uint64 pg_popcount(const char *buf, int bytes)
Definition: pg_bitutils.h:339

References BITS_PER_BYTE, bloom_filter::bitset, bloom_filter::m, and pg_popcount().

Referenced by bt_check_every_level(), and create_and_test_bloom().

◆ k_hashes()

static void k_hashes ( bloom_filter filter,
uint32 hashes,
unsigned char *  elem,
size_t  len 
)
static

Definition at line 250 of file bloomfilter.c.

251 {
252  uint64 hash;
253  uint32 x,
254  y;
255  uint64 m;
256  int i;
257 
258  /* Use 64-bit hashing to get two independent 32-bit hashes */
259  hash = DatumGetUInt64(hash_any_extended(elem, len, filter->seed));
260  x = (uint32) hash;
261  y = (uint32) (hash >> 32);
262  m = filter->m;
263 
264  x = mod_m(x, m);
265  y = mod_m(y, m);
266 
267  /* Accumulate hashes */
268  hashes[0] = x;
269  for (i = 1; i < filter->k_hash_funcs; i++)
270  {
271  x = mod_m(x + y, m);
272  y = mod_m(y + i, m);
273 
274  hashes[i] = x;
275  }
276 }
static uint32 mod_m(uint32 val, uint64 m)
Definition: bloomfilter.c:288
static Datum hash_any_extended(const unsigned char *k, int keylen, uint64 seed)
Definition: hashfn.h:37
int y
Definition: isn.c:71
int x
Definition: isn.c:70
static uint64 DatumGetUInt64(Datum X)
Definition: postgres.h:419
static unsigned hash(unsigned *uv, int n)
Definition: rege_dfa.c:715

References DatumGetUInt64(), hash(), hash_any_extended(), i, bloom_filter::k_hash_funcs, len, bloom_filter::m, mod_m(), bloom_filter::seed, x, and y.

Referenced by bloom_add_element(), and bloom_lacks_element().

◆ mod_m()

static uint32 mod_m ( uint32  val,
uint64  m 
)
inlinestatic

Definition at line 288 of file bloomfilter.c.

289 {
290  Assert(m <= PG_UINT32_MAX + UINT64CONST(1));
291  Assert(((m - 1) & m) == 0);
292 
293  return val & (m - 1);
294 }
#define PG_UINT32_MAX
Definition: c.h:544
#define Assert(condition)
Definition: c.h:812
long val
Definition: informix.c:689

References Assert, PG_UINT32_MAX, UINT64CONST, and val.

Referenced by k_hashes().

◆ my_bloom_power()

static int my_bloom_power ( uint64  target_bitset_bits)
static

Definition at line 210 of file bloomfilter.c.

211 {
212  int bloom_power = -1;
213 
214  while (target_bitset_bits > 0 && bloom_power < 32)
215  {
216  bloom_power++;
217  target_bitset_bits >>= 1;
218  }
219 
220  return bloom_power;
221 }

Referenced by bloom_create().

◆ optimal_k()

static int optimal_k ( uint64  bitset_bits,
int64  total_elems 
)
static

Definition at line 229 of file bloomfilter.c.

230 {
231  int k = rint(log(2.0) * bitset_bits / total_elems);
232 
233  return Max(1, Min(k, MAX_HASH_FUNCS));
234 }

References Max, MAX_HASH_FUNCS, and Min.

Referenced by bloom_create().