summaryrefslogtreecommitdiffstats
path: root/include/framework/scheduling/Version.h
blob: 8d3d03864b443136e03e92e5216bb60bf541f257 (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
/*
 * include/framework/scheduling/Version.h
 *
 * Copyright (C) 2023-2024 Douglas B. Rumbaugh <drumbaugh@psu.edu>
 *
 * Distributed under the Modified BSD License.
 *
 */
#pragma once

#include <mutex>

#include "framework/structure/BufferView.h"
#include "framework/structure/ExtensionStructure.h"
#include "framework/structure/MutableBuffer.h"

namespace de {

template <ShardInterface ShardType, QueryInterface<ShardType> QueryType>
class Version {
private:
  typedef typename ShardType::RECORD RecordType;
  typedef MutableBuffer<RecordType> BufferType;
  typedef ExtensionStructure<ShardType, QueryType> StructureType;
  typedef BufferView<RecordType> BufferViewType;

public:
  Version(size_t vid = 0)
      : m_buffer(nullptr), m_structure(nullptr), m_id(vid), m_buffer_head(0),
        m_pending_buffer_head(-1) {}

  Version(size_t number, std::unique_ptr<StructureType> structure, BufferType *buff,
          size_t head)
      : m_buffer(buff), m_structure(std::move(structure)), m_id(number),
        m_buffer_head(head), m_pending_buffer_head(-1) {}

  ~Version() = default;

  /*
   * Versions are *not* copyable or movable. Only one can exist, and all users
   * of it work with pointers
   */
  Version(const Version &) = delete;
  Version(Version &&) = delete;
  Version &operator=(const Version &) = delete;
  Version &operator=(Version &&) = delete;

  size_t get_id() const { return m_id; }

  void set_id(size_t id) { m_id = id;}

  const StructureType *get_structure() const { return m_structure.get(); }

  StructureType *get_mutable_structure() { return m_structure.get(); }

  bool set_structure(std::unique_ptr<StructureType> new_struct) {
    if (m_structure) {
      return false;
    }

    m_structure = std::move(new_struct);
    return true;
  }

  BufferViewType get_buffer() const {
    return m_buffer->get_buffer_view(m_buffer_head);
  }

  /*
   * Returns a new Version object that is a copy of this one. The new object
   * will also contain a copy of the m_structure, rather than a reference to
   * the same one. The epoch number of the new epoch will be set to the
   * provided argument.
   */
  Version *clone(size_t number) {
    auto version = new Version(number);
    version->m_buffer = m_buffer;
    version->m_buffer_head = m_buffer_head;

    if (m_structure) {
      version->m_structure = std::unique_ptr(m_structure->copy());
    }

    return version;
  }

  void set_next_buffer_head(size_t new_head) {
    m_pending_buffer_head = new_head;
  }

  bool advance_buffer_head() {
    m_buffer_head = m_pending_buffer_head;
    return m_buffer->advance_head(m_buffer_head);
  }

private:
  BufferType *m_buffer;
  std::unique_ptr<StructureType> m_structure;

  /*
   * The number of currently active jobs
   * (queries/merges) operating on this
   * epoch. An epoch can only be retired
   * when this number is 0.
   */
  size_t m_id;
  size_t m_buffer_head;
  ssize_t m_pending_buffer_head;
};
} // namespace de