Commit 11fa10ff authored by Cristiano Urban's avatar Cristiano Urban
Browse files

Copied file catalog files.

parents
Loading
Loading
Loading
Loading

00-init.sql

0 → 100644
+98 −0
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');
CREATE TYPE LocationType AS ENUM ('virtual', 'tape', 'user', 'LBT');

CREATE TABLE Node (
    node_id                BIGSERIAL     NOT NULL,
    parent_path            LTREE         default NULL,
    parent_relative_path   LTREE         default NULL,
    name                   VARCHAR       NOT NULL,
    os_name                VARCHAR       default NULL,
    type                   NodeType      NOT NULL,
    location_type          LocationType  default NULL,
    format                 VARCHAR       default NULL,
    -- format serve per distinguere unstuctured (format=NULL) da structured che hanno un formato noto
    async_trans            BOOLEAN       default NULL,
    -- async_trans serve per indicare se il nodo e` ospitato da un cold storage e deve essere necessariamente 
    -- trasferito con un trasferimento asincrono 
    busy_state             BOOLEAN       default NULL,
    owner_id               VARCHAR       default NULL,
    creator_id             VARCHAR       default NULL,
    group_read             VARCHAR[]     default NULL,
    group_write            VARCHAR[]     default NULL,
    is_public              BOOLEAN       default NULL,
    delta                  BIGINT        default NULL,
    /* potrebbe essere un delta di dati trasferito durante un trasferimento asincrono.
       Dovrebbe stare sul servizio che fa il trasferimento  (es. redis) */
    content_type           VARCHAR       default NULL,
    content_encoding       VARCHAR       default NULL,
    content_length         BIGINT        default NULL,
    content_md5            TEXT          default NULL,
    created_on             TIMESTAMP     default CURRENT_TIMESTAMP,
    last_modified          TIMESTAMP     default NULL,
    -- link                  TEXT          default NULL,
    accept_views           TEXT[]        default NULL,
    provide_views          TEXT[]        default NULL,
    -- serve per mappare il nome del servizio di storage da interrogare per accedere al contenuto di questo nodo 
    -- storage_id            VARCHAR,    
    protocols              TEXT[]        default NULL,
    PRIMARY KEY (node_id)
);


CREATE TABLE NodeProperty (
    node_id           BIGSERIAL,
    property_uri      VARCHAR       NOT NULL,
    property_value    VARCHAR       default NULL,
    last_modified     TIMESTAMP     default CURRENT_TIMESTAMP,
    -- support replication with a fake primary key
    -- _rep_support     BIGINT       NOT NULL PRIMARY KEY,
    foreign key (node_id) references Node (node_id)
);


CREATE TABLE DeletedNode (
    node_id           BIGSERIAL     NOT NULL,
    name              VARCHAR       NOT NULL,
    owner_id          VARCHAR       NOT NULL,
    last_modified     TIMESTAMP     default CURRENT_TIMESTAMP,
    PRIMARY KEY (node_id)
);


CREATE TABLE Users (
    rap_id            VARCHAR       NOT NULL,
    user_name         VARCHAR       NOT NULL,
    e_mail            VARCHAR       NOT NULL,
    PRIMARY KEY (rap_id)
);

01-pgsql_path.sql

0 → 100644
+24 −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; 

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

02-indexes.sql

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

CREATE INDEX file_name_idx ON Node USING btree(name);

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);

ALTER TABLE Node ADD COLUMN path ltree GENERATED ALWAYS AS (path(parent_path, node_id)) STORED;
CREATE INDEX file_path_gist_idx ON Node USING GIST(path);
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);

03-os_path_view.sql

0 → 100644
+45 −0
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';

CREATE VIEW node_os_path AS
SELECT node_id, '/' AS os_path FROM Node WHERE path = ''
UNION
SELECT node_id, '/' || string_agg(name, '/') AS os_path
FROM (
    SELECT (CASE WHEN os_name IS NOT NULL THEN os_name ELSE name END) AS name, p.node_id
    FROM Node n
    JOIN (
        SELECT UNNEST(string_to_array(relative_path::varchar, '.')) AS rel_id, node_id
        FROM Node
    ) AS p ON n.node_id::varchar = p.rel_id
    ORDER BY p.node_id, nlevel(n.path)
) AS j GROUP BY node_id;

-- View that maps node_id to vos_path

CREATE VIEW node_vos_path AS
SELECT node_id, '/' AS vos_path FROM Node WHERE path = ''
UNION
SELECT node_id, '/' || string_agg(name, '/') AS os_path
FROM (
    SELECT name, p.node_id
    FROM Node n
    JOIN (
        SELECT UNNEST(string_to_array(path::varchar, '.')) AS rel_id, node_id
        FROM Node
    ) AS p ON n.node_id::varchar = p.rel_id
    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

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;
+21 −0
Original line number Diff line number Diff line
/*
    Author: Sonia Zorba
    Email: sonia.zorba@inaf.it
*/

-- Generate the os_path from the Node id
-- It could be used for initializing the os_path generated column
CREATE OR REPLACE FUNCTION get_os_path(input_id int) RETURNS varchar AS
$func$
SELECT string_agg(name, '/') FROM (
    WITH RECURSIVE paths AS (
            SELECT node_id, name, path, parent_path, 1 AS level
            FROM Node
            WHERE node_id = input_id
        UNION ALL
            SELECT f.node_id, f.name, f.path, f.parent_path, p.level + 1
            FROM Node f
            INNER JOIN paths p ON p.parent_path = f.path
    ) SELECT name FROM paths ORDER BY LEVEL DESC
) AS names;
$func$ LANGUAGE sql;