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