Tuple.cpp 6.0 KB

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