From 413bf0444b3850517c26cedab2c8f71d639bfb91 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Wed, 11 Nov 2015 13:30:41 +0000 Subject: [PATCH] Update DB tables and insert set along with new files. --- dindexer.sql | 137 ++++++++++++++++++++++++++++++++-------------- src/dbbackend.cpp | 6 +- 2 files changed, 100 insertions(+), 43 deletions(-) diff --git a/dindexer.sql b/dindexer.sql index 154b740..69393f2 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 02:04:53 GMT +-- Started on 2015-11-11 13:26:58 GMT SET statement_timeout = 0; SET lock_timeout = 0; @@ -14,7 +14,7 @@ SET check_function_bodies = false; SET client_min_messages = warning; -- --- TOC entry 176 (class 3079 OID 11869) +-- TOC entry 178 (class 3079 OID 11869) -- Name: plpgsql; Type: EXTENSION; Schema: -; Owner: -- @@ -22,8 +22,8 @@ CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog; -- --- TOC entry 2022 (class 0 OID 0) --- Dependencies: 176 +-- TOC entry 2035 (class 0 OID 0) +-- Dependencies: 178 -- Name: EXTENSION plpgsql; Type: COMMENT; Schema: -; Owner: -- @@ -33,7 +33,7 @@ COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language'; SET search_path = public, pg_catalog; -- --- TOC entry 189 (class 1255 OID 31290) +-- TOC entry 191 (class 1255 OID 31290) -- Name: upcase_hash(); Type: FUNCTION; Schema: public; Owner: michele -- @@ -55,10 +55,10 @@ SET default_with_oids = false; -- -- TOC entry 175 (class 1259 OID 31281) --- Name: Files; Type: TABLE; Schema: public; Owner: michele; Tablespace: +-- Name: files; Type: TABLE; Schema: public; Owner: michele; Tablespace: -- -CREATE TABLE "Files" ( +CREATE TABLE files ( id bigint NOT NULL, path text NOT NULL, level smallint NOT NULL, @@ -66,18 +66,20 @@ CREATE TABLE "Files" ( is_directory boolean NOT NULL, is_symlink boolean NOT NULL, size bigint, - hash character(48) NOT NULL + hash character(48) NOT NULL, + is_hash_valid boolean DEFAULT true NOT NULL, + CONSTRAINT chk_files_dirsize_zero CHECK (((is_directory = false) OR (size = 0))) ); -ALTER TABLE "Files" OWNER TO michele; +ALTER TABLE files OWNER TO michele; -- -- TOC entry 174 (class 1259 OID 31279) --- Name: Files_id_seq; Type: SEQUENCE; Schema: public; Owner: michele +-- Name: files_id_seq; Type: SEQUENCE; Schema: public; Owner: michele -- -CREATE SEQUENCE "Files_id_seq" +CREATE SEQUENCE files_id_seq START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -85,80 +87,133 @@ CREATE SEQUENCE "Files_id_seq" CACHE 1; -ALTER TABLE "Files_id_seq" OWNER TO michele; +ALTER TABLE files_id_seq OWNER TO michele; -- --- TOC entry 2023 (class 0 OID 0) +-- TOC entry 2036 (class 0 OID 0) -- Dependencies: 174 --- Name: Files_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: michele +-- Name: files_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: michele -- -ALTER SEQUENCE "Files_id_seq" OWNED BY "Files".id; +ALTER SEQUENCE files_id_seq OWNED BY files.id; -- --- TOC entry 1897 (class 2604 OID 31284) +-- TOC entry 177 (class 1259 OID 31411) +-- Name: sets; Type: TABLE; Schema: public; Owner: michele; Tablespace: +-- + +CREATE TABLE sets ( + id integer NOT NULL, + "desc" text NOT NULL, + creation time with time zone DEFAULT now() NOT NULL +); + + +ALTER TABLE sets OWNER TO michele; + +-- +-- TOC entry 176 (class 1259 OID 31409) +-- Name: sets_id_seq; Type: SEQUENCE; Schema: public; Owner: michele +-- + +CREATE SEQUENCE sets_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +ALTER TABLE sets_id_seq OWNER TO michele; + +-- +-- TOC entry 2037 (class 0 OID 0) +-- Dependencies: 176 +-- Name: sets_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: michele +-- + +ALTER SEQUENCE sets_id_seq OWNED BY sets.id; + + +-- +-- TOC entry 1904 (class 2604 OID 31284) -- Name: id; Type: DEFAULT; Schema: public; Owner: michele -- -ALTER TABLE ONLY "Files" ALTER COLUMN id SET DEFAULT nextval('"Files_id_seq"'::regclass); +ALTER TABLE ONLY files ALTER COLUMN id SET DEFAULT nextval('files_id_seq'::regclass); -- --- TOC entry 2014 (class 0 OID 31281) --- Dependencies: 175 --- Data for Name: Files; Type: TABLE DATA; Schema: public; Owner: michele +-- TOC entry 1907 (class 2604 OID 31414) +-- Name: id; Type: DEFAULT; Schema: public; Owner: michele -- -COPY "Files" (id, path, level, group_id, is_directory, is_symlink, size, hash) FROM stdin; -\. +ALTER TABLE ONLY sets ALTER COLUMN id SET DEFAULT nextval('sets_id_seq'::regclass); -- --- TOC entry 2024 (class 0 OID 0) --- Dependencies: 174 --- Name: Files_id_seq; Type: SEQUENCE SET; Schema: public; Owner: michele --- - -SELECT pg_catalog.setval('"Files_id_seq"', 128, true); - - --- --- TOC entry 1900 (class 2606 OID 31289) +-- TOC entry 1912 (class 2606 OID 31289) -- Name: pk_files_id; Type: CONSTRAINT; Schema: public; Owner: michele; Tablespace: -- -ALTER TABLE ONLY "Files" +ALTER TABLE ONLY files ADD CONSTRAINT pk_files_id PRIMARY KEY (id); -- --- TOC entry 1902 (class 2606 OID 31294) +-- TOC entry 1916 (class 2606 OID 31420) +-- Name: pk_sets_id; Type: CONSTRAINT; Schema: public; Owner: michele; Tablespace: +-- + +ALTER TABLE ONLY sets + ADD CONSTRAINT pk_sets_id PRIMARY KEY (id); + + +-- +-- TOC entry 1914 (class 2606 OID 31294) -- Name: uniq_item; Type: CONSTRAINT; Schema: public; Owner: michele; Tablespace: -- -ALTER TABLE ONLY "Files" +ALTER TABLE ONLY files ADD CONSTRAINT uniq_item UNIQUE (path, group_id); -- --- TOC entry 1898 (class 1259 OID 31292) +-- TOC entry 1909 (class 1259 OID 31426) +-- Name: fki_files_sets; Type: INDEX; Schema: public; Owner: michele; Tablespace: +-- + +CREATE INDEX fki_files_sets ON files USING btree (group_id); + + +-- +-- TOC entry 1910 (class 1259 OID 31292) -- Name: idx_paths; Type: INDEX; Schema: public; Owner: michele; Tablespace: -- -CREATE INDEX idx_paths ON "Files" USING btree (path); +CREATE INDEX idx_paths ON files USING btree (path); -- --- TOC entry 1903 (class 2620 OID 31291) +-- TOC entry 1918 (class 2620 OID 31291) -- Name: triggerupcasehash; Type: TRIGGER; Schema: public; Owner: michele -- -CREATE TRIGGER triggerupcasehash BEFORE INSERT OR UPDATE ON "Files" FOR EACH ROW EXECUTE PROCEDURE upcase_hash(); +CREATE TRIGGER triggerupcasehash BEFORE INSERT OR UPDATE ON files FOR EACH ROW EXECUTE PROCEDURE upcase_hash(); -- --- TOC entry 2021 (class 0 OID 0) +-- TOC entry 1917 (class 2606 OID 31421) +-- Name: fk_files_sets; Type: FK CONSTRAINT; Schema: public; Owner: michele +-- + +ALTER TABLE ONLY files + ADD CONSTRAINT fk_files_sets FOREIGN KEY (group_id) REFERENCES sets(id); + + +-- +-- TOC entry 2034 (class 0 OID 0) -- Dependencies: 8 -- Name: public; Type: ACL; Schema: -; Owner: postgres -- @@ -169,7 +224,7 @@ GRANT ALL ON SCHEMA public TO postgres; GRANT ALL ON SCHEMA public TO PUBLIC; --- Completed on 2015-11-11 02:04:55 GMT +-- Completed on 2015-11-11 13:27:00 GMT -- -- PostgreSQL database dump complete diff --git a/src/dbbackend.cpp b/src/dbbackend.cpp index 570bed8..da35c9d 100644 --- a/src/dbbackend.cpp +++ b/src/dbbackend.cpp @@ -35,10 +35,11 @@ namespace din { conn.connect(); conn.query_void("BEGIN;"); + conn.query_void("INSERT INTO \"sets\" (\"desc\") VALUES ('test group');"); //TODO: use COPY instead of INSERT INTO for (std::size_t z = 0; z < parData.size(); z += g_batch_size) { 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) VALUES "; const char* comma = ""; for (auto i = z; i < std::min(z + g_batch_size, parData.size()); ++i) { @@ -46,7 +47,8 @@ namespace din { query << comma; query << '(' << conn.escape_literal(itm.path) << ",'" << itm.hash << "'," << itm.level << ',' - << 10 << ',' << (itm.is_directory ? "true" : "false") << ',' + << "currval('\"sets_id_seq\"')" << ',' + << (itm.is_directory ? "true" : "false") << ',' << (itm.is_symlink ? "true" : "false") << ',' << itm.size << ')' ; comma = ",";