1
0
Fork 0
mirror of https://github.com/KingDuckZ/dindexer.git synced 2025-07-03 14:14:11 +00:00

Store access time and modify time in the db.

This commit is contained in:
King_DuckZ 2015-12-01 16:35:46 +00:00
parent f1c3deea63
commit 5f8756ef1b
4 changed files with 37 additions and 9 deletions

View file

@ -4,7 +4,7 @@
-- Dumped from database version 9.4.5 -- Dumped from database version 9.4.5
-- Dumped by pg_dump version 9.4.5 -- Dumped by pg_dump version 9.4.5
-- Started on 2015-11-30 18:28:52 GMT -- Started on 2015-12-01 16:35:15 GMT
SET statement_timeout = 0; SET statement_timeout = 0;
SET lock_timeout = 0; SET lock_timeout = 0;
@ -68,6 +68,8 @@ CREATE TABLE files (
size bigint, size bigint,
hash character(48) NOT NULL, hash character(48) NOT NULL,
is_hash_valid boolean DEFAULT true NOT NULL, is_hash_valid boolean DEFAULT true NOT NULL,
access_time timestamp with time zone,
modify_time timestamp with time zone,
CONSTRAINT chk_files_dirsize_zero CHECK (((is_directory = false) OR (size = 0))) CONSTRAINT chk_files_dirsize_zero CHECK (((is_directory = false) OR (size = 0)))
); );
@ -242,7 +244,7 @@ GRANT ALL ON SCHEMA public TO postgres;
GRANT ALL ON SCHEMA public TO PUBLIC; GRANT ALL ON SCHEMA public TO PUBLIC;
-- Completed on 2015-11-30 18:28:54 GMT -- Completed on 2015-12-01 16:35:18 GMT
-- --
-- PostgreSQL database dump complete -- PostgreSQL database dump complete

View file

@ -23,6 +23,8 @@
#include <utility> #include <utility>
#include <boost/lexical_cast.hpp> #include <boost/lexical_cast.hpp>
#include <exception> #include <exception>
#include <memory>
#include <boost/utility/string_ref.hpp>
namespace din { namespace din {
namespace { namespace {
@ -36,6 +38,12 @@ namespace din {
<< ");"; << ");";
return oss.str(); return oss.str();
} }
boost::string_ref time_to_str (const std::time_t parTime, char* parBuff, std::size_t parLength) {
const auto gtm = std::gmtime(&parTime);
const auto len = std::strftime(parBuff, parLength, "%F %T%z", gtm);
return boost::string_ref(parBuff, len);
}
} //unnamed namespace } //unnamed namespace
bool read_from_db (FileRecordData& parItem, SetRecordDataFull& parSet, const DinDBSettings& parDB, std::string&& parHash) { bool read_from_db (FileRecordData& parItem, SetRecordDataFull& parSet, const DinDBSettings& parDB, std::string&& parHash) {
@ -90,6 +98,9 @@ namespace din {
return; return;
} }
const std::size_t strtime_buff_size = 512;
std::unique_ptr<char[]> strtime_buff(new char[strtime_buff_size]);
pq::Connection conn(std::string(parDB.username), std::string(parDB.password), std::string(parDB.dbname), std::string(parDB.address), parDB.port); pq::Connection conn(std::string(parDB.username), std::string(parDB.password), std::string(parDB.dbname), std::string(parDB.address), parDB.port);
conn.connect(); conn.connect();
@ -98,7 +109,10 @@ namespace din {
//TODO: use COPY instead of INSERT INTO //TODO: use COPY instead of INSERT INTO
for (std::size_t z = 0; z < parData.size(); z += g_batch_size) { for (std::size_t z = 0; z < parData.size(); z += g_batch_size) {
std::ostringstream query; std::ostringstream query;
query << "INSERT INTO \"files\" (path, hash, level, group_id, is_directory, is_symlink, size) VALUES "; query << "INSERT INTO \"files\" " <<
"(path, hash, level, group_id, is_directory, is_symlink, size, " <<
"access_time, modify_time) VALUES "
;
const char* comma = ""; const char* comma = "";
for (auto i = z; i < std::min(z + g_batch_size, parData.size()); ++i) { for (auto i = z; i < std::min(z + g_batch_size, parData.size()); ++i) {
@ -108,8 +122,10 @@ namespace din {
<< itm.level << ',' << itm.level << ','
<< "currval('\"sets_id_seq\"')" << ',' << "currval('\"sets_id_seq\"')" << ','
<< (itm.is_directory ? "true" : "false") << ',' << (itm.is_directory ? "true" : "false") << ','
<< (itm.is_symlink ? "true" : "false") << ',' << itm.size << ')' << (itm.is_symlink ? "true" : "false") << ',' << itm.size
; << ',' << '\'' << time_to_str(itm.atime, strtime_buff.get(), strtime_buff_size) << '\''
<< ',' << '\'' << time_to_str(itm.mtime, strtime_buff.get(), strtime_buff_size) << '\''
<< ')';
comma = ","; comma = ",";
} }
query << ';'; query << ';';

View file

@ -22,6 +22,7 @@
#include <vector> #include <vector>
#include <cstdint> #include <cstdint>
#include <boost/utility/string_ref.hpp> #include <boost/utility/string_ref.hpp>
#include <ctime>
namespace din { namespace din {
struct DinDBSettings; struct DinDBSettings;
@ -29,6 +30,8 @@ namespace din {
struct FileRecordData { struct FileRecordData {
std::string path; std::string path;
std::string hash; std::string hash;
std::time_t atime;
std::time_t mtime;
uint16_t level; uint16_t level;
uint64_t size; uint64_t size;
bool is_directory; bool is_directory;

View file

@ -44,12 +44,15 @@ namespace din {
typedef TigerHash HashType; typedef TigerHash HashType;
struct FileEntry { struct FileEntry {
FileEntry ( const char* parPath, int parLevel, bool parIsDir, bool parIsSymLink) : FileEntry ( const char* parPath, const fastf::FileStats& parSt ) :
path(parPath), path(parPath),
hash {}, hash {},
level(static_cast<uint16_t>(parLevel)), access_time(parSt.atime),
is_dir(parIsDir), modify_time(parSt.mtime),
is_symlink(parIsSymLink) //file_size(0),
level(static_cast<uint16_t>(parSt.level)),
is_dir(parSt.is_dir),
is_symlink(parSt.is_symlink)
{ {
} }
@ -62,6 +65,8 @@ namespace din {
std::string path; std::string path;
HashType hash; HashType hash;
std::time_t access_time;
std::time_t modify_time;
uint64_t file_size; uint64_t file_size;
uint16_t level; uint16_t level;
bool is_dir; bool is_dir;
@ -300,6 +305,8 @@ namespace din {
data.push_back(FileRecordData { data.push_back(FileRecordData {
make_relative_path(base_path, PathName(itm.path)).path(), make_relative_path(base_path, PathName(itm.path)).path(),
tiger_to_string(itm.hash), tiger_to_string(itm.hash),
itm.access_time,
itm.modify_time,
itm.level, itm.level,
itm.file_size, itm.file_size,
itm.is_dir, itm.is_dir,