v17-0003-Add-radix-implementation.patch
text/x-patch
Filename: v17-0003-Add-radix-implementation.patch
Type: text/x-patch
Part: 8
Patch
Format: format-patch
Series: patch v17-0003
Subject: Add radix implementation.
| File | + | − |
|---|---|---|
| src/backend/lib/Makefile | 1 | 0 |
| src/backend/lib/meson.build | 1 | 0 |
| src/backend/lib/radixtree.c | 2514 | 0 |
| src/include/lib/radixtree.h | 42 | 0 |
| src/test/modules/Makefile | 1 | 0 |
| src/test/modules/meson.build | 1 | 0 |
| src/test/modules/test_radixtree/expected/test_radixtree.out | 36 | 0 |
| src/test/modules/test_radixtree/.gitignore | 4 | 0 |
| src/test/modules/test_radixtree/Makefile | 23 | 0 |
| src/test/modules/test_radixtree/meson.build | 34 | 0 |
| src/test/modules/test_radixtree/README | 7 | 0 |
| src/test/modules/test_radixtree/sql/test_radixtree.sql | 7 | 0 |
| src/test/modules/test_radixtree/test_radixtree--1.0.sql | 8 | 0 |
| src/test/modules/test_radixtree/test_radixtree.c | 581 | 0 |
| src/test/modules/test_radixtree/test_radixtree.control | 4 | 0 |
From dfe269fb71621a6ec580ef3d8ae601bdbc0c4b91 Mon Sep 17 00:00:00 2001
From: Masahiko Sawada <sawada.mshk@gmail.com>
Date: Wed, 14 Sep 2022 12:38:51 +0000
Subject: [PATCH v17 3/9] Add radix implementation.
---
src/backend/lib/Makefile | 1 +
src/backend/lib/meson.build | 1 +
src/backend/lib/radixtree.c | 2514 +++++++++++++++++
src/include/lib/radixtree.h | 42 +
src/test/modules/Makefile | 1 +
src/test/modules/meson.build | 1 +
src/test/modules/test_radixtree/.gitignore | 4 +
src/test/modules/test_radixtree/Makefile | 23 +
src/test/modules/test_radixtree/README | 7 +
.../expected/test_radixtree.out | 36 +
src/test/modules/test_radixtree/meson.build | 34 +
.../test_radixtree/sql/test_radixtree.sql | 7 +
.../test_radixtree/test_radixtree--1.0.sql | 8 +
.../modules/test_radixtree/test_radixtree.c | 581 ++++
.../test_radixtree/test_radixtree.control | 4 +
15 files changed, 3264 insertions(+)
create mode 100644 src/backend/lib/radixtree.c
create mode 100644 src/include/lib/radixtree.h
create mode 100644 src/test/modules/test_radixtree/.gitignore
create mode 100644 src/test/modules/test_radixtree/Makefile
create mode 100644 src/test/modules/test_radixtree/README
create mode 100644 src/test/modules/test_radixtree/expected/test_radixtree.out
create mode 100644 src/test/modules/test_radixtree/meson.build
create mode 100644 src/test/modules/test_radixtree/sql/test_radixtree.sql
create mode 100644 src/test/modules/test_radixtree/test_radixtree--1.0.sql
create mode 100644 src/test/modules/test_radixtree/test_radixtree.c
create mode 100644 src/test/modules/test_radixtree/test_radixtree.control
diff --git a/src/backend/lib/Makefile b/src/backend/lib/Makefile
index 9dad31398a..4c1db794b6 100644
--- a/src/backend/lib/Makefile
+++ b/src/backend/lib/Makefile
@@ -22,6 +22,7 @@ OBJS = \
integerset.o \
knapsack.o \
pairingheap.o \
+ radixtree.o \
rbtree.o \
include $(top_srcdir)/src/backend/common.mk
diff --git a/src/backend/lib/meson.build b/src/backend/lib/meson.build
index 974cab8776..5f8df32c5c 100644
--- a/src/backend/lib/meson.build
+++ b/src/backend/lib/meson.build
@@ -11,4 +11,5 @@ backend_sources += files(
'knapsack.c',
'pairingheap.c',
'rbtree.c',
+ 'radixtree.c',
)
diff --git a/src/backend/lib/radixtree.c b/src/backend/lib/radixtree.c
new file mode 100644
index 0000000000..5203127f76
--- /dev/null
+++ b/src/backend/lib/radixtree.c
@@ -0,0 +1,2514 @@
+/*-------------------------------------------------------------------------
+ *
+ * radixtree.c
+ * Implementation for adaptive radix tree.
+ *
+ * This module employs the idea from the paper "The Adaptive Radix Tree: ARTful
+ * Indexing for Main-Memory Databases" by Viktor Leis, Alfons Kemper, and Thomas
+ * Neumann, 2013. The radix tree uses adaptive node sizes, a small number of node
+ * types, each with a different numbers of elements. Depending on the number of
+ * children, the appropriate node type is used.
+ *
+ * There are some differences from the proposed implementation. For instance,
+ * there is not support for path compression and lazy path expansion. The radix
+ * tree supports fixed length of the key so we don't expect the tree level
+ * wouldn't be high.
+ *
+ * Both the key and the value are 64-bit unsigned integer. The inner nodes and
+ * the leaf nodes have slightly different structure: for inner tree nodes,
+ * shift > 0, store the pointer to its child node as the value. The leaf nodes,
+ * shift == 0, have the 64-bit unsigned integer that is specified by the user as
+ * the value. The paper refers to this technique as "Multi-value leaves". We
+ * choose it to avoid an additional pointer traversal. It is the reason this code
+ * currently does not support variable-length keys.
+ *
+ * XXX: Most functions in this file have two variants for inner nodes and leaf
+ * nodes, therefore there are duplication codes. While this sometimes makes the
+ * code maintenance tricky, this reduces branch prediction misses when judging
+ * whether the node is a inner node of a leaf node.
+ *
+ * XXX: the radix tree node never be shrunk.
+ *
+ * Interface
+ * ---------
+ *
+ * rt_create - Create a new, empty radix tree
+ * rt_free - Free the radix tree
+ * rt_search - Search a key-value pair
+ * rt_set - Set a key-value pair
+ * rt_delete - Delete a key-value pair
+ * rt_begin_iterate - Begin iterating through all key-value pairs
+ * rt_iterate_next - Return next key-value pair, if any
+ * rt_end_iter - End iteration
+ * rt_memory_usage - Get the memory usage
+ * rt_num_entries - Get the number of key-value pairs
+ *
+ * rt_create() creates an empty radix tree in the given memory context
+ * and memory contexts for all kinds of radix tree node under the memory context.
+ *
+ * rt_iterate_next() ensures returning key-value pairs in the ascending
+ * order of the key.
+ *
+ * Copyright (c) 2022, PostgreSQL Global Development Group
+ *
+ * IDENTIFICATION
+ * src/backend/lib/radixtree.c
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "postgres.h"
+
+#include "lib/radixtree.h"
+#include "lib/stringinfo.h"
+#include "miscadmin.h"
+#include "nodes/bitmapset.h"
+#include "port/pg_bitutils.h"
+#include "port/pg_lfind.h"
+#include "utils/memutils.h"
+
+#ifdef RT_DEBUG
+#define UINT64_FORMAT_HEX "%" INT64_MODIFIER "X"
+#endif
+
+/* The number of bits encoded in one tree level */
+#define RT_NODE_SPAN BITS_PER_BYTE
+
+/* The number of maximum slots in the node */
+#define RT_NODE_MAX_SLOTS (1 << RT_NODE_SPAN)
+
+/* Mask for extracting a chunk from the key */
+#define RT_CHUNK_MASK ((1 << RT_NODE_SPAN) - 1)
+
+/* Maximum shift the radix tree uses */
+#define RT_MAX_SHIFT key_get_shift(UINT64_MAX)
+
+/* Tree level the radix tree uses */
+#define RT_MAX_LEVEL ((sizeof(uint64) * BITS_PER_BYTE) / RT_NODE_SPAN)
+
+/* Invalid index used in node-125 */
+#define RT_NODE_125_INVALID_IDX 0xFF
+
+/* Get a chunk from the key */
+#define RT_GET_KEY_CHUNK(key, shift) ((uint8) (((key) >> (shift)) & RT_CHUNK_MASK))
+
+/* For accessing bitmaps */
+#define BM_IDX(x) ((x) / BITS_PER_BITMAPWORD)
+#define BM_BIT(x) ((x) % BITS_PER_BITMAPWORD)
+
+/* Enum used rt_node_search() */
+typedef enum
+{
+ RT_ACTION_FIND = 0, /* find the key-value */
+ RT_ACTION_DELETE, /* delete the key-value */
+} rt_action;
+
+/*
+ * Supported radix tree node kinds and size classes.
+ *
+ * There are 4 node kinds and each node kind have one or two size classes,
+ * partial and full. The size classes in the same node kind have the same
+ * node structure but have the different number of fanout that is stored
+ * in 'fanout' of rt_node. For example in size class 15, when a 16th element
+ * is to be inserted, we allocate a larger area and memcpy the entire old
+ * node to it.
+ *
+ * This technique allows us to limit the node kinds to 4, which limits the
+ * number of cases in switch statements. It also allows a possible future
+ * optimization to encode the node kind in a pointer tag.
+ *
+ * These size classes have been chose carefully so that it minimizes the
+ * allocator padding in both the inner and leaf nodes on DSA.
+ * node
+ */
+#define RT_NODE_KIND_4 0x00
+#define RT_NODE_KIND_32 0x01
+#define RT_NODE_KIND_125 0x02
+#define RT_NODE_KIND_256 0x03
+#define RT_NODE_KIND_COUNT 4
+
+typedef enum rt_size_class
+{
+ RT_CLASS_4_FULL = 0,
+ RT_CLASS_32_PARTIAL,
+ RT_CLASS_32_FULL,
+ RT_CLASS_125_FULL,
+ RT_CLASS_256
+
+#define RT_SIZE_CLASS_COUNT (RT_CLASS_256 + 1)
+} rt_size_class;
+
+/* Common type for all nodes types */
+typedef struct rt_node
+{
+ /*
+ * Number of children. We use uint16 to be able to indicate 256 children
+ * at the fanout of 8.
+ */
+ uint16 count;
+
+ /* Max number of children. We can use uint8 because we never need to store 256 */
+ /* WIP: if we don't have a variable sized node4, this should instead be in the base
+ types as needed, since saving every byte is crucial for the smallest node kind */
+ uint8 fanout;
+
+ /*
+ * Shift indicates which part of the key space is represented by this
+ * node. That is, the key is shifted by 'shift' and the lowest
+ * RT_NODE_SPAN bits are then represented in chunk.
+ */
+ uint8 shift;
+ uint8 chunk;
+
+ /* Node kind, one per search/set algorithm */
+ uint8 kind;
+} rt_node;
+#define NODE_IS_LEAF(n) (((rt_node *) (n))->shift == 0)
+#define NODE_IS_EMPTY(n) (((rt_node *) (n))->count == 0)
+#define VAR_NODE_HAS_FREE_SLOT(node) \
+ ((node)->base.n.count < (node)->base.n.fanout)
+#define FIXED_NODE_HAS_FREE_SLOT(node, class) \
+ ((node)->base.n.count < rt_size_class_info[class].fanout)
+
+/* Base type of each node kinds for leaf and inner nodes */
+/* The base types must be a be able to accommodate the largest size
+class for variable-sized node kinds*/
+typedef struct rt_node_base_4
+{
+ rt_node n;
+
+ /* 4 children, for key chunks */
+ uint8 chunks[4];
+} rt_node_base_4;
+
+typedef struct rt_node_base32
+{
+ rt_node n;
+
+ /* 32 children, for key chunks */
+ uint8 chunks[32];
+} rt_node_base_32;
+
+/*
+ * node-125 uses slot_idx array, an array of RT_NODE_MAX_SLOTS length, typically
+ * 256, to store indexes into a second array that contains up to 125 values (or
+ * child pointers in inner nodes).
+ */
+typedef struct rt_node_base125
+{
+ rt_node n;
+
+ /* The index of slots for each fanout */
+ uint8 slot_idxs[RT_NODE_MAX_SLOTS];
+
+ /* isset is a bitmap to track which slot is in use */
+ bitmapword isset[BM_IDX(128)];
+} rt_node_base_125;
+
+typedef struct rt_node_base256
+{
+ rt_node n;
+} rt_node_base_256;
+
+/*
+ * Inner and leaf nodes.
+ *
+ * Theres are separate for two main reasons:
+ *
+ * 1) the value type might be different than something fitting into a pointer
+ * width type
+ * 2) Need to represent non-existing values in a key-type independent way.
+ *
+ * 1) is clearly worth being concerned about, but it's not clear 2) is as
+ * good. It might be better to just indicate non-existing entries the same way
+ * in inner nodes.
+ */
+typedef struct rt_node_inner_4
+{
+ rt_node_base_4 base;
+
+ /* number of children depends on size class */
+ rt_node *children[FLEXIBLE_ARRAY_MEMBER];
+} rt_node_inner_4;
+
+typedef struct rt_node_leaf_4
+{
+ rt_node_base_4 base;
+
+ /* number of values depends on size class */
+ uint64 values[FLEXIBLE_ARRAY_MEMBER];
+} rt_node_leaf_4;
+
+typedef struct rt_node_inner_32
+{
+ rt_node_base_32 base;
+
+ /* number of children depends on size class */
+ rt_node *children[FLEXIBLE_ARRAY_MEMBER];
+} rt_node_inner_32;
+
+typedef struct rt_node_leaf_32
+{
+ rt_node_base_32 base;
+
+ /* number of values depends on size class */
+ uint64 values[FLEXIBLE_ARRAY_MEMBER];
+} rt_node_leaf_32;
+
+typedef struct rt_node_inner_125
+{
+ rt_node_base_125 base;
+
+ /* number of children depends on size class */
+ rt_node *children[FLEXIBLE_ARRAY_MEMBER];
+} rt_node_inner_125;
+
+typedef struct rt_node_leaf_125
+{
+ rt_node_base_125 base;
+
+ /* number of values depends on size class */
+ uint64 values[FLEXIBLE_ARRAY_MEMBER];
+} rt_node_leaf_125;
+
+/*
+ * node-256 is the largest node type. This node has RT_NODE_MAX_SLOTS length array
+ * for directly storing values (or child pointers in inner nodes).
+ */
+typedef struct rt_node_inner_256
+{
+ rt_node_base_256 base;
+
+ /* Slots for 256 children */
+ rt_node *children[RT_NODE_MAX_SLOTS];
+} rt_node_inner_256;
+
+typedef struct rt_node_leaf_256
+{
+ rt_node_base_256 base;
+
+ /* isset is a bitmap to track which slot is in use */
+ bitmapword isset[BM_IDX(RT_NODE_MAX_SLOTS)];
+
+ /* Slots for 256 values */
+ uint64 values[RT_NODE_MAX_SLOTS];
+} rt_node_leaf_256;
+
+/* Information for each size class */
+typedef struct rt_size_class_elem
+{
+ const char *name;
+ int fanout;
+
+ /* slab chunk size */
+ Size inner_size;
+ Size leaf_size;
+
+ /* slab block size */
+ Size inner_blocksize;
+ Size leaf_blocksize;
+} rt_size_class_elem;
+
+/*
+ * Calculate the slab blocksize so that we can allocate at least 32 chunks
+ * from the block.
+ */
+#define NODE_SLAB_BLOCK_SIZE(size) \
+ Max((SLAB_DEFAULT_BLOCK_SIZE / (size)) * (size), (size) * 32)
+static rt_size_class_elem rt_size_class_info[RT_SIZE_CLASS_COUNT] = {
+ [RT_CLASS_4_FULL] = {
+ .name = "radix tree node 4",
+ .fanout = 4,
+ .inner_size = sizeof(rt_node_inner_4) + 4 * sizeof(rt_node *),
+ .leaf_size = sizeof(rt_node_leaf_4) + 4 * sizeof(uint64),
+ .inner_blocksize = NODE_SLAB_BLOCK_SIZE(sizeof(rt_node_inner_4) + 4 * sizeof(rt_node *)),
+ .leaf_blocksize = NODE_SLAB_BLOCK_SIZE(sizeof(rt_node_leaf_4) + 4 * sizeof(uint64)),
+ },
+ [RT_CLASS_32_PARTIAL] = {
+ .name = "radix tree node 15",
+ .fanout = 15,
+ .inner_size = sizeof(rt_node_inner_32) + 15 * sizeof(rt_node *),
+ .leaf_size = sizeof(rt_node_leaf_32) + 15 * sizeof(uint64),
+ .inner_blocksize = NODE_SLAB_BLOCK_SIZE(sizeof(rt_node_inner_32) + 15 * sizeof(rt_node *)),
+ .leaf_blocksize = NODE_SLAB_BLOCK_SIZE(sizeof(rt_node_leaf_32) + 15 * sizeof(uint64)),
+ },
+ [RT_CLASS_32_FULL] = {
+ .name = "radix tree node 32",
+ .fanout = 32,
+ .inner_size = sizeof(rt_node_inner_32) + 32 * sizeof(rt_node *),
+ .leaf_size = sizeof(rt_node_leaf_32) + 32 * sizeof(uint64),
+ .inner_blocksize = NODE_SLAB_BLOCK_SIZE(sizeof(rt_node_inner_32) + 32 * sizeof(rt_node *)),
+ .leaf_blocksize = NODE_SLAB_BLOCK_SIZE(sizeof(rt_node_leaf_32) + 32 * sizeof(uint64)),
+ },
+ [RT_CLASS_125_FULL] = {
+ .name = "radix tree node 125",
+ .fanout = 125,
+ .inner_size = sizeof(rt_node_inner_125) + 125 * sizeof(rt_node *),
+ .leaf_size = sizeof(rt_node_leaf_125) + 125 * sizeof(uint64),
+ .inner_blocksize = NODE_SLAB_BLOCK_SIZE(sizeof(rt_node_inner_125) + 125 * sizeof(rt_node *)),
+ .leaf_blocksize = NODE_SLAB_BLOCK_SIZE(sizeof(rt_node_leaf_125) + 125 * sizeof(uint64)),
+ },
+ [RT_CLASS_256] = {
+ .name = "radix tree node 256",
+ .fanout = 256,
+ .inner_size = sizeof(rt_node_inner_256),
+ .leaf_size = sizeof(rt_node_leaf_256),
+ .inner_blocksize = NODE_SLAB_BLOCK_SIZE(sizeof(rt_node_inner_256)),
+ .leaf_blocksize = NODE_SLAB_BLOCK_SIZE(sizeof(rt_node_leaf_256)),
+ },
+};
+
+/* Map from the node kind to its minimum size class */
+static rt_size_class kind_min_size_class[RT_NODE_KIND_COUNT] = {
+ [RT_NODE_KIND_4] = RT_CLASS_4_FULL,
+ [RT_NODE_KIND_32] = RT_CLASS_32_PARTIAL,
+ [RT_NODE_KIND_125] = RT_CLASS_125_FULL,
+ [RT_NODE_KIND_256] = RT_CLASS_256,
+};
+
+/*
+ * Iteration support.
+ *
+ * Iterating the radix tree returns each pair of key and value in the ascending
+ * order of the key. To support this, the we iterate nodes of each level.
+ *
+ * rt_node_iter struct is used to track the iteration within a node.
+ *
+ * rt_iter is the struct for iteration of the radix tree, and uses rt_node_iter
+ * in order to track the iteration of each level. During the iteration, we also
+ * construct the key whenever updating the node iteration information, e.g., when
+ * advancing the current index within the node or when moving to the next node
+ * at the same level.
+ */
+typedef struct rt_node_iter
+{
+ rt_node *node; /* current node being iterated */
+ int current_idx; /* current position. -1 for initial value */
+} rt_node_iter;
+
+struct rt_iter
+{
+ radix_tree *tree;
+
+ /* Track the iteration on nodes of each level */
+ rt_node_iter stack[RT_MAX_LEVEL];
+ int stack_len;
+
+ /* The key is being constructed during the iteration */
+ uint64 key;
+};
+
+/* A radix tree with nodes */
+struct radix_tree
+{
+ MemoryContext context;
+
+ rt_node *root;
+ uint64 max_val;
+ uint64 num_keys;
+
+ MemoryContextData *inner_slabs[RT_SIZE_CLASS_COUNT];
+ MemoryContextData *leaf_slabs[RT_SIZE_CLASS_COUNT];
+
+ /* statistics */
+#ifdef RT_DEBUG
+ int32 cnt[RT_SIZE_CLASS_COUNT];
+#endif
+};
+
+static void rt_new_root(radix_tree *tree, uint64 key);
+static rt_node *rt_alloc_node(radix_tree *tree, rt_size_class size_class, bool inner);
+static inline void rt_init_node(rt_node *node, uint8 kind, rt_size_class size_class,
+ bool inner);
+static void rt_free_node(radix_tree *tree, rt_node *node);
+static void rt_extend(radix_tree *tree, uint64 key);
+static inline bool rt_node_search_inner(rt_node *node, uint64 key, rt_action action,
+ rt_node **child_p);
+static inline bool rt_node_search_leaf(rt_node *node, uint64 key, rt_action action,
+ uint64 *value_p);
+static bool rt_node_insert_inner(radix_tree *tree, rt_node *parent, rt_node *node,
+ uint64 key, rt_node *child);
+static bool rt_node_insert_leaf(radix_tree *tree, rt_node *parent, rt_node *node,
+ uint64 key, uint64 value);
+static inline rt_node *rt_node_inner_iterate_next(rt_iter *iter, rt_node_iter *node_iter);
+static inline bool rt_node_leaf_iterate_next(rt_iter *iter, rt_node_iter *node_iter,
+ uint64 *value_p);
+static void rt_update_iter_stack(rt_iter *iter, rt_node *from_node, int from);
+static inline void rt_iter_update_key(rt_iter *iter, uint8 chunk, uint8 shift);
+
+/* verification (available only with assertion) */
+static void rt_verify_node(rt_node *node);
+
+/*
+ * Return index of the first element in 'base' that equals 'key'. Return -1
+ * if there is no such element.
+ */
+static inline int
+node_4_search_eq(rt_node_base_4 *node, uint8 chunk)
+{
+ int idx = -1;
+
+ for (int i = 0; i < node->n.count; i++)
+ {
+ if (node->chunks[i] == chunk)
+ {
+ idx = i;
+ break;
+ }
+ }
+
+ return idx;
+}
+
+/*
+ * Return index of the chunk to insert into chunks in the given node.
+ */
+static inline int
+node_4_get_insertpos(rt_node_base_4 *node, uint8 chunk)
+{
+ int idx;
+
+ for (idx = 0; idx < node->n.count; idx++)
+ {
+ if (node->chunks[idx] >= chunk)
+ break;
+ }
+
+ return idx;
+}
+
+/*
+ * Return index of the first element in 'base' that equals 'key'. Return -1
+ * if there is no such element.
+ */
+static inline int
+node_32_search_eq(rt_node_base_32 *node, uint8 chunk)
+{
+ int count = node->n.count;
+#ifndef USE_NO_SIMD
+ Vector8 spread_chunk;
+ Vector8 haystack1;
+ Vector8 haystack2;
+ Vector8 cmp1;
+ Vector8 cmp2;
+ uint32 bitfield;
+ int index_simd = -1;
+#endif
+
+#if defined(USE_NO_SIMD) || defined(USE_ASSERT_CHECKING)
+ int index = -1;
+
+ for (int i = 0; i < count; i++)
+ {
+ if (node->chunks[i] == chunk)
+ {
+ index = i;
+ break;
+ }
+ }
+#endif
+
+#ifndef USE_NO_SIMD
+ spread_chunk = vector8_broadcast(chunk);
+ vector8_load(&haystack1, &node->chunks[0]);
+ vector8_load(&haystack2, &node->chunks[sizeof(Vector8)]);
+ cmp1 = vector8_eq(spread_chunk, haystack1);
+ cmp2 = vector8_eq(spread_chunk, haystack2);
+ bitfield = vector8_highbit_mask(cmp1) | (vector8_highbit_mask(cmp2) << sizeof(Vector8));
+ bitfield &= ((UINT64CONST(1) << count) - 1);
+
+ if (bitfield)
+ index_simd = pg_rightmost_one_pos32(bitfield);
+
+ Assert(index_simd == index);
+ return index_simd;
+#else
+ return index;
+#endif
+}
+
+/*
+ * Return index of the chunk to insert into chunks in the given node.
+ */
+static inline int
+node_32_get_insertpos(rt_node_base_32 *node, uint8 chunk)
+{
+ int count = node->n.count;
+#ifndef USE_NO_SIMD
+ Vector8 spread_chunk;
+ Vector8 haystack1;
+ Vector8 haystack2;
+ Vector8 cmp1;
+ Vector8 cmp2;
+ Vector8 min1;
+ Vector8 min2;
+ uint32 bitfield;
+ int index_simd;
+#endif
+
+#if defined(USE_NO_SIMD) || defined(USE_ASSERT_CHECKING)
+ int index;
+
+ for (index = 0; index < count; index++)
+ {
+ if (node->chunks[index] >= chunk)
+ break;
+ }
+#endif
+
+#ifndef USE_NO_SIMD
+ spread_chunk = vector8_broadcast(chunk);
+ vector8_load(&haystack1, &node->chunks[0]);
+ vector8_load(&haystack2, &node->chunks[sizeof(Vector8)]);
+ min1 = vector8_min(spread_chunk, haystack1);
+ min2 = vector8_min(spread_chunk, haystack2);
+ cmp1 = vector8_eq(spread_chunk, min1);
+ cmp2 = vector8_eq(spread_chunk, min2);
+ bitfield = vector8_highbit_mask(cmp1) | (vector8_highbit_mask(cmp2) << sizeof(Vector8));
+ bitfield &= ((UINT64CONST(1) << count) - 1);
+
+ if (bitfield)
+ index_simd = pg_rightmost_one_pos32(bitfield);
+ else
+ index_simd = count;
+
+ Assert(index_simd == index);
+ return index_simd;
+#else
+ return index;
+#endif
+}
+
+/*
+ * Functions to manipulate both chunks array and children/values array.
+ * These are used for node-4 and node-32.
+ */
+
+/* Shift the elements right at 'idx' by one */
+static inline void
+chunk_children_array_shift(uint8 *chunks, rt_node **children, int count, int idx)
+{
+ memmove(&(chunks[idx + 1]), &(chunks[idx]), sizeof(uint8) * (count - idx));
+ memmove(&(children[idx + 1]), &(children[idx]), sizeof(rt_node *) * (count - idx));
+}
+
+static inline void
+chunk_values_array_shift(uint8 *chunks, uint64 *values, int count, int idx)
+{
+ memmove(&(chunks[idx + 1]), &(chunks[idx]), sizeof(uint8) * (count - idx));
+ memmove(&(values[idx + 1]), &(values[idx]), sizeof(uint64 *) * (count - idx));
+}
+
+/* Delete the element at 'idx' */
+static inline void
+chunk_children_array_delete(uint8 *chunks, rt_node **children, int count, int idx)
+{
+ memmove(&(chunks[idx]), &(chunks[idx + 1]), sizeof(uint8) * (count - idx - 1));
+ memmove(&(children[idx]), &(children[idx + 1]), sizeof(rt_node *) * (count - idx - 1));
+}
+
+static inline void
+chunk_values_array_delete(uint8 *chunks, uint64 *values, int count, int idx)
+{
+ memmove(&(chunks[idx]), &(chunks[idx + 1]), sizeof(uint8) * (count - idx - 1));
+ memmove(&(values[idx]), &(values[idx + 1]), sizeof(uint64) * (count - idx - 1));
+}
+
+/* Copy both chunks and children/values arrays */
+static inline void
+chunk_children_array_copy(uint8 *src_chunks, rt_node **src_children,
+ uint8 *dst_chunks, rt_node **dst_children)
+{
+ const int fanout = rt_size_class_info[RT_CLASS_4_FULL].fanout;
+ const Size chunk_size = sizeof(uint8) * fanout;
+ const Size children_size = sizeof(rt_node *) * fanout;
+
+ memcpy(dst_chunks, src_chunks, chunk_size);
+ memcpy(dst_children, src_children, children_size);
+}
+
+static inline void
+chunk_values_array_copy(uint8 *src_chunks, uint64 *src_values,
+ uint8 *dst_chunks, uint64 *dst_values)
+{
+ const int fanout = rt_size_class_info[RT_CLASS_4_FULL].fanout;
+ const Size chunk_size = sizeof(uint8) * fanout;
+ const Size values_size = sizeof(uint64) * fanout;
+
+ memcpy(dst_chunks, src_chunks, chunk_size);
+ memcpy(dst_values, src_values, values_size);
+}
+
+/* Functions to manipulate inner and leaf node-125 */
+
+/* Does the given chunk in the node has the value? */
+static inline bool
+node_125_is_chunk_used(rt_node_base_125 *node, uint8 chunk)
+{
+ return node->slot_idxs[chunk] != RT_NODE_125_INVALID_IDX;
+}
+
+static inline rt_node *
+node_inner_125_get_child(rt_node_inner_125 *node, uint8 chunk)
+{
+ Assert(!NODE_IS_LEAF(node));
+ return node->children[node->base.slot_idxs[chunk]];
+}
+
+static inline uint64
+node_leaf_125_get_value(rt_node_leaf_125 *node, uint8 chunk)
+{
+ Assert(NODE_IS_LEAF(node));
+ Assert(((rt_node_base_125 *) node)->slot_idxs[chunk] != RT_NODE_125_INVALID_IDX);
+ return node->values[node->base.slot_idxs[chunk]];
+}
+
+static void
+node_inner_125_delete(rt_node_inner_125 *node, uint8 chunk)
+{
+ int slotpos = node->base.slot_idxs[chunk];
+ int idx = BM_IDX(slotpos);
+ int bitnum = BM_BIT(slotpos);
+
+ Assert(!NODE_IS_LEAF(node));
+
+ node->base.isset[idx] &= ~((bitmapword) 1 << bitnum);
+ node->children[node->base.slot_idxs[chunk]] = NULL;
+ node->base.slot_idxs[chunk] = RT_NODE_125_INVALID_IDX;
+}
+
+static void
+node_leaf_125_delete(rt_node_leaf_125 *node, uint8 chunk)
+{
+ int slotpos = node->base.slot_idxs[chunk];
+ int idx = BM_IDX(slotpos);
+ int bitnum = BM_BIT(slotpos);
+
+ Assert(NODE_IS_LEAF(node));
+ node->base.isset[idx] &= ~((bitmapword) 1 << bitnum);
+ node->base.slot_idxs[chunk] = RT_NODE_125_INVALID_IDX;
+}
+
+/* Return an unused slot in node-125 */
+static int
+node_125_find_unused_slot(bitmapword *isset)
+{
+ int slotpos;
+ int idx;
+ bitmapword inverse;
+
+ /* get the first word with at least one bit not set */
+ for (idx = 0; idx < BM_IDX(128); idx++)
+ {
+ if (isset[idx] < ~((bitmapword) 0))
+ break;
+ }
+
+ /* To get the first unset bit in X, get the first set bit in ~X */
+ inverse = ~(isset[idx]);
+ slotpos = idx * BITS_PER_BITMAPWORD;
+ slotpos += bmw_rightmost_one_pos(inverse);
+
+ /* mark the slot used */
+ isset[idx] |= bmw_rightmost_one(inverse);
+
+ return slotpos;
+ }
+
+static inline void
+node_inner_125_insert(rt_node_inner_125 *node, uint8 chunk, rt_node *child)
+{
+ int slotpos;
+
+ Assert(!NODE_IS_LEAF(node));
+
+ slotpos = node_125_find_unused_slot(node->base.isset);
+ Assert(slotpos < node->base.n.fanout);
+
+ node->base.slot_idxs[chunk] = slotpos;
+ node->children[slotpos] = child;
+}
+
+/* Set the slot at the corresponding chunk */
+static inline void
+node_leaf_125_insert(rt_node_leaf_125 *node, uint8 chunk, uint64 value)
+{
+ int slotpos;
+
+ Assert(NODE_IS_LEAF(node));
+
+ slotpos = node_125_find_unused_slot(node->base.isset);
+ Assert(slotpos < node->base.n.fanout);
+
+ node->base.slot_idxs[chunk] = slotpos;
+ node->values[slotpos] = value;
+}
+
+/* Update the child corresponding to 'chunk' to 'child' */
+static inline void
+node_inner_125_update(rt_node_inner_125 *node, uint8 chunk, rt_node *child)
+{
+ Assert(!NODE_IS_LEAF(node));
+ node->children[node->base.slot_idxs[chunk]] = child;
+}
+
+static inline void
+node_leaf_125_update(rt_node_leaf_125 *node, uint8 chunk, uint64 value)
+{
+ Assert(NODE_IS_LEAF(node));
+ node->values[node->base.slot_idxs[chunk]] = value;
+}
+
+/* Functions to manipulate inner and leaf node-256 */
+
+/* Return true if the slot corresponding to the given chunk is in use */
+static inline bool
+node_inner_256_is_chunk_used(rt_node_inner_256 *node, uint8 chunk)
+{
+ Assert(!NODE_IS_LEAF(node));
+ return (node->children[chunk] != NULL);
+}
+
+static inline bool
+node_leaf_256_is_chunk_used(rt_node_leaf_256 *node, uint8 chunk)
+{
+ int idx = BM_IDX(chunk);
+ int bitnum = BM_BIT(chunk);
+
+ Assert(NODE_IS_LEAF(node));
+ return (node->isset[idx] & ((bitmapword) 1 << bitnum)) != 0;
+}
+
+static inline rt_node *
+node_inner_256_get_child(rt_node_inner_256 *node, uint8 chunk)
+{
+ Assert(!NODE_IS_LEAF(node));
+ Assert(node_inner_256_is_chunk_used(node, chunk));
+ return node->children[chunk];
+}
+
+static inline uint64
+node_leaf_256_get_value(rt_node_leaf_256 *node, uint8 chunk)
+{
+ Assert(NODE_IS_LEAF(node));
+ Assert(node_leaf_256_is_chunk_used(node, chunk));
+ return node->values[chunk];
+}
+
+/* Set the child in the node-256 */
+static inline void
+node_inner_256_set(rt_node_inner_256 *node, uint8 chunk, rt_node *child)
+{
+ Assert(!NODE_IS_LEAF(node));
+ node->children[chunk] = child;
+}
+
+/* Set the value in the node-256 */
+static inline void
+node_leaf_256_set(rt_node_leaf_256 *node, uint8 chunk, uint64 value)
+{
+ int idx = BM_IDX(chunk);
+ int bitnum = BM_BIT(chunk);
+
+ Assert(NODE_IS_LEAF(node));
+ node->isset[idx] |= ((bitmapword) 1 << bitnum);
+ node->values[chunk] = value;
+}
+
+/* Set the slot at the given chunk position */
+static inline void
+node_inner_256_delete(rt_node_inner_256 *node, uint8 chunk)
+{
+ Assert(!NODE_IS_LEAF(node));
+ node->children[chunk] = NULL;
+}
+
+static inline void
+node_leaf_256_delete(rt_node_leaf_256 *node, uint8 chunk)
+{
+ int idx = BM_IDX(chunk);
+ int bitnum = BM_BIT(chunk);
+
+ Assert(NODE_IS_LEAF(node));
+ node->isset[idx] &= ~((bitmapword) 1 << bitnum);
+}
+
+/*
+ * Return the shift that is satisfied to store the given key.
+ */
+static inline int
+key_get_shift(uint64 key)
+{
+ return (key == 0)
+ ? 0
+ : (pg_leftmost_one_pos64(key) / RT_NODE_SPAN) * RT_NODE_SPAN;
+}
+
+/*
+ * Return the max value stored in a node with the given shift.
+ */
+static uint64
+shift_get_max_val(int shift)
+{
+ if (shift == RT_MAX_SHIFT)
+ return UINT64_MAX;
+
+ return (UINT64CONST(1) << (shift + RT_NODE_SPAN)) - 1;
+}
+
+/*
+ * Create a new node as the root. Subordinate nodes will be created during
+ * the insertion.
+ */
+static void
+rt_new_root(radix_tree *tree, uint64 key)
+{
+ int shift = key_get_shift(key);
+ bool inner = shift > 0;
+ rt_node *newnode;
+
+ newnode = rt_alloc_node(tree, RT_CLASS_4_FULL, inner);
+ rt_init_node(newnode, RT_NODE_KIND_4, RT_CLASS_4_FULL, inner);
+ newnode->shift = shift;
+ tree->max_val = shift_get_max_val(shift);
+ tree->root = newnode;
+}
+
+/*
+ * Allocate a new node with the given node kind.
+ */
+static rt_node *
+rt_alloc_node(radix_tree *tree, rt_size_class size_class, bool inner)
+{
+ rt_node *newnode;
+
+ if (inner)
+ newnode = (rt_node *) MemoryContextAlloc(tree->inner_slabs[size_class],
+ rt_size_class_info[size_class].inner_size);
+ else
+ newnode = (rt_node *) MemoryContextAlloc(tree->leaf_slabs[size_class],
+ rt_size_class_info[size_class].leaf_size);
+
+#ifdef RT_DEBUG
+ /* update the statistics */
+ tree->cnt[size_class]++;
+#endif
+
+ return newnode;
+}
+
+/* Initialize the node contents */
+static inline void
+rt_init_node(rt_node *node, uint8 kind, rt_size_class size_class, bool inner)
+{
+ if (inner)
+ MemSet(node, 0, rt_size_class_info[size_class].inner_size);
+ else
+ MemSet(node, 0, rt_size_class_info[size_class].leaf_size);
+
+ node->kind = kind;
+ node->fanout = rt_size_class_info[size_class].fanout;
+
+ /* Initialize slot_idxs to invalid values */
+ if (kind == RT_NODE_KIND_125)
+ {
+ rt_node_base_125 *n125 = (rt_node_base_125 *) node;
+
+ memset(n125->slot_idxs, RT_NODE_125_INVALID_IDX, sizeof(n125->slot_idxs));
+ }
+
+ /*
+ * Technically it's 256, but we cannot store that in a uint8,
+ * and this is the max size class to it will never grow.
+ */
+ if (kind == RT_NODE_KIND_256)
+ node->fanout = 0;
+}
+
+static inline void
+rt_copy_node(rt_node *newnode, rt_node *oldnode)
+{
+ newnode->shift = oldnode->shift;
+ newnode->chunk = oldnode->chunk;
+ newnode->count = oldnode->count;
+}
+
+/*
+ * Create a new node with 'new_kind' and the same shift, chunk, and
+ * count of 'node'.
+ */
+static rt_node*
+rt_grow_node_kind(radix_tree *tree, rt_node *node, uint8 new_kind)
+{
+ rt_node *newnode;
+ bool inner = !NODE_IS_LEAF(node);
+
+ newnode = rt_alloc_node(tree, kind_min_size_class[new_kind], inner);
+ rt_init_node(newnode, new_kind, kind_min_size_class[new_kind], inner);
+ rt_copy_node(newnode, node);
+
+ return newnode;
+}
+
+/* Free the given node */
+static void
+rt_free_node(radix_tree *tree, rt_node *node)
+{
+ /* If we're deleting the root node, make the tree empty */
+ if (tree->root == node)
+ {
+ tree->root = NULL;
+ tree->max_val = 0;
+ }
+
+#ifdef RT_DEBUG
+ {
+ int i;
+
+ /* update the statistics */
+ for (i = 0; i < RT_SIZE_CLASS_COUNT; i++)
+ {
+ if (node->fanout == rt_size_class_info[i].fanout)
+ break;
+ }
+
+ /* fanout of node256 is intentionally 0 */
+ if (i == RT_SIZE_CLASS_COUNT)
+ i = RT_CLASS_256;
+
+ tree->cnt[i]--;
+ Assert(tree->cnt[i] >= 0);
+ }
+#endif
+
+ pfree(node);
+}
+
+/*
+ * Replace old_child with new_child, and free the old one.
+ */
+static void
+rt_replace_node(radix_tree *tree, rt_node *parent, rt_node *old_child,
+ rt_node *new_child, uint64 key)
+{
+ Assert(old_child->chunk == new_child->chunk);
+ Assert(old_child->shift == new_child->shift);
+
+ if (parent == old_child)
+ {
+ /* Replace the root node with the new large node */
+ tree->root = new_child;
+ }
+ else
+ {
+ bool replaced PG_USED_FOR_ASSERTS_ONLY;
+
+ replaced = rt_node_insert_inner(tree, NULL, parent, key, new_child);
+ Assert(replaced);
+ }
+
+ rt_free_node(tree, old_child);
+}
+
+/*
+ * The radix tree doesn't sufficient height. Extend the radix tree so it can
+ * store the key.
+ */
+static void
+rt_extend(radix_tree *tree, uint64 key)
+{
+ int target_shift;
+ int shift = tree->root->shift + RT_NODE_SPAN;
+
+ target_shift = key_get_shift(key);
+
+ /* Grow tree from 'shift' to 'target_shift' */
+ while (shift <= target_shift)
+ {
+ rt_node_inner_4 *node;
+
+ node = (rt_node_inner_4 *) rt_alloc_node(tree, RT_CLASS_4_FULL, true);
+ rt_init_node((rt_node *) node, RT_NODE_KIND_4, RT_CLASS_4_FULL, true);
+ node->base.n.shift = shift;
+ node->base.n.count = 1;
+ node->base.chunks[0] = 0;
+ node->children[0] = tree->root;
+
+ tree->root->chunk = 0;
+ tree->root = (rt_node *) node;
+
+ shift += RT_NODE_SPAN;
+ }
+
+ tree->max_val = shift_get_max_val(target_shift);
+}
+
+/*
+ * The radix tree doesn't have inner and leaf nodes for given key-value pair.
+ * Insert inner and leaf nodes from 'node' to bottom.
+ */
+static inline void
+rt_set_extend(radix_tree *tree, uint64 key, uint64 value, rt_node *parent,
+ rt_node *node)
+{
+ int shift = node->shift;
+
+ while (shift >= RT_NODE_SPAN)
+ {
+ rt_node *newchild;
+ int newshift = shift - RT_NODE_SPAN;
+ bool inner = newshift > 0;
+
+ newchild = rt_alloc_node(tree, RT_CLASS_4_FULL, inner);
+ rt_init_node(newchild, RT_NODE_KIND_4, RT_CLASS_4_FULL, inner);
+ newchild->shift = newshift;
+ newchild->chunk = RT_GET_KEY_CHUNK(key, node->shift);
+ rt_node_insert_inner(tree, parent, node, key, newchild);
+
+ parent = node;
+ node = newchild;
+ shift -= RT_NODE_SPAN;
+ }
+
+ rt_node_insert_leaf(tree, parent, node, key, value);
+ tree->num_keys++;
+}
+
+/*
+ * Search for the child pointer corresponding to 'key' in the given node, and
+ * do the specified 'action'.
+ *
+ * Return true if the key is found, otherwise return false. On success, the child
+ * pointer is set to child_p.
+ */
+static inline bool
+rt_node_search_inner(rt_node *node, uint64 key, rt_action action, rt_node **child_p)
+{
+ uint8 chunk = RT_GET_KEY_CHUNK(key, node->shift);
+ bool found = false;
+ rt_node *child = NULL;
+
+ switch (node->kind)
+ {
+ case RT_NODE_KIND_4:
+ {
+ rt_node_inner_4 *n4 = (rt_node_inner_4 *) node;
+ int idx = node_4_search_eq((rt_node_base_4 *) n4, chunk);
+
+ if (idx < 0)
+ break;
+
+ found = true;
+
+ if (action == RT_ACTION_FIND)
+ child = n4->children[idx];
+ else /* RT_ACTION_DELETE */
+ chunk_children_array_delete(n4->base.chunks, n4->children,
+ n4->base.n.count, idx);
+
+ break;
+ }
+ case RT_NODE_KIND_32:
+ {
+ rt_node_inner_32 *n32 = (rt_node_inner_32 *) node;
+ int idx = node_32_search_eq((rt_node_base_32 *) n32, chunk);
+
+ if (idx < 0)
+ break;
+
+ found = true;
+ if (action == RT_ACTION_FIND)
+ child = n32->children[idx];
+ else /* RT_ACTION_DELETE */
+ chunk_children_array_delete(n32->base.chunks, n32->children,
+ n32->base.n.count, idx);
+ break;
+ }
+ case RT_NODE_KIND_125:
+ {
+ rt_node_inner_125 *n125 = (rt_node_inner_125 *) node;
+
+ if (!node_125_is_chunk_used((rt_node_base_125 *) n125, chunk))
+ break;
+
+ found = true;
+
+ if (action == RT_ACTION_FIND)
+ child = node_inner_125_get_child(n125, chunk);
+ else /* RT_ACTION_DELETE */
+ node_inner_125_delete(n125, chunk);
+
+ break;
+ }
+ case RT_NODE_KIND_256:
+ {
+ rt_node_inner_256 *n256 = (rt_node_inner_256 *) node;
+
+ if (!node_inner_256_is_chunk_used(n256, chunk))
+ break;
+
+ found = true;
+ if (action == RT_ACTION_FIND)
+ child = node_inner_256_get_child(n256, chunk);
+ else /* RT_ACTION_DELETE */
+ node_inner_256_delete(n256, chunk);
+
+ break;
+ }
+ }
+
+ /* update statistics */
+ if (action == RT_ACTION_DELETE && found)
+ node->count--;
+
+ if (found && child_p)
+ *child_p = child;
+
+ return found;
+}
+
+/*
+ * Search for the value corresponding to 'key' in the given node, and do the
+ * specified 'action'.
+ *
+ * Return true if the key is found, otherwise return false. On success, the pointer
+ * to the value is set to value_p.
+ */
+static inline bool
+rt_node_search_leaf(rt_node *node, uint64 key, rt_action action, uint64 *value_p)
+{
+ uint8 chunk = RT_GET_KEY_CHUNK(key, node->shift);
+ bool found = false;
+ uint64 value = 0;
+
+ switch (node->kind)
+ {
+ case RT_NODE_KIND_4:
+ {
+ rt_node_leaf_4 *n4 = (rt_node_leaf_4 *) node;
+ int idx = node_4_search_eq((rt_node_base_4 *) n4, chunk);
+
+ if (idx < 0)
+ break;
+
+ found = true;
+
+ if (action == RT_ACTION_FIND)
+ value = n4->values[idx];
+ else /* RT_ACTION_DELETE */
+ chunk_values_array_delete(n4->base.chunks, (uint64 *) n4->values,
+ n4->base.n.count, idx);
+
+ break;
+ }
+ case RT_NODE_KIND_32:
+ {
+ rt_node_leaf_32 *n32 = (rt_node_leaf_32 *) node;
+ int idx = node_32_search_eq((rt_node_base_32 *) n32, chunk);
+
+ if (idx < 0)
+ break;
+
+ found = true;
+ if (action == RT_ACTION_FIND)
+ value = n32->values[idx];
+ else /* RT_ACTION_DELETE */
+ chunk_values_array_delete(n32->base.chunks, (uint64 *) n32->values,
+ n32->base.n.count, idx);
+ break;
+ }
+ case RT_NODE_KIND_125:
+ {
+ rt_node_leaf_125 *n125 = (rt_node_leaf_125 *) node;
+
+ if (!node_125_is_chunk_used((rt_node_base_125 *) n125, chunk))
+ break;
+
+ found = true;
+
+ if (action == RT_ACTION_FIND)
+ value = node_leaf_125_get_value(n125, chunk);
+ else /* RT_ACTION_DELETE */
+ node_leaf_125_delete(n125, chunk);
+
+ break;
+ }
+ case RT_NODE_KIND_256:
+ {
+ rt_node_leaf_256 *n256 = (rt_node_leaf_256 *) node;
+
+ if (!node_leaf_256_is_chunk_used(n256, chunk))
+ break;
+
+ found = true;
+ if (action == RT_ACTION_FIND)
+ value = node_leaf_256_get_value(n256, chunk);
+ else /* RT_ACTION_DELETE */
+ node_leaf_256_delete(n256, chunk);
+
+ break;
+ }
+ }
+
+ /* update statistics */
+ if (action == RT_ACTION_DELETE && found)
+ node->count--;
+
+ if (found && value_p)
+ *value_p = value;
+
+ return found;
+}
+
+/* Insert the child to the inner node */
+static bool
+rt_node_insert_inner(radix_tree *tree, rt_node *parent, rt_node *node, uint64 key,
+ rt_node *child)
+{
+ uint8 chunk = RT_GET_KEY_CHUNK(key, node->shift);
+ bool chunk_exists = false;
+ rt_node *newnode = NULL;
+
+ Assert(!NODE_IS_LEAF(node));
+
+ switch (node->kind)
+ {
+ case RT_NODE_KIND_4:
+ {
+ rt_node_inner_4 *n4 = (rt_node_inner_4 *) node;
+ int idx;
+
+ idx = node_4_search_eq(&n4->base, chunk);
+ if (idx != -1)
+ {
+ /* found the existing chunk */
+ chunk_exists = true;
+ n4->children[idx] = child;
+ break;
+ }
+
+ if (unlikely(!VAR_NODE_HAS_FREE_SLOT(n4)))
+ {
+ rt_node_inner_32 *new32;
+
+ /* grow node from 4 to 32 */
+ newnode = rt_grow_node_kind(tree, node, RT_NODE_KIND_32);
+ new32 = (rt_node_inner_32 *) newnode;
+ chunk_children_array_copy(n4->base.chunks, n4->children,
+ new32->base.chunks, new32->children);
+
+ Assert(parent != NULL);
+ rt_replace_node(tree, parent, node, newnode, key);
+ node = newnode;
+ }
+ else
+ {
+ int insertpos = node_4_get_insertpos(&n4->base, chunk);
+ uint16 count = n4->base.n.count;
+
+ /* shift chunks and children */
+ if (count != 0 && insertpos < count)
+ chunk_children_array_shift(n4->base.chunks, n4->children,
+ count, insertpos);
+
+ n4->base.chunks[insertpos] = chunk;
+ n4->children[insertpos] = child;
+ break;
+ }
+ }
+ /* FALLTHROUGH */
+ case RT_NODE_KIND_32:
+ {
+ const rt_size_class_elem minclass = rt_size_class_info[RT_CLASS_32_PARTIAL];
+ const rt_size_class_elem maxclass = rt_size_class_info[RT_CLASS_32_FULL];
+ rt_node_inner_32 *n32 = (rt_node_inner_32 *) node;
+ int idx;
+
+ idx = node_32_search_eq(&n32->base, chunk);
+ if (idx != -1)
+ {
+ /* found the existing chunk */
+ chunk_exists = true;
+ n32->children[idx] = child;
+ break;
+ }
+
+ if (unlikely(!VAR_NODE_HAS_FREE_SLOT(n32)) &&
+ n32->base.n.count == minclass.fanout)
+ {
+ /* grow to the next size class of this kind */
+ newnode = rt_alloc_node(tree, RT_CLASS_32_FULL, true);
+ memcpy(newnode, node, minclass.inner_size);
+ newnode->fanout = maxclass.fanout;
+
+ Assert(parent != NULL);
+ rt_replace_node(tree, parent, node, newnode, key);
+ node = newnode;
+
+ /* also update pointer for this kind */
+ n32 = (rt_node_inner_32 *) newnode;
+ }
+
+ if (unlikely(!VAR_NODE_HAS_FREE_SLOT(n32)))
+ {
+ rt_node_inner_125 *new125;
+
+ /* grow node from 32 to 125 */
+ newnode = rt_grow_node_kind(tree, node, RT_NODE_KIND_125);
+ new125 = (rt_node_inner_125 *) newnode;
+ for (int i = 0; i < n32->base.n.count; i++)
+ node_inner_125_insert(new125, n32->base.chunks[i], n32->children[i]);
+
+ Assert(parent != NULL);
+ rt_replace_node(tree, parent, node, newnode, key);
+ node = newnode;
+ }
+ else
+ {
+ int insertpos = node_32_get_insertpos(&n32->base, chunk);
+ int16 count = n32->base.n.count;
+
+ if (insertpos < count)
+ {
+ Assert(count > 0);
+ chunk_children_array_shift(n32->base.chunks, n32->children,
+ count, insertpos);
+ }
+
+ n32->base.chunks[insertpos] = chunk;
+ n32->children[insertpos] = child;
+ break;
+ }
+ }
+ /* FALLTHROUGH */
+ case RT_NODE_KIND_125:
+ {
+ rt_node_inner_125 *n125 = (rt_node_inner_125 *) node;
+ int cnt = 0;
+
+ if (node_125_is_chunk_used(&n125->base, chunk))
+ {
+ /* found the existing chunk */
+ chunk_exists = true;
+ node_inner_125_update(n125, chunk, child);
+ break;
+ }
+
+ if (unlikely(!VAR_NODE_HAS_FREE_SLOT(n125)))
+ {
+ rt_node_inner_256 *new256;
+ Assert(parent != NULL);
+
+ /* grow node from 125 to 256 */
+ newnode = rt_grow_node_kind(tree, node, RT_NODE_KIND_256);
+ new256 = (rt_node_inner_256 *) newnode;
+ for (int i = 0; i < RT_NODE_MAX_SLOTS && cnt < n125->base.n.count; i++)
+ {
+ if (!node_125_is_chunk_used(&n125->base, i))
+ continue;
+
+ node_inner_256_set(new256, i, node_inner_125_get_child(n125, i));
+ cnt++;
+ }
+
+ rt_replace_node(tree, parent, node, newnode, key);
+ node = newnode;
+ }
+ else
+ {
+ node_inner_125_insert(n125, chunk, child);
+ break;
+ }
+ }
+ /* FALLTHROUGH */
+ case RT_NODE_KIND_256:
+ {
+ rt_node_inner_256 *n256 = (rt_node_inner_256 *) node;
+
+ chunk_exists = node_inner_256_is_chunk_used(n256, chunk);
+ Assert(chunk_exists || FIXED_NODE_HAS_FREE_SLOT(n256, RT_CLASS_256));
+
+ node_inner_256_set(n256, chunk, child);
+ break;
+ }
+ }
+
+ /* Update statistics */
+ if (!chunk_exists)
+ node->count++;
+
+ /*
+ * Done. Finally, verify the chunk and value is inserted or replaced
+ * properly in the node.
+ */
+ rt_verify_node(node);
+
+ return chunk_exists;
+}
+
+/* Insert the value to the leaf node */
+static bool
+rt_node_insert_leaf(radix_tree *tree, rt_node *parent, rt_node *node,
+ uint64 key, uint64 value)
+{
+ uint8 chunk = RT_GET_KEY_CHUNK(key, node->shift);
+ bool chunk_exists = false;
+
+ Assert(NODE_IS_LEAF(node));
+
+ switch (node->kind)
+ {
+ case RT_NODE_KIND_4:
+ {
+ rt_node_leaf_4 *n4 = (rt_node_leaf_4 *) node;
+ int idx;
+
+ idx = node_4_search_eq((rt_node_base_4 *) n4, chunk);
+ if (idx != -1)
+ {
+ /* found the existing chunk */
+ chunk_exists = true;
+ n4->values[idx] = value;
+ break;
+ }
+
+ if (unlikely(!VAR_NODE_HAS_FREE_SLOT(n4)))
+ {
+ rt_node_leaf_32 *new32;
+ Assert(parent != NULL);
+
+ /* grow node from 4 to 32 */
+ new32 = (rt_node_leaf_32 *) rt_grow_node_kind(tree, (rt_node *) n4,
+ RT_NODE_KIND_32);
+ chunk_values_array_copy(n4->base.chunks, n4->values,
+ new32->base.chunks, new32->values);
+ rt_replace_node(tree, parent, (rt_node *) n4, (rt_node *) new32, key);
+ node = (rt_node *) new32;
+ }
+ else
+ {
+ int insertpos = node_4_get_insertpos((rt_node_base_4 *) n4, chunk);
+ int count = n4->base.n.count;
+
+ /* shift chunks and values */
+ if (count != 0 && insertpos < count)
+ chunk_values_array_shift(n4->base.chunks, n4->values,
+ count, insertpos);
+
+ n4->base.chunks[insertpos] = chunk;
+ n4->values[insertpos] = value;
+ break;
+ }
+ }
+ /* FALLTHROUGH */
+ case RT_NODE_KIND_32:
+ {
+ rt_node_leaf_32 *n32 = (rt_node_leaf_32 *) node;
+ int idx;
+
+ idx = node_32_search_eq((rt_node_base_32 *) n32, chunk);
+ if (idx != -1)
+ {
+ /* found the existing chunk */
+ chunk_exists = true;
+ n32->values[idx] = value;
+ break;
+ }
+
+ if (unlikely(!VAR_NODE_HAS_FREE_SLOT(n32)))
+ {
+ Assert(parent != NULL);
+
+ if (n32->base.n.count == rt_size_class_info[RT_CLASS_32_PARTIAL].fanout)
+ {
+ /* use the same node kind, but expand to the next size class */
+ const Size size = rt_size_class_info[RT_CLASS_32_PARTIAL].leaf_size;
+ const int fanout = rt_size_class_info[RT_CLASS_32_FULL].fanout;
+ rt_node_leaf_32 *new32;
+
+ new32 = (rt_node_leaf_32 *) rt_alloc_node(tree, RT_CLASS_32_FULL, false);
+ memcpy(new32, n32, size);
+ new32->base.n.fanout = fanout;
+
+ rt_replace_node(tree, parent, (rt_node *) n32, (rt_node *) new32, key);
+
+ /* must update both pointers here */
+ node = (rt_node *) new32;
+ n32 = new32;
+
+ goto retry_insert_leaf_32;
+ }
+ else
+ {
+ rt_node_leaf_125 *new125;
+
+ /* grow node from 32 to 125 */
+ new125 = (rt_node_leaf_125 *) rt_grow_node_kind(tree, (rt_node *) n32,
+ RT_NODE_KIND_125);
+ for (int i = 0; i < n32->base.n.count; i++)
+ node_leaf_125_insert(new125, n32->base.chunks[i], n32->values[i]);
+
+ rt_replace_node(tree, parent, (rt_node *) n32, (rt_node *) new125,
+ key);
+ node = (rt_node *) new125;
+ }
+ }
+ else
+ {
+ retry_insert_leaf_32:
+ {
+ int insertpos = node_32_get_insertpos((rt_node_base_32 *) n32, chunk);
+ int count = n32->base.n.count;
+
+ if (count != 0 && insertpos < count)
+ chunk_values_array_shift(n32->base.chunks, n32->values,
+ count, insertpos);
+
+ n32->base.chunks[insertpos] = chunk;
+ n32->values[insertpos] = value;
+ break;
+ }
+ }
+ }
+ /* FALLTHROUGH */
+ case RT_NODE_KIND_125:
+ {
+ rt_node_leaf_125 *n125 = (rt_node_leaf_125 *) node;
+ int cnt = 0;
+
+ if (node_125_is_chunk_used((rt_node_base_125 *) n125, chunk))
+ {
+ /* found the existing chunk */
+ chunk_exists = true;
+ node_leaf_125_update(n125, chunk, value);
+ break;
+ }
+
+ if (unlikely(!VAR_NODE_HAS_FREE_SLOT(n125)))
+ {
+ rt_node_leaf_256 *new256;
+ Assert(parent != NULL);
+
+ /* grow node from 125 to 256 */
+ new256 = (rt_node_leaf_256 *) rt_grow_node_kind(tree, (rt_node *) n125,
+ RT_NODE_KIND_256);
+ for (int i = 0; i < RT_NODE_MAX_SLOTS && cnt < n125->base.n.count; i++)
+ {
+ if (!node_125_is_chunk_used((rt_node_base_125 *) n125, i))
+ continue;
+
+ node_leaf_256_set(new256, i, node_leaf_125_get_value(n125, i));
+ cnt++;
+ }
+
+ rt_replace_node(tree, parent, (rt_node *) n125, (rt_node *) new256,
+ key);
+ node = (rt_node *) new256;
+ }
+ else
+ {
+ node_leaf_125_insert(n125, chunk, value);
+ break;
+ }
+ }
+ /* FALLTHROUGH */
+ case RT_NODE_KIND_256:
+ {
+ rt_node_leaf_256 *n256 = (rt_node_leaf_256 *) node;
+
+ chunk_exists = node_leaf_256_is_chunk_used(n256, chunk);
+ Assert(chunk_exists || FIXED_NODE_HAS_FREE_SLOT(n256, RT_CLASS_256));
+
+ node_leaf_256_set(n256, chunk, value);
+ break;
+ }
+ }
+
+ /* Update statistics */
+ if (!chunk_exists)
+ node->count++;
+
+ /*
+ * Done. Finally, verify the chunk and value is inserted or replaced
+ * properly in the node.
+ */
+ rt_verify_node(node);
+
+ return chunk_exists;
+}
+
+/*
+ * Create the radix tree in the given memory context and return it.
+ */
+radix_tree *
+rt_create(MemoryContext ctx)
+{
+ radix_tree *tree;
+ MemoryContext old_ctx;
+
+ old_ctx = MemoryContextSwitchTo(ctx);
+
+ tree = palloc(sizeof(radix_tree));
+ tree->context = ctx;
+ tree->root = NULL;
+ tree->max_val = 0;
+ tree->num_keys = 0;
+
+ /* Create the slab allocator for each size class */
+ for (int i = 0; i < RT_SIZE_CLASS_COUNT; i++)
+ {
+ tree->inner_slabs[i] = SlabContextCreate(ctx,
+ rt_size_class_info[i].name,
+ rt_size_class_info[i].inner_blocksize,
+ rt_size_class_info[i].inner_size);
+ tree->leaf_slabs[i] = SlabContextCreate(ctx,
+ rt_size_class_info[i].name,
+ rt_size_class_info[i].leaf_blocksize,
+ rt_size_class_info[i].leaf_size);
+#ifdef RT_DEBUG
+ tree->cnt[i] = 0;
+#endif
+ }
+
+ MemoryContextSwitchTo(old_ctx);
+
+ return tree;
+}
+
+/*
+ * Free the given radix tree.
+ */
+void
+rt_free(radix_tree *tree)
+{
+ for (int i = 0; i < RT_SIZE_CLASS_COUNT; i++)
+ {
+ MemoryContextDelete(tree->inner_slabs[i]);
+ MemoryContextDelete(tree->leaf_slabs[i]);
+ }
+
+ pfree(tree);
+}
+
+/*
+ * Set key to value. If the entry already exists, we update its value to 'value'
+ * and return true. Returns false if entry doesn't yet exist.
+ */
+bool
+rt_set(radix_tree *tree, uint64 key, uint64 value)
+{
+ int shift;
+ bool updated;
+ rt_node *node;
+ rt_node *parent;
+
+ /* Empty tree, create the root */
+ if (!tree->root)
+ rt_new_root(tree, key);
+
+ /* Extend the tree if necessary */
+ if (key > tree->max_val)
+ rt_extend(tree, key);
+
+ Assert(tree->root);
+
+ shift = tree->root->shift;
+ node = parent = tree->root;
+
+ /* Descend the tree until a leaf node */
+ while (shift >= 0)
+ {
+ rt_node *child;
+
+ if (NODE_IS_LEAF(node))
+ break;
+
+ if (!rt_node_search_inner(node, key, RT_ACTION_FIND, &child))
+ {
+ rt_set_extend(tree, key, value, parent, node);
+ return false;
+ }
+
+ parent = node;
+ node = child;
+ shift -= RT_NODE_SPAN;
+ }
+
+ updated = rt_node_insert_leaf(tree, parent, node, key, value);
+
+ /* Update the statistics */
+ if (!updated)
+ tree->num_keys++;
+
+ return updated;
+}
+
+/*
+ * Search the given key in the radix tree. Return true if there is the key,
+ * otherwise return false. On success, we set the value to *val_p so it must
+ * not be NULL.
+ */
+bool
+rt_search(radix_tree *tree, uint64 key, uint64 *value_p)
+{
+ rt_node *node;
+ int shift;
+
+ Assert(value_p != NULL);
+
+ if (!tree->root || key > tree->max_val)
+ return false;
+
+ node = tree->root;
+ shift = tree->root->shift;
+
+ /* Descend the tree until a leaf node */
+ while (shift >= 0)
+ {
+ rt_node *child;
+
+ if (NODE_IS_LEAF(node))
+ break;
+
+ if (!rt_node_search_inner(node, key, RT_ACTION_FIND, &child))
+ return false;
+
+ node = child;
+ shift -= RT_NODE_SPAN;
+ }
+
+ return rt_node_search_leaf(node, key, RT_ACTION_FIND, value_p);
+}
+
+/*
+ * Delete the given key from the radix tree. Return true if the key is found (and
+ * deleted), otherwise do nothing and return false.
+ */
+bool
+rt_delete(radix_tree *tree, uint64 key)
+{
+ rt_node *node;
+ rt_node *stack[RT_MAX_LEVEL] = {0};
+ int shift;
+ int level;
+ bool deleted;
+
+ if (!tree->root || key > tree->max_val)
+ return false;
+
+ /*
+ * Descend the tree to search the key while building a stack of nodes we
+ * visited.
+ */
+ node = tree->root;
+ shift = tree->root->shift;
+ level = -1;
+ while (shift > 0)
+ {
+ rt_node *child;
+
+ /* Push the current node to the stack */
+ stack[++level] = node;
+
+ if (!rt_node_search_inner(node, key, RT_ACTION_FIND, &child))
+ return false;
+
+ node = child;
+ shift -= RT_NODE_SPAN;
+ }
+
+ /* Delete the key from the leaf node if exists */
+ Assert(NODE_IS_LEAF(node));
+ deleted = rt_node_search_leaf(node, key, RT_ACTION_DELETE, NULL);
+
+ if (!deleted)
+ {
+ /* no key is found in the leaf node */
+ return false;
+ }
+
+ /* Found the key to delete. Update the statistics */
+ tree->num_keys--;
+
+ /*
+ * Return if the leaf node still has keys and we don't need to delete the
+ * node.
+ */
+ if (!NODE_IS_EMPTY(node))
+ return true;
+
+ /* Free the empty leaf node */
+ rt_free_node(tree, node);
+
+ /* Delete the key in inner nodes recursively */
+ while (level >= 0)
+ {
+ node = stack[level--];
+
+ deleted = rt_node_search_inner(node, key, RT_ACTION_DELETE, NULL);
+ Assert(deleted);
+
+ /* If the node didn't become empty, we stop deleting the key */
+ if (!NODE_IS_EMPTY(node))
+ break;
+
+ /* The node became empty */
+ rt_free_node(tree, node);
+ }
+
+ return true;
+}
+
+/* Create and return the iterator for the given radix tree */
+rt_iter *
+rt_begin_iterate(radix_tree *tree)
+{
+ MemoryContext old_ctx;
+ rt_iter *iter;
+ int top_level;
+
+ old_ctx = MemoryContextSwitchTo(tree->context);
+
+ iter = (rt_iter *) palloc0(sizeof(rt_iter));
+ iter->tree = tree;
+
+ /* empty tree */
+ if (!iter->tree->root)
+ return iter;
+
+ top_level = iter->tree->root->shift / RT_NODE_SPAN;
+ iter->stack_len = top_level;
+
+ /*
+ * Descend to the left most leaf node from the root. The key is being
+ * constructed while descending to the leaf.
+ */
+ rt_update_iter_stack(iter, iter->tree->root, top_level);
+
+ MemoryContextSwitchTo(old_ctx);
+
+ return iter;
+}
+
+/*
+ * Update each node_iter for inner nodes in the iterator node stack.
+ */
+static void
+rt_update_iter_stack(rt_iter *iter, rt_node *from_node, int from)
+{
+ int level = from;
+ rt_node *node = from_node;
+
+ for (;;)
+ {
+ rt_node_iter *node_iter = &(iter->stack[level--]);
+
+ node_iter->node = node;
+ node_iter->current_idx = -1;
+
+ /* We don't advance the leaf node iterator here */
+ if (NODE_IS_LEAF(node))
+ return;
+
+ /* Advance to the next slot in the inner node */
+ node = rt_node_inner_iterate_next(iter, node_iter);
+
+ /* We must find the first children in the node */
+ Assert(node);
+ }
+}
+
+/*
+ * Return true with setting key_p and value_p if there is next key. Otherwise,
+ * return false.
+ */
+bool
+rt_iterate_next(rt_iter *iter, uint64 *key_p, uint64 *value_p)
+{
+ /* Empty tree */
+ if (!iter->tree->root)
+ return false;
+
+ for (;;)
+ {
+ rt_node *child = NULL;
+ uint64 value;
+ int level;
+ bool found;
+
+ /* Advance the leaf node iterator to get next key-value pair */
+ found = rt_node_leaf_iterate_next(iter, &(iter->stack[0]), &value);
+
+ if (found)
+ {
+ *key_p = iter->key;
+ *value_p = value;
+ return true;
+ }
+
+ /*
+ * We've visited all values in the leaf node, so advance inner node
+ * iterators from the level=1 until we find the next child node.
+ */
+ for (level = 1; level <= iter->stack_len; level++)
+ {
+ child = rt_node_inner_iterate_next(iter, &(iter->stack[level]));
+
+ if (child)
+ break;
+ }
+
+ /* the iteration finished */
+ if (!child)
+ return false;
+
+ /*
+ * Set the node to the node iterator and update the iterator stack
+ * from this node.
+ */
+ rt_update_iter_stack(iter, child, level - 1);
+
+ /* Node iterators are updated, so try again from the leaf */
+ }
+
+ return false;
+}
+
+void
+rt_end_iterate(rt_iter *iter)
+{
+ pfree(iter);
+}
+
+static inline void
+rt_iter_update_key(rt_iter *iter, uint8 chunk, uint8 shift)
+{
+ iter->key &= ~(((uint64) RT_CHUNK_MASK) << shift);
+ iter->key |= (((uint64) chunk) << shift);
+}
+
+/*
+ * Advance the slot in the inner node. Return the child if exists, otherwise
+ * null.
+ */
+static inline rt_node *
+rt_node_inner_iterate_next(rt_iter *iter, rt_node_iter *node_iter)
+{
+ rt_node *child = NULL;
+ bool found = false;
+ uint8 key_chunk;
+
+ switch (node_iter->node->kind)
+ {
+ case RT_NODE_KIND_4:
+ {
+ rt_node_inner_4 *n4 = (rt_node_inner_4 *) node_iter->node;
+
+ node_iter->current_idx++;
+ if (node_iter->current_idx >= n4->base.n.count)
+ break;
+
+ child = n4->children[node_iter->current_idx];
+ key_chunk = n4->base.chunks[node_iter->current_idx];
+ found = true;
+ break;
+ }
+ case RT_NODE_KIND_32:
+ {
+ rt_node_inner_32 *n32 = (rt_node_inner_32 *) node_iter->node;
+
+ node_iter->current_idx++;
+ if (node_iter->current_idx >= n32->base.n.count)
+ break;
+
+ child = n32->children[node_iter->current_idx];
+ key_chunk = n32->base.chunks[node_iter->current_idx];
+ found = true;
+ break;
+ }
+ case RT_NODE_KIND_125:
+ {
+ rt_node_inner_125 *n125 = (rt_node_inner_125 *) node_iter->node;
+ int i;
+
+ for (i = node_iter->current_idx + 1; i < RT_NODE_MAX_SLOTS; i++)
+ {
+ if (node_125_is_chunk_used((rt_node_base_125 *) n125, i))
+ break;
+ }
+
+ if (i >= RT_NODE_MAX_SLOTS)
+ break;
+
+ node_iter->current_idx = i;
+ child = node_inner_125_get_child(n125, i);
+ key_chunk = i;
+ found = true;
+ break;
+ }
+ case RT_NODE_KIND_256:
+ {
+ rt_node_inner_256 *n256 = (rt_node_inner_256 *) node_iter->node;
+ int i;
+
+ for (i = node_iter->current_idx + 1; i < RT_NODE_MAX_SLOTS; i++)
+ {
+ if (node_inner_256_is_chunk_used(n256, i))
+ break;
+ }
+
+ if (i >= RT_NODE_MAX_SLOTS)
+ break;
+
+ node_iter->current_idx = i;
+ child = node_inner_256_get_child(n256, i);
+ key_chunk = i;
+ found = true;
+ break;
+ }
+ }
+
+ if (found)
+ rt_iter_update_key(iter, key_chunk, node_iter->node->shift);
+
+ return child;
+}
+
+/*
+ * Advance the slot in the leaf node. On success, return true and the value
+ * is set to value_p, otherwise return false.
+ */
+static inline bool
+rt_node_leaf_iterate_next(rt_iter *iter, rt_node_iter *node_iter,
+ uint64 *value_p)
+{
+ rt_node *node = node_iter->node;
+ bool found = false;
+ uint64 value;
+ uint8 key_chunk;
+
+ switch (node->kind)
+ {
+ case RT_NODE_KIND_4:
+ {
+ rt_node_leaf_4 *n4 = (rt_node_leaf_4 *) node_iter->node;
+
+ node_iter->current_idx++;
+ if (node_iter->current_idx >= n4->base.n.count)
+ break;
+
+ value = n4->values[node_iter->current_idx];
+ key_chunk = n4->base.chunks[node_iter->current_idx];
+ found = true;
+ break;
+ }
+ case RT_NODE_KIND_32:
+ {
+ rt_node_leaf_32 *n32 = (rt_node_leaf_32 *) node_iter->node;
+
+ node_iter->current_idx++;
+ if (node_iter->current_idx >= n32->base.n.count)
+ break;
+
+ value = n32->values[node_iter->current_idx];
+ key_chunk = n32->base.chunks[node_iter->current_idx];
+ found = true;
+ break;
+ }
+ case RT_NODE_KIND_125:
+ {
+ rt_node_leaf_125 *n125 = (rt_node_leaf_125 *) node_iter->node;
+ int i;
+
+ for (i = node_iter->current_idx + 1; i < RT_NODE_MAX_SLOTS; i++)
+ {
+ if (node_125_is_chunk_used((rt_node_base_125 *) n125, i))
+ break;
+ }
+
+ if (i >= RT_NODE_MAX_SLOTS)
+ break;
+
+ node_iter->current_idx = i;
+ value = node_leaf_125_get_value(n125, i);
+ key_chunk = i;
+ found = true;
+ break;
+ }
+ case RT_NODE_KIND_256:
+ {
+ rt_node_leaf_256 *n256 = (rt_node_leaf_256 *) node_iter->node;
+ int i;
+
+ for (i = node_iter->current_idx + 1; i < RT_NODE_MAX_SLOTS; i++)
+ {
+ if (node_leaf_256_is_chunk_used(n256, i))
+ break;
+ }
+
+ if (i >= RT_NODE_MAX_SLOTS)
+ break;
+
+ node_iter->current_idx = i;
+ value = node_leaf_256_get_value(n256, i);
+ key_chunk = i;
+ found = true;
+ break;
+ }
+ }
+
+ if (found)
+ {
+ rt_iter_update_key(iter, key_chunk, node_iter->node->shift);
+ *value_p = value;
+ }
+
+ return found;
+}
+
+/*
+ * Return the number of keys in the radix tree.
+ */
+uint64
+rt_num_entries(radix_tree *tree)
+{
+ return tree->num_keys;
+}
+
+/*
+ * Return the statistics of the amount of memory used by the radix tree.
+ */
+uint64
+rt_memory_usage(radix_tree *tree)
+{
+ Size total = sizeof(radix_tree);
+
+ for (int i = 0; i < RT_SIZE_CLASS_COUNT; i++)
+ {
+ total += MemoryContextMemAllocated(tree->inner_slabs[i], true);
+ total += MemoryContextMemAllocated(tree->leaf_slabs[i], true);
+ }
+
+ return total;
+}
+
+/*
+ * Verify the radix tree node.
+ */
+static void
+rt_verify_node(rt_node *node)
+{
+#ifdef USE_ASSERT_CHECKING
+ Assert(node->count >= 0);
+
+ switch (node->kind)
+ {
+ case RT_NODE_KIND_4:
+ {
+ rt_node_base_4 *n4 = (rt_node_base_4 *) node;
+
+ for (int i = 1; i < n4->n.count; i++)
+ Assert(n4->chunks[i - 1] < n4->chunks[i]);
+
+ break;
+ }
+ case RT_NODE_KIND_32:
+ {
+ rt_node_base_32 *n32 = (rt_node_base_32 *) node;
+
+ for (int i = 1; i < n32->n.count; i++)
+ Assert(n32->chunks[i - 1] < n32->chunks[i]);
+
+ break;
+ }
+ case RT_NODE_KIND_125:
+ {
+ rt_node_base_125 *n125 = (rt_node_base_125 *) node;
+ int cnt = 0;
+
+ for (int i = 0; i < RT_NODE_MAX_SLOTS; i++)
+ {
+ uint8 slot = n125->slot_idxs[i];
+ int bitnum = BM_BIT(slot);
+
+ if (!node_125_is_chunk_used(n125, i))
+ continue;
+
+ /* Check if the corresponding slot is used */
+ Assert(slot < node->fanout);
+ Assert((n125->isset[i] & ((bitmapword) 1 << bitnum)) != 0);
+
+ cnt++;
+ }
+
+ Assert(n125->n.count == cnt);
+ break;
+ }
+ case RT_NODE_KIND_256:
+ {
+ if (NODE_IS_LEAF(node))
+ {
+ rt_node_leaf_256 *n256 = (rt_node_leaf_256 *) node;
+ int cnt = 0;
+
+ for (int i = 0; i < BM_IDX(RT_NODE_MAX_SLOTS); i++)
+ cnt += bmw_popcount(n256->isset[i]);
+
+ /* Check if the number of used chunk matches */
+ Assert(n256->base.n.count == cnt);
+
+ break;
+ }
+ }
+ }
+#endif
+}
+
+/***************** DEBUG FUNCTIONS *****************/
+#ifdef RT_DEBUG
+void
+rt_stats(radix_tree *tree)
+{
+ ereport(NOTICE, (errmsg("num_keys = " UINT64_FORMAT ", height = %d, n4 = %u, n15 = %u, n32 = %u, n125 = %u, n256 = %u",
+ tree->num_keys,
+ tree->root->shift / RT_NODE_SPAN,
+ tree->cnt[RT_CLASS_4_FULL],
+ tree->cnt[RT_CLASS_32_PARTIAL],
+ tree->cnt[RT_CLASS_32_FULL],
+ tree->cnt[RT_CLASS_125_FULL],
+ tree->cnt[RT_CLASS_256])));
+}
+
+static void
+rt_dump_node(rt_node *node, int level, bool recurse)
+{
+ char space[125] = {0};
+
+ fprintf(stderr, "[%s] kind %d, fanout %d, count %u, shift %u, chunk 0x%X:\n",
+ NODE_IS_LEAF(node) ? "LEAF" : "INNR",
+ (node->kind == RT_NODE_KIND_4) ? 4 :
+ (node->kind == RT_NODE_KIND_32) ? 32 :
+ (node->kind == RT_NODE_KIND_125) ? 125 : 256,
+ node->fanout == 0 ? 256 : node->fanout,
+ node->count, node->shift, node->chunk);
+
+ if (level > 0)
+ sprintf(space, "%*c", level * 4, ' ');
+
+ switch (node->kind)
+ {
+ case RT_NODE_KIND_4:
+ {
+ for (int i = 0; i < node->count; i++)
+ {
+ if (NODE_IS_LEAF(node))
+ {
+ rt_node_leaf_4 *n4 = (rt_node_leaf_4 *) node;
+
+ fprintf(stderr, "%schunk 0x%X value 0x" UINT64_FORMAT_HEX "\n",
+ space, n4->base.chunks[i], n4->values[i]);
+ }
+ else
+ {
+ rt_node_inner_4 *n4 = (rt_node_inner_4 *) node;
+
+ fprintf(stderr, "%schunk 0x%X ->",
+ space, n4->base.chunks[i]);
+
+ if (recurse)
+ rt_dump_node(n4->children[i], level + 1, recurse);
+ else
+ fprintf(stderr, "\n");
+ }
+ }
+ break;
+ }
+ case RT_NODE_KIND_32:
+ {
+ for (int i = 0; i < node->count; i++)
+ {
+ if (NODE_IS_LEAF(node))
+ {
+ rt_node_leaf_32 *n32 = (rt_node_leaf_32 *) node;
+
+ fprintf(stderr, "%schunk 0x%X value 0x" UINT64_FORMAT_HEX "\n",
+ space, n32->base.chunks[i], n32->values[i]);
+ }
+ else
+ {
+ rt_node_inner_32 *n32 = (rt_node_inner_32 *) node;
+
+ fprintf(stderr, "%schunk 0x%X ->",
+ space, n32->base.chunks[i]);
+
+ if (recurse)
+ {
+ rt_dump_node(n32->children[i], level + 1, recurse);
+ }
+ else
+ fprintf(stderr, "\n");
+ }
+ }
+ break;
+ }
+ case RT_NODE_KIND_125:
+ {
+ rt_node_base_125 *b125 = (rt_node_base_125 *) node;
+
+ fprintf(stderr, "slot_idxs ");
+ for (int i = 0; i < RT_NODE_MAX_SLOTS; i++)
+ {
+ if (!node_125_is_chunk_used(b125, i))
+ continue;
+
+ fprintf(stderr, " [%d]=%d, ", i, b125->slot_idxs[i]);
+ }
+ if (NODE_IS_LEAF(node))
+ {
+ rt_node_leaf_125 *n = (rt_node_leaf_125 *) node;
+
+ fprintf(stderr, ", isset-bitmap:");
+ for (int i = 0; i < BM_IDX(128); i++)
+ {
+ fprintf(stderr, UINT64_FORMAT_HEX " ", (uint64) n->base.isset[i]);
+ }
+ fprintf(stderr, "\n");
+ }
+
+ for (int i = 0; i < RT_NODE_MAX_SLOTS; i++)
+ {
+ if (!node_125_is_chunk_used(b125, i))
+ continue;
+
+ if (NODE_IS_LEAF(node))
+ {
+ rt_node_leaf_125 *n125 = (rt_node_leaf_125 *) b125;
+
+ fprintf(stderr, "%schunk 0x%X value 0x" UINT64_FORMAT_HEX "\n",
+ space, i, node_leaf_125_get_value(n125, i));
+ }
+ else
+ {
+ rt_node_inner_125 *n125 = (rt_node_inner_125 *) b125;
+
+ fprintf(stderr, "%schunk 0x%X ->",
+ space, i);
+
+ if (recurse)
+ rt_dump_node(node_inner_125_get_child(n125, i),
+ level + 1, recurse);
+ else
+ fprintf(stderr, "\n");
+ }
+ }
+ break;
+ }
+ case RT_NODE_KIND_256:
+ {
+ for (int i = 0; i < RT_NODE_MAX_SLOTS; i++)
+ {
+ if (NODE_IS_LEAF(node))
+ {
+ rt_node_leaf_256 *n256 = (rt_node_leaf_256 *) node;
+
+ if (!node_leaf_256_is_chunk_used(n256, i))
+ continue;
+
+ fprintf(stderr, "%schunk 0x%X value 0x" UINT64_FORMAT_HEX "\n",
+ space, i, node_leaf_256_get_value(n256, i));
+ }
+ else
+ {
+ rt_node_inner_256 *n256 = (rt_node_inner_256 *) node;
+
+ if (!node_inner_256_is_chunk_used(n256, i))
+ continue;
+
+ fprintf(stderr, "%schunk 0x%X ->",
+ space, i);
+
+ if (recurse)
+ rt_dump_node(node_inner_256_get_child(n256, i), level + 1,
+ recurse);
+ else
+ fprintf(stderr, "\n");
+ }
+ }
+ break;
+ }
+ }
+}
+
+void
+rt_dump_search(radix_tree *tree, uint64 key)
+{
+ rt_node *node;
+ int shift;
+ int level = 0;
+
+ elog(NOTICE, "-----------------------------------------------------------");
+ elog(NOTICE, "max_val = " UINT64_FORMAT "(0x" UINT64_FORMAT_HEX ")",
+ tree->max_val, tree->max_val);
+
+ if (!tree->root)
+ {
+ elog(NOTICE, "tree is empty");
+ return;
+ }
+
+ if (key > tree->max_val)
+ {
+ elog(NOTICE, "key " UINT64_FORMAT "(0x" UINT64_FORMAT_HEX ") is larger than max val",
+ key, key);
+ return;
+ }
+
+ node = tree->root;
+ shift = tree->root->shift;
+ while (shift >= 0)
+ {
+ rt_node *child;
+
+ rt_dump_node(node, level, false);
+
+ if (NODE_IS_LEAF(node))
+ {
+ uint64 dummy;
+
+ /* We reached at a leaf node, find the corresponding slot */
+ rt_node_search_leaf(node, key, RT_ACTION_FIND, &dummy);
+
+ break;
+ }
+
+ if (!rt_node_search_inner(node, key, RT_ACTION_FIND, &child))
+ break;
+
+ node = child;
+ shift -= RT_NODE_SPAN;
+ level++;
+ }
+}
+
+void
+rt_dump(radix_tree *tree)
+{
+
+ for (int i = 0; i < RT_SIZE_CLASS_COUNT; i++)
+ fprintf(stderr, "%s\tinner_size %zu\tinner_blocksize %zu\tleaf_size %zu\tleaf_blocksize %zu\n",
+ rt_size_class_info[i].name,
+ rt_size_class_info[i].inner_size,
+ rt_size_class_info[i].inner_blocksize,
+ rt_size_class_info[i].leaf_size,
+ rt_size_class_info[i].leaf_blocksize);
+ fprintf(stderr, "max_val = " UINT64_FORMAT "\n", tree->max_val);
+
+ if (!tree->root)
+ {
+ fprintf(stderr, "empty tree\n");
+ return;
+ }
+
+ rt_dump_node(tree->root, 0, true);
+}
+#endif
diff --git a/src/include/lib/radixtree.h b/src/include/lib/radixtree.h
new file mode 100644
index 0000000000..d5d7668617
--- /dev/null
+++ b/src/include/lib/radixtree.h
@@ -0,0 +1,42 @@
+/*-------------------------------------------------------------------------
+ *
+ * radixtree.h
+ * Interface for radix tree.
+ *
+ * Copyright (c) 2022, PostgreSQL Global Development Group
+ *
+ * IDENTIFICATION
+ * src/include/lib/radixtree.h
+ *
+ *-------------------------------------------------------------------------
+ */
+#ifndef RADIXTREE_H
+#define RADIXTREE_H
+
+#include "postgres.h"
+
+#define RT_DEBUG 1
+
+typedef struct radix_tree radix_tree;
+typedef struct rt_iter rt_iter;
+
+extern radix_tree *rt_create(MemoryContext ctx);
+extern void rt_free(radix_tree *tree);
+extern bool rt_search(radix_tree *tree, uint64 key, uint64 *val_p);
+extern bool rt_set(radix_tree *tree, uint64 key, uint64 val);
+extern rt_iter *rt_begin_iterate(radix_tree *tree);
+
+extern bool rt_iterate_next(rt_iter *iter, uint64 *key_p, uint64 *value_p);
+extern void rt_end_iterate(rt_iter *iter);
+extern bool rt_delete(radix_tree *tree, uint64 key);
+
+extern uint64 rt_memory_usage(radix_tree *tree);
+extern uint64 rt_num_entries(radix_tree *tree);
+
+#ifdef RT_DEBUG
+extern void rt_dump(radix_tree *tree);
+extern void rt_dump_search(radix_tree *tree, uint64 key);
+extern void rt_stats(radix_tree *tree);
+#endif
+
+#endif /* RADIXTREE_H */
diff --git a/src/test/modules/Makefile b/src/test/modules/Makefile
index c629cbe383..9659eb85d7 100644
--- a/src/test/modules/Makefile
+++ b/src/test/modules/Makefile
@@ -28,6 +28,7 @@ SUBDIRS = \
test_pg_db_role_setting \
test_pg_dump \
test_predtest \
+ test_radixtree \
test_rbtree \
test_regex \
test_rls_hooks \
diff --git a/src/test/modules/meson.build b/src/test/modules/meson.build
index 1baa6b558d..232cbdac80 100644
--- a/src/test/modules/meson.build
+++ b/src/test/modules/meson.build
@@ -24,6 +24,7 @@ subdir('test_parser')
subdir('test_pg_db_role_setting')
subdir('test_pg_dump')
subdir('test_predtest')
+subdir('test_radixtree')
subdir('test_rbtree')
subdir('test_regex')
subdir('test_rls_hooks')
diff --git a/src/test/modules/test_radixtree/.gitignore b/src/test/modules/test_radixtree/.gitignore
new file mode 100644
index 0000000000..5dcb3ff972
--- /dev/null
+++ b/src/test/modules/test_radixtree/.gitignore
@@ -0,0 +1,4 @@
+# Generated subdirectories
+/log/
+/results/
+/tmp_check/
diff --git a/src/test/modules/test_radixtree/Makefile b/src/test/modules/test_radixtree/Makefile
new file mode 100644
index 0000000000..da06b93da3
--- /dev/null
+++ b/src/test/modules/test_radixtree/Makefile
@@ -0,0 +1,23 @@
+# src/test/modules/test_radixtree/Makefile
+
+MODULE_big = test_radixtree
+OBJS = \
+ $(WIN32RES) \
+ test_radixtree.o
+PGFILEDESC = "test_radixtree - test code for src/backend/lib/radixtree.c"
+
+EXTENSION = test_radixtree
+DATA = test_radixtree--1.0.sql
+
+REGRESS = test_radixtree
+
+ifdef USE_PGXS
+PG_CONFIG = pg_config
+PGXS := $(shell $(PG_CONFIG) --pgxs)
+include $(PGXS)
+else
+subdir = src/test/modules/test_radixtree
+top_builddir = ../../../..
+include $(top_builddir)/src/Makefile.global
+include $(top_srcdir)/contrib/contrib-global.mk
+endif
diff --git a/src/test/modules/test_radixtree/README b/src/test/modules/test_radixtree/README
new file mode 100644
index 0000000000..a8b271869a
--- /dev/null
+++ b/src/test/modules/test_radixtree/README
@@ -0,0 +1,7 @@
+test_integerset contains unit tests for testing the integer set implementation
+in src/backend/lib/integerset.c.
+
+The tests verify the correctness of the implementation, but they can also be
+used as a micro-benchmark. If you set the 'intset_test_stats' flag in
+test_integerset.c, the tests will print extra information about execution time
+and memory usage.
diff --git a/src/test/modules/test_radixtree/expected/test_radixtree.out b/src/test/modules/test_radixtree/expected/test_radixtree.out
new file mode 100644
index 0000000000..ce645cb8b5
--- /dev/null
+++ b/src/test/modules/test_radixtree/expected/test_radixtree.out
@@ -0,0 +1,36 @@
+CREATE EXTENSION test_radixtree;
+--
+-- All the logic is in the test_radixtree() function. It will throw
+-- an error if something fails.
+--
+SELECT test_radixtree();
+NOTICE: testing basic operations with leaf node 4
+NOTICE: testing basic operations with inner node 4
+NOTICE: testing basic operations with leaf node 32
+NOTICE: testing basic operations with inner node 32
+NOTICE: testing basic operations with leaf node 125
+NOTICE: testing basic operations with inner node 125
+NOTICE: testing basic operations with leaf node 256
+NOTICE: testing basic operations with inner node 256
+NOTICE: testing radix tree node types with shift "0"
+NOTICE: testing radix tree node types with shift "8"
+NOTICE: testing radix tree node types with shift "16"
+NOTICE: testing radix tree node types with shift "24"
+NOTICE: testing radix tree node types with shift "32"
+NOTICE: testing radix tree node types with shift "40"
+NOTICE: testing radix tree node types with shift "48"
+NOTICE: testing radix tree node types with shift "56"
+NOTICE: testing radix tree with pattern "all ones"
+NOTICE: testing radix tree with pattern "alternating bits"
+NOTICE: testing radix tree with pattern "clusters of ten"
+NOTICE: testing radix tree with pattern "clusters of hundred"
+NOTICE: testing radix tree with pattern "one-every-64k"
+NOTICE: testing radix tree with pattern "sparse"
+NOTICE: testing radix tree with pattern "single values, distance > 2^32"
+NOTICE: testing radix tree with pattern "clusters, distance > 2^32"
+NOTICE: testing radix tree with pattern "clusters, distance > 2^60"
+ test_radixtree
+----------------
+
+(1 row)
+
diff --git a/src/test/modules/test_radixtree/meson.build b/src/test/modules/test_radixtree/meson.build
new file mode 100644
index 0000000000..f96bf159d6
--- /dev/null
+++ b/src/test/modules/test_radixtree/meson.build
@@ -0,0 +1,34 @@
+# FIXME: prevent install during main install, but not during test :/
+
+test_radixtree_sources = files(
+ 'test_radixtree.c',
+)
+
+if host_system == 'windows'
+ test_radixtree_sources += rc_lib_gen.process(win32ver_rc, extra_args: [
+ '--NAME', 'test_radixtree',
+ '--FILEDESC', 'test_radixtree - test code for src/backend/lib/radixtree.c',])
+endif
+
+test_radixtree = shared_module('test_radixtree',
+ test_radixtree_sources,
+ kwargs: pg_mod_args,
+)
+testprep_targets += test_radixtree
+
+install_data(
+ 'test_radixtree.control',
+ 'test_radixtree--1.0.sql',
+ kwargs: contrib_data_args,
+)
+
+tests += {
+ 'name': 'test_radixtree',
+ 'sd': meson.current_source_dir(),
+ 'bd': meson.current_build_dir(),
+ 'regress': {
+ 'sql': [
+ 'test_radixtree',
+ ],
+ },
+}
diff --git a/src/test/modules/test_radixtree/sql/test_radixtree.sql b/src/test/modules/test_radixtree/sql/test_radixtree.sql
new file mode 100644
index 0000000000..41ece5e9f5
--- /dev/null
+++ b/src/test/modules/test_radixtree/sql/test_radixtree.sql
@@ -0,0 +1,7 @@
+CREATE EXTENSION test_radixtree;
+
+--
+-- All the logic is in the test_radixtree() function. It will throw
+-- an error if something fails.
+--
+SELECT test_radixtree();
diff --git a/src/test/modules/test_radixtree/test_radixtree--1.0.sql b/src/test/modules/test_radixtree/test_radixtree--1.0.sql
new file mode 100644
index 0000000000..074a5a7ea7
--- /dev/null
+++ b/src/test/modules/test_radixtree/test_radixtree--1.0.sql
@@ -0,0 +1,8 @@
+/* src/test/modules/test_radixtree/test_radixtree--1.0.sql */
+
+-- complain if script is sourced in psql, rather than via CREATE EXTENSION
+\echo Use "CREATE EXTENSION test_radixtree" to load this file. \quit
+
+CREATE FUNCTION test_radixtree()
+RETURNS pg_catalog.void STRICT
+AS 'MODULE_PATHNAME' LANGUAGE C;
diff --git a/src/test/modules/test_radixtree/test_radixtree.c b/src/test/modules/test_radixtree/test_radixtree.c
new file mode 100644
index 0000000000..ea993e63df
--- /dev/null
+++ b/src/test/modules/test_radixtree/test_radixtree.c
@@ -0,0 +1,581 @@
+/*--------------------------------------------------------------------------
+ *
+ * test_radixtree.c
+ * Test radixtree set data structure.
+ *
+ * Copyright (c) 2022, PostgreSQL Global Development Group
+ *
+ * IDENTIFICATION
+ * src/test/modules/test_radixtree/test_radixtree.c
+ *
+ * -------------------------------------------------------------------------
+ */
+#include "postgres.h"
+
+#include "common/pg_prng.h"
+#include "fmgr.h"
+#include "lib/radixtree.h"
+#include "miscadmin.h"
+#include "nodes/bitmapset.h"
+#include "storage/block.h"
+#include "storage/itemptr.h"
+#include "utils/memutils.h"
+#include "utils/timestamp.h"
+
+#define UINT64_HEX_FORMAT "%" INT64_MODIFIER "X"
+
+/*
+ * If you enable this, the "pattern" tests will print information about
+ * how long populating, probing, and iterating the test set takes, and
+ * how much memory the test set consumed. That can be used as
+ * micro-benchmark of various operations and input patterns (you might
+ * want to increase the number of values used in each of the test, if
+ * you do that, to reduce noise).
+ *
+ * The information is printed to the server's stderr, mostly because
+ * that's where MemoryContextStats() output goes.
+ */
+static const bool rt_test_stats = false;
+
+static int rt_node_kind_fanouts[] = {
+ 0,
+ 4, /* RT_NODE_KIND_4 */
+ 32, /* RT_NODE_KIND_32 */
+ 125, /* RT_NODE_KIND_125 */
+ 256 /* RT_NODE_KIND_256 */
+};
+/*
+ * A struct to define a pattern of integers, for use with the test_pattern()
+ * function.
+ */
+typedef struct
+{
+ char *test_name; /* short name of the test, for humans */
+ char *pattern_str; /* a bit pattern */
+ uint64 spacing; /* pattern repeats at this interval */
+ uint64 num_values; /* number of integers to set in total */
+} test_spec;
+
+/* Test patterns borrowed from test_integerset.c */
+static const test_spec test_specs[] = {
+ {
+ "all ones", "1111111111",
+ 10, 1000000
+ },
+ {
+ "alternating bits", "0101010101",
+ 10, 1000000
+ },
+ {
+ "clusters of ten", "1111111111",
+ 10000, 1000000
+ },
+ {
+ "clusters of hundred",
+ "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111",
+ 10000, 1000000
+ },
+ {
+ "one-every-64k", "1",
+ 65536, 1000000
+ },
+ {
+ "sparse", "100000000000000000000000000000001",
+ 10000000, 1000000
+ },
+ {
+ "single values, distance > 2^32", "1",
+ UINT64CONST(10000000000), 100000
+ },
+ {
+ "clusters, distance > 2^32", "10101010",
+ UINT64CONST(10000000000), 1000000
+ },
+ {
+ "clusters, distance > 2^60", "10101010",
+ UINT64CONST(2000000000000000000),
+ 23 /* can't be much higher than this, or we
+ * overflow uint64 */
+ }
+};
+
+PG_MODULE_MAGIC;
+
+PG_FUNCTION_INFO_V1(test_radixtree);
+
+static void
+test_empty(void)
+{
+ radix_tree *radixtree;
+ rt_iter *iter;
+ uint64 dummy;
+ uint64 key;
+ uint64 val;
+
+ radixtree = rt_create(CurrentMemoryContext);
+
+ if (rt_search(radixtree, 0, &dummy))
+ elog(ERROR, "rt_search on empty tree returned true");
+
+ if (rt_search(radixtree, 1, &dummy))
+ elog(ERROR, "rt_search on empty tree returned true");
+
+ if (rt_search(radixtree, PG_UINT64_MAX, &dummy))
+ elog(ERROR, "rt_search on empty tree returned true");
+
+ if (rt_delete(radixtree, 0))
+ elog(ERROR, "rt_delete on empty tree returned true");
+
+ if (rt_num_entries(radixtree) != 0)
+ elog(ERROR, "rt_num_entries on empty tree return non-zero");
+
+ iter = rt_begin_iterate(radixtree);
+
+ if (rt_iterate_next(iter, &key, &val))
+ elog(ERROR, "rt_itereate_next on empty tree returned true");
+
+ rt_end_iterate(iter);
+
+ rt_free(radixtree);
+}
+
+static void
+test_basic(int children, bool test_inner)
+{
+ radix_tree *radixtree;
+ uint64 *keys;
+ int shift = test_inner ? 8 : 0;
+
+ elog(NOTICE, "testing basic operations with %s node %d",
+ test_inner ? "inner" : "leaf", children);
+
+ radixtree = rt_create(CurrentMemoryContext);
+
+ /* prepare keys in order like 1, 32, 2, 31, 2, ... */
+ keys = palloc(sizeof(uint64) * children);
+ for (int i = 0; i < children; i++)
+ {
+ if (i % 2 == 0)
+ keys[i] = (uint64) ((i / 2) + 1) << shift;
+ else
+ keys[i] = (uint64) (children - (i / 2)) << shift;
+ }
+
+ /* insert keys */
+ for (int i = 0; i < children; i++)
+ {
+ if (rt_set(radixtree, keys[i], keys[i]))
+ elog(ERROR, "new inserted key 0x" UINT64_HEX_FORMAT " is found ", keys[i]);
+ }
+
+ /* update keys */
+ for (int i = 0; i < children; i++)
+ {
+ if (!rt_set(radixtree, keys[i], keys[i] + 1))
+ elog(ERROR, "could not update key 0x" UINT64_HEX_FORMAT, keys[i]);
+ }
+
+ /* repeat deleting and inserting keys */
+ for (int i = 0; i < children; i++)
+ {
+ if (!rt_delete(radixtree, keys[i]))
+ elog(ERROR, "could not delete key 0x" UINT64_HEX_FORMAT, keys[i]);
+ if (rt_set(radixtree, keys[i], keys[i]))
+ elog(ERROR, "new inserted key 0x" UINT64_HEX_FORMAT " is found ", keys[i]);
+ }
+
+ pfree(keys);
+ rt_free(radixtree);
+}
+
+/*
+ * Check if keys from start to end with the shift exist in the tree.
+ */
+static void
+check_search_on_node(radix_tree *radixtree, uint8 shift, int start, int end,
+ int incr)
+{
+ for (int i = start; i < end; i++)
+ {
+ uint64 key = ((uint64) i << shift);
+ uint64 val;
+
+ if (!rt_search(radixtree, key, &val))
+ elog(ERROR, "key 0x" UINT64_HEX_FORMAT " is not found on node-%d",
+ key, end);
+ if (val != key)
+ elog(ERROR, "rt_search with key 0x" UINT64_HEX_FORMAT " returns 0x" UINT64_HEX_FORMAT ", expected 0x" UINT64_HEX_FORMAT,
+ key, val, key);
+ }
+}
+
+static void
+test_node_types_insert(radix_tree *radixtree, uint8 shift, bool insert_asc)
+{
+ uint64 num_entries;
+ int ninserted = 0;
+ int start = insert_asc ? 0 : 256;
+ int incr = insert_asc ? 1 : -1;
+ int end = insert_asc ? 256 : 0;
+ int node_kind_idx = 1;
+
+ for (int i = start; i != end; i += incr)
+ {
+ uint64 key = ((uint64) i << shift);
+ bool found;
+
+ found = rt_set(radixtree, key, key);
+ if (found)
+ elog(ERROR, "newly inserted key 0x" UINT64_HEX_FORMAT " is found", key);
+
+ /*
+ * After filling all slots in each node type, check if the values
+ * are stored properly.
+ */
+ if (ninserted == rt_node_kind_fanouts[node_kind_idx] - 1)
+ {
+ int check_start = insert_asc
+ ? rt_node_kind_fanouts[node_kind_idx - 1]
+ : rt_node_kind_fanouts[node_kind_idx];
+ int check_end = insert_asc
+ ? rt_node_kind_fanouts[node_kind_idx]
+ : rt_node_kind_fanouts[node_kind_idx - 1];
+
+ check_search_on_node(radixtree, shift, check_start, check_end, incr);
+ node_kind_idx++;
+ }
+
+ ninserted++;
+ }
+
+ num_entries = rt_num_entries(radixtree);
+
+ if (num_entries != 256)
+ elog(ERROR,
+ "rt_num_entries returned " UINT64_FORMAT ", expected " UINT64_FORMAT,
+ num_entries, UINT64CONST(256));
+}
+
+static void
+test_node_types_delete(radix_tree *radixtree, uint8 shift)
+{
+ uint64 num_entries;
+
+ for (int i = 0; i < 256; i++)
+ {
+ uint64 key = ((uint64) i << shift);
+ bool found;
+
+ found = rt_delete(radixtree, key);
+
+ if (!found)
+ elog(ERROR, "could not delete key 0x" UINT64_HEX_FORMAT, key);
+ }
+
+ num_entries = rt_num_entries(radixtree);
+
+ /* The tree must be empty */
+ if (num_entries != 0)
+ elog(ERROR,
+ "rt_num_entries returned " UINT64_FORMAT ", expected " UINT64_FORMAT,
+ num_entries, UINT64CONST(256));
+}
+
+/*
+ * Test for inserting and deleting key-value pairs to each node type at the given shift
+ * level.
+ */
+static void
+test_node_types(uint8 shift)
+{
+ radix_tree *radixtree;
+
+ elog(NOTICE, "testing radix tree node types with shift \"%d\"", shift);
+
+ radixtree = rt_create(CurrentMemoryContext);
+
+ /*
+ * Insert and search entries for every node type at the 'shift' level,
+ * then delete all entries to make it empty, and insert and search entries
+ * again.
+ */
+ test_node_types_insert(radixtree, shift, true);
+ test_node_types_delete(radixtree, shift);
+ test_node_types_insert(radixtree, shift, false);
+
+ rt_free(radixtree);
+}
+
+/*
+ * Test with a repeating pattern, defined by the 'spec'.
+ */
+static void
+test_pattern(const test_spec * spec)
+{
+ radix_tree *radixtree;
+ rt_iter *iter;
+ MemoryContext radixtree_ctx;
+ TimestampTz starttime;
+ TimestampTz endtime;
+ uint64 n;
+ uint64 last_int;
+ uint64 ndeleted;
+ uint64 nbefore;
+ uint64 nafter;
+ int patternlen;
+ uint64 *pattern_values;
+ uint64 pattern_num_values;
+
+ elog(NOTICE, "testing radix tree with pattern \"%s\"", spec->test_name);
+ if (rt_test_stats)
+ fprintf(stderr, "-----\ntesting radix tree with pattern \"%s\"\n", spec->test_name);
+
+ /* Pre-process the pattern, creating an array of integers from it. */
+ patternlen = strlen(spec->pattern_str);
+ pattern_values = palloc(patternlen * sizeof(uint64));
+ pattern_num_values = 0;
+ for (int i = 0; i < patternlen; i++)
+ {
+ if (spec->pattern_str[i] == '1')
+ pattern_values[pattern_num_values++] = i;
+ }
+
+ /*
+ * Allocate the radix tree.
+ *
+ * Allocate it in a separate memory context, so that we can print its
+ * memory usage easily.
+ */
+ radixtree_ctx = AllocSetContextCreate(CurrentMemoryContext,
+ "radixtree test",
+ ALLOCSET_SMALL_SIZES);
+ MemoryContextSetIdentifier(radixtree_ctx, spec->test_name);
+ radixtree = rt_create(radixtree_ctx);
+
+ /*
+ * Add values to the set.
+ */
+ starttime = GetCurrentTimestamp();
+
+ n = 0;
+ last_int = 0;
+ while (n < spec->num_values)
+ {
+ uint64 x = 0;
+
+ for (int i = 0; i < pattern_num_values && n < spec->num_values; i++)
+ {
+ bool found;
+
+ x = last_int + pattern_values[i];
+
+ found = rt_set(radixtree, x, x);
+
+ if (found)
+ elog(ERROR, "newly inserted key 0x" UINT64_HEX_FORMAT " found", x);
+
+ n++;
+ }
+ last_int += spec->spacing;
+ }
+
+ endtime = GetCurrentTimestamp();
+
+ if (rt_test_stats)
+ fprintf(stderr, "added " UINT64_FORMAT " values in %d ms\n",
+ spec->num_values, (int) (endtime - starttime) / 1000);
+
+ /*
+ * Print stats on the amount of memory used.
+ *
+ * We print the usage reported by rt_memory_usage(), as well as the stats
+ * from the memory context. They should be in the same ballpark, but it's
+ * hard to automate testing that, so if you're making changes to the
+ * implementation, just observe that manually.
+ */
+ if (rt_test_stats)
+ {
+ uint64 mem_usage;
+
+ /*
+ * Also print memory usage as reported by rt_memory_usage(). It
+ * should be in the same ballpark as the usage reported by
+ * MemoryContextStats().
+ */
+ mem_usage = rt_memory_usage(radixtree);
+ fprintf(stderr, "rt_memory_usage() reported " UINT64_FORMAT " (%0.2f bytes / integer)\n",
+ mem_usage, (double) mem_usage / spec->num_values);
+
+ MemoryContextStats(radixtree_ctx);
+ }
+
+ /* Check that rt_num_entries works */
+ n = rt_num_entries(radixtree);
+ if (n != spec->num_values)
+ elog(ERROR, "rt_num_entries returned " UINT64_FORMAT ", expected " UINT64_FORMAT, n, spec->num_values);
+
+ /*
+ * Test random-access probes with rt_search()
+ */
+ starttime = GetCurrentTimestamp();
+
+ for (n = 0; n < 100000; n++)
+ {
+ bool found;
+ bool expected;
+ uint64 x;
+ uint64 v;
+
+ /*
+ * Pick next value to probe at random. We limit the probes to the
+ * last integer that we added to the set, plus an arbitrary constant
+ * (1000). There's no point in probing the whole 0 - 2^64 range, if
+ * only a small part of the integer space is used. We would very
+ * rarely hit values that are actually in the set.
+ */
+ x = pg_prng_uint64_range(&pg_global_prng_state, 0, last_int + 1000);
+
+ /* Do we expect this value to be present in the set? */
+ if (x >= last_int)
+ expected = false;
+ else
+ {
+ uint64 idx = x % spec->spacing;
+
+ if (idx >= patternlen)
+ expected = false;
+ else if (spec->pattern_str[idx] == '1')
+ expected = true;
+ else
+ expected = false;
+ }
+
+ /* Is it present according to rt_search() ? */
+ found = rt_search(radixtree, x, &v);
+
+ if (found != expected)
+ elog(ERROR, "mismatch at 0x" UINT64_HEX_FORMAT ": %d vs %d", x, found, expected);
+ if (found && (v != x))
+ elog(ERROR, "found 0x" UINT64_HEX_FORMAT ", expected 0x" UINT64_HEX_FORMAT,
+ v, x);
+ }
+ endtime = GetCurrentTimestamp();
+ if (rt_test_stats)
+ fprintf(stderr, "probed " UINT64_FORMAT " values in %d ms\n",
+ n, (int) (endtime - starttime) / 1000);
+
+ /*
+ * Test iterator
+ */
+ starttime = GetCurrentTimestamp();
+
+ iter = rt_begin_iterate(radixtree);
+ n = 0;
+ last_int = 0;
+ while (n < spec->num_values)
+ {
+ for (int i = 0; i < pattern_num_values && n < spec->num_values; i++)
+ {
+ uint64 expected = last_int + pattern_values[i];
+ uint64 x;
+ uint64 val;
+
+ if (!rt_iterate_next(iter, &x, &val))
+ break;
+
+ if (x != expected)
+ elog(ERROR,
+ "iterate returned wrong key; got 0x" UINT64_HEX_FORMAT ", expected 0x" UINT64_HEX_FORMAT " at %d",
+ x, expected, i);
+ if (val != expected)
+ elog(ERROR,
+ "iterate returned wrong value; got 0x" UINT64_HEX_FORMAT ", expected 0x" UINT64_HEX_FORMAT " at %d", x, expected, i);
+ n++;
+ }
+ last_int += spec->spacing;
+ }
+ endtime = GetCurrentTimestamp();
+ if (rt_test_stats)
+ fprintf(stderr, "iterated " UINT64_FORMAT " values in %d ms\n",
+ n, (int) (endtime - starttime) / 1000);
+
+ rt_end_iterate(iter);
+
+ if (n < spec->num_values)
+ elog(ERROR, "iterator stopped short after " UINT64_FORMAT " entries, expected " UINT64_FORMAT, n, spec->num_values);
+ if (n > spec->num_values)
+ elog(ERROR, "iterator returned " UINT64_FORMAT " entries, " UINT64_FORMAT " was expected", n, spec->num_values);
+
+ /*
+ * Test random-access probes with rt_delete()
+ */
+ starttime = GetCurrentTimestamp();
+
+ nbefore = rt_num_entries(radixtree);
+ ndeleted = 0;
+ for (n = 0; n < 1; n++)
+ {
+ bool found;
+ uint64 x;
+ uint64 v;
+
+ /*
+ * Pick next value to probe at random. We limit the probes to the
+ * last integer that we added to the set, plus an arbitrary constant
+ * (1000). There's no point in probing the whole 0 - 2^64 range, if
+ * only a small part of the integer space is used. We would very
+ * rarely hit values that are actually in the set.
+ */
+ x = pg_prng_uint64_range(&pg_global_prng_state, 0, last_int + 1000);
+
+ /* Is it present according to rt_search() ? */
+ found = rt_search(radixtree, x, &v);
+
+ if (!found)
+ continue;
+
+ /* If the key is found, delete it and check again */
+ if (!rt_delete(radixtree, x))
+ elog(ERROR, "could not delete key 0x" UINT64_HEX_FORMAT, x);
+ if (rt_search(radixtree, x, &v))
+ elog(ERROR, "found deleted key 0x" UINT64_HEX_FORMAT, x);
+ if (rt_delete(radixtree, x))
+ elog(ERROR, "deleted already-deleted key 0x" UINT64_HEX_FORMAT, x);
+
+ ndeleted++;
+ }
+ endtime = GetCurrentTimestamp();
+ if (rt_test_stats)
+ fprintf(stderr, "deleted " UINT64_FORMAT " values in %d ms\n",
+ ndeleted, (int) (endtime - starttime) / 1000);
+
+ nafter = rt_num_entries(radixtree);
+
+ /* Check that rt_num_entries works */
+ if ((nbefore - ndeleted) != nafter)
+ elog(ERROR, "rt_num_entries returned " UINT64_FORMAT ", expected " UINT64_FORMAT "after " UINT64_FORMAT " deletion",
+ nafter, (nbefore - ndeleted), ndeleted);
+
+ MemoryContextDelete(radixtree_ctx);
+}
+
+Datum
+test_radixtree(PG_FUNCTION_ARGS)
+{
+ test_empty();
+
+ for (int i = 1; i < lengthof(rt_node_kind_fanouts); i++)
+ {
+ test_basic(rt_node_kind_fanouts[i], false);
+ test_basic(rt_node_kind_fanouts[i], true);
+ }
+
+ for (int shift = 0; shift <= (64 - 8); shift += 8)
+ test_node_types(shift);
+
+ /* Test different test patterns, with lots of entries */
+ for (int i = 0; i < lengthof(test_specs); i++)
+ test_pattern(&test_specs[i]);
+
+ PG_RETURN_VOID();
+}
diff --git a/src/test/modules/test_radixtree/test_radixtree.control b/src/test/modules/test_radixtree/test_radixtree.control
new file mode 100644
index 0000000000..e53f2a3e0c
--- /dev/null
+++ b/src/test/modules/test_radixtree/test_radixtree.control
@@ -0,0 +1,4 @@
+comment = 'Test code for radix tree'
+default_version = '1.0'
+module_pathname = '$libdir/test_radixtree'
+relocatable = true
--
2.39.0