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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// Copyright 2015 Pierre-Étienne Meunier and Florent Becker. See the
// COPYRIGHT file at the top-level directory of this distribution and
// at http://pijul.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use {Transaction, Representable, PageT, MutTxn, Db, Error, CursorStack, PAGE_SIZE_U16};
use transaction::Cow;
use skiplist::{SkipListPage, FIRST_BINDING_SIZE, record_size};
use skiplist;
use std;
use rand::Rng;

impl<'env, T> MutTxn<'env, T> {
    /// Insert a binding to a database, returning `false` if and only
    /// if the exact same binding (key *and* value) was already in the database.
    pub fn put<R: Rng, K: Representable, V: Representable>(&mut self,
                                                           rng: &mut R,
                                                           db: &mut Db<K, V>,
                                                           key: K,
                                                           value: V)
                                                           -> Result<bool, Error> {

        let mut cursor = CursorStack::new();
        if cursor.set(self, db, Some((key, Some(value))))? {
            return Ok(false);
        }
        // Now insert in the leaf and split all pages that need to be split.

        let leaf_pointer = cursor.pointer;

        // Split all pages that need it.
        //
        // This loop uses unsafe slices, but the memory management is
        // simple: all pointers used in the loop are valid until we
        // explicitely call free, after the loop.
        let last_allocated;
        {
            let mut inserted_right_child = 0;
            let mut inserted_key = key;
            let mut inserted_value = value;
            let mut inserted_left_child = 0u64;

            loop {

                let mut page = self.load_cow_page(cursor.current().page);

                debug!("page: {:?}", page);

                // If we're "CoWing" the first double-pointed page on the stack, decrease its RC.
                if cursor.pointer == cursor.first_rc_level {
                    let page = cursor.current().page;
                    let rc = self.rc(page);
                    if rc >= 2 {
                        try!(self.set_rc(rng, page, rc - 1));
                    }
                }

                if page.can_alloc(inserted_key, inserted_value) != 0 {
                    debug!("can alloc");
                    // We can allocate. Do we need to duplicate this
                    // page (because of RC or first_free)?
                    let needs_dup = page.first_free() + record_size(inserted_key, inserted_value) >
                                    PAGE_SIZE_U16;

                    match page {
                        Cow::MutPage(ref mut page) if cursor.pointer < cursor.first_rc_level &&
                                                      !needs_dup => {
                            debug!("does not need dup");

                            // No, there will be no other references to
                            // this page after this method (`put`)
                            // returns. Here, simply insert.

                            // Update the right child from the previous split
                            // (if any, `inserted_left_child` is 0 else).
                            debug!("page right child: {:?}", cursor.current()[0]);
                            page.set_right_child(cursor.current()[0], inserted_left_child);
                            page.skiplist_insert_after_cursor(rng,
                                                              &mut cursor.current_mut().cursor,
                                                              inserted_key,
                                                              inserted_value,
                                                              inserted_right_child);
                            use PageT;
                            last_allocated = page.page_offset();
                        }
                        _ => {
                            debug!("needs dup {:?} {:?} {:?}",
                                   cursor.pointer,
                                   cursor.first_rc_level,
                                   needs_dup);
                            // Yes, we do need to duplicate this
                            // page. Propagate RC to pages
                            // below.
                            let page = self.load_cow_page(cursor.current().page);

                            // Iterate over the page, incorporating the new element.
                            use PageT;
                            let it = Iter {
                                iter: page.iter_all().peekable(),
                                off: page.offset(cursor.current()[0] as isize),
                                next_is_extra: false,
                                extra_left: inserted_left_child,
                                extra: (Some((inserted_key, inserted_value)), inserted_right_child),
                            };

                            // Increase the RC of everyone, but the left
                            // side of the split (which was just
                            // allocated now).
                            let incr_rc = cursor.pointer >= cursor.first_rc_level;
                            last_allocated = try!(self.spread_on_one_page(rng,
                                                                          it.map(|(b, c)| {
                                let fresh = c == inserted_left_child || c == inserted_right_child;
                                (b, c, incr_rc && !fresh, incr_rc)
                            })));

                            if cursor.pointer < cursor.first_rc_level {
                                unsafe {
                                    // free of the current page.
                                    self.free_page(cursor.current().page)
                                }
                            }
                        }
                    }
                    break;

                } else {
                    // We cannot allocate, we need to split this page

                    debug!("cannot alloc");
                    let (left, right, sep_key, sep_value) = {
                        let it = Iter {
                            iter: page.iter_all().peekable(),
                            off: page.offset(cursor.current()[0] as isize),
                            next_is_extra: false,
                            extra_left: inserted_left_child,
                            extra: (Some((inserted_key, inserted_value)), inserted_right_child),
                        };
                        // We're going to add a page, hence
                        // FIRST_BINDING_SIZE more bytes will be
                        // needed.
                        let total = page.occupied() + record_size(key, value) + FIRST_BINDING_SIZE;
                        if cursor.pointer >= cursor.first_rc_level {
                            let it = it.map(|(b, c)| {
                                if c == inserted_left_child || c == inserted_right_child {
                                    (b, c, false, true)
                                } else {
                                    (b, c, true, true)
                                }
                            });
                            try!(self.spread_on_two_pages(rng, it, total))
                        } else {
                            let it = it.map(|(b, c)| (b, c, false, false));
                            try!(self.spread_on_two_pages(rng, it, total))
                        }
                    };

                    // Extend the lifetime of "inserted_key".
                    inserted_key = sep_key;
                    inserted_left_child = left;
                    inserted_right_child = right;
                    inserted_value = sep_value;

                    cursor.pointer -= 1;
                    if cursor.pointer == 0 {

                        // We've just split the root! we need to
                        // allocate a page to collect the two sides of
                        // the split.
                        last_allocated = try!(self.split_root(rng,
                                                              &mut cursor,
                                                              inserted_left_child,
                                                              inserted_right_child,
                                                              inserted_key,
                                                              inserted_value));
                        break;
                    }
                }
            }
        }
        debug!("freeing pages from {:?} to {:?} {:?} (inclusive)",
               cursor.pointer + 1,
               leaf_pointer,
               cursor.first_rc_level);
        // Now, cursor_pointer is the reference to a page that doesn't
        // need to split anymore (possibly the new root).

        // Free all split pages.
        let last_free = std::cmp::min(leaf_pointer + 1, cursor.first_rc_level);
        let first_free = std::cmp::min(cursor.pointer + 1, last_free);
        for page_cursor in &cursor.stack[first_free..last_free] {
            // free the page that was split.
            let rc = self.rc(page_cursor.page);
            if rc <= 1 {
                debug!("Freeing {:?}", page_cursor.page);
                try!(self.remove_rc(rng, page_cursor.page));
                unsafe { self.free_page(page_cursor.page) }
            } else {
                debug!("Decrease RC for page {:?}, new rc: {:?}",
                       page_cursor.page,
                       rc - 1);
                try!(self.set_rc(rng, page_cursor.page, rc - 1));
            }
        }

        // In the above loop, `break` only occur after an insertion of
        // a (key, value) with right child `inserted_right_child`. For
        // the loop below to update the reference properly in the
        // pages above, we need to update that child here (it might be
        // 0 if we're still at the leaf).

        // Has the root split?
        if cursor.pointer > 0 {
            cursor.stack[cursor.pointer].page = last_allocated;
            cursor.pointer -= 1;
            // The root has not split, the splits stopped earlier. There are remaining pages.
            while cursor.pointer > 0 {

                if cursor.pointer == cursor.first_rc_level {
                    let page = cursor.current().page;
                    let rc = self.rc(page);
                    if rc >= 2 {
                        try!(self.set_rc(rng, page, rc - 1));
                    }
                }
                try!(self.update_put::<R, K, V>(rng, &mut cursor));
                cursor.pointer -= 1;
            }

            db.0 = cursor[1].page

        } else {
            // The root has split, no need to do anything.
            debug!("the root has split");
            db.0 = last_allocated
        }
        Ok(true)
    }


    fn update_put<R: Rng, K: Representable, V: Representable>(&mut self,
                                                              rng: &mut R,
                                                              cursor: &mut CursorStack)
                                                              -> Result<(), Error> {
        use PageT;
        let mut page = self.load_cow_page(cursor.current().page);
        match page {

            Cow::MutPage(ref mut p) if cursor.pointer < cursor.first_rc_level => {

                p.set_right_child(cursor.current()[0], cursor[cursor.pointer + 1].page);
                cursor.current_mut().page = p.offset;
            }
            Cow::MutPage(_) | Cow::Page(_) => {
                // Here, the page needs to be CoWed.
                let incr_rc = cursor.pointer >= cursor.first_rc_level;
                let current_ptr = page.offset(cursor.current()[0] as isize);
                let new_page =
                    try!(self.spread_on_one_page::<_, K, V, _>(rng,
                                                               page.iter_all()
                                                                   .map(|(off, b, c)| {
                            // Both pages will survive, increase reference count.
                            if off != current_ptr {
                                (b, c, incr_rc, incr_rc)
                            } else {
                                let c = cursor[cursor.pointer + 1].page;
                                (b, c, false, incr_rc)
                            }
                        })));
                // If nothing points to the page anymore, free.
                if cursor.pointer < cursor.first_rc_level {
                    unsafe {
                        use PageT;
                        debug!("line {:?}, freeing {:?}", line!(), page.page_offset());
                        self.free_page(page.page_offset())
                    }
                }
                debug!("NEW_PAGE = {:?}", new_page);
                cursor.current_mut().page = new_page;
            }
        }
        Ok(())
    }
}

/// Iterator on a page, plus a middle element immediately after `off`,
/// with a fixed left and right child.
pub struct Iter<'a, P: SkipListPage + 'a, K: Representable, V: Representable> {
    iter: std::iter::Peekable<skiplist::Iter<'a, P, K, V>>,
    off: *const u8,
    next_is_extra: bool,
    extra_left: u64,
    extra: (Option<(K, V)>, u64),
}

impl<'a, P: SkipListPage + 'a, K: Representable, V: Representable> Iterator for Iter<'a, P, K, V> {
    type Item = (Option<(K, V)>, u64);

    fn next(&mut self) -> Option<Self::Item> {

        if self.next_is_extra {
            debug!("next_is_extra");
            self.next_is_extra = false;
            self.off = std::ptr::null();
            Some(self.extra)

        } else {
            debug!("not next_is_extra");

            if let Some((a, b, d)) = self.iter.next() {

                // if a == self.off, the extra element is to be
                // inserted immediately after this element.  Set
                // next_is_extra, and replace the current element's
                // right child with extra's left child.
                debug!("{:?}", a);
                if a == self.off {
                    self.next_is_extra = true;
                    Some((b, self.extra_left))
                } else {
                    Some((b, d))
                }

            } else {
                debug!("finished, extra = {:?}", self.off);
                None

            }
        }
    }
}