summaryrefslogtreecommitdiff
path: root/src/third_party/wiredtiger/src/include/column.i
blob: 07b627315e64bdd7c283a24bffd755fc51f3ed94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*-
 * Copyright (c) 2014-2016 MongoDB, Inc.
 * Copyright (c) 2008-2014 WiredTiger, Inc.
 *	All rights reserved.
 *
 * See the file LICENSE for redistribution information.
 */

/*
 * __col_insert_search_gt --
 *	Search a column-store insert list for the next larger record.
 */
static inline WT_INSERT *
__col_insert_search_gt(WT_INSERT_HEAD *ins_head, uint64_t recno)
{
	WT_INSERT *ins, **insp;
	int i;

	/* If there's no insert chain to search, we're done. */
	if ((ins = WT_SKIP_LAST(ins_head)) == NULL)
		return (NULL);

	/* Fast path check for targets past the end of the skiplist. */
	if (recno >= WT_INSERT_RECNO(ins))
		return (NULL);

	/*
	 * The insert list is a skip list: start at the highest skip level, then
	 * go as far as possible at each level before stepping down to the next.
	 */
	ins = NULL;
	for (i = WT_SKIP_MAXDEPTH - 1, insp = &ins_head->head[i]; i >= 0;)
		if (*insp != NULL && recno >= WT_INSERT_RECNO(*insp)) {
			ins = *insp;	/* GTE: keep going at this level */
			insp = &(*insp)->next[i];
		} else {
			--i;		/* LT: drop down a level */
			--insp;
		}

	/*
	 * If we didn't find any records smaller than the target, we never set
	 * the return value, set it to the first record in the list. Otherwise,
	 * it references a record less-than-or-equal to the target, move to a
	 * later record, that is, a subsequent record greater than the target.
	 * Because inserts happen concurrently, additional records might be
	 * inserted after the searched-for record that are still smaller than
	 * the target, continue to move forward until reaching a record larger
	 * than the target. There isn't any safety testing because we confirmed
	 * such a record exists before searching.
	 */
	if (ins == NULL)
		ins = WT_SKIP_FIRST(ins_head);
	while (recno >= WT_INSERT_RECNO(ins))
		ins = WT_SKIP_NEXT(ins);
	return (ins);
}

/*
 * __col_insert_search_lt --
 *	Search a column-store insert list for the next smaller record.
 */
static inline WT_INSERT *
__col_insert_search_lt(WT_INSERT_HEAD *ins_head, uint64_t recno)
{
	WT_INSERT *ins, **insp;
	int i;

	/* If there's no insert chain to search, we're done. */
	if ((ins = WT_SKIP_FIRST(ins_head)) == NULL)
		return (NULL);

	/* Fast path check for targets before the skiplist. */
	if (recno <= WT_INSERT_RECNO(ins))
		return (NULL);

	/*
	 * The insert list is a skip list: start at the highest skip level, then
	 * go as far as possible at each level before stepping down to the next.
	 */
	for (i = WT_SKIP_MAXDEPTH - 1, insp = &ins_head->head[i]; i >= 0;)
		if (*insp != NULL && recno > WT_INSERT_RECNO(*insp)) {
			ins = *insp;	/* GT: keep going at this level */
			insp = &(*insp)->next[i];
		} else  {
			--i;		/* LTE: drop down a level */
			--insp;
		}

	return (ins);
}

/*
 * __col_insert_search_match --
 *	Search a column-store insert list for an exact match.
 */
static inline WT_INSERT *
__col_insert_search_match(WT_INSERT_HEAD *ins_head, uint64_t recno)
{
	WT_INSERT **insp, *ret_ins;
	uint64_t ins_recno;
	int cmp, i;

	/* If there's no insert chain to search, we're done. */
	if ((ret_ins = WT_SKIP_LAST(ins_head)) == NULL)
		return (NULL);

	/* Fast path the check for values at the end of the skiplist. */
	if (recno > WT_INSERT_RECNO(ret_ins))
		return (NULL);
	if (recno == WT_INSERT_RECNO(ret_ins))
		return (ret_ins);

	/*
	 * The insert list is a skip list: start at the highest skip level, then
	 * go as far as possible at each level before stepping down to the next.
	 */
	for (i = WT_SKIP_MAXDEPTH - 1, insp = &ins_head->head[i]; i >= 0; ) {
		if (*insp == NULL) {
			--i;
			--insp;
			continue;
		}

		ins_recno = WT_INSERT_RECNO(*insp);
		cmp = (recno == ins_recno) ? 0 : (recno < ins_recno) ? -1 : 1;

		if (cmp == 0)			/* Exact match: return */
			return (*insp);
		if (cmp > 0)			/* Keep going at this level */
			insp = &(*insp)->next[i];
		else {				/* Drop down a level */
			--i;
			--insp;
		}
	}

	return (NULL);
}

/*
 * __col_insert_search --
 *	Search a column-store insert list, creating a skiplist stack as we go.
 */
static inline WT_INSERT *
__col_insert_search(WT_INSERT_HEAD *ins_head,
    WT_INSERT ***ins_stack, WT_INSERT **next_stack, uint64_t recno)
{
	WT_INSERT **insp, *ret_ins;
	uint64_t ins_recno;
	int cmp, i;

	/* If there's no insert chain to search, we're done. */
	if ((ret_ins = WT_SKIP_LAST(ins_head)) == NULL)
		return (NULL);

	/* Fast path appends. */
	if (recno >= WT_INSERT_RECNO(ret_ins)) {
		for (i = 0; i < WT_SKIP_MAXDEPTH; i++) {
			ins_stack[i] = (i == 0) ? &ret_ins->next[0] :
			    (ins_head->tail[i] != NULL) ?
			    &ins_head->tail[i]->next[i] : &ins_head->head[i];
			next_stack[i] = NULL;
		}
		return (ret_ins);
	}

	/*
	 * The insert list is a skip list: start at the highest skip level, then
	 * go as far as possible at each level before stepping down to the next.
	 */
	for (i = WT_SKIP_MAXDEPTH - 1, insp = &ins_head->head[i]; i >= 0; ) {
		if ((ret_ins = *insp) == NULL) {
			next_stack[i] = NULL;
			ins_stack[i--] = insp--;
			continue;
		}

		/*
		 * When no exact match is found, the search returns the smallest
		 * key larger than the searched-for key, or the largest key
		 * smaller than the searched-for key, if there is no larger key.
		 * Our callers depend on that: specifically, the fixed-length
		 * column store cursor code interprets returning a key smaller
		 * than the searched-for key to mean the searched-for key is
		 * larger than any key on the page. Don't change that behavior,
		 * things will break.
		 */
		ins_recno = WT_INSERT_RECNO(ret_ins);
		cmp = (recno == ins_recno) ? 0 : (recno < ins_recno) ? -1 : 1;

		if (cmp > 0)			/* Keep going at this level */
			insp = &ret_ins->next[i];
		else if (cmp == 0)		/* Exact match: return */
			for (; i >= 0; i--) {
				next_stack[i] = ret_ins->next[i];
				ins_stack[i] = &ret_ins->next[i];
			}
		else {				/* Drop down a level */
			next_stack[i] = ret_ins;
			ins_stack[i--] = insp--;
		}
	}
	return (ret_ins);
}

/*
 * __col_var_last_recno --
 *	Return the last record number for a variable-length column-store page.
 */
static inline uint64_t
__col_var_last_recno(WT_REF *ref)
{
	WT_COL_RLE *repeat;
	WT_PAGE *page;

	page = ref->page;

	/*
	 * If there's an append list, there may be more records on the page.
	 * This function ignores those records, our callers must handle that
	 * explicitly, if they care.
	 */
	if (!WT_COL_VAR_REPEAT_SET(page))
		return (page->entries == 0 ? 0 :
		    ref->ref_recno + (page->entries - 1));

	repeat = &page->pg_var_repeats[page->pg_var_nrepeats - 1];
	return ((repeat->recno + repeat->rle) - 1 +
	    (page->entries - (repeat->indx + 1)));
}

/*
 * __col_fix_last_recno --
 *	Return the last record number for a fixed-length column-store page.
 */
static inline uint64_t
__col_fix_last_recno(WT_REF *ref)
{
	WT_PAGE *page;

	page = ref->page;

	/*
	 * If there's an append list, there may be more records on the page.
	 * This function ignores those records, our callers must handle that
	 * explicitly, if they care.
	 */
	return (page->entries == 0 ? 0 : ref->ref_recno + (page->entries - 1));
}

/*
 * __col_var_search --
 *	Search a variable-length column-store page for a record.
 */
static inline WT_COL *
__col_var_search(WT_REF *ref, uint64_t recno, uint64_t *start_recnop)
{
	WT_COL_RLE *repeat;
	WT_PAGE *page;
	uint64_t start_recno;
	uint32_t base, indx, limit, start_indx;

	page = ref->page;

	/*
	 * Find the matching slot.
	 *
	 * This is done in two stages: first, we do a binary search among any
	 * repeating records to find largest repeating less than the search key.
	 * Once there, we can do a simple offset calculation to find the correct
	 * slot for this record number, because we know any intervening records
	 * have repeat counts of 1.
	 */
	for (base = 0,
	    limit = WT_COL_VAR_REPEAT_SET(page) ? page->pg_var_nrepeats : 0;
	    limit != 0; limit >>= 1) {
		indx = base + (limit >> 1);

		repeat = page->pg_var_repeats + indx;
		if (recno >= repeat->recno &&
		    recno < repeat->recno + repeat->rle) {
			if (start_recnop != NULL)
				*start_recnop = repeat->recno;
			return (page->pg_var + repeat->indx);
		}
		if (recno < repeat->recno)
			continue;
		base = indx + 1;
		--limit;
	}

	/*
	 * We didn't find an exact match, move forward from the largest repeat
	 * less than the search key.
	 */
	if (base == 0) {
		start_indx = 0;
		start_recno = ref->ref_recno;
	} else {
		repeat = page->pg_var_repeats + (base - 1);
		start_indx = repeat->indx + 1;
		start_recno = repeat->recno + repeat->rle;
	}

	/*
	 * !!!
	 * The test could be written more simply as:
	 *
	 * 	(recno >= start_recno + (page->entries - start_indx))
	 *
	 * It's split into two parts because the simpler test will overflow if
	 * searching for large record numbers.
	 */
	if (recno >= start_recno &&
	    recno - start_recno >= page->entries - start_indx)
		return (NULL);

	return (page->pg_var + start_indx + (uint32_t)(recno - start_recno));
}