Tuple.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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/Vector.h>
  8. #include <LibSQL/Forward.h>
  9. #include <LibSQL/TupleDescriptor.h>
  10. #include <LibSQL/Value.h>
  11. namespace SQL {
  12. /**
  13. * A Tuple is an element of a random-access data structure persisted in a Heap.
  14. * Tuple objects stored in such a structure have a definition controlling the
  15. * number of parts or columns the tuple has, the types of the parts, and the
  16. * sort order of these parts. Besides having an optional definition, a Tuple
  17. * consists of one Value object per part. In addition, tuples have a u32 pointer
  18. * member which points to a Heap location.
  19. *
  20. * Tuple is a base class; concrete subclasses are Key, which implements the
  21. * elements of an index, and Row, which implements the rows in a table.
  22. */
  23. class Tuple {
  24. public:
  25. Tuple();
  26. explicit Tuple(NonnullRefPtr<TupleDescriptor> const&, Block::Index = 0);
  27. Tuple(NonnullRefPtr<TupleDescriptor> const&, Serializer&);
  28. Tuple(Tuple const&);
  29. virtual ~Tuple() = default;
  30. Tuple& operator=(Tuple const&);
  31. [[nodiscard]] ByteString to_byte_string() const;
  32. explicit operator ByteString() const { return to_byte_string(); }
  33. bool operator<(Tuple const& other) const { return compare(other) < 0; }
  34. bool operator<=(Tuple const& other) const { return compare(other) <= 0; }
  35. bool operator==(Tuple const& other) const { return compare(other) == 0; }
  36. bool operator!=(Tuple const& other) const { return compare(other) != 0; }
  37. bool operator>(Tuple const& other) const { return compare(other) > 0; }
  38. bool operator>=(Tuple const& other) const { return compare(other) >= 0; }
  39. [[nodiscard]] bool is_null() const { return m_data.is_empty(); }
  40. [[nodiscard]] bool has(ByteString const& name) const { return index_of(name).has_value(); }
  41. Value const& operator[](size_t ix) const { return m_data[ix]; }
  42. Value& operator[](size_t ix) { return m_data[ix]; }
  43. Value const& operator[](ByteString const& name) const;
  44. Value& operator[](ByteString const& name);
  45. void append(Value const&);
  46. Tuple& operator+=(Value const&);
  47. void extend(Tuple const&);
  48. [[nodiscard]] Block::Index block_index() const { return m_block_index; }
  49. void set_block_index(Block::Index index) { m_block_index = index; }
  50. [[nodiscard]] size_t size() const { return m_data.size(); }
  51. [[nodiscard]] virtual size_t length() const;
  52. void clear() { m_data.clear(); }
  53. [[nodiscard]] NonnullRefPtr<TupleDescriptor> descriptor() const { return m_descriptor; }
  54. [[nodiscard]] int compare(Tuple const&) const;
  55. [[nodiscard]] int match(Tuple const&) const;
  56. [[nodiscard]] u32 hash() const;
  57. [[nodiscard]] Vector<Value> take_data() { return move(m_data); }
  58. protected:
  59. [[nodiscard]] Optional<size_t> index_of(StringView) const;
  60. void copy_from(Tuple const&);
  61. virtual void serialize(Serializer&) const;
  62. virtual void deserialize(Serializer&);
  63. private:
  64. NonnullRefPtr<TupleDescriptor> m_descriptor;
  65. Vector<Value> m_data;
  66. Block::Index m_block_index { 0 };
  67. friend Serializer;
  68. };
  69. }