summaryrefslogtreecommitdiffstats
path: root/include/framework/reconstruction/FixedShardCountPolicy.h
blob: 0768daa6f853c4ab985846016b59fafff7bcea61 (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
/*
 * include/framework/reconstruction/FixedShardCountPolicy.h
 *
 * Copyright (C) 2023-2024 Douglas B. Rumbaugh <drumbaugh@psu.edu>
 *                         Dong Xie <dongx@psu.edu>
 *
 * Distributed under the Modified BSD License.
 *
 */
#pragma once
#include <cmath>

#include "framework/reconstruction/ReconstructionPolicy.h"
#include "framework/scheduling/Version.h"
#include "util/types.h"

namespace de {
template <ShardInterface ShardType, QueryInterface<ShardType> QueryType>
class FixedShardCountPolicy : public ReconstructionPolicy<ShardType, QueryType> {
  typedef std::vector<std::shared_ptr<InternalLevel<ShardType, QueryType>>>
      LevelVector;

public:
  FixedShardCountPolicy(size_t buffer_size, size_t shard_count, size_t max_record_count) 
  : m_buffer_size(buffer_size), m_shard_count(shard_count), m_max_reccnt(max_record_count) {}

  ReconstructionVector
  get_reconstruction_tasks(const Version<ShardType, QueryType> *version,
                           size_t incoming_reccnt) const override {
    ReconstructionVector reconstructions;
    return reconstructions;

  }

  ReconstructionVector
  get_flush_tasks(const Version<ShardType, QueryType> *version) const override {

    auto levels = version->get_structure()->get_level_vector();

    ReconstructionVector v;

    if (levels.size() == 0) {
      v.add_reconstruction(ReconstructionTask{
          {{buffer_shid}}, 0, m_buffer_size, ReconstructionType::Append});
      return v;
    }

    ShardID last_shid = {0, (shard_index) (levels[0]->get_shard_count() - 1)};

    if (levels[0]->get_shard(last_shid.shard_idx)->get_record_count() + m_buffer_size <= capacity()) {
        v.add_reconstruction(ReconstructionTask{
          {{buffer_shid, last_shid}}, 0, m_buffer_size, ReconstructionType::Merge});
        return v;
    } else {
        v.add_reconstruction(ReconstructionTask{
          {{buffer_shid}}, 0, m_buffer_size, ReconstructionType::Append});
        return v;
    }
  }

private:
  inline size_t capacity() const {
    double bps = (double) m_max_reccnt / (double) m_buffer_size / m_shard_count;
    return ceil(bps) * m_buffer_size;
  }

  size_t m_buffer_size;
  size_t m_shard_count;
  size_t m_max_reccnt;
};
} // namespace de