Tuple.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 <LibSQL/Forward.h>
  9. #include <LibSQL/TupleDescriptor.h>
  10. #include <LibSQL/Value.h>
  11. namespace SQL {
  12. /**
  13. * A Key is an element of a random-access data structure persisted in a Heap.
  14. * Key objects stored in such a structure have a definition controlling the
  15. * number of parts or columns the key has, the types of the parts, and the
  16. * sort order of these parts. Besides having an optional definition, a Key
  17. * consists of one Value object per part. In addition, keys have a u32 pointer
  18. * member which points to a Heap location.
  19. *
  20. * Key objects without a definition can be used to locate/find objects in
  21. * a searchable data collection.
  22. *
  23. * FIXME Currently the Key definition is passed as an `IndexDefinition` meta
  24. * data object, meaning that names are associated with both the definition
  25. * and the parts of the key. These names are not used, meaning that key
  26. * definitions should probably be constructed in a different way.
  27. */
  28. class Tuple {
  29. public:
  30. Tuple();
  31. explicit Tuple(TupleDescriptor const&, u32 pointer = 0);
  32. Tuple(TupleDescriptor const&, ByteBuffer&, size_t&);
  33. Tuple(TupleDescriptor const&, ByteBuffer&);
  34. Tuple(Tuple const&);
  35. virtual ~Tuple() = default;
  36. Tuple& operator=(Tuple const&);
  37. [[nodiscard]] String to_string() const;
  38. explicit operator String() const { return to_string(); }
  39. bool operator<(Tuple const& other) const { return compare(other) < 0; }
  40. bool operator<=(Tuple const& other) const { return compare(other) <= 0; }
  41. bool operator==(Tuple const& other) const { return compare(other) == 0; }
  42. bool operator!=(Tuple const& other) const { return compare(other) != 0; }
  43. bool operator>(Tuple const& other) const { return compare(other) > 0; }
  44. bool operator>=(Tuple const& other) const { return compare(other) >= 0; }
  45. [[nodiscard]] bool is_null() const { return m_data.is_empty(); }
  46. [[nodiscard]] bool has(String const& name) const { return index_of(name).has_value(); }
  47. Value const& operator[](size_t ix) const;
  48. Value& operator[](size_t ix);
  49. Value const& operator[](String const& name) const;
  50. Value& operator[](String const& name);
  51. void append(Value const&);
  52. Tuple& operator+=(Value const&);
  53. [[nodiscard]] bool is_compatible(Tuple const&) const;
  54. [[nodiscard]] u32 pointer() const { return m_pointer; }
  55. void set_pointer(u32 ptr) { m_pointer = ptr; }
  56. [[nodiscard]] size_t size() const;
  57. [[nodiscard]] size_t length() const { return m_descriptor.size(); }
  58. [[nodiscard]] TupleDescriptor descriptor() const { return m_descriptor; }
  59. [[nodiscard]] int compare(Tuple const&) const;
  60. [[nodiscard]] int match(Tuple const&) const;
  61. [[nodiscard]] u32 hash() const;
  62. virtual void serialize(ByteBuffer&) const;
  63. [[nodiscard]] virtual size_t data_length() const { return descriptor().data_length(); }
  64. protected:
  65. [[nodiscard]] Optional<size_t> index_of(String) const;
  66. void copy_from(Tuple const&);
  67. void deserialize(ByteBuffer&, size_t&);
  68. private:
  69. TupleDescriptor m_descriptor;
  70. Vector<Value> m_data;
  71. u32 m_pointer { 0 };
  72. };
  73. }