Heap.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. #include <LibSQL/Meta.h>
  14. #include <LibSQL/Serialize.h>
  15. namespace SQL {
  16. constexpr static u32 BLOCKSIZE = 1024;
  17. /**
  18. * A Heap is a logical container for database (SQL) data. Conceptually a
  19. * Heap can be a database file, or a memory block, or another storage medium.
  20. * It contains datastructures, like B-Trees, hash_index tables, or tuple stores
  21. * (basically a list of data tuples).
  22. *
  23. * A Heap can be thought of the backing storage of a single database. It's
  24. * assumed that a single SQL database is backed by a single Heap.
  25. *
  26. * Currently only B-Trees and tuple stores are implemented.
  27. */
  28. class Heap : public Core::Object {
  29. C_OBJECT(Heap);
  30. public:
  31. explicit Heap(String);
  32. virtual ~Heap() override { flush(); }
  33. u32 size() const { return m_end_of_file; }
  34. Result<ByteBuffer, String> read_block(u32);
  35. bool write_block(u32, ByteBuffer&);
  36. u32 new_record_pointer();
  37. [[nodiscard]] bool has_block(u32 block) const { return block < size(); }
  38. u32 schemas_root() const { return m_schemas_root; }
  39. void set_schemas_root(u32 root)
  40. {
  41. m_schemas_root = root;
  42. update_zero_block();
  43. }
  44. u32 tables_root() const { return m_tables_root; }
  45. void set_tables_root(u32 root)
  46. {
  47. m_tables_root = root;
  48. update_zero_block();
  49. }
  50. u32 table_columns_root() const { return m_table_columns_root; }
  51. void set_table_columns_root(u32 root)
  52. {
  53. m_table_columns_root = root;
  54. update_zero_block();
  55. }
  56. u32 version() const { return m_version; }
  57. u32 user_value(size_t index) const
  58. {
  59. VERIFY(index < m_user_values.size());
  60. return m_user_values[index];
  61. }
  62. void set_user_value(size_t index, u32 value)
  63. {
  64. VERIFY(index < m_user_values.size());
  65. m_user_values[index] = value;
  66. update_zero_block();
  67. }
  68. void add_to_wal(u32 block, ByteBuffer& buffer) { m_write_ahead_log.set(block, buffer); }
  69. void flush();
  70. private:
  71. bool seek_block(u32);
  72. void read_zero_block();
  73. void initialize_zero_block();
  74. void update_zero_block();
  75. RefPtr<Core::File> m_file;
  76. u32 m_free_list { 0 };
  77. u32 m_next_block { 1 };
  78. u32 m_end_of_file { 1 };
  79. u32 m_schemas_root { 0 };
  80. u32 m_tables_root { 0 };
  81. u32 m_table_columns_root { 0 };
  82. u32 m_version { 0x00000001 };
  83. Array<u32, 16> m_user_values;
  84. HashMap<u32, ByteBuffer> m_write_ahead_log;
  85. };
  86. }