Tuple.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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/Serialize.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()
  16. , m_data()
  17. {
  18. }
  19. Tuple::Tuple(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.append(Value(element.type));
  26. }
  27. }
  28. Tuple::Tuple(TupleDescriptor const& descriptor, ByteBuffer& buffer, size_t& offset)
  29. : Tuple(descriptor)
  30. {
  31. deserialize(buffer, offset);
  32. }
  33. Tuple::Tuple(TupleDescriptor const& descriptor, ByteBuffer& buffer)
  34. : Tuple(descriptor)
  35. {
  36. size_t offset = 0;
  37. deserialize(buffer, offset);
  38. }
  39. void Tuple::deserialize(ByteBuffer& buffer, size_t& offset)
  40. {
  41. dbgln_if(SQL_DEBUG, "deserialize tuple at offset {}", offset);
  42. deserialize_from<u32>(buffer, offset, m_pointer);
  43. dbgln_if(SQL_DEBUG, "pointer: {}", m_pointer);
  44. m_data.clear();
  45. for (auto& part : m_descriptor) {
  46. m_data.append(Value(part.type, buffer, offset));
  47. dbgln_if(SQL_DEBUG, "Deserialized element {} = {}", part.name, m_data.last().to_string().value());
  48. }
  49. }
  50. void Tuple::serialize(ByteBuffer& buffer) const
  51. {
  52. VERIFY(m_descriptor.size() == m_data.size());
  53. dbgln_if(SQL_DEBUG, "Serializing tuple pointer {}", pointer());
  54. serialize_to<u32>(buffer, pointer());
  55. for (auto ix = 0u; ix < m_descriptor.size(); ix++) {
  56. auto& key_part = m_data[ix];
  57. if constexpr (SQL_DEBUG) {
  58. auto str_opt = key_part.to_string();
  59. auto& key_part_definition = m_descriptor[ix];
  60. dbgln("Serialized part {} = {}", key_part_definition.name, (str_opt.has_value()) ? str_opt.value() : "(null)");
  61. }
  62. key_part.serialize(buffer);
  63. }
  64. }
  65. Tuple::Tuple(Tuple const& other)
  66. : m_descriptor()
  67. , m_data()
  68. {
  69. copy_from(other);
  70. }
  71. Tuple& Tuple::operator=(Tuple const& other)
  72. {
  73. if (this != &other) {
  74. copy_from(other);
  75. }
  76. return *this;
  77. }
  78. Optional<size_t> Tuple::index_of(String name) const
  79. {
  80. auto n = move(name);
  81. for (auto ix = 0u; ix < m_descriptor.size(); ix++) {
  82. auto& part = m_descriptor[ix];
  83. if (part.name == n) {
  84. return (int)ix;
  85. }
  86. }
  87. return {};
  88. }
  89. Value const& Tuple::operator[](size_t ix) const
  90. {
  91. VERIFY(ix < m_data.size());
  92. return m_data[ix];
  93. }
  94. Value& Tuple::operator[](size_t ix)
  95. {
  96. VERIFY(ix < m_data.size());
  97. return m_data[ix];
  98. }
  99. Value const& Tuple::operator[](String const& name) const
  100. {
  101. auto index = index_of(name);
  102. VERIFY(index.has_value());
  103. return (*this)[index.value()];
  104. }
  105. Value& Tuple::operator[](String const& name)
  106. {
  107. auto index = index_of(name);
  108. VERIFY(index.has_value());
  109. return (*this)[index.value()];
  110. }
  111. void Tuple::append(const Value& value)
  112. {
  113. VERIFY(m_descriptor.size() == 0);
  114. m_data.append(value);
  115. }
  116. Tuple& Tuple::operator+=(Value const& value)
  117. {
  118. append(value);
  119. return *this;
  120. }
  121. bool Tuple::is_compatible(Tuple const& other) const
  122. {
  123. if ((m_descriptor.size() == 0) && (other.m_descriptor.size() == 0)) {
  124. return true;
  125. }
  126. if (m_descriptor.size() != other.m_descriptor.size()) {
  127. return false;
  128. }
  129. for (auto ix = 0u; ix < m_descriptor.size(); ix++) {
  130. auto& my_part = m_descriptor[ix];
  131. auto& other_part = other.m_descriptor[ix];
  132. if (my_part.type != other_part.type) {
  133. return false;
  134. }
  135. if (my_part.order != other_part.order) {
  136. return false;
  137. }
  138. }
  139. return true;
  140. }
  141. String Tuple::to_string() const
  142. {
  143. StringBuilder builder;
  144. for (auto& part : m_data) {
  145. if (!builder.is_empty()) {
  146. builder.append('|');
  147. }
  148. auto str_opt = part.to_string();
  149. builder.append((str_opt.has_value()) ? str_opt.value() : "(null)");
  150. }
  151. if (pointer() != 0) {
  152. builder.appendff(":{}", pointer());
  153. }
  154. return builder.build();
  155. }
  156. Vector<String> Tuple::to_string_vector() const
  157. {
  158. Vector<String> ret;
  159. for (auto& value : m_data) {
  160. ret.append(value.to_string().value());
  161. }
  162. return ret;
  163. }
  164. size_t Tuple::size() const
  165. {
  166. size_t sz = sizeof(u32);
  167. for (auto& part : m_data) {
  168. sz += part.size();
  169. }
  170. return sz;
  171. }
  172. void Tuple::copy_from(const Tuple& other)
  173. {
  174. m_descriptor.clear();
  175. for (TupleElement const& part : other.m_descriptor) {
  176. m_descriptor.append(part);
  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. }