Tuple.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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(m_descriptor->size() == 0);
  107. m_data.append(value);
  108. }
  109. Tuple& Tuple::operator+=(Value const& value)
  110. {
  111. append(value);
  112. return *this;
  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. String Tuple::to_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_string());
  153. }
  154. if (pointer() != 0) {
  155. builder.appendff(":{}", pointer());
  156. }
  157. return builder.build();
  158. }
  159. Vector<String> Tuple::to_string_vector() const
  160. {
  161. Vector<String> ret;
  162. for (auto& value : m_data) {
  163. ret.append(value.to_string());
  164. }
  165. return ret;
  166. }
  167. void Tuple::copy_from(const Tuple& other)
  168. {
  169. if (*m_descriptor != *other.m_descriptor) {
  170. m_descriptor->clear();
  171. for (TupleElementDescriptor const& part : *other.m_descriptor) {
  172. m_descriptor->append(part);
  173. }
  174. }
  175. m_data.clear();
  176. for (auto& part : other.m_data) {
  177. m_data.append(part);
  178. }
  179. m_pointer = other.pointer();
  180. }
  181. int Tuple::compare(const Tuple& other) const
  182. {
  183. auto num_values = min(m_data.size(), other.m_data.size());
  184. VERIFY(num_values > 0);
  185. for (auto ix = 0u; ix < num_values; ix++) {
  186. auto ret = m_data[ix].compare(other.m_data[ix]);
  187. if (ret != 0) {
  188. if ((ix < m_descriptor->size()) && (*m_descriptor)[ix].order == Order::Descending)
  189. ret = -ret;
  190. return ret;
  191. }
  192. }
  193. return 0;
  194. }
  195. int Tuple::match(const Tuple& other) const
  196. {
  197. auto other_index = 0u;
  198. for (auto& part : *other.descriptor()) {
  199. auto other_value = other[other_index];
  200. if (other_value.is_null())
  201. return 0;
  202. auto my_index = index_of(part.name);
  203. if (!my_index.has_value())
  204. return -1;
  205. auto ret = m_data[my_index.value()].compare(other_value);
  206. if (ret != 0)
  207. return ((*m_descriptor)[my_index.value()].order == Order::Descending) ? -ret : ret;
  208. other_index++;
  209. }
  210. return 0;
  211. }
  212. u32 Tuple::hash() const
  213. {
  214. u32 ret = 0u;
  215. for (auto& value : m_data) {
  216. // This is an extension of the pair_int_hash function from AK/HashFunctions.h:
  217. if (!ret)
  218. ret = value.hash();
  219. else
  220. ret = int_hash((ret * 209) ^ (value.hash() * 413));
  221. }
  222. return ret;
  223. }
  224. }