[88c706]: db / db-insert.c Maximize Restore History

Download this file

db-insert.c    281 lines (218 with data), 6.4 kB

  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
#include <assert.h>
#include "db.h"
static void copy_item(value_t * dest, const value_t * src,
unsigned int nr_item)
{
unsigned int i;
for (i = 0 ; i < nr_item ; ++i) {
dest[i] = src[i];
}
}
static void copy_item_backward(value_t * dest, value_t * src,
unsigned int nr_item)
{
unsigned int i;
for (i = nr_item ; i-- > 0 ; ) {
dest[i] = src[i];
}
}
static void split_page(root_t * root,
page_idx_t page_idx, page_idx_t new_page_idx,
value_t * value, value_t * excess_elt, unsigned int pos)
{
page_t * page, * new_page;
new_page = page_nr_to_page_ptr(root, new_page_idx);
page = page_nr_to_page_ptr(root, page_idx);
/* split page in two piece of equal size */
if (pos < MIN_PAGE) {
/* save the pivot. */
*excess_elt = page->page_table[MIN_PAGE - 1];
/* split-up the page. */
copy_item(&new_page->page_table[0],
&page->page_table[MIN_PAGE],
MIN_PAGE);
assert((MIN_PAGE - (pos + 1)) < MAX_PAGE);
/* shift the old page to make the hole. */
/* pos < MIN_PAGE so on copy size is >= 0 */
copy_item_backward(&page->page_table[pos + 1],
&page->page_table[pos],
MIN_PAGE - (pos + 1));
/* insert the item. */
page->page_table[pos] = *value;
} else if (pos > MIN_PAGE) {
/* save the pivot. */
*excess_elt = page->page_table[MIN_PAGE];
assert((pos - (MIN_PAGE+1)) < MAX_PAGE);
/* split-up the page. */
copy_item(&new_page->page_table[0],
&page->page_table[MIN_PAGE + 1],
pos - (MIN_PAGE+1));
/* insert the elt. */
new_page->page_table[pos - (MIN_PAGE + 1)] = *value;
assert(((int)(MAX_PAGE - pos)) >= 0);
copy_item(&new_page->page_table[pos - MIN_PAGE],
&page->page_table[pos],
MAX_PAGE - pos);
} else { /* pos == MIN_PAGE */
/* the pivot is the item to insert */
*excess_elt = *value;
/* split-up the page */
copy_item(&new_page->page_table[0],
&page->page_table[MIN_PAGE],
MIN_PAGE);
}
/* can setup now the page */
page->count = new_page->count = MIN_PAGE;
new_page->p0 = excess_elt->child_page;
excess_elt->child_page = new_page_idx;
}
static int do_reorg(root_t * root, page_idx_t page_idx, unsigned int pos,
value_t * excess_elt, value_t * value)
{
int need_reorg;
page_t * page;
page = page_nr_to_page_ptr(root, page_idx);
assert(page->count <= MAX_PAGE);
/* the insertion pos can be at the end of the page so <= */
assert(pos <= MAX_PAGE);
if (page->count < MAX_PAGE) {
/* insert at pos, shift to make a hole. */
assert((page->count - pos < MAX_PAGE));
copy_item_backward(&page->page_table[pos + 1],
&page->page_table[pos],
page->count - pos);
page->page_table[pos] = *value;
page->count++;
need_reorg = 0;
} else {
page_idx_t new_page_idx = add_page(root);
/* we can not pass page, the page pointer can be invalidated
* by add_page so pass page_idx here, call will re-get the page
* pointer */
split_page(root, page_idx, new_page_idx, value,
excess_elt, pos);
need_reorg = 1;
}
return need_reorg;
}
#if MIN_PAGE > 6
static int do_insert(root_t * root, page_idx_t page_idx,
value_t * excess_elt, value_t * value)
{
int need_reorg;
int left, right, pos;
page_idx_t child_page_idx;
page_t * page;
page = page_nr_to_page_ptr(root, page_idx);
assert(page->count != 0);
left = 0;
right = page->count - 1;
/* a little what experimental, compiler with cond move insn
* can generate code w/o branching inside the loop */
/* tests show than 60 to 70 % of cpu time comes from this loop. */
do {
pos = (left + right) >> 1;
if (page->page_table[pos].key >= value->key)
right = pos - 1;
if (page->page_table[pos].key <= value->key)
left = pos + 1;
} while (left <= right);
/* if left - right == 2 ==> found at pos.
* else if right == -1 that's a leftmost elt
* else left is insertion place */
if (left - right == 2) {
/* found: even if the write is non-atomic we do not need
* to lock() because we work only in the case of one writer,
* multiple reader. */
page->page_table[pos].info += value->info;
return 0;
}
if (right == -1) {
child_page_idx = page->p0;
} else {
child_page_idx = page->page_table[right].child_page;
}
need_reorg = 0;
if (child_page_idx != nil_page) {
need_reorg = do_insert(root, child_page_idx,
excess_elt, value);
*value = *excess_elt;
} else {
need_reorg = 1;
}
if (need_reorg) {
need_reorg = do_reorg(root, page_idx, left, excess_elt, value);
}
return need_reorg;
}
#else
static int do_insert(root_t * root, page_idx_t page_idx,
value_t * excess_elt, value_t * value)
{
int need_reorg;
int pos;
page_idx_t child_page_idx;
page_t * page;
page = page_nr_to_page_ptr(root, page_idx);
assert(page->count != 0);
for (pos = 0 ; pos < page->count ; ++pos) {
if (page->page_table[pos].key >= value->key)
break;
}
if (pos != page->count && page->page_table[pos].key == value->key) {
/* found: even if the write is non-atomic we do not need
* to lock() because we work only in the case of one writer,
* multiple reader. */
page->page_table[pos].info += value->info;
return 0;
}
if (pos == 0) {
child_page_idx = page->p0;
} else {
child_page_idx = page->page_table[pos-1].child_page;
}
need_reorg = 0;
if (child_page_idx != nil_page) {
need_reorg = do_insert(root, child_page_idx,
excess_elt, value);
*value = *excess_elt;
} else {
need_reorg = 1;
}
if (need_reorg) {
need_reorg = do_reorg(root, page_idx, pos, excess_elt, value);
}
return need_reorg;
}
#endif
void insert(root_t * root, unsigned int key, unsigned int info)
{
value_t excess_elt;
value_t value;
int need_reorg;
value.key = key;
value.info = info;
value.child_page = nil_page;
if (!root->base_area) {
/* create the root. */
page_t * page;
root->root = add_page(root);
page = page_nr_to_page_ptr(root, root->root);
page->page_table[0] = value;
page->count = 1;
return;
}
need_reorg = do_insert(root, root->root, &excess_elt, &value);
if (need_reorg) {
/* increase the level of tree. */
page_t * new_page;
page_idx_t old_root;
old_root = root->root;
root->root = add_page(root);
/* page pointer can be invalidated by add_page, reload it */
new_page = page_nr_to_page_ptr(root, root->root);
new_page->page_table[0] = excess_elt;
new_page->count = 1;
new_page->p0 = old_root;
}
}