Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove enum string feature #7858

Merged
merged 5 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 4 additions & 54 deletions src/realm/spec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,6 @@ void Spec::init(MemRef mem) noexcept
m_top.add(0);
}

// Enumkeys array is only there when there are StringEnum columns
if (auto ref = m_top.get_as_ref(s_enum_keys_ndx)) {
m_enumkeys.init_from_ref(ref);
}
else {
m_enumkeys.detach();
}

if (m_top.get_as_ref(s_col_keys_ndx) == 0) {
// This is an upgrade - create column key array
MemRef mem_ref = Array::create_empty_array(Array::type_Normal, false, m_top.get_alloc()); // Throws
Expand Down Expand Up @@ -96,14 +88,6 @@ void Spec::update_from_parent() noexcept
m_types.update_from_parent();
m_names.update_from_parent();
m_attr.update_from_parent();

if (m_top.get_as_ref(s_enum_keys_ndx) != 0) {
m_enumkeys.update_from_parent();
}
else {
m_enumkeys.detach();
}

m_keys.update_from_parent();

update_internals();
Expand All @@ -115,50 +99,35 @@ MemRef Spec::create_empty_spec(Allocator& alloc)
// The 'spec_set' contains the specification (types and names) of
// all columns and sub-tables
Array spec_set(alloc);
_impl::DeepArrayDestroyGuard dg(&spec_set);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

couldn't we keep this one?

spec_set.create(Array::type_HasRefs); // Throws

_impl::DeepArrayRefDestroyGuard dg_2(alloc);
{
// One type for each column
bool context_flag = false;
MemRef mem = Array::create_empty_array(Array::type_Normal, context_flag, alloc); // Throws
dg_2.reset(mem.get_ref());
int_fast64_t v(from_ref(mem.get_ref()));
spec_set.add(v); // Throws
dg_2.release();
spec_set.add(from_ref(mem.get_ref())); // Throws
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the old code seems to be very cautious about allocation failure...
are we safe without it because we can rely on commit rollbacks? Alternatively, could we order things like this?

spec_set.add(0);
MemRef mem = create_empty_array(...);
spec_set.set(spec_set.size() - 1, from_ref(mem.get_ref()));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think all these destroy guards are from the time where we could have freestanding tables. They are not needed when we have transactions. Allocations will always be done in the slab area and it will be reset by every commit/rollback (reset_free_space_tracking()).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks for confirming!

}
{
size_t size = 0;
// One name for each column
MemRef mem = ArrayStringShort::create_array(size, alloc); // Throws
dg_2.reset(mem.get_ref());
int_fast64_t v = from_ref(mem.get_ref());
spec_set.add(v); // Throws
dg_2.release();
spec_set.add(from_ref(mem.get_ref())); // Throws
}
{
// One attrib set for each column
bool context_flag = false;
MemRef mem = Array::create_empty_array(Array::type_Normal, context_flag, alloc); // Throws
dg_2.reset(mem.get_ref());
int_fast64_t v = from_ref(mem.get_ref());
spec_set.add(v); // Throws
dg_2.release();
spec_set.add(from_ref(mem.get_ref())); // Throws
}
spec_set.add(0); // Nested collections array
spec_set.add(0); // Enumkeys array
{
// One key for each column
bool context_flag = false;
MemRef mem = Array::create_empty_array(Array::type_Normal, context_flag, alloc); // Throws
dg_2.reset(mem.get_ref());
int_fast64_t v = from_ref(mem.get_ref());
spec_set.add(v); // Throws
dg_2.release();
spec_set.add(from_ref(mem.get_ref())); // Throws
}

dg.release();
return spec_set.get_mem();
}

Expand Down Expand Up @@ -204,10 +173,6 @@ void Spec::insert_column(size_t column_ndx, ColKey col_key, ColumnType type, Str
m_attr.insert(column_ndx, attr); // Throws
m_keys.insert(column_ndx, col_key.value);

if (m_enumkeys.is_attached() && type != col_type_BackLink) {
m_enumkeys.insert(column_ndx, 0);
}

update_internals();
}

Expand All @@ -216,21 +181,6 @@ void Spec::erase_column(size_t column_ndx)
REALM_ASSERT(column_ndx < m_types.size());

if (ColumnType(int(m_types.get(column_ndx))) != col_type_BackLink) {
// Remove this column from the enum keys lookup and clean it up if it's now empty
if (m_enumkeys.is_attached()) {
m_enumkeys.erase(column_ndx); // Throws
bool all_empty = true;
for (size_t i = 0; i < m_enumkeys.size(); i++) {
if (m_enumkeys.get(i) != 0) {
all_empty = false;
break;
}
}
if (all_empty) {
m_enumkeys.destroy_deep();
m_top.set(4, 0);
}
}
m_num_public_columns--;
m_names.erase(column_ndx); // Throws
}
Expand Down
6 changes: 2 additions & 4 deletions src/realm/spec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class Spec {
static constexpr size_t s_names_ndx = 1;
static constexpr size_t s_attributes_ndx = 2;
static constexpr size_t s_vacant_1 = 3;
static constexpr size_t s_enum_keys_ndx = 4;
// static constexpr size_t s_enum_keys_ndx = 4;
static constexpr size_t s_col_keys_ndx = 5;
static constexpr size_t s_spec_max_size = 6;

Expand All @@ -100,7 +100,7 @@ class Spec {
ArrayStringShort m_names; // 2nd slot in m_top
Array m_attr; // 3rd slot in m_top
// 4th slot in m_top not cached
Array m_enumkeys; // 5th slot in m_top
// 5th slot in m_top
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest adding to the comment something like this: "5th slot in m_top, old enum keys which was never released"

Array m_keys; // 6th slot in m_top
size_t m_num_public_columns = 0;

Expand Down Expand Up @@ -150,13 +150,11 @@ inline Spec::Spec(Allocator& alloc) noexcept
, m_types(alloc)
, m_names(alloc)
, m_attr(alloc)
, m_enumkeys(alloc)
, m_keys(alloc)
{
m_types.set_parent(&m_top, s_types_ndx);
m_names.set_parent(&m_top, s_names_ndx);
m_attr.set_parent(&m_top, s_attributes_ndx);
m_enumkeys.set_parent(&m_top, s_enum_keys_ndx);
m_keys.set_parent(&m_top, s_col_keys_ndx);
}

Expand Down