From b9837275d0f4abe614826b82134f58df2f783249 Mon Sep 17 00:00:00 2001
From: Tomas Vondra <tomas@2ndquadrant.com>
Date: Sat, 7 Nov 2020 20:36:38 +0100
Subject: [PATCH 6/8] one-hash tweaks

---
 src/backend/access/brin/brin_bloom.c | 25 +++++++++++++++++--------
 1 file changed, 17 insertions(+), 8 deletions(-)

diff --git a/src/backend/access/brin/brin_bloom.c b/src/backend/access/brin/brin_bloom.c
index 73f8478b4c..d5581abf7b 100644
--- a/src/backend/access/brin/brin_bloom.c
+++ b/src/backend/access/brin/brin_bloom.c
@@ -276,7 +276,7 @@ typedef struct BloomFilter
 	uint8	nhashes;	/* number of hash functions */
 	uint32	nbits;		/* number of bits in the bitmap (size) */
 	uint32	nbits_set;	/* number of bits set to 1 */
-	uint16	partlens[BLOOM_MAX_NUM_PARTITIONS];	/* partition lengths */
+	uint32	partlens[BLOOM_MAX_NUM_PARTITIONS];	/* partition lengths */
 
 	/* data of the bloom filter (used both for sorted and hashed phase) */
 	char	data[FLEXIBLE_ARRAY_MEMBER];
@@ -292,7 +292,7 @@ static BloomFilter *bloom_switch_to_hashing(BloomFilter *filter);
  *
  * WIP: very naive prime sieve; could be optimized using segmented ranges
  */
-static uint16 *
+static uint32 *
 generate_primes(int limit)
 {
 	/* upper bound of number of primes below limit */
@@ -300,7 +300,7 @@ generate_primes(int limit)
 	int numprimes = 1.26 * limit / log(limit);
 
 	bool *is_composite = (bool *) palloc0(limit * sizeof(bool));
-	uint16 *primes = (uint16 *) palloc0(numprimes * sizeof(uint16));
+	uint32 *primes = (uint32 *) palloc0(numprimes * sizeof(uint32));
 
 	int maxfactor = floor(sqrt(limit));
 	int factor = 2;	/* first prime */
@@ -338,19 +338,24 @@ generate_primes(int limit)
  * WIP: one-hashing bf paper ref somewhere
  */
 static uint32
-set_bloom_partitions(int nbits, uint8 nhashes, uint16 *partlens)
+set_bloom_partitions(int nbits, uint8 nhashes, uint32 *partlens)
 {
 	int		min, diff, incr;
 	int		pidx = 0;
 	int		sum = 0;
 	int		target_partlen = nbits / nhashes;
+	uint32 *primes;
+
+	elog(LOG, "generating primes nbits %u nhashes %u target_partlen %d", nbits, nhashes, target_partlen);
 
 	/*
 	 * Increase the limit to ensure we have some primes higher than
 	 * the target partition length. The 100 value is arbitrary, but
 	 * should be well over what we need.
 	 */
-	uint16 *primes = generate_primes(target_partlen + 100);
+	primes = generate_primes(target_partlen + 100);
+
+	elog(LOG, "primes generated");
 
 	/*
 	 * In our array of primes, find a sequence of length nhashes, whose
@@ -358,12 +363,14 @@ set_bloom_partitions(int nbits, uint8 nhashes, uint16 *partlens)
 	 * array will be filled with zeros, so we need to guard against that.
 	 */
 	while (primes[pidx + nhashes - 1] <= target_partlen &&
-		   primes[pidx] > 0)
+		   primes[pidx + nhashes] > 0)
 		pidx++;
 
 	for (int i = 0; i < nhashes; i++)
 		sum += primes[pidx + i];
 
+	elog(LOG, "nbits %d sum %d", nbits, sum);
+
 	/*
 	 * Since all the primes are less than or equal the desired partition
 	 * length, the sum is somewhat less than nbits. Increment the starting
@@ -383,7 +390,9 @@ set_bloom_partitions(int nbits, uint8 nhashes, uint16 *partlens)
 		pidx++;
 	}
 
-	memcpy(partlens, &primes[pidx], nhashes * sizeof(uint16));
+	elog(LOG, "nbits %d sum %d", nbits, sum);
+
+	memcpy(partlens, &primes[pidx], nhashes * sizeof(uint32));
 
 	/* WIP: assuming it's not important to pfree primes */
 
@@ -680,7 +689,7 @@ bloom_switch_to_hashing(BloomFilter *filter)
 
 	newfilter->nhashes = filter->nhashes;
 	newfilter->nbits = filter->nbits;
-	memcpy(newfilter->partlens, filter->partlens, filter->nhashes * sizeof(uint16));
+	memcpy(newfilter->partlens, filter->partlens, filter->nhashes * sizeof(uint32));
 	newfilter->flags |= BLOOM_FLAG_PHASE_HASH;
 
 	SET_VARSIZE(newfilter, len);
-- 
2.26.2

