-- Oracle tables for WMComponent.DBS3Buffer component

CREATE TABLE dbsbuffer_dataset (
    id                NUMBER(11)     GENERATED BY DEFAULT AS IDENTITY,
    path              VARCHAR2(500)  NOT NULL,
    processing_ver    NUMBER(11)     NOT NULL,
    acquisition_era   VARCHAR2(255),
    valid_status      VARCHAR2(20),
    global_tag        VARCHAR2(255),
    parent           VARCHAR2(500),
    prep_id          VARCHAR2(255),
    CONSTRAINT dbsbuffer_dataset_pk PRIMARY KEY (id),
    CONSTRAINT uq_dbs_dat UNIQUE (path)
)
/

CREATE TABLE dbsbuffer_dataset_subscription (
    id                NUMBER(11)     GENERATED BY DEFAULT AS IDENTITY,
    dataset_id        NUMBER(11)     NOT NULL,
    site              VARCHAR2(100)  NOT NULL,
    custodial         NUMBER(11)     DEFAULT 0,
    priority          VARCHAR2(10)   DEFAULT 'Low',
    subscribed        NUMBER(11)     DEFAULT 0,
    delete_blocks     NUMBER(11),
    dataset_lifetime  NUMBER(11)     DEFAULT 0 NOT NULL,
    CONSTRAINT dbsbuffer_dataset_sub_pk PRIMARY KEY (id),
    CONSTRAINT uq_dbs_dat_sub UNIQUE (dataset_id, site, custodial, priority),
    CONSTRAINT fk_dsetsubscription_datasetid FOREIGN KEY (dataset_id)
        REFERENCES dbsbuffer_dataset(id) ON DELETE CASCADE
)
/

CREATE TABLE dbsbuffer_algo (
    id              NUMBER(11)     GENERATED BY DEFAULT AS IDENTITY,
    app_name        VARCHAR2(100),
    app_ver         VARCHAR2(100),
    app_fam         VARCHAR2(100),
    pset_hash       VARCHAR2(700),
    config_content  CLOB,
    in_dbs          NUMBER(11),
    CONSTRAINT dbsbuffer_algo_pk PRIMARY KEY (id),
    CONSTRAINT uq_dbs_alg UNIQUE (app_name, app_ver, app_fam, pset_hash)
)
/

CREATE TABLE dbsbuffer_algo_dataset_assoc (
    id          NUMBER(11)     GENERATED BY DEFAULT AS IDENTITY,
    algo_id     NUMBER(11)     NOT NULL,
    dataset_id  NUMBER(11)     NOT NULL,
    in_dbs      NUMBER(11)     DEFAULT 0,
    CONSTRAINT dbsbuffer_algodset_assoc_pk PRIMARY KEY (id),
    CONSTRAINT fk_algodset_assoc_algo_id FOREIGN KEY (algo_id)
        REFERENCES dbsbuffer_algo(id) ON DELETE CASCADE,
    CONSTRAINT fk_algodset_assoc_dataset_id FOREIGN KEY (dataset_id)
        REFERENCES dbsbuffer_dataset(id) ON DELETE CASCADE
)
/

CREATE TABLE dbsbuffer_workflow (
    id                          NUMBER(11)     GENERATED BY DEFAULT AS IDENTITY,
    name                        VARCHAR2(700),
    task                        VARCHAR2(700),
    block_close_max_wait_time   NUMBER(11),
    block_close_max_files       NUMBER(11),
    block_close_max_events      NUMBER(11),
    block_close_max_size        NUMBER(20),
    completed                   NUMBER(11)     DEFAULT 0,
    CONSTRAINT dbsbuffer_workflow_pk PRIMARY KEY (id),
    CONSTRAINT uq_dbs_wor UNIQUE (name, task)
)
/

CREATE TABLE dbsbuffer_file (
    id                  NUMBER(11)     GENERATED BY DEFAULT AS IDENTITY,
    lfn                 VARCHAR2(500)  NOT NULL,
    filesize            NUMBER(20),
    events              NUMBER(11),
    dataset_algo        NUMBER(11)      NOT NULL,
    block_id            NUMBER(11),
    status              VARCHAR2(20),
    in_phedex           NUMBER(11)      DEFAULT 0,
    workflow            NUMBER(11),
    LastModificationDate NUMBER(11),
    CONSTRAINT dbsbuffer_file_pk PRIMARY KEY (id),
    CONSTRAINT uq_dbs_fil UNIQUE (lfn),
    CONSTRAINT fk_file_workflow FOREIGN KEY (workflow)
        REFERENCES dbsbuffer_workflow(id) ON DELETE CASCADE,
    CONSTRAINT fk_file_dataset_algo FOREIGN KEY (dataset_algo)
        REFERENCES dbsbuffer_algo_dataset_assoc(id) ON DELETE CASCADE
)
/

CREATE TABLE dbsbuffer_file_parent (
    child   NUMBER(11)    NOT NULL,
    parent  NUMBER(11)    NOT NULL,
    CONSTRAINT pk_dbs_fil_par PRIMARY KEY (child, parent),
    CONSTRAINT fk_file_parent_child FOREIGN KEY (child)
        REFERENCES dbsbuffer_file(id) ON DELETE CASCADE,
    CONSTRAINT fk_file_parent_parent FOREIGN KEY (parent)
        REFERENCES dbsbuffer_file(id) ON DELETE CASCADE
)
/

CREATE TABLE dbsbuffer_file_runlumi_map (
    filename    NUMBER(11)    NOT NULL,
    run         NUMBER(11)    NOT NULL,
    lumi        NUMBER(11)    NOT NULL,
    num_events  NUMBER(11),
    CONSTRAINT fk_file_runlumi_filename FOREIGN KEY (filename)
        REFERENCES dbsbuffer_file(id) ON DELETE CASCADE
)
/

CREATE TABLE dbsbuffer_location (
    id    NUMBER(11)     GENERATED BY DEFAULT AS IDENTITY,
    pnn   VARCHAR2(255)  NOT NULL,
    CONSTRAINT dbsbuffer_location_pk PRIMARY KEY (id),
    CONSTRAINT uq_dbs_loc UNIQUE (pnn)
)
/

CREATE TABLE dbsbuffer_file_location (
    filename  NUMBER(11)    NOT NULL,
    location  NUMBER(11)    NOT NULL,
    CONSTRAINT pk_dbs_fil_loc PRIMARY KEY (filename, location),
    CONSTRAINT fk_file_location_filename FOREIGN KEY (filename)
        REFERENCES dbsbuffer_file(id) ON DELETE CASCADE,
    CONSTRAINT fk_file_location_location FOREIGN KEY (location)
        REFERENCES dbsbuffer_location(id) ON DELETE CASCADE
)
/

CREATE TABLE dbsbuffer_block (
    id           NUMBER(11)     GENERATED BY DEFAULT AS IDENTITY,
    dataset_id   NUMBER(11)     NOT NULL,
    blockname    VARCHAR2(250)  NOT NULL,
    location     NUMBER(11)     NOT NULL,
    create_time  NUMBER(11),
    status       VARCHAR2(20),
    deleted      NUMBER(11)     DEFAULT 0,
    rule_id      VARCHAR2(40)   DEFAULT '0' NOT NULL,
    CONSTRAINT dbsbuffer_block_pk PRIMARY KEY (id),
    CONSTRAINT uq_dbs_blo UNIQUE (blockname, location),
    CONSTRAINT fk_block_dataset_id FOREIGN KEY (dataset_id)
        REFERENCES dbsbuffer_dataset(id) ON DELETE CASCADE,
    CONSTRAINT fk_block_location FOREIGN KEY (location)
        REFERENCES dbsbuffer_location(id) ON DELETE CASCADE
)
/

CREATE TABLE dbsbuffer_checksum_type (
    id    NUMBER(11)     GENERATED BY DEFAULT AS IDENTITY,
    type  VARCHAR2(255),
    CONSTRAINT dbsbuffer_checksum_type_pk PRIMARY KEY (id)
)
/

CREATE TABLE dbsbuffer_file_checksums (
    fileid  NUMBER(11)     NOT NULL,
    typeid  NUMBER(11)     NOT NULL,
    cksum   VARCHAR2(100),
    CONSTRAINT pk_dbs_fil_che PRIMARY KEY (fileid, typeid),
    CONSTRAINT fk_file_checksums_fileid FOREIGN KEY (fileid)
        REFERENCES dbsbuffer_file(id) ON DELETE CASCADE,
    CONSTRAINT fk_file_checksums_typeid FOREIGN KEY (typeid)
        REFERENCES dbsbuffer_checksum_type(id) ON DELETE CASCADE
)
/

-- Indexes
CREATE INDEX idx_dbs_fil_che_1 ON dbsbuffer_file_checksums (typeid)
/

CREATE INDEX idx_dbs_fil_che_2 ON dbsbuffer_file_checksums (fileid)
/

CREATE INDEX idx_dbs_dat_sub_1 ON dbsbuffer_dataset_subscription (dataset_id)
/

CREATE INDEX idx_dbs_fil_par_1 ON dbsbuffer_file_parent (child)
/

CREATE INDEX idx_dbs_fil_par_2 ON dbsbuffer_file_parent (parent)
/

CREATE INDEX idx_dbs_blo_1 ON dbsbuffer_block (location)
/

CREATE INDEX idx_dbs_blo_2 ON dbsbuffer_block (dataset_id)
/

CREATE INDEX idx_dbs_alg_ass_1 ON dbsbuffer_algo_dataset_assoc (dataset_id)
/

CREATE INDEX idx_dbs_alg_ass_2 ON dbsbuffer_algo_dataset_assoc (algo_id)
/

CREATE INDEX idx_dbs_fil_run_1 ON dbsbuffer_file_runlumi_map (filename)
/

CREATE INDEX idx_dbs_fil_loc_1 ON dbsbuffer_file_location (location)
/

CREATE INDEX idx_dbs_fil_loc_2 ON dbsbuffer_file_location (filename)
/

CREATE INDEX idx_dbs_fil_1 ON dbsbuffer_file (workflow)
/

-- Initial data
INSERT INTO dbsbuffer_checksum_type (type) VALUES ('cksum')
/

INSERT INTO dbsbuffer_checksum_type (type) VALUES ('adler32')
/

INSERT INTO dbsbuffer_checksum_type (type) VALUES ('md5')
/ 