summaryrefslogtreecommitdiffstats
path: root/src/crimson/common/fixed_kv_node_layout.h
blob: 676563594e0cebc08315b201adb5beb0a3fe9db8 (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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#pragma once

#include <algorithm>
#include <iostream>

#include <boost/iterator/counting_iterator.hpp>

#include "include/byteorder.h"

#include "crimson/common/layout.h"

namespace crimson::common {

template <typename T, bool is_const>
struct maybe_const_t {
};
template<typename T>
struct maybe_const_t<T, true> {
  using type = const T*;
};
template<typename T>
struct maybe_const_t<T, false> {
  using type = T*;
};


/**
 * FixedKVNodeLayout
 *
 * Reusable implementation of a fixed size block mapping
 * K -> V with internal representations KINT and VINT.
 *
 * Uses absl::container_internal::Layout for the actual memory layout.
 *
 * The primary interface exposed is centered on the iterator
 * and related methods.
 *
 * Also included are helpers for doing splits and merges as for a btree.
 */
template <
  size_t CAPACITY,
  typename Meta,
  typename MetaInt,
  typename K,
  typename KINT,
  typename V,
  typename VINT,
  bool VALIDATE_INVARIANTS=true>
class FixedKVNodeLayout {
  char *buf = nullptr;

  using L = absl::container_internal::Layout<ceph_le32, MetaInt, KINT, VINT>;
  static constexpr L layout{1, 1, CAPACITY, CAPACITY};

public:
  template <bool is_const>
  struct iter_t {
    friend class FixedKVNodeLayout;
    using parent_t = typename maybe_const_t<FixedKVNodeLayout, is_const>::type;

    parent_t node;
    uint16_t offset = 0;

    iter_t() = default;
    iter_t(
      parent_t parent,
      uint16_t offset) : node(parent), offset(offset) {}

    iter_t(const iter_t &) noexcept = default;
    iter_t(iter_t &&) noexcept = default;
    template<bool is_const_ = is_const>
    iter_t(const iter_t<false>& it, std::enable_if_t<is_const_, int> = 0)
      : iter_t{it.node, it.offset}
    {}
    iter_t &operator=(const iter_t &) = default;
    iter_t &operator=(iter_t &&) = default;

    // Work nicely with for loops without requiring a nested type.
    using reference = iter_t&;
    iter_t &operator*() { return *this; }
    iter_t *operator->() { return this; }

    iter_t operator++(int) {
      auto ret = *this;
      ++offset;
      return ret;
    }

    iter_t &operator++() {
      ++offset;
      return *this;
    }

    iter_t operator--(int) {
      assert(offset > 0);
      auto ret = *this;
      --offset;
      return ret;
    }

    iter_t &operator--() {
      assert(offset > 0);
      --offset;
      return *this;
    }

    uint16_t operator-(const iter_t &rhs) const {
      assert(rhs.node == node);
      return offset - rhs.offset;
    }

    iter_t operator+(uint16_t off) const {
      return iter_t(
	node,
	offset + off);
    }
    iter_t operator-(uint16_t off) const {
      return iter_t(
	node,
	offset - off);
    }

    friend bool operator==(const iter_t &lhs, const iter_t &rhs) {
      assert(lhs.node == rhs.node);
      return lhs.offset == rhs.offset;
    }

    friend bool operator!=(const iter_t &lhs, const iter_t &rhs) {
      return !(lhs == rhs);
    }

    friend bool operator==(const iter_t<is_const> &lhs, const iter_t<!is_const> &rhs) {
      assert(lhs.node == rhs.node);
      return lhs.offset == rhs.offset;
    }
    friend bool operator!=(const iter_t<is_const> &lhs, const iter_t<!is_const> &rhs) {
      return !(lhs == rhs);
    }
    K get_key() const {
      return K(node->get_key_ptr()[offset]);
    }

    K get_next_key_or_max() const {
      auto next = *this + 1;
      if (next == node->end())
	return std::numeric_limits<K>::max();
      else
	return next->get_key();
    }

    void set_val(V val) const {
      static_assert(!is_const);
      node->get_val_ptr()[offset] = VINT(val);
    }

    V get_val() const {
      return V(node->get_val_ptr()[offset]);
    };

    bool contains(K addr) const {
      return (get_key() <= addr) && (get_next_key_or_max() > addr);
    }

    uint16_t get_offset() const {
      return offset;
    }

  private:
    void set_key(K _lb) const {
      static_assert(!is_const);
      KINT lb;
      lb = _lb;
      node->get_key_ptr()[offset] = lb;
    }

    typename maybe_const_t<char, is_const>::type get_key_ptr() const {
      return reinterpret_cast<
	typename maybe_const_t<char, is_const>::type>(
	  node->get_key_ptr() + offset);
    }

    typename maybe_const_t<char, is_const>::type get_val_ptr() const {
      return reinterpret_cast<
	typename maybe_const_t<char, is_const>::type>(
	  node->get_val_ptr() + offset);
    }
  };
  using const_iterator = iter_t<true>;
  using iterator = iter_t<false>;

  struct delta_t {
    enum class op_t : uint8_t {
      INSERT,
      REMOVE,
      UPDATE,
    } op;
    KINT key;
    VINT val;

    void replay(FixedKVNodeLayout &l) {
      switch (op) {
      case op_t::INSERT: {
	l.insert(l.lower_bound(key), key, val);
	break;
      }
      case op_t::REMOVE: {
	auto iter = l.find(key);
	assert(iter != l.end());
	l.remove(iter);
	break;
      }
      case op_t::UPDATE: {
	auto iter = l.find(key);
	assert(iter != l.end());
	l.update(iter, val);
	break;
      }
      default:
	assert(0 == "Impossible");
      }
    }

    bool operator==(const delta_t &rhs) const {
      return op == rhs.op &&
	key == rhs.key &&
	val == rhs.val;
    }
  };

public:
  class delta_buffer_t {
    std::vector<delta_t> buffer;
  public:
    bool empty() const {
      return buffer.empty();
    }
    void insert(
      const K &key,
      const V &val) {
      KINT k;
      k = key;
      buffer.push_back(
	delta_t{
	  delta_t::op_t::INSERT,
	  k,
	  VINT(val)
	});
    }
    void update(
      const K &key,
      const V &val) {
      KINT k;
      k = key;
      buffer.push_back(
	delta_t{
	  delta_t::op_t::UPDATE,
	  k,
	  VINT(val)
	});
    }
    void remove(const K &key) {
      KINT k;
      k = key;
      buffer.push_back(
	delta_t{
	  delta_t::op_t::REMOVE,
	  k,
	  VINT()
	});
    }
    void replay(FixedKVNodeLayout &node) {
      for (auto &i: buffer) {
	i.replay(node);
      }
    }
    size_t get_bytes() const {
      return buffer.size() * sizeof(delta_t);
    }
    void copy_out(char *out, size_t len) {
      assert(len == get_bytes());
      ::memcpy(out, reinterpret_cast<const void *>(buffer.data()), get_bytes());
      buffer.clear();
    }
    void copy_in(const char *out, size_t len) {
      assert(empty());
      assert(len % sizeof(delta_t) == 0);
      buffer = std::vector(
	reinterpret_cast<const delta_t*>(out),
	reinterpret_cast<const delta_t*>(out + len));
    }
    bool operator==(const delta_buffer_t &rhs) const {
      return buffer == rhs.buffer;
    }
  };

  void journal_insert(
    const_iterator _iter,
    const K &key,
    const V &val,
    delta_buffer_t *recorder) {
    auto iter = iterator(this, _iter.offset);
    if (recorder) {
      recorder->insert(
	key,
	val);
    }
    insert(iter, key, val);
  }

  void journal_update(
    const_iterator _iter,
    const V &val,
    delta_buffer_t *recorder) {
    auto iter = iterator(this, _iter.offset);
    if (recorder) {
      recorder->update(iter->get_key(), val);
    }
    update(iter, val);
  }

  void journal_replace(
    const_iterator _iter,
    const K &key,
    const V &val,
    delta_buffer_t *recorder) {
    auto iter = iterator(this, _iter.offset);
    if (recorder) {
      recorder->remove(iter->get_key());
      recorder->insert(key, val);
    }
    replace(iter, key, val);
  }


  void journal_remove(
    const_iterator _iter,
    delta_buffer_t *recorder) {
    auto iter = iterator(this, _iter.offset);
    if (recorder) {
      recorder->remove(iter->get_key());
    }
    remove(iter);
  }


  FixedKVNodeLayout(char *buf) :
    buf(buf) {}

  virtual ~FixedKVNodeLayout() = default;

  const_iterator begin() const {
    return const_iterator(
      this,
      0);
  }

  const_iterator end() const {
    return const_iterator(
      this,
      get_size());
  }

  iterator begin() {
    return iterator(
      this,
      0);
  }

  iterator end() {
    return iterator(
      this,
      get_size());
  }

  const_iterator iter_idx(uint16_t off) const {
    return const_iterator(
      this,
      off);
  }

  const_iterator find(K l) const {
    auto ret = begin();
    for (; ret != end(); ++ret) {
      if (ret->get_key() == l)
	break;
    }
    return ret;
  }
  iterator find(K l) {
    const auto &tref = *this;
    return iterator(this, tref.find(l).offset);
  }

  const_iterator lower_bound(K l) const {
    auto it = std::lower_bound(boost::make_counting_iterator<uint16_t>(0),
	                       boost::make_counting_iterator<uint16_t>(get_size()),
		               l,
		               [this](uint16_t i, K key) {
			         const_iterator iter(this, i);
			         return iter->get_key() < key;
			       });
    return const_iterator(this, *it);
  }

  iterator lower_bound(K l) {
    const auto &tref = *this;
    return iterator(this, tref.lower_bound(l).offset);
  }

  const_iterator upper_bound(K l) const {
    auto it = std::upper_bound(boost::make_counting_iterator<uint16_t>(0),
	                       boost::make_counting_iterator<uint16_t>(get_size()),
		               l,
		               [this](K key, uint16_t i) {
			         const_iterator iter(this, i);
			         return key < iter->get_key();
			       });
    return const_iterator(this, *it);
  }

  iterator upper_bound(K l) {
    const auto &tref = *this;
    return iterator(this, tref.upper_bound(l).offset);
  }

  const_iterator get_split_pivot() const {
    return iter_idx(get_size() / 2);
  }

  uint16_t get_size() const {
    return *layout.template Pointer<0>(buf);
  }

  /**
   * set_size
   *
   * Set size representation to match size
   */
  void set_size(uint16_t size) {
    *layout.template Pointer<0>(buf) = size;
  }

  /**
   * get_meta/set_meta
   *
   * Enables stashing a templated type within the layout.
   * Cannot be modified after initial write as it is not represented
   * in delta_t
   */
  Meta get_meta() const {
    MetaInt &metaint = *layout.template Pointer<1>(buf);
    return Meta(metaint);
  }
  void set_meta(const Meta &meta) {
    *layout.template Pointer<1>(buf) = MetaInt(meta);
  }

  constexpr static size_t get_capacity() {
    return CAPACITY;
  }

  bool operator==(const FixedKVNodeLayout &rhs) const {
    if (get_size() != rhs.get_size()) {
      return false;
    }

    auto iter = begin();
    auto iter2 = rhs.begin();
    while (iter != end()) {
      if (iter->get_key() != iter2->get_key() ||
	  iter->get_val() != iter2->get_val()) {
	return false;
      }
      iter++;
      iter2++;
    }
    return true;
  }

  /**
   * split_into
   *
   * Takes *this and splits its contents into left and right.
   */
  K split_into(
    FixedKVNodeLayout &left,
    FixedKVNodeLayout &right) const {
    auto piviter = get_split_pivot();

    left.copy_from_foreign(left.begin(), begin(), piviter);
    left.set_size(piviter - begin());

    right.copy_from_foreign(right.begin(), piviter, end());
    right.set_size(end() - piviter);

    auto [lmeta, rmeta] = get_meta().split_into(piviter->get_key());
    left.set_meta(lmeta);
    right.set_meta(rmeta);

    return piviter->get_key();
  }

  /**
   * merge_from
   *
   * Takes two nodes and copies their contents into *this.
   *
   * precondition: left.size() + right.size() < CAPACITY
   */
  void merge_from(
    const FixedKVNodeLayout &left,
    const FixedKVNodeLayout &right)
  {
    copy_from_foreign(
      end(),
      left.begin(),
      left.end());
    set_size(left.get_size());
    copy_from_foreign(
      end(),
      right.begin(),
      right.end());
    set_size(left.get_size() + right.get_size());
    set_meta(Meta::merge_from(left.get_meta(), right.get_meta()));
  }

  /**
   * balance_into_new_nodes
   *
   * Takes the contents of left and right and copies them into
   * replacement_left and replacement_right such that in the
   * event that the number of elements is odd the extra goes to
   * the left side iff prefer_left.
   */
  static K balance_into_new_nodes(
    const FixedKVNodeLayout &left,
    const FixedKVNodeLayout &right,
    bool prefer_left,
    FixedKVNodeLayout &replacement_left,
    FixedKVNodeLayout &replacement_right)
  {
    auto total = left.get_size() + right.get_size();
    auto pivot_idx = (left.get_size() + right.get_size()) / 2;
    if (total % 2 && prefer_left) {
      pivot_idx++;
    }
    auto replacement_pivot = pivot_idx >= left.get_size() ?
      right.iter_idx(pivot_idx - left.get_size())->get_key() :
      left.iter_idx(pivot_idx)->get_key();

    if (pivot_idx < left.get_size()) {
      replacement_left.copy_from_foreign(
	replacement_left.end(),
	left.begin(),
	left.iter_idx(pivot_idx));
      replacement_left.set_size(pivot_idx);

      replacement_right.copy_from_foreign(
	replacement_right.end(),
	left.iter_idx(pivot_idx),
	left.end());

      replacement_right.set_size(left.get_size() - pivot_idx);
      replacement_right.copy_from_foreign(
	replacement_right.end(),
	right.begin(),
	right.end());
      replacement_right.set_size(total - pivot_idx);
    } else {
      replacement_left.copy_from_foreign(
	replacement_left.end(),
	left.begin(),
	left.end());
      replacement_left.set_size(left.get_size());

      replacement_left.copy_from_foreign(
	replacement_left.end(),
	right.begin(),
	right.iter_idx(pivot_idx - left.get_size()));
      replacement_left.set_size(pivot_idx);

      replacement_right.copy_from_foreign(
	replacement_right.end(),
	right.iter_idx(pivot_idx - left.get_size()),
	right.end());
      replacement_right.set_size(total - pivot_idx);
    }

    auto [lmeta, rmeta] = Meta::rebalance(
      left.get_meta(), right.get_meta(), replacement_pivot);
    replacement_left.set_meta(lmeta);
    replacement_right.set_meta(rmeta);
    return replacement_pivot;
  }

private:
  void insert(
    iterator iter,
    const K &key,
    const V &val) {
    if (VALIDATE_INVARIANTS) {
      if (iter != begin()) {
	assert((iter - 1)->get_key() < key);
      }
      if (iter != end()) {
	assert(iter->get_key() > key);
      }
      assert(get_size() < CAPACITY);
    }
    copy_from_local(iter + 1, iter, end());
    iter->set_key(key);
    iter->set_val(val);
    set_size(get_size() + 1);
  }

  void update(
    iterator iter,
    V val) {
    assert(iter != end());
    iter->set_val(val);
  }

  void replace(
    iterator iter,
    const K &key,
    const V &val) {
    assert(iter != end());
    if (VALIDATE_INVARIANTS) {
      if (iter != begin()) {
	assert((iter - 1)->get_key() < key);
      }
      if ((iter + 1) != end()) {
	assert((iter + 1)->get_key() > key);
      }
    }
    iter->set_key(key);
    iter->set_val(val);
  }

  void remove(iterator iter) {
    assert(iter != end());
    copy_from_local(iter, iter + 1, end());
    set_size(get_size() - 1);
  }

  /**
   * get_key_ptr
   *
   * Get pointer to start of key array
   */
  KINT *get_key_ptr() {
    return layout.template Pointer<2>(buf);
  }
  const KINT *get_key_ptr() const {
    return layout.template Pointer<2>(buf);
  }

  /**
   * get_val_ptr
   *
   * Get pointer to start of val array
   */
  VINT *get_val_ptr() {
    return layout.template Pointer<3>(buf);
  }
  const VINT *get_val_ptr() const {
    return layout.template Pointer<3>(buf);
  }

  /**
   * node_resolve/unresolve_vals
   *
   * If the representation for values depends in some way on the
   * node in which they are located, users may implement
   * resolve/unresolve to enable copy_from_foreign to handle that
   * transition.
   */
  virtual void node_resolve_vals(iterator from, iterator to) const {}
  virtual void node_unresolve_vals(iterator from, iterator to) const {}

  /**
   * copy_from_foreign
   *
   * Copies entries from [from_src, to_src) to tgt.
   *
   * tgt and from_src must be from different nodes.
   * from_src and to_src must be from the same node.
   */
  static void copy_from_foreign(
    iterator tgt,
    const_iterator from_src,
    const_iterator to_src) {
    assert(tgt->node != from_src->node);
    assert(to_src->node == from_src->node);
    memcpy(
      tgt->get_val_ptr(), from_src->get_val_ptr(),
      to_src->get_val_ptr() - from_src->get_val_ptr());
    memcpy(
      tgt->get_key_ptr(), from_src->get_key_ptr(),
      to_src->get_key_ptr() - from_src->get_key_ptr());
    from_src->node->node_resolve_vals(tgt, tgt + (to_src - from_src));
    tgt->node->node_unresolve_vals(tgt, tgt + (to_src - from_src));
  }

  /**
   * copy_from_local
   *
   * Copies entries from [from_src, to_src) to tgt.
   *
   * tgt, from_src, and to_src must be from the same node.
   */
  static void copy_from_local(
    iterator tgt,
    iterator from_src,
    iterator to_src) {
    assert(tgt->node == from_src->node);
    assert(to_src->node == from_src->node);
    memmove(
      tgt->get_val_ptr(), from_src->get_val_ptr(),
      to_src->get_val_ptr() - from_src->get_val_ptr());
    memmove(
      tgt->get_key_ptr(), from_src->get_key_ptr(),
      to_src->get_key_ptr() - from_src->get_key_ptr());
  }
};

}