aboutsummaryrefslogtreecommitdiff
path: root/komfydb/storage/heap_page.cc
blob: f2b3f12da345110f7c3e582a81bb79c0d4b7449d (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
#include "komfydb/storage/heap_page.h"
#include "komfydb/common/int_field.h"
#include "komfydb/common/string_field.h"
#include "komfydb/config.h"
#include "komfydb/utils/status_macros.h"

namespace {

using namespace komfydb::common;

absl::StatusOr<bool> TuplePresent(std::vector<uint8_t>& header, int i) {
  if (i / 8 >= header.size() || i < 0)
    return absl::InvalidArgumentError("Index out of range");
  return header[i / 8] & (1 << (i % 8));
}

std::unique_ptr<IntField> ParseInt(std::vector<uint8_t>& data, int& data_idx) {
  std::unique_ptr<IntField> field =
      std::make_unique<IntField>(*((int*)&data[data_idx]));
  data_idx += Type::INT_LEN;
  return field;
}

std::unique_ptr<StringField> ParseString(std::vector<uint8_t>& data,
                                         int& data_idx) {
  std::unique_ptr<StringField> value =
      std::make_unique<StringField>((char*)&data[data_idx]);
  data_idx += Type::STR_LEN;
  return value;
}

void DumpString(Field* field, std::vector<uint8_t>& result) {
  std::string data;
  field->GetValue(data);
  int padding = Type::STR_LEN - data.size();
  result.insert(result.end(), data.begin(), data.end());
  result.insert(result.end(), padding, '\0');
}

void DumpInt(Field* field, std::vector<uint8_t>& result) {
  int data;
  field->GetValue(data);
  uint8_t* bytes = reinterpret_cast<uint8_t*>(&data);
  result.insert(result.end(), bytes, bytes + sizeof(data));
}

}  // namespace

namespace komfydb::storage {

absl::StatusOr<std::unique_ptr<HeapPage>> HeapPage::Create(
    PageId pid, TupleDesc* td, std::vector<uint8_t>& data) {
  int n_slots = (CONFIG_PAGE_SIZE * 8) / (td->GetSize() * 8 + 1);
  int header_len = (n_slots + 7) / 8;
  int n_fields = td->Length();
  int data_idx = header_len;
  std::vector<uint8_t> header;
  std::vector<Record> records;

  header.insert(header.end(), data.begin(), data.begin() + header_len);
  for (int i = 0; i < n_slots; i++) {
    Record record(td, pid, i);

    for (int j = 0; j < n_fields; j++) {
      ASSIGN_OR_RETURN(Type field_type, td->GetFieldType(j));

      if (field_type.GetValue() == Type::INT) {
        RETURN_IF_ERROR(record.SetField(j, ParseInt(data, data_idx)));
      } else if (field_type.GetValue() == Type::STRING) {
        RETURN_IF_ERROR(record.SetField(j, ParseString(data, data_idx)));
      }
    }
    records.push_back(std::move(record));
  }

  return std::unique_ptr<HeapPage>(
      new HeapPage(pid, td, header, std::move(records), n_slots));
}

PageId HeapPage::GetId() {
  return pid;
}

// std::optional<TransactionId> HeapPage::DirtiedBy() {
// }

// void HeapPage::MarkDirty(bool dirty, TransactionId tid) {
// }

absl::StatusOr<std::vector<uint8_t>> HeapPage::GetPageData() {
  // TODO: uncomment when DirtiedBy is implemented
  // if (!DirtiedBy())
  //   return old_data;

  std::vector<uint8_t> result = header;
  int tuple_len = td->Length();
  int record_idx = 0;

  for (int i = 0; i < num_slots; i++) {
    ASSIGN_OR_RETURN(bool tuple_present, TuplePresent(header, i));
    if (!tuple_present) {
      result.insert(result.end(), td->GetSize(), '\0');
      continue;
    }
    Record& record = records[record_idx];
    record_idx++;
    for (int j = 0; j < tuple_len; j++) {
      ASSIGN_OR_RETURN(Type field_type, td->GetFieldType(j));
      ASSIGN_OR_RETURN(Field * field, record.GetField(j));

      if (field_type.GetValue() == Type::STRING)
        DumpString(field, result);
      else if (field_type.GetValue() == Type::INT)
        DumpInt(field, result);
    }
  }
  result.insert(result.end(), CONFIG_PAGE_SIZE - result.size(), '\0');
  return result;
}

absl::StatusOr<std::unique_ptr<Page>> HeapPage::GetBeforeImage() {
  absl::MutexLock l(&old_data_lock);
  ASSIGN_OR_RETURN(std::unique_ptr<HeapPage> result, Create(pid, td, old_data));
  return result;
}

absl::Status HeapPage::SetBeforeImage() {
  absl::MutexLock l(&old_data_lock);
  ASSIGN_OR_RETURN(old_data, GetPageData());
  return absl::OkStatus();
}

std::vector<Record>& HeapPage::GetRecords() {
  return records;
}

};  // namespace komfydb::storage