Heap.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Debug.h>
  8. #include <AK/HashMap.h>
  9. #include <AK/String.h>
  10. #include <AK/Vector.h>
  11. #include <LibCore/File.h>
  12. #include <LibCore/Object.h>
  13. namespace SQL {
  14. constexpr static u32 BLOCKSIZE = 1024;
  15. /**
  16. * A Heap is a logical container for database (SQL) data. Conceptually a
  17. * Heap can be a database file, or a memory block, or another storage medium.
  18. * It contains datastructures, like B-Trees, hash_index tables, or tuple stores
  19. * (basically a list of data tuples).
  20. *
  21. * A Heap can be thought of the backing storage of a single database. It's
  22. * assumed that a single SQL database is backed by a single Heap.
  23. *
  24. * Currently only B-Trees and tuple stores are implemented.
  25. */
  26. class Heap : public Core::Object {
  27. C_OBJECT(Heap);
  28. public:
  29. explicit Heap(String);
  30. virtual ~Heap() override { flush(); }
  31. u32 size() const { return m_end_of_file; }
  32. Result<ByteBuffer, String> read_block(u32);
  33. bool write_block(u32, ByteBuffer&);
  34. u32 new_record_pointer();
  35. [[nodiscard]] bool has_block(u32 block) const { return block < size(); }
  36. u32 schemas_root() const { return m_schemas_root; }
  37. void set_schemas_root(u32 root)
  38. {
  39. m_schemas_root = root;
  40. update_zero_block();
  41. }
  42. u32 tables_root() const { return m_tables_root; }
  43. void set_tables_root(u32 root)
  44. {
  45. m_tables_root = root;
  46. update_zero_block();
  47. }
  48. u32 table_columns_root() const { return m_table_columns_root; }
  49. void set_table_columns_root(u32 root)
  50. {
  51. m_table_columns_root = root;
  52. update_zero_block();
  53. }
  54. u32 version() const { return m_version; }
  55. u32 user_value(size_t index) const
  56. {
  57. VERIFY(index < m_user_values.size());
  58. return m_user_values[index];
  59. }
  60. void set_user_value(size_t index, u32 value)
  61. {
  62. VERIFY(index < m_user_values.size());
  63. m_user_values[index] = value;
  64. update_zero_block();
  65. }
  66. void add_to_wal(u32 block, ByteBuffer& buffer)
  67. {
  68. dbgln_if(SQL_DEBUG, "Adding to WAL: block #{}, size {}", block, buffer.size());
  69. dbgln_if(SQL_DEBUG, "{:02x} {:02x} {:02x} {:02x} {:02x} {:02x} {:02x} {:02x}",
  70. *buffer.offset_pointer(0), *buffer.offset_pointer(1),
  71. *buffer.offset_pointer(2), *buffer.offset_pointer(3),
  72. *buffer.offset_pointer(4), *buffer.offset_pointer(5),
  73. *buffer.offset_pointer(6), *buffer.offset_pointer(7));
  74. m_write_ahead_log.set(block, buffer);
  75. }
  76. void flush();
  77. private:
  78. bool seek_block(u32);
  79. void read_zero_block();
  80. void initialize_zero_block();
  81. void update_zero_block();
  82. RefPtr<Core::File> m_file;
  83. u32 m_free_list { 0 };
  84. u32 m_next_block { 1 };
  85. u32 m_end_of_file { 1 };
  86. u32 m_schemas_root { 0 };
  87. u32 m_tables_root { 0 };
  88. u32 m_table_columns_root { 0 };
  89. u32 m_version { 0x00000001 };
  90. Array<u32, 16> m_user_values;
  91. HashMap<u32, ByteBuffer> m_write_ahead_log;
  92. };
  93. }