From 6b5fe8557193551fba6a7db8fb341b7a4d2ff6bb Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Tue, 17 May 2016 21:12:44 +0200 Subject: [PATCH] Make mime_type and mime_charset on the fly For the same motivation explained in the parent commit, make string_refs on the fly instead of storing them. --- include/dindexer-machinery/recorddata.hpp | 42 +++++++++++++++++++---- src/machinery/scantask/mime.cpp | 3 +- src/scan/dbbackend.cpp | 4 +-- 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/include/dindexer-machinery/recorddata.hpp b/include/dindexer-machinery/recorddata.hpp index 5032b6b..46f886e 100644 --- a/include/dindexer-machinery/recorddata.hpp +++ b/include/dindexer-machinery/recorddata.hpp @@ -25,6 +25,7 @@ #include #include #include +#include namespace mchlib { struct FileRecordData { @@ -39,11 +40,13 @@ namespace mchlib { mime_full(), atime(parATime), mtime(parMTime), - mime_type(), - mime_charset(), size(0), level(parLevel), path_offset(0), + mime_type_offset(0), + mime_type_length(0), + mime_charset_offset(0), + mime_charset_length(0), is_directory(parIsDir), is_symlink(parIsSymLink), unreadable(false), @@ -57,11 +60,13 @@ namespace mchlib { mime_full(), atime(parATime), mtime(parMTime), - mime_type(), - mime_charset(), size(0), level(parLevel), path_offset(parRelPathOffs), + mime_type_offset(0), + mime_type_length(0), + mime_charset_offset(0), + mime_charset_length(0), is_directory(parIsDir), is_symlink(parIsSymLink), unreadable(false), @@ -83,17 +88,42 @@ namespace mchlib { #endif boost::string_ref path ( void ) const { return boost::string_ref(abs_path).substr(path_offset); } + boost::string_ref mime_type ( void ) const { return boost::string_ref(mime_full.get()).substr(mime_type_offset, mime_type_length); } + boost::string_ref mime_charset ( void ) const { return boost::string_ref(mime_full.get()).substr(mime_charset_offset, mime_charset_length); } + void set_mime_parts ( boost::string_ref parType, boost::string_ref parCharset ) { + const auto& mime = mime_full.get(); + { + assert(std::less()(mime.data(), parType.data())); + assert(std::less_equal()(parType.data() + parType.size(), mime.data())); + assert(parType.data() - mime.data() < USHRT_MAX); + assert(parType.size() < USHRT_MAX); + assert(parType.size() + (parType.data() - mime.data()) <= mime.size()); + mime_type_offset = static_cast(parType.data() - mime.data()); + mime_type_length = static_cast(parType.size()); + } + { + assert(std::less()(mime.data(), parCharset.data())); + assert(std::less_equal()(parCharset.data() + parCharset.size(), mime.data())); + assert(parCharset.data() - mime.data() < USHRT_MAX); + assert(parCharset.size() < USHRT_MAX); + assert(parCharset.size() + (parCharset.data() - mime.data()) <= mime.size()); + mime_charset_offset = static_cast(parCharset.data() - mime.data()); + mime_charset_length = static_cast(parCharset.size()); + } + } TigerHash hash; std::string abs_path; mime_string mime_full; std::time_t atime; std::time_t mtime; - boost::string_ref mime_type; - boost::string_ref mime_charset; uint64_t size; uint16_t level; uint16_t path_offset; //Relative path starting character into abs_path + uint16_t mime_type_offset; //Mime type starting character into mime_full + uint16_t mime_type_length; //Mime type string length + uint16_t mime_charset_offset; //Mime charset starting character into mime_full + uint16_t mime_charset_length; //Mime charset string length bool is_directory; bool is_symlink; bool unreadable; diff --git a/src/machinery/scantask/mime.cpp b/src/machinery/scantask/mime.cpp index 678e190..110d0c4 100644 --- a/src/machinery/scantask/mime.cpp +++ b/src/machinery/scantask/mime.cpp @@ -41,8 +41,7 @@ namespace mchlib { for (auto& itm : list) { itm.mime_full = mime.analyze(itm.abs_path); auto mime_pair = split_mime(itm.mime_full); - itm.mime_type = mime_pair.first; - itm.mime_charset = mime_pair.second; + itm.set_mime_parts(mime_pair.first, mime_pair.second); } } diff --git a/src/scan/dbbackend.cpp b/src/scan/dbbackend.cpp index c7c290e..04f2ebc 100644 --- a/src/scan/dbbackend.cpp +++ b/src/scan/dbbackend.cpp @@ -138,8 +138,8 @@ namespace din { system_clock::from_time_t(itm.mtime), itm.hash_valid, itm.unreadable, - itm.mime_type, - itm.mime_charset + itm.mime_type(), + itm.mime_charset() ); } conn.query("COMMIT;");