1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-28 08:15:07 +00:00

LibSQL: Make TupleDescriptor a shared pointer instead of a stack object

Tuple descriptors are basically the same for for example all rows in
a table. Makes sense to share them instead of copying them for every
single row.
This commit is contained in:
Jan de Visser 2021-07-13 13:47:08 -04:00 committed by Andreas Kling
parent 9e225d2d05
commit a5e28f2897
17 changed files with 95 additions and 94 deletions

View file

@ -16,28 +16,28 @@
namespace SQL {
Tuple::Tuple()
: m_descriptor()
: m_descriptor(adopt_ref(*new TupleDescriptor))
, m_data()
{
}
Tuple::Tuple(TupleDescriptor const& descriptor, u32 pointer)
Tuple::Tuple(NonnullRefPtr<TupleDescriptor> const& descriptor, u32 pointer)
: m_descriptor(descriptor)
, m_data()
, m_pointer(pointer)
{
for (auto& element : descriptor) {
m_data.append(Value(element.type));
for (auto& element : *descriptor) {
m_data.empend(element.type);
}
}
Tuple::Tuple(TupleDescriptor const& descriptor, ByteBuffer& buffer, size_t& offset)
Tuple::Tuple(NonnullRefPtr<TupleDescriptor> const& descriptor, ByteBuffer& buffer, size_t& offset)
: Tuple(descriptor)
{
deserialize(buffer, offset);
}
Tuple::Tuple(TupleDescriptor const& descriptor, ByteBuffer& buffer)
Tuple::Tuple(NonnullRefPtr<TupleDescriptor> const& descriptor, ByteBuffer& buffer)
: Tuple(descriptor)
{
size_t offset = 0;
@ -50,22 +50,22 @@ void Tuple::deserialize(ByteBuffer& buffer, size_t& offset)
deserialize_from<u32>(buffer, offset, m_pointer);
dbgln_if(SQL_DEBUG, "pointer: {}", m_pointer);
m_data.clear();
for (auto& part : m_descriptor) {
m_data.append(Value(part.type, buffer, offset));
dbgln_if(SQL_DEBUG, "Deserialized element {} = {}", part.name, m_data.last().to_string().value());
for (auto& part : *m_descriptor) {
m_data.empend(part.type, buffer, offset);
dbgln_if(SQL_DEBUG, "Deserialized element {} = {}", part.name, m_data.last().to_string());
}
}
void Tuple::serialize(ByteBuffer& buffer) const
{
VERIFY(m_descriptor.size() == m_data.size());
VERIFY(m_descriptor->size() == m_data.size());
dbgln_if(SQL_DEBUG, "Serializing tuple pointer {}", pointer());
serialize_to<u32>(buffer, pointer());
for (auto ix = 0u; ix < m_descriptor.size(); ix++) {
for (auto ix = 0u; ix < m_descriptor->size(); ix++) {
auto& key_part = m_data[ix];
if constexpr (SQL_DEBUG) {
auto str_opt = key_part.to_string();
auto& key_part_definition = m_descriptor[ix];
auto& key_part_definition = (*m_descriptor)[ix];
dbgln("Serialized part {} = {}", key_part_definition.name, (str_opt.has_value()) ? str_opt.value() : "(null)");
}
key_part.serialize(buffer);
@ -73,7 +73,7 @@ void Tuple::serialize(ByteBuffer& buffer) const
}
Tuple::Tuple(Tuple const& other)
: m_descriptor()
: m_descriptor(other.m_descriptor)
, m_data()
{
copy_from(other);
@ -90,8 +90,8 @@ Tuple& Tuple::operator=(Tuple const& other)
Optional<size_t> Tuple::index_of(String name) const
{
auto n = move(name);
for (auto ix = 0u; ix < m_descriptor.size(); ix++) {
auto& part = m_descriptor[ix];
for (auto ix = 0u; ix < m_descriptor->size(); ix++) {
auto& part = (*m_descriptor)[ix];
if (part.name == n) {
return (int)ix;
}
@ -127,7 +127,7 @@ Value& Tuple::operator[](String const& name)
void Tuple::append(const Value& value)
{
VERIFY(m_descriptor.size() == 0);
VERIFY(m_descriptor->size() == 0);
m_data.append(value);
}
@ -139,15 +139,15 @@ Tuple& Tuple::operator+=(Value const& value)
bool Tuple::is_compatible(Tuple const& other) const
{
if ((m_descriptor.size() == 0) && (other.m_descriptor.size() == 0)) {
if ((m_descriptor->size() == 0) && (other.m_descriptor->size() == 0)) {
return true;
}
if (m_descriptor.size() != other.m_descriptor.size()) {
if (m_descriptor->size() != other.m_descriptor->size()) {
return false;
}
for (auto ix = 0u; ix < m_descriptor.size(); ix++) {
auto& my_part = m_descriptor[ix];
auto& other_part = other.m_descriptor[ix];
for (auto ix = 0u; ix < m_descriptor->size(); ix++) {
auto& my_part = (*m_descriptor)[ix];
auto& other_part = (*other.m_descriptor)[ix];
if (my_part.type != other_part.type) {
return false;
}
@ -183,20 +183,13 @@ Vector<String> Tuple::to_string_vector() const
return ret;
}
size_t Tuple::size() const
{
size_t sz = sizeof(u32);
for (auto& part : m_data) {
sz += part.size();
}
return sz;
}
void Tuple::copy_from(const Tuple& other)
{
m_descriptor.clear();
for (TupleElement const& part : other.m_descriptor) {
m_descriptor.append(part);
if (*m_descriptor != *other.m_descriptor) {
m_descriptor->clear();
for (TupleElement const& part : *other.m_descriptor) {
m_descriptor->append(part);
}
}
m_data.clear();
for (auto& part : other.m_data) {
@ -212,7 +205,7 @@ int Tuple::compare(const Tuple& other) const
for (auto ix = 0u; ix < num_values; ix++) {
auto ret = m_data[ix].compare(other.m_data[ix]);
if (ret != 0) {
if ((ix < m_descriptor.size()) && m_descriptor[ix].order == Order::Descending)
if ((ix < m_descriptor->size()) && (*m_descriptor)[ix].order == Order::Descending)
ret = -ret;
return ret;
}
@ -223,7 +216,7 @@ int Tuple::compare(const Tuple& other) const
int Tuple::match(const Tuple& other) const
{
auto other_index = 0u;
for (auto& part : other.descriptor()) {
for (auto& part : *other.descriptor()) {
auto other_value = other[other_index];
if (other_value.is_null())
return 0;
@ -232,7 +225,7 @@ int Tuple::match(const Tuple& other) const
return -1;
auto ret = m_data[my_index.value()].compare(other_value);
if (ret != 0)
return (m_descriptor[my_index.value()].order == Order::Descending) ? -ret : ret;
return ((*m_descriptor)[my_index.value()].order == Order::Descending) ? -ret : ret;
other_index++;
}
return 0;