Tuple.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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/String.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(String name) const
  72. {
  73. auto n = move(name);
  74. for (auto ix = 0u; ix < m_descriptor->size(); ix++) {
  75. auto& part = (*m_descriptor)[ix];
  76. if (part.name == n) {
  77. return (int)ix;
  78. }
  79. }
  80. return {};
  81. }
  82. Value const& Tuple::operator[](size_t ix) const
  83. {
  84. VERIFY(ix < m_data.size());
  85. return m_data[ix];
  86. }
  87. Value& Tuple::operator[](size_t ix)
  88. {
  89. VERIFY(ix < m_data.size());
  90. return m_data[ix];
  91. }
  92. Value const& Tuple::operator[](String const& name) const
  93. {
  94. auto index = index_of(name);
  95. VERIFY(index.has_value());
  96. return (*this)[index.value()];
  97. }
  98. Value& Tuple::operator[](String const& name)
  99. {
  100. auto index = index_of(name);
  101. VERIFY(index.has_value());
  102. return (*this)[index.value()];
  103. }
  104. void Tuple::append(const Value& value)
  105. {
  106. VERIFY(descriptor()->size() >= size());
  107. if (descriptor()->size() == size()) {
  108. descriptor()->append(value.descriptor());
  109. }
  110. m_data.append(value);
  111. }
  112. Tuple& Tuple::operator+=(Value const& value)
  113. {
  114. append(value);
  115. return *this;
  116. }
  117. bool Tuple::is_compatible(Tuple const& other) const
  118. {
  119. if ((m_descriptor->size() == 0) && (other.m_descriptor->size() == 0)) {
  120. return true;
  121. }
  122. if (m_descriptor->size() != other.m_descriptor->size()) {
  123. return false;
  124. }
  125. for (auto ix = 0u; ix < m_descriptor->size(); ix++) {
  126. auto& my_part = (*m_descriptor)[ix];
  127. auto& other_part = (*other.m_descriptor)[ix];
  128. if (my_part.type != other_part.type) {
  129. return false;
  130. }
  131. if (my_part.order != other_part.order) {
  132. return false;
  133. }
  134. }
  135. return true;
  136. }
  137. size_t Tuple::length() const
  138. {
  139. size_t len = 2 * sizeof(u32);
  140. for (auto ix = 0u; ix < m_descriptor->size(); ix++) {
  141. auto& descriptor = (*m_descriptor)[ix];
  142. auto& value = m_data[ix];
  143. len += descriptor.length();
  144. len += value.length();
  145. }
  146. return len;
  147. }
  148. String Tuple::to_string() const
  149. {
  150. StringBuilder builder;
  151. for (auto& part : m_data) {
  152. if (!builder.is_empty()) {
  153. builder.append('|');
  154. }
  155. builder.append(part.to_string());
  156. }
  157. if (pointer() != 0) {
  158. builder.appendff(":{}", pointer());
  159. }
  160. return builder.build();
  161. }
  162. Vector<String> Tuple::to_string_vector() const
  163. {
  164. Vector<String> ret;
  165. for (auto& value : m_data) {
  166. ret.append(value.to_string());
  167. }
  168. return ret;
  169. }
  170. void Tuple::copy_from(const Tuple& other)
  171. {
  172. if (*m_descriptor != *other.m_descriptor) {
  173. m_descriptor->clear();
  174. for (TupleElementDescriptor const& part : *other.m_descriptor) {
  175. m_descriptor->append(part);
  176. }
  177. }
  178. m_data.clear();
  179. for (auto& part : other.m_data) {
  180. m_data.append(part);
  181. }
  182. m_pointer = other.pointer();
  183. }
  184. int Tuple::compare(const Tuple& other) const
  185. {
  186. auto num_values = min(m_data.size(), other.m_data.size());
  187. VERIFY(num_values > 0);
  188. for (auto ix = 0u; ix < num_values; ix++) {
  189. auto ret = m_data[ix].compare(other.m_data[ix]);
  190. if (ret != 0) {
  191. if ((ix < m_descriptor->size()) && (*m_descriptor)[ix].order == Order::Descending)
  192. ret = -ret;
  193. return ret;
  194. }
  195. }
  196. return 0;
  197. }
  198. int Tuple::match(const Tuple& other) const
  199. {
  200. auto other_index = 0u;
  201. for (auto& part : *other.descriptor()) {
  202. auto other_value = other[other_index];
  203. if (other_value.is_null())
  204. return 0;
  205. auto my_index = index_of(part.name);
  206. if (!my_index.has_value())
  207. return -1;
  208. auto ret = m_data[my_index.value()].compare(other_value);
  209. if (ret != 0)
  210. return ((*m_descriptor)[my_index.value()].order == Order::Descending) ? -ret : ret;
  211. other_index++;
  212. }
  213. return 0;
  214. }
  215. u32 Tuple::hash() const
  216. {
  217. u32 ret = 0u;
  218. for (auto& value : m_data) {
  219. // This is an extension of the pair_int_hash function from AK/HashFunctions.h:
  220. if (!ret)
  221. ret = value.hash();
  222. else
  223. ret = int_hash((ret * 209) ^ (value.hash() * 413));
  224. }
  225. return ret;
  226. }
  227. }