Commit a38759e1 authored by Sonia Zorba's avatar Sonia Zorba
Browse files

Removed test data insertion. Refactoring of SQL files. Added license

parent 6af5b720
Loading
Loading
Loading
Loading
Loading
+39 −29
Original line number Diff line number Diff line
/**_____________________________________________________________________________
 *
 *                                 OATS - INAF
 *  Osservatorio Astronomico di Tireste - Istituto Nazionale di Astrofisica
 *  Astronomical Observatory of Trieste - National Institute for Astrophysics
 * ____________________________________________________________________________
 *
 * Copyright (C) 2020  Istituto Nazionale di Astrofisica
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc., 
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * _____________________________________________________________________________
 **/

/*
    VOSpace front-end
*/

CREATE EXTENSION IF NOT EXISTS ltree;


CREATE TYPE NodeType AS ENUM ('container', 'data', 'link', 'structured');
CREATE TYPE LocationType AS ENUM ('async', 'portal', 'user');
CREATE TYPE StorageType AS ENUM ('cold', 'hot', 'local', 'portal');
@@ -93,6 +65,8 @@ CREATE TABLE node (
    FOREIGN KEY (location_id) REFERENCES location (location_id)
);

CREATE INDEX file_name_idx ON node USING btree(name);


CREATE TABLE deleted_node (
    node_id                BIGSERIAL     NOT NULL,
@@ -142,3 +116,39 @@ CREATE TABLE users (
    e_mail            VARCHAR       NOT NULL,
    PRIMARY KEY (user_id)
);


CREATE TYPE JobPhaseEnum AS ENUM ('PENDING', 'QUEUED', 'EXECUTING', 'ABORTED', 'COMPLETED', 'HELD', 'SUSPENDED', 'ERROR', 'ARCHIVED');
CREATE TYPE ErrorType AS ENUM('transient','fatal'); 

CREATE TABLE job (
    job_id varchar not null,
    owner_id varchar not null,
    job_type varchar not null,
    phase JobPhaseEnum not null,
    start_time timestamp,
    end_time timestamp,
    creation_time timestamp default CURRENT_TIMESTAMP,
    job_info jsonb,
    processed_blocks int default null,
    total_blocks int default null,
    results jsonb,
    error_message varchar,
    error_type ErrorType,
    error_has_detail boolean,
    error_detail varchar,
    primary key(job_id)
);

CREATE INDEX owner_idx ON job USING btree(owner_id);


-- Stores the content of StructuredDataNodes representing a list of files.

CREATE TABLE list_of_files (
    list_node_id     BIGSERIAL NOT NULL,
    node_id          BIGSERIAL NOT NULL,
    PRIMARY KEY (list_node_id, node_id),
    FOREIGN KEY (list_node_id) REFERENCES node (node_id),
    FOREIGN KEY (node_id) REFERENCES node (node_id)
);
+34 −0
Original line number Diff line number Diff line
/*
    Author: Sonia Zorba
    Email: sonia.zorba@inaf.it
*/
-- Generates the path from parent_path and id
CREATE OR REPLACE FUNCTION path(parent_path ltree, id bigint) RETURNS ltree AS
$func$
BEGIN
    RETURN text2ltree(CASE
        WHEN parent_path IS NULL THEN ''
        WHEN parent_path::varchar = '' THEN id::varchar
        ELSE (parent_path::varchar || '.' || id::varchar)
    END);
END
$func$ LANGUAGE plpgsql IMMUTABLE; 

CREATE INDEX file_name_idx ON node USING btree(name);
-- Testing the function
SELECT CASE WHEN
path(NULL, 0) = text2ltree('')
AND path(text2ltree(''), 1) = text2ltree('1')
AND path(text2ltree('1'), 2) = text2ltree('1.2')
THEN 'PASSED' ELSE 'FAILED' END AS test;


-- Create node path indexes and generated columns using the previously defined function

CREATE INDEX file_parent_path_gist_idx ON node USING GIST(parent_path);
CREATE INDEX file_parent_path_idx ON node USING btree(parent_path);
@@ -14,3 +29,6 @@ CREATE UNIQUE INDEX file_path_idx ON node USING btree(path);

ALTER TABLE node ADD COLUMN relative_path ltree GENERATED ALWAYS AS (path(parent_relative_path, node_id)) STORED;
CREATE INDEX file_rel_path_gist_idx ON node USING GIST(relative_path);

-- Create root node
INSERT INTO node (parent_path, parent_relative_path, name, type, creator_id, location_id, is_public) VALUES (NULL, NULL, '', 'container', '0', 1, true);

01-pgsql_path.sql

deleted100644 → 0
+0 −23
Original line number Diff line number Diff line
/*
    Author: Sonia Zorba
    Email: sonia.zorba@inaf.it
*/

-- Generates the path from parent_path and id
CREATE OR REPLACE FUNCTION path(parent_path ltree, id bigint) RETURNS ltree AS
$func$
BEGIN
    RETURN text2ltree(CASE
        WHEN parent_path IS NULL THEN ''
        WHEN parent_path::varchar = '' THEN id::varchar
        ELSE (parent_path::varchar || '.' || id::varchar)
    END);
END
$func$ LANGUAGE plpgsql IMMUTABLE; 

-- Testing the function
SELECT CASE WHEN
path(NULL, 0) = text2ltree('')
AND path(text2ltree(''), 1) = text2ltree('1')
AND path(text2ltree('1'), 2) = text2ltree('1.2')
THEN 'PASSED' ELSE 'FAILED' END AS test;
 No newline at end of file
+6 −10
Original line number Diff line number Diff line
/*
    Author: Sonia Zorba
    Email: sonia.zorba@inaf.it
*/

-- This view allows to obtain the the ltree_path from the os_path
-- Example query:
-- SELECT path FROM node n JOIN node_os_path o ON n.node_id = o.node_id WHERE os_path = '/curban/store';
-- View that maps node_id to os_path (relative path of file on disk)

CREATE VIEW node_os_path AS
SELECT node_id, '/' AS os_path FROM node WHERE path = ''
@@ -21,7 +14,7 @@ FROM (
    ORDER BY p.node_id, nlevel(n.path)
) AS j GROUP BY node_id;

-- View that maps node_id to vos_path
-- View that maps node_id to vos_path (path in VOSpace)

CREATE VIEW node_vos_path AS
SELECT node_id, '/' AS vos_path FROM node WHERE path = ''
@@ -37,9 +30,12 @@ FROM (
    ORDER BY p.node_id, nlevel(n.path)
) AS j GROUP BY node_id;

-- This view allows to obtain the the os_path from the vos_path
-- View that combines the two previous views to obtain the os_path from the vos_path and vice versa

CREATE VIEW node_path AS
SELECT v.node_id, vos_path, os_path
FROM node_vos_path v
LEFT JOIN node_os_path o ON o.node_id = v.node_id;

-- Example query to obtain the the ltree_path from the os_path:
-- SELECT path FROM node n JOIN node_os_path o ON n.node_id = o.node_id WHERE os_path = '/curban/store';
+0 −0

File moved.

Loading