From 911da3bb008e95cedadbf8a6d712916080e245b0 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Wed, 11 Nov 2015 20:06:14 +0000 Subject: [PATCH] Add column 'type' to sets and take its value on command line. --- dindexer.sql | 45 +++++++++++++++++++++++++++++-------------- src/commandline.cpp | 20 +++++++++++++++++++ src/dbbackend.cpp | 11 ++++++----- src/dbbackend.hpp | 8 +++++++- src/indexer.cpp | 7 +++++-- src/indexer.hpp | 2 +- src/main.cpp | 2 +- src/pq/connection.cpp | 6 +++++- src/pq/connection.hpp | 2 ++ 9 files changed, 78 insertions(+), 25 deletions(-) diff --git a/dindexer.sql b/dindexer.sql index 82fedab..45028c0 100644 --- a/dindexer.sql +++ b/dindexer.sql @@ -4,7 +4,7 @@ -- Dumped from database version 9.4.5 -- Dumped by pg_dump version 9.4.5 --- Started on 2015-11-11 13:26:58 GMT +-- Started on 2015-11-11 20:03:40 GMT SET statement_timeout = 0; SET lock_timeout = 0; @@ -22,7 +22,7 @@ CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog; -- --- TOC entry 2035 (class 0 OID 0) +-- TOC entry 2037 (class 0 OID 0) -- Dependencies: 178 -- Name: EXTENSION plpgsql; Type: COMMENT; Schema: -; Owner: -- @@ -90,7 +90,7 @@ CREATE SEQUENCE files_id_seq ALTER TABLE files_id_seq OWNER TO @USERNAME@; -- --- TOC entry 2036 (class 0 OID 0) +-- TOC entry 2038 (class 0 OID 0) -- Dependencies: 174 -- Name: files_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: @USERNAME@ -- @@ -106,12 +106,29 @@ ALTER SEQUENCE files_id_seq OWNED BY files.id; CREATE TABLE sets ( id integer NOT NULL, "desc" text NOT NULL, - creation time with time zone DEFAULT now() NOT NULL + creation time with time zone DEFAULT now() NOT NULL, + type character(1) DEFAULT 'D'::bpchar NOT NULL, + CONSTRAINT chk_sets_type CHECK ((((((((type = 'D'::bpchar) OR (type = 'V'::bpchar)) OR (type = 'B'::bpchar)) OR (type = 'F'::bpchar)) OR (type = 'H'::bpchar)) OR (type = 'Z'::bpchar)) OR (type = 'O'::bpchar))) ); ALTER TABLE sets OWNER TO @USERNAME@; +-- +-- TOC entry 2039 (class 0 OID 0) +-- Dependencies: 177 +-- Name: COLUMN sets.type; Type: COMMENT; Schema: public; Owner: @USERNAME@ +-- + +COMMENT ON COLUMN sets.type IS 'D = directory +V = DVD +B = BluRay +F = Floppy Disk +H = Hard Disk +Z = Iomega Zip +O = Other'; + + -- -- TOC entry 176 (class 1259 OID 31409) -- Name: sets_id_seq; Type: SEQUENCE; Schema: public; Owner: @USERNAME@ @@ -128,7 +145,7 @@ CREATE SEQUENCE sets_id_seq ALTER TABLE sets_id_seq OWNER TO @USERNAME@; -- --- TOC entry 2037 (class 0 OID 0) +-- TOC entry 2040 (class 0 OID 0) -- Dependencies: 176 -- Name: sets_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: @USERNAME@ -- @@ -153,7 +170,7 @@ ALTER TABLE ONLY sets ALTER COLUMN id SET DEFAULT nextval('sets_id_seq'::regclas -- --- TOC entry 1912 (class 2606 OID 31289) +-- TOC entry 1914 (class 2606 OID 31289) -- Name: pk_files_id; Type: CONSTRAINT; Schema: public; Owner: @USERNAME@; Tablespace: -- @@ -162,7 +179,7 @@ ALTER TABLE ONLY files -- --- TOC entry 1916 (class 2606 OID 31420) +-- TOC entry 1918 (class 2606 OID 31420) -- Name: pk_sets_id; Type: CONSTRAINT; Schema: public; Owner: @USERNAME@; Tablespace: -- @@ -171,7 +188,7 @@ ALTER TABLE ONLY sets -- --- TOC entry 1914 (class 2606 OID 31294) +-- TOC entry 1916 (class 2606 OID 31294) -- Name: uniq_item; Type: CONSTRAINT; Schema: public; Owner: @USERNAME@; Tablespace: -- @@ -180,7 +197,7 @@ ALTER TABLE ONLY files -- --- TOC entry 1909 (class 1259 OID 31426) +-- TOC entry 1911 (class 1259 OID 31426) -- Name: fki_files_sets; Type: INDEX; Schema: public; Owner: @USERNAME@; Tablespace: -- @@ -188,7 +205,7 @@ CREATE INDEX fki_files_sets ON files USING btree (group_id); -- --- TOC entry 1910 (class 1259 OID 31292) +-- TOC entry 1912 (class 1259 OID 31292) -- Name: idx_paths; Type: INDEX; Schema: public; Owner: @USERNAME@; Tablespace: -- @@ -196,7 +213,7 @@ CREATE INDEX idx_paths ON files USING btree (path); -- --- TOC entry 1918 (class 2620 OID 31291) +-- TOC entry 1920 (class 2620 OID 31291) -- Name: triggerupcasehash; Type: TRIGGER; Schema: public; Owner: @USERNAME@ -- @@ -204,7 +221,7 @@ CREATE TRIGGER triggerupcasehash BEFORE INSERT OR UPDATE ON files FOR EACH ROW E -- --- TOC entry 1917 (class 2606 OID 31421) +-- TOC entry 1919 (class 2606 OID 31421) -- Name: fk_files_sets; Type: FK CONSTRAINT; Schema: public; Owner: @USERNAME@ -- @@ -213,7 +230,7 @@ ALTER TABLE ONLY files -- --- TOC entry 2034 (class 0 OID 0) +-- TOC entry 2036 (class 0 OID 0) -- Dependencies: 8 -- Name: public; Type: ACL; Schema: -; Owner: postgres -- @@ -224,7 +241,7 @@ GRANT ALL ON SCHEMA public TO postgres; GRANT ALL ON SCHEMA public TO PUBLIC; --- Completed on 2015-11-11 13:27:00 GMT +-- Completed on 2015-11-11 20:03:45 GMT -- -- PostgreSQL database dump complete diff --git a/src/commandline.cpp b/src/commandline.cpp index 5502928..ea97784 100644 --- a/src/commandline.cpp +++ b/src/commandline.cpp @@ -19,14 +19,30 @@ #include "dindexerConfig.h" #include #include +#include #define STRINGIZE_IMPL(s) #s #define STRINGIZE(s) STRINGIZE_IMPL(s) +#if defined(lengthof) +# undef lengthof +#endif +//http://stackoverflow.com/questions/4415524/common-array-length-macro-for-c#4415646 +#define lengthof(x) ((sizeof(x)/sizeof(0[x])) / ((std::size_t)(!(sizeof(x) % sizeof(0[x]))))) + namespace po = boost::program_options; namespace din { namespace { + const char g_allowed_types[] = { + 'D', //Directory + 'V', //DVD + 'B', //BluRay + 'F', //Floppy Disk + 'H', //Hard Disk + 'Z', //Iomega Zip + 'O' //Other + }; const char* const g_version_string = PROGRAM_NAME " v" STRINGIZE(VERSION_MAJOR) "." @@ -48,6 +64,7 @@ namespace din { po::options_description set_options("Set options"); set_options.add_options() ("setname,n", po::value()->default_value("New set"), "Name to be given to the new set being scanned.") + ("type,t", po::value()->default_value('V'), "Default set type. Valid values are B, D, F, H, O, V, Z.") ; po::options_description positional_options("Positional options"); positional_options.add_options() @@ -92,6 +109,9 @@ namespace din { if (parVarMap.count("search-path") == 0) { throw std::invalid_argument("No search path specified"); } + if (g_allowed_types + lengthof(g_allowed_types) == std::find(g_allowed_types, g_allowed_types + lengthof(g_allowed_types), parVarMap["type"].as())) { + throw std::invalid_argument("Invalid value for parameter \"type\""); + } return false; } } //namespace din diff --git a/src/dbbackend.cpp b/src/dbbackend.cpp index f6b33c6..90fcdfa 100644 --- a/src/dbbackend.cpp +++ b/src/dbbackend.cpp @@ -26,16 +26,17 @@ namespace din { namespace { const std::size_t g_batch_size = 100; - std::string make_set_insert_query (pq::Connection& parConn, const std::string& parSetName) { + std::string make_set_insert_query (pq::Connection& parConn, const SetRecordData& parSetData) { std::ostringstream oss; - oss << "INSERT INTO \"sets\" (\"desc\") VALUES (" - << parConn.escaped_literal(parSetName) + oss << "INSERT INTO \"sets\" (\"desc\",\"type\") VALUES (" + << parConn.escaped_literal(parSetData.name) << ',' + << '\'' << parSetData.type << '\'' << ");"; return oss.str(); } } //unnamed namespace - void write_to_db (const DinDBSettings& parDB, const std::vector& parData, const std::string& parSetName) { + void write_to_db (const DinDBSettings& parDB, const std::vector& parData, const SetRecordData& parSetData) { if (parData.empty()) { return; } @@ -44,7 +45,7 @@ namespace din { conn.connect(); conn.query_void("BEGIN;"); - conn.query_void(make_set_insert_query(conn, parSetName)); + conn.query_void(make_set_insert_query(conn, parSetData)); //TODO: use COPY instead of INSERT INTO for (std::size_t z = 0; z < parData.size(); z += g_batch_size) { std::ostringstream query; diff --git a/src/dbbackend.hpp b/src/dbbackend.hpp index 27290c4..204a2b8 100644 --- a/src/dbbackend.hpp +++ b/src/dbbackend.hpp @@ -21,6 +21,7 @@ #include #include #include +#include namespace din { struct DinDBSettings; @@ -34,7 +35,12 @@ namespace din { const bool is_symlink; }; - void write_to_db ( const DinDBSettings& parDB, const std::vector& parData, const std::string& parSetName ); + struct SetRecordData { + const boost::string_ref name; + const char type; + }; + + void write_to_db ( const DinDBSettings& parDB, const std::vector& parData, const SetRecordData& parSetData ); } //namespace din #endif diff --git a/src/indexer.cpp b/src/indexer.cpp index 6e8f9b3..a0593e0 100644 --- a/src/indexer.cpp +++ b/src/indexer.cpp @@ -235,7 +235,8 @@ namespace din { #endif } - void Indexer::add_to_db (const std::string& parSetName) const { + void Indexer::add_to_db (const std::string& parSetName, char parType) const { + assert(m_local_data->done_count == m_local_data->paths.size()); PathName base_path(m_local_data->paths.front().path); std::vector data; data.reserve(m_local_data->paths.size()); @@ -249,7 +250,9 @@ namespace din { itm.is_symlink }); } - write_to_db(m_local_data->db_settings, data, parSetName); + + SetRecordData set_data {parSetName, parType}; + write_to_db(m_local_data->db_settings, data, set_data); } bool Indexer::add_path (const char* parPath, int parLevel, bool parIsDir, bool parIsSymLink) { diff --git a/src/indexer.hpp b/src/indexer.hpp index fbf69ab..4b11bf6 100644 --- a/src/indexer.hpp +++ b/src/indexer.hpp @@ -38,7 +38,7 @@ namespace din { std::size_t total_items ( void ) const; std::size_t processed_items ( void ) const; void calculate_hash ( void ); - void add_to_db ( const std::string& parSetName ) const; + void add_to_db ( const std::string& parSetName, char parType ) const; bool empty ( void ) const; private: diff --git a/src/main.cpp b/src/main.cpp index 3b94bd2..a63db57 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -63,7 +63,7 @@ int main (int parArgc, char* parArgv[]) { } else { indexer.calculate_hash(); - indexer.add_to_db(vm["setname"].as()); + indexer.add_to_db(vm["setname"].as(), vm["type"].as()); } return 0; } diff --git a/src/pq/connection.cpp b/src/pq/connection.cpp index 293f061..0f485a9 100644 --- a/src/pq/connection.cpp +++ b/src/pq/connection.cpp @@ -125,9 +125,13 @@ namespace pq { } std::string Connection::escaped_literal (const std::string& parString) { + return this->escaped_literal(boost::string_ref(parString)); + } + + std::string Connection::escaped_literal (boost::string_ref parString) { typedef std::unique_ptr PQArrayType; - PQArrayType clean_str(PQescapeLiteral(m_localData->connection, parString.c_str(), parString.size()), &PQfreemem); + PQArrayType clean_str(PQescapeLiteral(m_localData->connection, parString.data(), parString.size()), &PQfreemem); return std::string(clean_str.get()); } } //namespace pq diff --git a/src/pq/connection.hpp b/src/pq/connection.hpp index daa2510..306abbb 100644 --- a/src/pq/connection.hpp +++ b/src/pq/connection.hpp @@ -22,6 +22,7 @@ #include #include #include +#include namespace pq { class Connection { @@ -37,6 +38,7 @@ namespace pq { ResultSet query ( const std::string& parQuery ); std::string escaped_literal ( const std::string& parString ); + std::string escaped_literal ( boost::string_ref parString ); private: struct LocalData;