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

Cleaning and refactoring block fs for readability #3552

Merged
merged 5 commits into from
Jun 21, 2022
Merged

Conversation

dafeda
Copy link
Contributor

@dafeda dafeda commented Jun 21, 2022

Read about block_fs and did some clean-up.

Pre review checklist

  • Added appropriate release note label
  • PR title captures the intent of the changes, and is fitting for release notes.
  • Commit history is consistent and clean, in line with the contribution guidelines.

Adding labels helps the maintainers when writing release notes. This is the list of release note labels.

@dafeda dafeda self-assigned this Jun 21, 2022
@dafeda dafeda added the release-notes:maintenance Automatically categorise as maintenance change in release notes label Jun 21, 2022
@dafeda dafeda changed the title Block fs Cleaning and refactoring block fs for readability Jun 21, 2022
@@ -318,22 +318,21 @@ static block_fs_type *block_fs_alloc_empty(const fs::path &mount_file,

block_fs->fsync_interval = fsync_interval;
block_fs->block_size = block_size;
{
Copy link
Contributor

Choose a reason for hiding this comment

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

I had a reread parts of "clean code" last night so take this as a highly influenced opinion. Perhaps the block should be extracted as a function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can the function be re-used you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

No, the style used by Robert C. Martin is shorter functions (that do one thing at one abstraction level) where the name serves as documentation, ie.:

static std::tuple<int, int> read_mount_file_header(const fs::path &mount_file){
    FILE *stream = util_fopen(mount_file.c_str(), "r");
    int id = util_fread_int(stream);
    int version = util_fread_int(stream);
    fclose(stream);
    return {id, version};
}

static void validate_mount_file(const fs::path &mount_file){
    auto &[id, version] = read_mount_file_header(mount_file);
    if (version != 0)
        throw std::runtime_error(fmt::format(
            "block_fs data version unexpected. Expected 0, got {}", version));
    if (id != MOUNT_MAP_MAGIC_INT)
        util_abort("%s: The file:%s does not seem to be a valid block_fs "
                   "mount map \n",
                   __func__, mount_file.c_str());
}

static block_fs_type *block_fs_alloc_empty(const fs::path &mount_file,
                                           int block_size, int fsync_interval,
                                           bool read_only) {
    block_fs_type *block_fs = new block_fs_type;
    UTIL_TYPE_ID_INIT(block_fs, BLOCK_FS_TYPE_ID);

    block_fs->fsync_interval = fsync_interval;
    block_fs->block_size = block_size;

    validate_mount_file(mount_file);

    block_fs_reinit(block_fs);

    block_fs->data_owner = !read_only;
    return block_fs;
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, should that second util_abort also be a throw std::runtime_error?

libres/lib/res_util/block_fs.cpp Show resolved Hide resolved
libres/lib/res_util/block_fs.cpp Outdated Show resolved Hide resolved
libres/lib/res_util/block_fs.cpp Show resolved Hide resolved
@eivindjahren
Copy link
Contributor

The changes are good. There is more we could do, but I leave it up to you whether you want that it a separate PR.

@codecov-commenter
Copy link

Codecov Report

Merging #3552 (6c1ca98) into main (e8a7eeb) will increase coverage by 0.01%.
The diff coverage is 48.21%.

@@            Coverage Diff             @@
##             main    #3552      +/-   ##
==========================================
+ Coverage   64.99%   65.01%   +0.01%     
==========================================
  Files         599      599              
  Lines       46993    46989       -4     
  Branches     4216     4210       -6     
==========================================
+ Hits        30544    30549       +5     
+ Misses      15071    15065       -6     
+ Partials     1378     1375       -3     
Impacted Files Coverage Δ
libres/lib/res_util/block_fs.cpp 68.11% <48.21%> (+2.05%) ⬆️
libres/lib/enkf/block_fs_driver.cpp 79.73% <0.00%> (+0.65%) ⬆️

📣 Codecov can now indicate which changes are the most critical in Pull Requests. Learn more

@dafeda dafeda merged commit 1f11f12 into equinor:main Jun 21, 2022
@dafeda dafeda deleted the block_fs branch June 21, 2022 08:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-notes:maintenance Automatically categorise as maintenance change in release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants