-- To deal with primary keys

create table requestid 
( 
    request_id int primary key default 0
);

create table transferid 
(
    transfer_id int primary key default 0
);



-- one transfer request can contain more than one transfer
-- if concurrency is not specified it is assumed to be '1'

create table request
(
    id                  int     primary key,
    concurrency         int     default 1,
    termination_time    float, 
    proxy_loc           text,
    username            text,
    started             boolean default false,
    all_or_none         boolean default false,
    maxAttempts         int default 1,
    delegated_epr       text,
    user_subject        text
);
-- false = its ok if some of transfers fail.no need to clean up

-- status = 0 = transfer FINISHED
-- status = 1 = transfer RETRYING 
-- status = 2 = transfer FAILED
-- status = 3 = transfer ACTIVE
-- status = 4 = transfer PENDING
-- status = 5 = transfer CANCELLED
-- status = 6 = Url Expanding
-- status = 7 = Url Expanding done
-- status = 8 = marked for deletion

create table transfer
(
	id					int primary key,
    request_id          int             not null,
	source_url			text			not null,
	dest_url			text			null,
	status				int 			default	4,
	attempts			int 			default 0,
    user_name           text            default null,
	dcau				boolean			default FALSE,
    parallel_streams    int             default 1,
    tcp_buffer_size     int             default 0,
    block_size          integer         default 16000,
    notpt               boolean         default FALSE,
    binary_mode         boolean         default TRUE,
    source_subject      text,
    dest_subject        text,
    retry_time          float           default 0,
    size                float           default 0,
    fault               text,        
    permissions         int,
    ignore_file_perm_errors   boolean    default false
);

create table restart
(
	transfer_id 		int,
	marker				text
);

create table factory
(
    total_transfers     float,
    total_bytes         float
);

create index transfer_tindex on transfer using btree (request_id,  attempts); 
