Tuple.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/ByteString.h>
  7. #include <AK/StringBuilder.h>
  8. #include <LibSQL/Serializer.h>
  9. #include <LibSQL/Tuple.h>
  10. #include <LibSQL/TupleDescriptor.h>
  11. #include <LibSQL/Value.h>
  12. namespace SQL {
  13. Tuple::Tuple()
  14. : m_descriptor(adopt_ref(*new TupleDescriptor))
  15. , m_data()
  16. {
  17. }
  18. Tuple::Tuple(NonnullRefPtr<TupleDescriptor> const& descriptor, Block::Index block_index)
  19. : m_descriptor(descriptor)
  20. , m_data()
  21. , m_block_index(block_index)
  22. {
  23. for (auto& element : *descriptor)
  24. m_data.empend(element.type);
  25. }
  26. Tuple::Tuple(NonnullRefPtr<TupleDescriptor> const& descriptor, Serializer& serializer)
  27. : Tuple(descriptor)
  28. {
  29. deserialize(serializer);
  30. }
  31. void Tuple::deserialize(Serializer& serializer)
  32. {
  33. dbgln_if(SQL_DEBUG, "deserialize tuple at offset {}", serializer.offset());
  34. serializer.deserialize_to<u32>(m_block_index);
  35. dbgln_if(SQL_DEBUG, "block_index: {}", m_block_index);
  36. auto number_of_elements = serializer.deserialize<u32>();
  37. m_data.clear();
  38. m_descriptor->clear();
  39. for (auto ix = 0u; ix < number_of_elements; ++ix) {
  40. m_descriptor->append(serializer.deserialize<TupleElementDescriptor>());
  41. m_data.append(serializer.deserialize<Value>());
  42. }
  43. }
  44. void Tuple::serialize(Serializer& serializer) const
  45. {
  46. VERIFY(m_descriptor->size() == m_data.size());
  47. dbgln_if(SQL_DEBUG, "Serializing tuple with block_index {}", block_index());
  48. serializer.serialize<u32>(block_index());
  49. serializer.serialize<u32>(m_descriptor->size());
  50. for (auto ix = 0u; ix < m_descriptor->size(); ix++) {
  51. serializer.serialize<TupleElementDescriptor>((*m_descriptor)[ix]);
  52. serializer.serialize<Value>(m_data[ix]);
  53. }
  54. }
  55. Tuple::Tuple(Tuple const& other)
  56. : m_descriptor(other.m_descriptor)
  57. , m_data()
  58. {
  59. copy_from(other);
  60. }
  61. Tuple& Tuple::operator=(Tuple const& other)
  62. {
  63. if (this != &other)
  64. copy_from(other);
  65. return *this;
  66. }
  67. Optional<size_t> Tuple::index_of(StringView name) const
  68. {
  69. for (auto ix = 0u; ix < m_descriptor->size(); ix++) {
  70. auto& part = (*m_descriptor)[ix];
  71. if (part.name == name)
  72. return ix;
  73. }
  74. return {};
  75. }
  76. Value const& Tuple::operator[](ByteString const& name) const
  77. {
  78. auto index = index_of(name);
  79. VERIFY(index.has_value());
  80. return (*this)[index.value()];
  81. }
  82. Value& Tuple::operator[](ByteString const& name)
  83. {
  84. auto index = index_of(name);
  85. VERIFY(index.has_value());
  86. return (*this)[index.value()];
  87. }
  88. void Tuple::append(Value const& value)
  89. {
  90. VERIFY(descriptor()->size() >= size());
  91. if (descriptor()->size() == size())
  92. descriptor()->append(value.descriptor());
  93. m_data.append(value);
  94. }
  95. Tuple& Tuple::operator+=(Value const& value)
  96. {
  97. append(value);
  98. return *this;
  99. }
  100. void Tuple::extend(Tuple const& other)
  101. {
  102. VERIFY((descriptor()->size() == size()) || (descriptor()->size() >= size() + other.size()));
  103. if (descriptor()->size() == size())
  104. descriptor()->extend(other.descriptor());
  105. m_data.extend(other.m_data);
  106. }
  107. size_t Tuple::length() const
  108. {
  109. size_t len = 2 * sizeof(u32);
  110. for (auto ix = 0u; ix < m_descriptor->size(); ix++) {
  111. auto& descriptor = (*m_descriptor)[ix];
  112. auto& value = m_data[ix];
  113. len += descriptor.length();
  114. len += value.length();
  115. }
  116. return len;
  117. }
  118. ByteString Tuple::to_byte_string() const
  119. {
  120. StringBuilder builder;
  121. for (auto& part : m_data) {
  122. if (!builder.is_empty())
  123. builder.append('|');
  124. builder.append(part.to_byte_string());
  125. }
  126. if (block_index() != 0)
  127. builder.appendff(":{}", block_index());
  128. return builder.to_byte_string();
  129. }
  130. void Tuple::copy_from(Tuple const& other)
  131. {
  132. if (*m_descriptor != *other.m_descriptor) {
  133. m_descriptor->clear();
  134. for (TupleElementDescriptor const& part : *other.m_descriptor)
  135. m_descriptor->append(part);
  136. }
  137. m_data.clear();
  138. for (auto& part : other.m_data)
  139. m_data.append(part);
  140. m_block_index = other.block_index();
  141. }
  142. int Tuple::compare(Tuple const& other) const
  143. {
  144. auto num_values = min(m_data.size(), other.m_data.size());
  145. VERIFY(num_values > 0);
  146. for (auto ix = 0u; ix < num_values; ix++) {
  147. auto ret = m_data[ix].compare(other.m_data[ix]);
  148. if (ret != 0) {
  149. if ((ix < m_descriptor->size()) && (*m_descriptor)[ix].order == Order::Descending)
  150. ret = -ret;
  151. return ret;
  152. }
  153. }
  154. return 0;
  155. }
  156. int Tuple::match(Tuple const& other) const
  157. {
  158. auto other_index = 0u;
  159. for (auto const& part : *other.descriptor()) {
  160. auto const& other_value = other[other_index];
  161. if (other_value.is_null())
  162. return 0;
  163. auto my_index = index_of(part.name);
  164. if (!my_index.has_value())
  165. return -1;
  166. auto ret = m_data[my_index.value()].compare(other_value);
  167. if (ret != 0)
  168. return ((*m_descriptor)[my_index.value()].order == Order::Descending) ? -ret : ret;
  169. other_index++;
  170. }
  171. return 0;
  172. }
  173. u32 Tuple::hash() const
  174. {
  175. u32 ret = 0u;
  176. for (auto& value : m_data) {
  177. // This is an extension of the pair_int_hash function from AK/HashFunctions.h:
  178. if (!ret)
  179. ret = value.hash();
  180. else
  181. ret = int_hash((ret * 209) ^ (value.hash() * 413));
  182. }
  183. return ret;
  184. }
  185. }