Tuple.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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(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, ByteBuffer& buffer, size_t& offset)
  29. : Tuple(descriptor)
  30. {
  31. deserialize(buffer, offset);
  32. }
  33. Tuple::Tuple(NonnullRefPtr<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::deserialize_from(buffer, offset));
  47. dbgln_if(SQL_DEBUG, "Deserialized element {} = {}", part.name, m_data.last().to_string());
  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 key_string = key_part.to_string();
  59. auto& key_part_definition = (*m_descriptor)[ix];
  60. dbgln("Serialized part {} = {}", key_part_definition.name, key_string);
  61. }
  62. key_part.serialize_to(buffer);
  63. }
  64. }
  65. Tuple::Tuple(Tuple const& other)
  66. : m_descriptor(other.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. builder.append(part.to_string());
  149. }
  150. if (pointer() != 0) {
  151. builder.appendff(":{}", pointer());
  152. }
  153. return builder.build();
  154. }
  155. Vector<String> Tuple::to_string_vector() const
  156. {
  157. Vector<String> ret;
  158. for (auto& value : m_data) {
  159. ret.append(value.to_string());
  160. }
  161. return ret;
  162. }
  163. void Tuple::copy_from(const Tuple& other)
  164. {
  165. if (*m_descriptor != *other.m_descriptor) {
  166. m_descriptor->clear();
  167. for (TupleElement const& part : *other.m_descriptor) {
  168. m_descriptor->append(part);
  169. }
  170. }
  171. m_data.clear();
  172. for (auto& part : other.m_data) {
  173. m_data.append(part);
  174. }
  175. m_pointer = other.pointer();
  176. }
  177. int Tuple::compare(const Tuple& other) const
  178. {
  179. auto num_values = min(m_data.size(), other.m_data.size());
  180. VERIFY(num_values > 0);
  181. for (auto ix = 0u; ix < num_values; ix++) {
  182. auto ret = m_data[ix].compare(other.m_data[ix]);
  183. if (ret != 0) {
  184. if ((ix < m_descriptor->size()) && (*m_descriptor)[ix].order == Order::Descending)
  185. ret = -ret;
  186. return ret;
  187. }
  188. }
  189. return 0;
  190. }
  191. int Tuple::match(const Tuple& other) const
  192. {
  193. auto other_index = 0u;
  194. for (auto& part : *other.descriptor()) {
  195. auto other_value = other[other_index];
  196. if (other_value.is_null())
  197. return 0;
  198. auto my_index = index_of(part.name);
  199. if (!my_index.has_value())
  200. return -1;
  201. auto ret = m_data[my_index.value()].compare(other_value);
  202. if (ret != 0)
  203. return ((*m_descriptor)[my_index.value()].order == Order::Descending) ? -ret : ret;
  204. other_index++;
  205. }
  206. return 0;
  207. }
  208. u32 Tuple::hash() const
  209. {
  210. u32 ret = 0u;
  211. for (auto& value : m_data) {
  212. // This is an extension of the pair_int_hash function from AK/HashFunctions.h:
  213. if (!ret)
  214. ret = value.hash();
  215. else
  216. ret = int_hash((ret * 209) ^ (value.hash() * 413));
  217. }
  218. return ret;
  219. }
  220. }