forked from postgrespro/pg_pathman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rangeset.c
260 lines (235 loc) · 5.18 KB
/
rangeset.c
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
/* ------------------------------------------------------------------------
*
* rangeset.c
* Index range functions
*
* Copyright (c) 2015-2016, Postgres Professional
*
* ------------------------------------------------------------------------
*/
#include "pathman.h"
/* Check if two ranges are intersecting */
bool
irange_intersects(IndexRange a, IndexRange b)
{
return (irange_lower(a) <= irange_upper(b)) &&
(irange_lower(b) <= irange_upper(a));
}
/* Check if two ranges are conjuncted */
bool
irange_conjuncted(IndexRange a, IndexRange b)
{
return (irange_lower(a) - 1 <= irange_upper(b)) &&
(irange_lower(b) - 1 <= irange_upper(a));
}
/* Make union of two ranges. They should have the same lossiness. */
IndexRange
irange_union(IndexRange a, IndexRange b)
{
Assert(irange_is_lossy(a) == irange_is_lossy(b));
return make_irange(Min(irange_lower(a), irange_lower(b)),
Max(irange_upper(a), irange_upper(b)),
irange_is_lossy(a));
}
/* Get intersection of two ranges */
IndexRange
irange_intersect(IndexRange a, IndexRange b)
{
return make_irange(Max(irange_lower(a), irange_lower(b)),
Min(irange_upper(a), irange_upper(b)),
irange_is_lossy(a) || irange_is_lossy(b));
}
#ifdef NOT_USED
/* Print range list in debug purposes */
static char *
print_irange(List *l)
{
ListCell *c;
StringInfoData str;
initStringInfo(&str);
foreach (c, l)
{
IndexRange ir = lfirst_irange(c);
appendStringInfo(&str, "[%d,%d]%c ", irange_lower(ir), irange_upper(ir),
irange_is_lossy(ir) ? 'l' : 'e');
}
return str.data;
}
#endif
/*
* Make union of two index rage lists.
*/
List *
irange_list_union(List *a, List *b)
{
ListCell *ca,
*cb;
List *result = NIL;
IndexRange cur;
bool have_cur = false;
ca = list_head(a);
cb = list_head(b);
while (ca || cb)
{
IndexRange next = 0;
/* Fetch next range with lesser lower bound */
if (ca && cb)
{
if (irange_lower(lfirst_irange(ca)) <= irange_lower(lfirst_irange(cb)))
{
next = lfirst_irange(ca);
ca = lnext(ca);
}
else
{
next = lfirst_irange(cb);
cb = lnext(cb);
}
}
else if (ca)
{
next = lfirst_irange(ca);
ca = lnext(ca);
}
else if (cb)
{
next = lfirst_irange(cb);
cb = lnext(cb);
}
if (!have_cur)
{
/* Put this range as current value if don't have it yet */
cur = next;
have_cur = true;
}
else
{
if (irange_conjuncted(next, cur))
{
/*
* Ranges are conjuncted, try to unify them.
*/
if (irange_is_lossy(next) == irange_is_lossy(cur))
{
cur = irange_union(next, cur);
}
else
{
if (!irange_is_lossy(cur))
{
result = lappend_irange(result, cur);
cur = make_irange(irange_upper(cur) + 1,
irange_upper(next),
irange_is_lossy(next));
}
else
{
result = lappend_irange(result,
make_irange(irange_lower(cur),
irange_lower(next) - 1,
irange_is_lossy(cur)));
cur = next;
}
}
}
else
{
/*
* Next range is not conjuncted with current. Put current to the
* result list and put next as current.
*/
result = lappend_irange(result, cur);
cur = next;
}
}
}
/* Put current value into result list if any */
if (have_cur)
result = lappend_irange(result, cur);
return result;
}
/*
* Find intersection of two range lists.
*/
List *
irange_list_intersect(List *a, List *b)
{
ListCell *ca,
*cb;
List *result = NIL;
IndexRange ra, rb;
ca = list_head(a);
cb = list_head(b);
while (ca && cb)
{
ra = lfirst_irange(ca);
rb = lfirst_irange(cb);
/* Only care about intersecting ranges */
if (irange_intersects(ra, rb))
{
IndexRange intersect, last;
/*
* Get intersection and try to "glue" it to previous range,
* put it separately otherwise.
*/
intersect = irange_intersect(ra, rb);
if (result != NIL)
{
last = llast_irange(result);
if (irange_conjuncted(last, intersect) &&
irange_is_lossy(last) == irange_is_lossy(intersect))
{
llast_int(result) = irange_union(last, intersect);
}
else
{
result = lappend_irange(result, intersect);
}
}
else
{
result = lappend_irange(result, intersect);
}
}
/*
* Fetch next ranges. We use upper bound of current range to determine
* which lists to fetch, since lower bound of next range is greater (or
* equal) to upper bound of current.
*/
if (irange_upper(ra) <= irange_upper(rb))
ca = lnext(ca);
if (irange_upper(ra) >= irange_upper(rb))
cb = lnext(cb);
}
return result;
}
/* Get total number of elements in range list */
int
irange_list_length(List *rangeset)
{
ListCell *lc;
int result = 0;
foreach (lc, rangeset)
{
IndexRange irange = lfirst_irange(lc);
result += irange_upper(irange) - irange_lower(irange) + 1;
}
return result;
}
/* Find particular index in range list */
bool
irange_list_find(List *rangeset, int index, bool *lossy)
{
ListCell *lc;
foreach (lc, rangeset)
{
IndexRange irange = lfirst_irange(lc);
if (index >= irange_lower(irange) && index <= irange_upper(irange))
{
if (lossy)
*lossy = irange_is_lossy(irange) ? true : false;
return true;
}
}
return false;
}