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

Bump file format version #7977

Merged
merged 3 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* None.

### Compatibility
* Fileformat: Generates files with format v24. Reads and automatically upgrade from fileformat v10. If you want to upgrade from an earlier file format version you will have to use RealmCore v13.x.y or earlier.
* Fileformat: Generates files with format v25. Reads and automatically upgrade from fileformat v10. If you want to upgrade from an earlier file format version you will have to use RealmCore v13.x.y or earlier.

-----------

Expand Down
7 changes: 4 additions & 3 deletions src/realm/backup_restore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ using VersionList = BackupHandler::VersionList;
using VersionTimeList = BackupHandler::VersionTimeList;

// Note: accepted versions should have new versions added at front
const VersionList BackupHandler::accepted_versions_ = {24, 23, 22, 21, 20, 11, 10};
const VersionList BackupHandler::accepted_versions_ = {25, 24, 23, 22, 21, 20, 11, 10};

// the pair is <version, age-in-seconds>
// we keep backup files in 3 months.
static constexpr int three_months = 3 * 31 * 24 * 60 * 60;
const VersionTimeList BackupHandler::delete_versions_{{23, three_months}, {22, three_months}, {21, three_months},
{20, three_months}, {11, three_months}, {10, three_months}};
const VersionTimeList BackupHandler::delete_versions_{{24, three_months}, {23, three_months}, {22, three_months},
{21, three_months}, {20, three_months}, {11, three_months},
{10, three_months}};


// helper functions
Expand Down
1 change: 1 addition & 0 deletions src/realm/group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ int Group::read_only_version_check(SlabAlloc& alloc, ref_type top_ref, const std
case 0:
file_format_ok = (top_ref == 0);
break;
case 24:
Copy link
Member

Choose a reason for hiding this comment

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

why this magic number here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the version(s) we can open in read-only mode

Copy link
Member

Choose a reason for hiding this comment

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

OK, got it, because v24 will be eventually rewritten in v25 after compression!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because we can read v24 without upgrade. If it is in read-only mode, we will not make any commits and therefore no compression will happen.

case g_current_file_format_version:
file_format_ok = true;
break;
Expand Down
5 changes: 4 additions & 1 deletion src/realm/group.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -764,14 +764,17 @@ class Group : public ArrayParent {
/// Backlinks in BPlusTree
/// Sort order of Strings changed (affects sets and the string index)
///
/// 25 Enhanced layout of NodeHeader to support compression.
/// Integer arrays are stored in a compressed format.
///
/// IMPORTANT: When introducing a new file format version, be sure to review
/// the file validity checks in Group::open() and DB::do_open, the file
/// format selection logic in
/// Group::get_target_file_format_version_for_session(), and the file format
/// upgrade logic in Group::upgrade_file_format(), AND the lists of accepted
/// file formats and the version deletion list residing in "backup_restore.cpp"

static constexpr int g_current_file_format_version = 24;
static constexpr int g_current_file_format_version = 25;

int get_file_format_version() const noexcept;
void set_file_format_version(int) noexcept;
Expand Down
7 changes: 6 additions & 1 deletion src/realm/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ void Transaction::upgrade_file_format(int target_file_format_version)
// Be sure to revisit the following upgrade logic when a new file format
// version is introduced. The following assert attempt to help you not
// forget it.
REALM_ASSERT_EX(target_file_format_version == 24, target_file_format_version);
REALM_ASSERT_EX(target_file_format_version == 25, target_file_format_version);

// DB::do_open() must ensure that only supported version are allowed.
// It does that by asking backup if the current file format version is
Expand Down Expand Up @@ -584,6 +584,11 @@ void Transaction::upgrade_file_format(int target_file_format_version)
t->migrate_col_keys();
}
}
if (current_file_format_version < 25) {
for (auto k : table_keys) {
get_table(k);
}
}
// NOTE: Additional future upgrade steps go here.
}

Expand Down
108 changes: 106 additions & 2 deletions test/test_upgrade_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <realm/version.hpp>
#include "test.hpp"
#include "test_table_helper.hpp"
#include "util/compare_groups.hpp"

#include <external/json/json.hpp>

Expand Down Expand Up @@ -273,8 +274,8 @@ TEST(Upgrade_Database_11)
TableRef foo = g.add_table_with_primary_key("foo", type_Int, "id", false);
TableRef bar = g.add_table_with_primary_key("bar", type_String, "name", false);
TableRef o = g.add_table("origin");
auto col1 = o->add_column_link(type_Link, "link1", *foo);
auto col2 = o->add_column_link(type_Link, "link2", *bar);
auto col1 = o->add_column(*foo, "link1");
auto col2 = o->add_column(*bar, "link2");

for (auto id : ids) {
auto obj = foo->create_object_with_primary_key(id);
Expand Down Expand Up @@ -884,4 +885,107 @@ TEST_IF(Upgrade_Database_23, REALM_MAX_BPNODE_SIZE == 4 || REALM_MAX_BPNODE_SIZE
g.write(path);
#endif // TEST_READ_UPGRADE_MODE
}

TEST_IF(Upgrade_Database_24, REALM_MAX_BPNODE_SIZE == 4 || REALM_MAX_BPNODE_SIZE == 1000)
{
std::string path = test_util::get_test_resource_path() + "test_upgrade_database_" +
util::to_string(REALM_MAX_BPNODE_SIZE) + "_24.realm";

#if TEST_READ_UPGRADE_MODE
CHECK_OR_RETURN(File::exists(path));

SHARED_GROUP_TEST_PATH(temp_copy);


// Make a copy of the database so that we keep the original file intact and unmodified
File::copy(path, temp_copy);
auto hist = make_in_realm_history();
DBOptions options;
options.logger = test_context.logger;
auto sg = DB::create(*hist, temp_copy, options);
auto wt = sg->start_write();
// rt->to_json(std::cout);
wt->verify();

SHARED_GROUP_TEST_PATH(path2);
wt->write(path2);
auto db2 = DB::create(path2);
auto wt2 = db2->start_write();
CHECK(test_util::compare_groups(*wt, *wt2));
#else
// NOTE: This code must be executed from an old file-format-version 24
// core in order to create a file-format-version 25 test file!

const size_t cnt = 10 * REALM_MAX_BPNODE_SIZE;

std::vector<StringData> string_values{
"white", "yellow", "red", "orange", "green", "blue", "grey", "violet", "purple", "black",
};

StringData long_string(R"(1. Jeg ved en lærkerede,
jeg siger ikke mer;
den findes på en hede,
et sted som ingen ser.

2. I reden er der unger,
og ungerne har dun.
De pipper de har tunger,
og reden er så lun.

3. Og de to gamle lærker,
de flyver tæt omkring.
Jeg tænker nok de mærker,
jeg gør dem ingenting.

4. Jeg lurer bag en slåen.
Der står jeg ganske nær.
Jeg rækker mig på tåen
og holder på mit vejr.

5. For ræven han vil bide
og drengen samle bær.
men ingen skal få vide,
hvor lærkereden er.
)");
StringData dict_value(
R"({"Seven":7, "Six":6, "Points": [1.25, 4.5, 6.75], "Attributes": {"Height": 202, "Weight": 92}})");

Timestamp now(std::chrono::system_clock::now());
auto now_seconds = now.get_seconds();

Group g;

TableRef t = g.add_table_with_primary_key("table", type_ObjectId, "_id", false);
auto col_int = t->add_column(type_Int, "int");
auto col_optint = t->add_column(type_Int, "optint", true);
/* auto col_bool = */ t->add_column(type_Bool, "bool");
auto col_string = t->add_column(type_String, "string");
/* auto col_binary = */ t->add_column(type_Binary, "binary");
auto col_mixed = t->add_column(type_Mixed, "any");
auto col_date = t->add_column(type_Timestamp, "date");
/* auto col_float = */ t->add_column(type_Float, "float");
/* auto col_double = */ t->add_column(type_Double, "double");
/* auto col_decimal = */ t->add_column(type_Decimal, "decimal");
/* auto col_uuid = */ t->add_column(type_UUID, "uuid");

TableRef target = g.add_table_with_primary_key("target", type_Int, "_id", false);
auto col_link = t->add_column(*target, "link");

auto target_key = target->create_object_with_primary_key(1).get_key();
for (size_t i = 0; i < cnt; i++) {

auto o = t->create_object_with_primary_key(ObjectId::gen());
o.set(col_int, uint64_t(0xffff) + i);
o.set(col_optint, uint64_t(0xffff) + (i % 10));
o.set(col_string, string_values[i % string_values.size()]);
o.set(col_date, Timestamp(now_seconds + i, 0));
o.set(col_link, target_key);
}
auto obj = t->create_object_with_primary_key(ObjectId::gen());
obj.set_json(col_mixed, dict_value);
obj.set(col_string, long_string);
g.write(path);
#endif // TEST_READ_UPGRADE_MODE
}

#endif // TEST_GROUP
Binary file added test/test_upgrade_database_1000_24.realm
Binary file not shown.
Binary file added test/test_upgrade_database_4_24.realm
Binary file not shown.
Loading