Skip to content

Commit

Permalink
Avoid string index upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
jedelbo authored and ironage committed Sep 21, 2023
1 parent e34e29d commit 333982a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 25 deletions.
6 changes: 3 additions & 3 deletions src/realm/index_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,7 @@ bool StringIndex::leaf_insert(ObjKey obj_key, key_type key, size_t offset, Strin
if (index_data == index_data_2 || suboffset > s_max_offset) {
// These strings have the same prefix up to this point but we
// don't want to recurse further, create a list in sorted order.
bool row_ndx_first = value < v2;
bool row_ndx_first = value.compare_signed(v2) < 0;
Array row_list(alloc);
row_list.create(Array::type_Normal); // Throws
row_list.add(row_ndx_first ? obj_key.value : obj_key2.value);
Expand Down Expand Up @@ -1629,7 +1629,7 @@ bool SortedListComparator::operator()(int64_t key_value, Mixed needle) // used i

// We need to be consistent with using the same compare method as how these strings
// were they were put into this ordered column in the first place.
return a < needle;
return a.compare_signed(needle) < 0;
}


Expand Down Expand Up @@ -1693,7 +1693,7 @@ void StringIndex::verify() const
while (it != it_end) {
Mixed it_data = get(ObjKey(*it));
size_t it_row = to_size_t(*it);
REALM_ASSERT(previous <= it_data);
REALM_ASSERT(previous.compare_signed(it_data) <= 0);
if (it != sub.cbegin() && previous == it_data) {
REALM_ASSERT_EX(it_row > last_row, it_row, last_row);
}
Expand Down
15 changes: 15 additions & 0 deletions src/realm/mixed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,21 @@ int Mixed::compare(const Mixed& b) const noexcept
// Observe! Changing this function breaks the file format for Set<Mixed> and the StringIndex
}

int Mixed::compare_signed(const Mixed& b) const noexcept
{
if (is_type(type_String) && b.is_type(type_String)) {
auto a_val = get_string();
auto b_val = b.get_string();
if (a_val == b_val)
return 0;
if (std::lexicographical_compare(a_val.data(), a_val.data() + a_val.size(), b_val.data(),
b_val.data() + b_val.size()))
return -1;
return 1;
}
return compare(b);
}

template <>
int64_t Mixed::export_to_type() const noexcept
{
Expand Down
2 changes: 2 additions & 0 deletions src/realm/mixed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ class Mixed {
bool is_same_type(const Mixed& b) const noexcept;
// Will use unsigned lexicographical comparison for strings
int compare(const Mixed& b) const noexcept;
// Will compare strings as arrays of signed chars
int compare_signed(const Mixed& b) const noexcept;
friend bool operator==(const Mixed& a, const Mixed& b) noexcept
{
return a.compare(b) == 0;
Expand Down
22 changes: 0 additions & 22 deletions src/realm/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -562,28 +562,6 @@ void Transaction::upgrade_file_format(int target_file_format_version)
for (auto k : table_keys) {
auto t = get_table(k);
t->migrate_string_sets(); // rewrite sets to use the new string order
t->for_each_public_column([&](ColKey col) {
if (col.get_type() != col_type_String) {
return IteratorControl::AdvanceToNext;
}
switch (t->search_index_type(col)) {
case IndexType::General: {
if (current_file_format_version < 22 && t->get_primary_key_column() == col) {
break; // this index was just added by a previous upgrade step above
}
t->remove_search_index(col);
t->add_search_index(col, IndexType::General);
break;
}
case IndexType::Fulltext:
t->remove_search_index(col);
t->add_search_index(col, IndexType::Fulltext);
break;
case IndexType::None:
break;
}
return IteratorControl::AdvanceToNext;
});
}
}
// NOTE: Additional future upgrade steps go here.
Expand Down

0 comments on commit 333982a

Please sign in to comment.