call SYSCS_UTIL.SYSCS_COMPRESS_TABLE('SA', 'HFJ_SEARCH_RESULT', 1);
CALL SYSCS_UTIL.SYSCS_INPLACE_COMPRESS_TABLE( 'SA', 'HFJ_SEARCH_RESULT', 0, 0, 1 );


dd if=/dev/urandom of=/opt/glassfish/tmp.tmp bs=1M count=500

# Reindex
curl -H "Authorization: Bearer " "http://fhirtest.uhn.ca/baseDstu3/\$mark-all-resources-for-reindexing"


# Delete all errored resources
update hfj_res_ver set forced_id_pid = null where res_id in (select res_id from hfj_resource where sp_index_status = 2);
update hfj_resource set forced_id_pid = null where res_id in (select res_id from hfj_resource where sp_index_status = 2);
delete from hfj_history_tag where res_id in (select res_id from hfj_resource where sp_index_status = 2);
delete from hfj_res_ver where res_id in (select res_id from hfj_resource where sp_index_status = 2);
delete from hfj_forced_id where resource_pid in (select res_id from hfj_resource where sp_index_status = 2);
delete from hfj_res_link where src_resource_id in (select res_id from hfj_resource where sp_index_status = 2);
delete from hfj_res_link where target_resource_id in (select res_id from hfj_resource where sp_index_status = 2);
delete from hfj_spidx_coords where res_id in (select res_id from hfj_resource where sp_index_status = 2);
delete from hfj_spidx_date where res_id in (select res_id from hfj_resource where sp_index_status = 2);
delete from hfj_spidx_number where res_id in (select res_id from hfj_resource where sp_index_status = 2);
delete from hfj_spidx_quantity where res_id in (select res_id from hfj_resource where sp_index_status = 2);
delete from hfj_spidx_string where res_id in (select res_id from hfj_resource where sp_index_status = 2);
delete from hfj_spidx_token where res_id in (select res_id from hfj_resource where sp_index_status = 2);
delete from hfj_spidx_uri where res_id in (select res_id from hfj_resource where sp_index_status = 2);
delete from hfj_res_tag where res_id in (select res_id from hfj_resource where sp_index_status = 2);
delete from hfj_search_result where resource_pid in (select res_id from hfj_resource where sp_index_status = 2);
delete from hfj_res_param_present where res_id in (select res_id from hfj_resource where sp_index_status = 2);
delete from hfj_resource where res_id in (select res_id from hfj_resource where sp_index_status = 2);


# Delete resource by id
delete from hfj_res_link where src_resource_id = 16940 or target_resource_id = 16940;
delete from hfj_spidx_date where res_id = 16940;
delete from hfj_spidx_string where res_id = 16940;
delete from hfj_spidx_token where res_id = 16940;
delete from hfj_spidx_uri where res_id = 16940;
delete from hfj_spidx_quantity where res_id = 16940;
delete from hfj_search_result where resource_pid = 16940;
delete from hfj_res_ver where res_id = 16940;
delete from hfj_res_param_present where res_id = 16940;
update hfj_resource set forced_id_pid = null where res_id = 16940;
delete from hfj_forced_id where resource_pid = 16940;
delete from hfj_resource where res_id = 16940;


# Drop all tables
drop table hfj_history_tag cascade constraints;
drop table hfj_res_ver cascade constraints;
drop table hfj_forced_id cascade constraints;
drop table hfj_res_link cascade constraints;
drop table hfj_res_link cascade constraints;
drop table hfj_spidx_coords cascade constraints;
drop table hfj_spidx_date cascade constraints;
drop table hfj_spidx_number cascade constraints;
drop table hfj_spidx_quantity cascade constraints;
drop table hfj_spidx_string cascade constraints;
drop table hfj_spidx_token cascade constraints;
drop table hfj_spidx_uri cascade constraints;
drop table hfj_res_tag cascade constraints;
drop table hfj_search_result cascade constraints;
drop table hfj_res_param_present cascade constraints;
drop table hfj_resource cascade constraints;
drop table hfj_idx_cmp_string_uniq cascade constraints;
drop table hfj_search cascade constraints;
drop table hfj_search_include cascade constraints;
drop table hfj_search_parm cascade constraints;
drop table hfj_subscription cascade constraints;
drop table hfj_subscription_flag_res cascade constraints;
drop table hfj_tag_def cascade constraints;
drop table trm_codesystem cascade constraints;
drop table trm_codesystem_var cascade constraints;
drop table trm_concept cascade constraints;
drop table trm_concept_pc_link cascade constraints;
drop table trm_concept_property cascade constraints;

# Delete all resources
update hfj_res_ver set forced_id_pid = null where res_id in (select res_id from hfj_resource);
update hfj_resource set forced_id_pid = null where res_id in (select res_id from hfj_resource);
delete from hfj_history_tag where res_id in (select res_id from hfj_resource);
delete from hfj_res_ver where res_id in (select res_id from hfj_resource);
delete from hfj_forced_id where resource_pid in (select res_id from hfj_resource);
delete from hfj_res_link where src_resource_id in (select res_id from hfj_resource);
delete from hfj_res_link where target_resource_id in (select res_id from hfj_resource);
delete from hfj_spidx_coords where res_id in (select res_id from hfj_resource);
delete from hfj_spidx_date where res_id in (select res_id from hfj_resource);
delete from hfj_spidx_number where res_id in (select res_id from hfj_resource);
delete from hfj_spidx_quantity where res_id in (select res_id from hfj_resource);
delete from hfj_spidx_string where res_id in (select res_id from hfj_resource);
delete from hfj_spidx_token where res_id in (select res_id from hfj_resource);
delete from hfj_spidx_uri where res_id in (select res_id from hfj_resource);
delete from hfj_res_tag where res_id in (select res_id from hfj_resource);
delete from hfj_search_result where resource_pid in (select res_id from hfj_resource);
delete from hfj_res_param_present where res_id in (select res_id from hfj_resource);
delete from hfj_idx_cmp_string_uniq where res_id in (select res_id from hfj_resource);
delete from hfj_subscription_stats where res_id in (select res_id from hfj_resource);
delete from hfj_resource where res_id in (select res_id from hfj_resource);

# Correct Version issues
update hfj_resource
set res_ver = res_ver + 1
where res_id in (
   select distinct res.res_id from hfj_resource res
	inner join hfj_res_ver rv on (rv.res_id = res.res_id)
	where ((res.res_ver + 1) = (rv.res_ver))
)

# Oracle
drop table hfj_history_tag cascade constraints;
drop table hfj_forced_id cascade constraints;
drop table hfj_res_link cascade constraints;
drop table hfj_spidx_coords cascade constraints;
drop table hfj_spidx_date cascade constraints;
drop table hfj_spidx_number cascade constraints;
drop table hfj_spidx_quantity cascade constraints;
drop table hfj_spidx_string cascade constraints;
drop table hfj_spidx_token cascade constraints;
drop table hfj_spidx_uri cascade constraints;
drop table hfj_res_tag cascade constraints;
drop table hfj_search_result cascade constraints;
drop table hfj_search_include cascade constraints;
drop table hfj_search cascade constraints;
drop table hfj_res_param_present cascade constraints;
DROP TABLE HFJ_RES_REINDEX_JOB cascade constraints;
DROP TABLE HFJ_SEARCH_PARM cascade constraints;
DROP TABLE HFJ_TAG_DEF cascade CONSTRAINTS;
drop table hfj_idx_cmp_string_uniq cascade constraints;
drop table hfj_subscription_stats cascade constraints;
drop table trm_concept_property cascade constraints;
drop table trm_concept_pc_link cascade constraints;
drop table trm_concept cascade constraints;
drop table trm_codesystem_ver cascade constraints;
drop table trm_codesystem cascade constraints;
DROP TABLE hfj_resource CASCADE CONSTRAINTS;
DROP TABLE hfj_res_ver CASCADE CONSTRAINTS;
DROP TABLE TRM_CONCEPT_DESIG CASCADE CONSTRAINTS;
DROP TABLE TRM_CONCEPT_MAP CASCADE CONSTRAINTS;
DROP TABLE TRM_CONCEPT_MAP_GROUP CASCADE CONSTRAINTS;
DROP TABLE TRM_CONCEPT_MAP_GRP_ELEMENT CASCADE CONSTRAINTS;
DROP TABLE TRM_CONCEPT_MAP_GRP_ELM_TGT CASCADE CONSTRAINTS;



drop table hfj_history_tag cascade;
drop table hfj_forced_id cascade;
drop table hfj_res_link cascade;
drop table hfj_spidx_coords cascade;
drop table hfj_spidx_date cascade;
drop table hfj_spidx_number cascade;
drop table hfj_spidx_quantity cascade;
drop table hfj_spidx_string cascade;
drop table hfj_spidx_token cascade;
drop table hfj_spidx_uri cascade;
drop table hfj_res_tag cascade;
drop table hfj_search_result cascade;
drop table hfj_search_include cascade;
drop table hfj_search cascade;
drop table hfj_res_param_present cascade;
drop table hfj_idx_cmp_string_uniq cascade;
drop table hfj_subscription_stats cascade;
drop table trm_concept_property cascade;
drop table trm_concept_pc_link cascade;
drop table trm_concept cascade;
drop table trm_codesystem_ver cascade;
drop table trm_codesystem cascade;
DROP TABLE hfj_resource cascade;
DROP TABLE hfj_res_ver cascade;
drop table hfj_search_parm cascade;
drop table hfj_tag_def cascade;

drop index IDX_FORCEDID_TYPE_FORCEDID;
alter table hfj_forced_id drop constraint idx_forcedid_type_resid;



Upgrading
drop index IDX_SP_STRING;
create index IDX_SP_STRING_HASH_NRM;
create index IDX_SP_STRING_HASH_EXCT;
drop index IDX_SP_TOKEN;
drop index IDX_SP_TOKEN_UNQUAL;
create index IDX_SP_TOKEN_HASH;
create index IDX_SP_TOKEN_HASH_S;
create index IDX_SP_TOKEN_HASH_SV;
create index IDX_SP_TOKEN_HASH_V;
drop index IDX_SP_DATE;
create index IDX_SP_DATE_HASH;
drop index IDX_SP_QUANTITY;
create index IDX_SP_QUANTITY_HASH;
create index IDX_SP_QUANTITY_HASH_UN;
drop index IDX_FORCEDID_TYPE_FORCEDID;
alter table hfj_forced_id drop constraint idx_forcedid_type_resid;
create index IDX_FORCEDID_TYPE_FID;
drop index IDX_SP_NUMBER;
create index IDX_SP_NUMBER_HASH_VAL;

drop column SP_ID from table HFJ_RES_PARAM_PRESENT;
drop index IDX_SEARCHPARM_RESTYPE_SPNAME;
drop index IDX_RESPARMPRESENT_SPID_RESID;
drop table HFJ_SEARCH_PARM;



# Delete everything
update hfj_res_ver set forced_id_pid = null where res_id in (select res_id from hfj_resource);
update hfj_resource set forced_id_pid = null where res_id in (select res_id from hfj_resource);
delete from hfj_history_tag where res_id in (select res_id from hfj_resource);
delete from hfj_res_ver where res_id in (select res_id from hfj_resource);
delete from hfj_forced_id where resource_pid in (select res_id from hfj_resource);
delete from hfj_res_link where src_resource_id in (select res_id from hfj_resource);
delete from hfj_res_link where target_resource_id in (select res_id from hfj_resource);
delete from hfj_spidx_coords;
delete from hfj_spidx_date;
delete from hfj_spidx_number;
delete from hfj_spidx_quantity;
delete from hfj_spidx_string;
delete from hfj_spidx_token;
delete from hfj_spidx_uri;
delete from hfj_res_tag;
delete from hfj_search_result;
delete from hfj_res_param_present;
delete from hfj_idx_cmp_string_uniq;
delete from hfj_subscription_stats;
DELETE FROM TRM_CONCEPT_MAP_GRP_ELM_TGT;
DELETE FROM TRM_CONCEPT_MAP_GRP_ELEMENT;
DELETE FROM TRM_CONCEPT_MAP_GROUP;
DELETE FROM TRM_CONCEPT_MAP;
DELETE FROM TRM_CONCEPT_DESIG;
DELETE FROM TRM_CONCEPT_PC_LINK;
DELETE FROM TRM_CONCEPT_PROPERTY;
DELETE FROM TRM_CONCEPT;
DELETE FROM TRM_CODESYSTEM_VER;
DELETE FROM TRM_CODESYSTEM;
delete from hfj_resource;




# Delete All (Oracle)
DROP TABLE HFJ_FORCED_ID CASCADE CONSTRAINTS PURGE;
DROP TABLE hfj_res_ver CASCADE CONSTRAINTS PURGE;
DROP TABLE  hfj_resource CASCADE CONSTRAINTS PURGE;
DROP TABLE  hfj_history_tag CASCADE CONSTRAINTS PURGE;
DROP TABLE  hfj_res_ver CASCADE CONSTRAINTS PURGE;
DROP TABLE  hfj_forced_id CASCADE CONSTRAINTS PURGE;
DROP TABLE  hfj_res_link CASCADE CONSTRAINTS PURGE;
DROP TABLE  hfj_res_link CASCADE CONSTRAINTS PURGE;
DROP TABLE  hfj_spidx_coords CASCADE CONSTRAINTS PURGE;
DROP TABLE  hfj_spidx_date CASCADE CONSTRAINTS PURGE;
DROP TABLE  hfj_spidx_number CASCADE CONSTRAINTS PURGE;
DROP TABLE  hfj_spidx_quantity CASCADE CONSTRAINTS PURGE;
DROP TABLE  hfj_spidx_string CASCADE CONSTRAINTS PURGE;
DROP TABLE  hfj_spidx_token CASCADE CONSTRAINTS PURGE;
DROP TABLE  hfj_spidx_uri CASCADE CONSTRAINTS PURGE;
DROP TABLE  hfj_res_tag CASCADE CONSTRAINTS PURGE;
DROP TABLE  hfj_search_result CASCADE CONSTRAINTS PURGE;
DROP TABLE  hfj_res_param_present CASCADE CONSTRAINTS PURGE;
DROP TABLE  hfj_idx_cmp_string_uniq CASCADE CONSTRAINTS PURGE;
DROP TABLE  hfj_subscription_stats CASCADE CONSTRAINTS PURGE;
DROP TABLE  TRM_CONCEPT_MAP_GRP_ELM_TGT CASCADE CONSTRAINTS PURGE;
DROP TABLE  TRM_CONCEPT_MAP_GRP_ELEMENT CASCADE CONSTRAINTS PURGE;
DROP TABLE  TRM_CONCEPT_MAP_GROUP CASCADE CONSTRAINTS PURGE;
DROP TABLE  TRM_CONCEPT_MAP CASCADE CONSTRAINTS PURGE;
DROP TABLE  TRM_CONCEPT_DESIG CASCADE CONSTRAINTS PURGE;
DROP TABLE  TRM_CONCEPT_PC_LINK CASCADE CONSTRAINTS PURGE;
DROP TABLE  TRM_CONCEPT_PROPERTY CASCADE CONSTRAINTS PURGE;
DROP TABLE  TRM_CONCEPT CASCADE CONSTRAINTS PURGE;
DROP TABLE  TRM_CODESYSTEM_VER CASCADE CONSTRAINTS PURGE;
DROP TABLE  TRM_CODESYSTEM CASCADE CONSTRAINTS PURGE;
DROP TABLE  hfj_resource CASCADE CONSTRAINTS PURGE;
DROP TABLE HFJ_SEARCH_RESULT CASCADE CONSTRAINTS PURGE;
DROP TABLE HFJ_SEARCH_PARM CASCADE CONSTRAINTS PURGE;
DROP TABLE HFJ_SEARCH_INCLUDE CASCADE CONSTRAINTS PURGE;
DROP TABLE HFJ_SEARCH  CASCADE CONSTRAINTS PURGE;
DROP TABLE HFJ_SUBSCRIPTION  CASCADE CONSTRAINTS PURGE;
DROP TABLE HFJ_SUBSCRIPTION_FLAG_RES CASCADE CONSTRAINTS PURGE;
DROP TABLE HFJ_TAG_DEF CASCADE CONSTRAINTS PURGE;

