summaryrefslogtreecommitdiff
path: root/src/database/include/table_reader.hpp
blob: 9f7cc4ee0d15260115980a4557092ac9d1a5c76c (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
#pragma once

#include <string>

#include "result.hpp"
#include "span.hpp"

#include "table.hpp"

namespace database {

class TableReader {
 public:
  enum ReadError {
    OUT_OF_RANGE,
    IO_ERROR,
    PARSE_ERROR,
  };

  auto ReadEntryAtIndex(Index_t index) -> cpp::result<DatabaseEntry, ReadError>;

  template <typename T>
  auto ReadColumnOffsetAtIndex(Column<T> col, Index_t index)
      -> cpp::result<IndexOffset_t, ReadError>;

  template <typename T>
  auto ParseColumnAtIndex(Column<T> col, Index_t index)
      -> cpp::result<T, ReadError> {
    return ReadColumnOffsetAtIndex(col, index).map([&](IndexOffset_t offset) {
      return ReadColumnAtOffset(col, offset);
    });
  }

  template <typename T>
  auto ParseColumnAtOffset(Column<T> col, IndexOffset_t offset)
      -> cpp::result<T, ReadError> {
    return ReadDataAtOffset(col.Filename(), offset)
        .flat_map([&](cpp::span<std::byte> data) {
          auto res = = col.ParseValue(data);
          if (res) {
            return *res;
          } else {
            return cpp::fail(PARSE_ERROR);
          }
        });
  }

 private:
  auto ReadDataAtOffset(std::string filename, IndexOffset_t offset)
      -> cpp::span<std::byte>;
};

}  // namespace database