From f6ed6e18b2281cee96af98a39bdfc453117e6a21 Mon Sep 17 00:00:00 2001
From: Masahiko Sawada <sawada.mshk@gmail.com>
Date: Thu, 16 Feb 2023 12:17:59 +0900
Subject: [PATCH v2899 1/2] comment update and test the shared tidstore.

---
 src/backend/access/common/tidstore.c          | 19 +++-------
 .../modules/test_tidstore/test_tidstore.c     | 37 +++++++++++++++++--
 2 files changed, 40 insertions(+), 16 deletions(-)

diff --git a/src/backend/access/common/tidstore.c b/src/backend/access/common/tidstore.c
index 015e3dea81..8c05e60d92 100644
--- a/src/backend/access/common/tidstore.c
+++ b/src/backend/access/common/tidstore.c
@@ -64,13 +64,9 @@
  * |---------------------------------------------| key
  *
  * The maximum height of the radix tree is 5 in this case.
- *
- * If the number of bits for offset number fits in a 64-bit value, we don't
- * encode tids but directly use the block number and the offset number as key
- * and value, respectively.
  */
 #define TIDSTORE_VALUE_NBITS	6	/* log(64, 2) */
-#define TIDSTORE_OFFSET_MASK ((1 << TIDSTORE_VALUE_NBITS) - 1)
+#define TIDSTORE_OFFSET_MASK	((1 << TIDSTORE_VALUE_NBITS) - 1)
 
 /* A magic value used to identify our TidStores. */
 #define TIDSTORE_MAGIC 0x826f6a10
@@ -99,9 +95,10 @@ typedef struct TidStoreControl
 	/* These values are never changed after creation */
 	size_t	max_bytes;		/* the maximum bytes a TidStore can use */
 	int		max_offset;		/* the maximum offset number */
-	int		offset_nbits;	/* the number of bits required for max_offset */
-	int		offset_key_nbits;	/* the number of bits of a offset number
-								 * used for the key */
+	int		offset_nbits;	/* the number of bits required for an offset
+							 * number */
+	int		offset_key_nbits;	/* the number of bits of an offset number
+								 * used in a key */
 
 	/* The below fields are used only in shared case */
 
@@ -227,10 +224,6 @@ tidstore_create(size_t max_bytes, int max_offset, dsa_area *area)
 	if (ts->control->offset_nbits < TIDSTORE_VALUE_NBITS)
 		ts->control->offset_nbits = TIDSTORE_VALUE_NBITS;
 
-	/*
-	 * We use tid encoding if the number of bits for the offset number doesn't
-	 * fix in a value, uint64.
-	 */
 	ts->control->offset_key_nbits =
 		ts->control->offset_nbits - TIDSTORE_VALUE_NBITS;
 
@@ -379,7 +372,7 @@ tidstore_add_tids(TidStore *ts, BlockNumber blkno, OffsetNumber *offsets,
 	{
 		uint64	off_bit;
 
-		/* encode the tid to key and val */
+		/* encode the tid to a key and partial offset */
 		key = encode_key_off(ts, blkno, offsets[i], &off_bit);
 
 		/* make sure we scanned the line pointer array in order */
diff --git a/src/test/modules/test_tidstore/test_tidstore.c b/src/test/modules/test_tidstore/test_tidstore.c
index 9b849ae8e8..9a1217f833 100644
--- a/src/test/modules/test_tidstore/test_tidstore.c
+++ b/src/test/modules/test_tidstore/test_tidstore.c
@@ -18,10 +18,13 @@
 #include "miscadmin.h"
 #include "storage/block.h"
 #include "storage/itemptr.h"
+#include "storage/lwlock.h"
 #include "utils/memutils.h"
 
 PG_MODULE_MAGIC;
 
+/* #define TEST_SHARED_TIDSTORE 1 */
+
 #define TEST_TIDSTORE_MAX_BYTES (2 * 1024 * 1024L) /* 2MB */
 
 PG_FUNCTION_INFO_V1(test_tidstore);
@@ -59,6 +62,18 @@ test_basic(int max_offset)
 	OffsetNumber offs[TEST_TIDSTORE_NUM_OFFSETS];
 	int blk_idx;
 
+#ifdef TEST_SHARED_TIDSTORE
+	int tranche_id = LWLockNewTrancheId();
+	dsa_area *dsa;
+
+	LWLockRegisterTranche(tranche_id, "test_tidstore");
+	dsa = dsa_create(tranche_id);
+
+	ts = tidstore_create(TEST_TIDSTORE_MAX_BYTES, max_offset, dsa);
+#else
+	ts = tidstore_create(TEST_TIDSTORE_MAX_BYTES, max_offset, NULL);
+#endif
+
 	/* prepare the offset array */
 	offs[0] = FirstOffsetNumber;
 	offs[1] = FirstOffsetNumber + 1;
@@ -66,8 +81,6 @@ test_basic(int max_offset)
 	offs[3] = max_offset - 1;
 	offs[4] = max_offset;
 
-	ts = tidstore_create(TEST_TIDSTORE_MAX_BYTES, max_offset, NULL);
-
 	/* add tids */
 	for (int i = 0; i < TEST_TIDSTORE_NUM_BLOCKS; i++)
 		tidstore_add_tids(ts, blks[i], offs, TEST_TIDSTORE_NUM_OFFSETS);
@@ -144,6 +157,10 @@ test_basic(int max_offset)
 	}
 
 	tidstore_destroy(ts);
+
+#ifdef TEST_SHARED_TIDSTORE
+	dsa_detach(dsa);
+#endif
 }
 
 static void
@@ -153,9 +170,19 @@ test_empty(void)
 	TidStoreIter *iter;
 	ItemPointerData tid;
 
-	elog(NOTICE, "testing empty tidstore");
+#ifdef TEST_SHARED_TIDSTORE
+	int tranche_id = LWLockNewTrancheId();
+	dsa_area *dsa;
+
+	LWLockRegisterTranche(tranche_id, "test_tidstore");
+	dsa = dsa_create(tranche_id);
 
+	ts = tidstore_create(TEST_TIDSTORE_MAX_BYTES, MaxHeapTuplesPerPage, dsa);
+#else
 	ts = tidstore_create(TEST_TIDSTORE_MAX_BYTES, MaxHeapTuplesPerPage, NULL);
+#endif
+
+	elog(NOTICE, "testing empty tidstore");
 
 	ItemPointerSet(&tid, 0, FirstOffsetNumber);
 	if (tidstore_lookup_tid(ts, &tid))
@@ -180,6 +207,10 @@ test_empty(void)
 	tidstore_end_iterate(iter);
 
 	tidstore_destroy(ts);
+
+#ifdef TEST_SHARED_TIDSTORE
+	dsa_detach(dsa);
+#endif
 }
 
 Datum
-- 
2.31.1

