folly::StringPiece -> std::string_view

This commit is contained in:
Marcus Holland-Moritz 2020-11-22 14:11:38 +01:00
parent 37994016de
commit e44519b75b
5 changed files with 54 additions and 16 deletions

View File

@ -24,12 +24,11 @@
#include <array> #include <array>
#include <functional> #include <functional>
#include <memory> #include <memory>
#include <string_view>
#include <vector> #include <vector>
#include <sys/stat.h> #include <sys/stat.h>
#include <folly/Range.h>
#include "file_interface.h" #include "file_interface.h"
#include "fstypes.h" #include "fstypes.h"
@ -95,7 +94,7 @@ class file : public entry {
, with_similarity_(with_similarity) {} , with_similarity_(with_similarity) {}
type_t type() const override; type_t type() const override;
folly::StringPiece hash() const; std::string_view hash() const;
void set_inode(std::shared_ptr<inode> ino); void set_inode(std::shared_ptr<inode> ino);
std::shared_ptr<inode> get_inode() const; std::shared_ptr<inode> get_inode() const;
void accept(entry_visitor& v, bool preorder) override; void accept(entry_visitor& v, bool preorder) override;

View File

@ -0,0 +1,40 @@
/* vim:set ts=2 sw=2 sts=2 et: */
/**
* \author Marcus Holland-Moritz (github@mhxnet.de)
* \copyright Copyright (c) Marcus Holland-Moritz
*
* This file is part of dwarfs.
*
* dwarfs is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* dwarfs is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with dwarfs. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <string_view>
#include <folly/Hash.h>
namespace folly {
template <>
struct hasher<std::string_view> {
using folly_is_avalanching = std::true_type;
size_t operator()(std::string_view r) const {
return static_cast<size_t>(
hash::SpookyHashV2::Hash64(r.data(), r.size(), 0));
}
};
}

View File

@ -21,10 +21,9 @@
#pragma once #pragma once
#include <string_view>
#include <vector> #include <vector>
#include <folly/Range.h>
#include "fstypes.h" #include "fstypes.h"
#include "logger.h" #include "logger.h"
@ -82,7 +81,7 @@ class metadata_writer {
} }
} }
void write(folly::StringPiece str) { void write(std::string_view str) {
if (!str.empty()) { if (!str.empty()) {
write(str.data(), str.size()); write(str.data(), str.size());
} }

View File

@ -158,8 +158,8 @@ void entry::pack(dir_entry_ug_time& de) const {
entry::type_t file::type() const { return E_FILE; } entry::type_t file::type() const { return E_FILE; }
folly::StringPiece file::hash() const { std::string_view file::hash() const {
return folly::StringPiece(hash_.begin(), hash_.end()); return std::string_view(&hash_[0], hash_.size());
} }
void file::set_inode(std::shared_ptr<inode> ino) { void file::set_inode(std::shared_ptr<inode> ino) {

View File

@ -34,7 +34,6 @@
#include <boost/system/system_error.hpp> #include <boost/system/system_error.hpp>
#include <folly/Conv.h> #include <folly/Conv.h>
#include <folly/Range.h>
#include <folly/String.h> #include <folly/String.h>
#include <folly/small_vector.h> #include <folly/small_vector.h>
@ -46,6 +45,7 @@
#include "dwarfs/entry.h" #include "dwarfs/entry.h"
#include "dwarfs/filesystem_writer.h" #include "dwarfs/filesystem_writer.h"
#include "dwarfs/fstypes.h" #include "dwarfs/fstypes.h"
#include "dwarfs/hash_util.h"
#include "dwarfs/inode_manager.h" #include "dwarfs/inode_manager.h"
#include "dwarfs/logger.h" #include "dwarfs/logger.h"
#include "dwarfs/metadata.h" #include "dwarfs/metadata.h"
@ -85,9 +85,9 @@ class scanner_ : public scanner::impl {
// TODO: StringPiece? // TODO: StringPiece?
// TODO: Use dense/unordered maps/sets and sort later? // TODO: Use dense/unordered maps/sets and sort later?
using file_name_table_t = using file_name_table_t =
fast_hash_map<size_t, fast_hash_set<folly::StringPiece, folly::Hash>>; fast_hash_map<size_t, fast_hash_set<std::string_view, folly::Hash>>;
std::unordered_map<folly::StringPiece, size_t, folly::Hash> std::unordered_map<std::string_view, size_t, folly::Hash>
compress_names_table(metadata_writer& mw, compress_names_table(metadata_writer& mw,
const file_name_table_t& file_name) const; const file_name_table_t& file_name) const;
@ -118,7 +118,7 @@ scanner_<LoggerPolicy>::scanner_(logger& lgr, worker_group& wg,
, log_(lgr) {} , log_(lgr) {}
template <typename LoggerPolicy> template <typename LoggerPolicy>
std::unordered_map<folly::StringPiece, size_t, folly::Hash> std::unordered_map<std::string_view, size_t, folly::Hash>
scanner_<LoggerPolicy>::compress_names_table( scanner_<LoggerPolicy>::compress_names_table(
metadata_writer& mw, const file_name_table_t& file_name) const { metadata_writer& mw, const file_name_table_t& file_name) const {
log_.info() << "compressing names table..."; log_.info() << "compressing names table...";
@ -130,7 +130,7 @@ scanner_<LoggerPolicy>::compress_names_table(
index.set_empty_key(0); index.set_empty_key(0);
uint32_t index_pos = 0; uint32_t index_pos = 0;
std::unordered_map<folly::StringPiece, size_t, folly::Hash> offset; std::unordered_map<std::string_view, size_t, folly::Hash> offset;
size_t saved = 0; size_t saved = 0;
size_t orig_offset = mw.offset(); size_t orig_offset = mw.offset();
@ -142,7 +142,7 @@ scanner_<LoggerPolicy>::compress_names_table(
for (auto size : sizes) { for (auto size : sizes) {
auto nsi = file_name.find(size); auto nsi = file_name.find(size);
assert(nsi != file_name.end()); assert(nsi != file_name.end());
std::vector<folly::StringPiece> names(nsi->second.size()); std::vector<std::string_view> names(nsi->second.size());
std::copy(nsi->second.begin(), nsi->second.end(), names.begin()); std::copy(nsi->second.begin(), nsi->second.end(), names.begin());
std::sort(names.begin(), names.end()); std::sort(names.begin(), names.end());
@ -250,7 +250,7 @@ class save_links_visitor : public entry_visitor {
private: private:
metadata_writer& mw_; metadata_writer& mw_;
std::unordered_map<folly::StringPiece, size_t, folly::Hash> offset_; std::unordered_map<std::string_view, size_t, folly::Hash> offset_;
}; };
class save_directories_visitor : public entry_visitor { class save_directories_visitor : public entry_visitor {
@ -374,7 +374,7 @@ void scanner_<LoggerPolicy>::scan(filesystem_writer& fsw,
wg_.wait(); wg_.wait();
size_t total{0}; size_t total{0};
std::unordered_map<folly::StringPiece, std::vector<file*>, folly::Hash> std::unordered_map<std::string_view, std::vector<file*>, folly::Hash>
file_hash; file_hash;
file_name_table_t file_name; file_name_table_t file_name;