From 8f0f4e794544619cb57832ea43307bee8efe1e37 Mon Sep 17 00:00:00 2001 From: Calle Wilund Date: Mon, 17 Aug 2015 15:53:45 +0200 Subject: [PATCH] Commitlog: do more extensive dir entry probes to determine type Since directory_entry "type" might not be set. Ensuring that code does not remain future free or easy to read. Fixes #157. --- db/commitlog/commitlog.cc | 18 +++++++++++---- tests/commitlog_test.cc | 48 ++++++++++++++++++++++++--------------- 2 files changed, 43 insertions(+), 23 deletions(-) diff --git a/db/commitlog/commitlog.cc b/db/commitlog/commitlog.cc index be217fa23f..256dc12c1d 100644 --- a/db/commitlog/commitlog.cc +++ b/db/commitlog/commitlog.cc @@ -167,11 +167,19 @@ public: } future<> process(directory_entry de) { - if (de.type && de.type == directory_entry_type::regular) { - descriptor d(de.name); - _ids = std::max(_ids, d.id); - } - return make_ready_future<>(); + auto entry_type = [this](const directory_entry & de) { + if (!de.type && !de.name.empty()) { + return engine().file_type(cfg.commit_log_location + "/" + de.name); + } + return make_ready_future>(de.type); + }; + return entry_type(de).then([de, this](auto type) { + if (type == directory_entry_type::regular) { + descriptor d(de.name); + _ids = std::max(_ids, d.id); + } + return make_ready_future<>(); + }); } future<> init(); diff --git a/tests/commitlog_test.cc b/tests/commitlog_test.cc index 0003e5fb6b..61fe22eb3b 100644 --- a/tests/commitlog_test.cc +++ b/tests/commitlog_test.cc @@ -69,15 +69,25 @@ static future> list_files(sstring path) { }); } +future> entry_type(const sstring & path, const directory_entry & de) { + if (!de.type && !de.name.empty()) { + return engine().file_type(path + "/" + de.name); + } + return make_ready_future>(de.type); +}; + static future count_files(sstring path) { - return list_files(path).then([](auto l) { - size_t n = 0; - for (auto & de : l->contents()) { - if (de.type == directory_entry_type::regular) { - ++n; - } - } - return make_ready_future(n); + return list_files(path).then([path](auto l) { + auto n = make_lw_shared(0); + return parallel_for_each(l->contents(), [n, path](auto de) { + return entry_type(path, de).then([n](auto type) { + if (type == directory_entry_type::regular) { + ++(*n); + } + }); + }).then([n] { + return make_ready_future(*n); + }); }); } @@ -85,18 +95,20 @@ static future count_files_with_size(sstring path) { return list_files(path).then([path](auto l) { auto n = make_lw_shared(0); return parallel_for_each(l->contents().begin(), l->contents().end(), [n, path](directory_entry de) { - if (de.type == directory_entry_type::regular) { - return engine().open_file_dma(path + "/" + de.name, open_flags::ro).then([n](file f) { - return do_with(std::move(f), [n] (auto& f) { - return f.stat().then([n](struct stat s) { - if (s.st_size > 0) { - ++(*n); - } + return entry_type(path, de).then([n, path, de](auto type) { + if (type == directory_entry_type::regular) { + return engine().open_file_dma(path + "/" + de.name, open_flags::ro).then([n](file f) { + return do_with(std::move(f), [n] (auto& f) { + return f.stat().then([n](struct stat s) { + if (s.st_size > 0) { + ++(*n); + } + }); }); }); - }); - } - return make_ready_future(); + } + return make_ready_future(); + }); }).then([n]() { return make_ready_future(*n);; });