xref: /freebsd/sys/contrib/openzfs/include/sys/btree.h (revision 271171e0)
1 /*
2  * CDDL HEADER START
3  *
4  * This file and its contents are supplied under the terms of the
5  * Common Development and Distribution License ("CDDL"), version 1.0.
6  * You may only use this file in accordance with the terms of version
7  * 1.0 of the CDDL.
8  *
9  * A full copy of the text of the CDDL should have accompanied this
10  * source.  A copy of the CDDL is also available via the Internet at
11  * http://www.illumos.org/license/CDDL.
12  *
13  * CDDL HEADER END
14  */
15 /*
16  * Copyright (c) 2019 by Delphix. All rights reserved.
17  */
18 
19 #ifndef	_BTREE_H
20 #define	_BTREE_H
21 
22 #ifdef	__cplusplus
23 extern "C" {
24 #endif
25 
26 #include	<sys/zfs_context.h>
27 
28 /*
29  * This file defines the interface for a B-Tree implementation for ZFS. The
30  * tree can be used to store arbitrary sortable data types with low overhead
31  * and good operation performance. In addition the tree intelligently
32  * optimizes bulk in-order insertions to improve memory use and performance.
33  *
34  * Note that for all B-Tree functions, the values returned are pointers to the
35  * internal copies of the data in the tree. The internal data can only be
36  * safely mutated if the changes cannot change the ordering of the element
37  * with respect to any other elements in the tree.
38  *
39  * The major drawback of the B-Tree is that any returned elements or indexes
40  * are only valid until a side-effectful operation occurs, since these can
41  * result in reallocation or relocation of data. Side effectful operations are
42  * defined as insertion, removal, and zfs_btree_destroy_nodes.
43  *
44  * The B-Tree has two types of nodes: core nodes, and leaf nodes. Core
45  * nodes have an array of children pointing to other nodes, and an array of
46  * elements that act as separators between the elements of the subtrees rooted
47  * at its children. Leaf nodes only contain data elements, and form the bottom
48  * layer of the tree. Unlike B+ Trees, in this B-Tree implementation the
49  * elements in the core nodes are not copies of or references to leaf node
50  * elements.  Each element occurs only once in the tree, no matter what kind
51  * of node it is in.
52  *
53  * The tree's height is the same throughout, unlike many other forms of search
54  * tree. Each node (except for the root) must be between half minus one and
55  * completely full of elements (and children) at all times. Any operation that
56  * would put the node outside of that range results in a rebalancing operation
57  * (taking, merging, or splitting).
58  *
59  * This tree was implemented using descriptions from Wikipedia's articles on
60  * B-Trees and B+ Trees.
61  */
62 
63 /*
64  * Decreasing these values results in smaller memmove operations, but more of
65  * them, and increased memory overhead. Increasing these values results in
66  * higher variance in operation time, and reduces memory overhead.
67  */
68 #define	BTREE_CORE_ELEMS	128
69 #define	BTREE_LEAF_SIZE		4096
70 
71 extern kmem_cache_t *zfs_btree_leaf_cache;
72 
73 typedef struct zfs_btree_hdr {
74 	struct zfs_btree_core	*bth_parent;
75 	/*
76 	 * Set to -1 to indicate core nodes. Other values represent first
77 	 * valid element offset for leaf nodes.
78 	 */
79 	uint32_t		bth_first;
80 	/*
81 	 * For both leaf and core nodes, represents the number of elements in
82 	 * the node. For core nodes, they will have bth_count + 1 children.
83 	 */
84 	uint32_t		bth_count;
85 } zfs_btree_hdr_t;
86 
87 typedef struct zfs_btree_core {
88 	zfs_btree_hdr_t	btc_hdr;
89 	zfs_btree_hdr_t	*btc_children[BTREE_CORE_ELEMS + 1];
90 	uint8_t		btc_elems[];
91 } zfs_btree_core_t;
92 
93 typedef struct zfs_btree_leaf {
94 	zfs_btree_hdr_t	btl_hdr;
95 	uint8_t		btl_elems[];
96 } zfs_btree_leaf_t;
97 
98 #define	BTREE_LEAF_ESIZE	(BTREE_LEAF_SIZE - \
99     offsetof(zfs_btree_leaf_t, btl_elems))
100 
101 typedef struct zfs_btree_index {
102 	zfs_btree_hdr_t	*bti_node;
103 	uint32_t	bti_offset;
104 	/*
105 	 * True if the location is before the list offset, false if it's at
106 	 * the listed offset.
107 	 */
108 	boolean_t	bti_before;
109 } zfs_btree_index_t;
110 
111 typedef struct btree {
112 	zfs_btree_hdr_t		*bt_root;
113 	int64_t			bt_height;
114 	size_t			bt_elem_size;
115 	uint32_t		bt_leaf_cap;
116 	uint64_t		bt_num_elems;
117 	uint64_t		bt_num_nodes;
118 	zfs_btree_leaf_t	*bt_bulk; // non-null if bulk loading
119 	int (*bt_compar) (const void *, const void *);
120 } zfs_btree_t;
121 
122 /*
123  * Allocate and deallocate caches for btree nodes.
124  */
125 void zfs_btree_init(void);
126 void zfs_btree_fini(void);
127 
128 /*
129  * Initialize an B-Tree. Arguments are:
130  *
131  * tree   - the tree to be initialized
132  * compar - function to compare two nodes, it must return exactly: -1, 0, or +1
133  *          -1 for <, 0 for ==, and +1 for >
134  * size   - the value of sizeof(struct my_type)
135  */
136 void zfs_btree_create(zfs_btree_t *, int (*) (const void *, const void *),
137     size_t);
138 
139 /*
140  * Find a node with a matching value in the tree. Returns the matching node
141  * found. If not found, it returns NULL and then if "where" is not NULL it sets
142  * "where" for use with zfs_btree_add_idx() or zfs_btree_nearest().
143  *
144  * node   - node that has the value being looked for
145  * where  - position for use with zfs_btree_nearest() or zfs_btree_add_idx(),
146  *          may be NULL
147  */
148 void *zfs_btree_find(zfs_btree_t *, const void *, zfs_btree_index_t *);
149 
150 /*
151  * Insert a node into the tree.
152  *
153  * node   - the node to insert
154  * where  - position as returned from zfs_btree_find()
155  */
156 void zfs_btree_add_idx(zfs_btree_t *, const void *, const zfs_btree_index_t *);
157 
158 /*
159  * Return the first or last valued node in the tree. Will return NULL if the
160  * tree is empty. The index can be NULL if the location of the first or last
161  * element isn't required.
162  */
163 void *zfs_btree_first(zfs_btree_t *, zfs_btree_index_t *);
164 void *zfs_btree_last(zfs_btree_t *, zfs_btree_index_t *);
165 
166 /*
167  * Return the next or previous valued node in the tree. The second index can
168  * safely be NULL, if the location of the next or previous value isn't
169  * required.
170  */
171 void *zfs_btree_next(zfs_btree_t *, const zfs_btree_index_t *,
172     zfs_btree_index_t *);
173 void *zfs_btree_prev(zfs_btree_t *, const zfs_btree_index_t *,
174     zfs_btree_index_t *);
175 
176 /*
177  * Get a value from a tree and an index.
178  */
179 void *zfs_btree_get(zfs_btree_t *, zfs_btree_index_t *);
180 
181 /*
182  * Add a single value to the tree. The value must not compare equal to any
183  * other node already in the tree. Note that the value will be copied out, not
184  * inserted directly. It is safe to free or destroy the value once this
185  * function returns.
186  */
187 void zfs_btree_add(zfs_btree_t *, const void *);
188 
189 /*
190  * Remove a single value from the tree.  The value must be in the tree. The
191  * pointer passed in may be a pointer into a tree-controlled buffer, but it
192  * need not be.
193  */
194 void zfs_btree_remove(zfs_btree_t *, const void *);
195 
196 /*
197  * Remove the value at the given location from the tree.
198  */
199 void zfs_btree_remove_idx(zfs_btree_t *, zfs_btree_index_t *);
200 
201 /*
202  * Return the number of nodes in the tree
203  */
204 ulong_t zfs_btree_numnodes(zfs_btree_t *);
205 
206 /*
207  * Used to destroy any remaining nodes in a tree. The cookie argument should
208  * be initialized to NULL before the first call. Returns a node that has been
209  * removed from the tree and may be free()'d. Returns NULL when the tree is
210  * empty.
211  *
212  * Once you call zfs_btree_destroy_nodes(), you can only continuing calling it
213  * and finally zfs_btree_destroy(). No other B-Tree routines will be valid.
214  *
215  * cookie - an index used to save state between calls to
216  * zfs_btree_destroy_nodes()
217  *
218  * EXAMPLE:
219  *	zfs_btree_t *tree;
220  *	struct my_data *node;
221  *	zfs_btree_index_t *cookie;
222  *
223  *	cookie = NULL;
224  *	while ((node = zfs_btree_destroy_nodes(tree, &cookie)) != NULL)
225  *		data_destroy(node);
226  *	zfs_btree_destroy(tree);
227  */
228 void *zfs_btree_destroy_nodes(zfs_btree_t *, zfs_btree_index_t **);
229 
230 /*
231  * Destroys all nodes in the tree quickly. This doesn't give the caller an
232  * opportunity to iterate over each node and do its own cleanup; for that, use
233  * zfs_btree_destroy_nodes().
234  */
235 void zfs_btree_clear(zfs_btree_t *);
236 
237 /*
238  * Final destroy of an B-Tree. Arguments are:
239  *
240  * tree   - the empty tree to destroy
241  */
242 void zfs_btree_destroy(zfs_btree_t *tree);
243 
244 /* Runs a variety of self-checks on the btree to verify integrity. */
245 void zfs_btree_verify(zfs_btree_t *tree);
246 
247 #ifdef	__cplusplus
248 }
249 #endif
250 
251 #endif	/* _BTREE_H */
252