1
0
Fork 0
mirror of https://github.com/KingDuckZ/dindexer.git synced 2025-02-17 11:45:50 +00:00

Add column 'type' to sets and take its value on command line.

This commit is contained in:
King_DuckZ 2015-11-11 20:06:14 +00:00
parent 585c7f45b7
commit 911da3bb00
9 changed files with 78 additions and 25 deletions

View file

@ -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

View file

@ -19,14 +19,30 @@
#include "dindexerConfig.h"
#include <boost/program_options.hpp>
#include <iostream>
#include <algorithm>
#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<std::string>()->default_value("New set"), "Name to be given to the new set being scanned.")
("type,t", po::value<char>()->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<char>())) {
throw std::invalid_argument("Invalid value for parameter \"type\"");
}
return false;
}
} //namespace din

View file

@ -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<FileRecordData>& parData, const std::string& parSetName) {
void write_to_db (const DinDBSettings& parDB, const std::vector<FileRecordData>& 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;

View file

@ -21,6 +21,7 @@
#include <string>
#include <vector>
#include <cstdint>
#include <boost/utility/string_ref.hpp>
namespace din {
struct DinDBSettings;
@ -34,7 +35,12 @@ namespace din {
const bool is_symlink;
};
void write_to_db ( const DinDBSettings& parDB, const std::vector<FileRecordData>& 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<FileRecordData>& parData, const SetRecordData& parSetData );
} //namespace din
#endif

View file

@ -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<FileRecordData> 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) {

View file

@ -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:

View file

@ -63,7 +63,7 @@ int main (int parArgc, char* parArgv[]) {
}
else {
indexer.calculate_hash();
indexer.add_to_db(vm["setname"].as<std::string>());
indexer.add_to_db(vm["setname"].as<std::string>(), vm["type"].as<char>());
}
return 0;
}

View file

@ -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<char[], void(*)(void*)> 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

View file

@ -22,6 +22,7 @@
#include <string>
#include <cstdint>
#include <memory>
#include <boost/utility/string_ref.hpp>
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;