Skip to content

Commit 28cfe5c

Browse files
committed
refactor (DFC): rewrite the file/replica deletion stored procedure as they were in 5.6
1 parent b20d7ba commit 28cfe5c

File tree

1 file changed

+46
-76
lines changed

1 file changed

+46
-76
lines changed

src/DIRAC/DataManagementSystem/DB/FileCatalogWithFkAndPsDB.sql

Lines changed: 46 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -903,47 +903,38 @@ BEGIN
903903
RESIGNAL;
904904
END;
905905

906-
START TRANSACTION;
907-
908-
-- Store the name of the tmp table once for all
909-
910-
SET @tmpTableName = CONCAT('tmpDirUsageDelRep_',CONNECTION_ID());
911-
912-
-- We create the table if it does not exist
913-
SET @sql = CONCAT('CREATE TEMPORARY TABLE IF NOT EXISTS ',@tmpTableName ,' (DirID INT, SEID INT, t_size BIGINT UNSIGNED, t_file INT, INDEX(DirID))');
906+
START TRANSACTION;
907+
908+
909+
SET @sql = CONCAT('UPDATE FC_DirectoryUsage d,
910+
(SELECT d1.DirID, d1.SEID, SUM(f.Size) as t_size, count(*) as t_file
911+
FROM FC_DirectoryUsage d1, FC_Files f, FC_Replicas r
912+
WHERE r.FileID = f.FileID
913+
AND f.DirID = d1.DirID
914+
AND r.SEID = d1.SEID
915+
AND f.FileID IN (', file_ids, ')
916+
GROUP BY d1.DirID, d1.SEID ) t
917+
SET d.SESize = d.SESize - t.t_size,
918+
d.SEFiles = d.SEFiles - t.t_file
919+
WHERE d.DirID = t.DirID
920+
AND d.SEID = t.SEID');
921+
922+
-- This is buggy in case we remove two files that have a replica on the same SE
923+
--
924+
-- SET @sql = CONCAT('UPDATE FC_DirectoryUsage d, FC_Files f, FC_Replicas r
925+
-- SET d.SESize = d.SESize - f.Size, d.SEFiles = d.SEFiles - 1
926+
-- WHERE r.FileID = f.FileID
927+
-- AND f.DirID = d.DirID
928+
-- AND r.SEID = d.SEID
929+
-- AND f.FileID IN (', file_ids, ')');
914930

915931
PREPARE stmt FROM @sql;
916932
EXECUTE stmt;
917933
DEALLOCATE PREPARE stmt;
918934

919-
-- Insert into it the values we will have to substract later on
920-
SET @sql = CONCAT('INSERT INTO ', @tmpTableName, '(DirID, SEID, t_size, t_file) SELECT d1.DirID, d1.SEID, SUM(f.Size) as t_size, count(*) as t_file
921-
FROM FC_DirectoryUsage d1, FC_Files f, FC_Replicas r
922-
WHERE r.FileID = f.FileID AND f.DirID = d1.DirID AND r.SEID = d1.SEID AND f.FileID IN (', file_ids, ') GROUP BY d1.DirID, d1.SEID');
923935

924-
PREPARE stmt FROM @sql;
925-
EXECUTE stmt;
926-
DEALLOCATE PREPARE stmt;
927-
928-
-- perform the update
929-
SET @sql = CONCAT('UPDATE FC_DirectoryUsage d, ',@tmpTableName,' t set d.`SESize` = d.`SESize` - t.t_size, d.`SEFiles` = d.`SEFiles` - t.t_file where d.DirID = t.DirID and d.`SEID`= t.SEID');
930936

931-
PREPARE stmt FROM @sql;
932-
EXECUTE stmt;
933-
DEALLOCATE PREPARE stmt;
934-
935-
936-
937-
-- delete the entries from the temporary table
938-
SET @sql = CONCAT('DELETE t FROM ',@tmpTableName, ' t JOIN FC_Files f ON t.DirID = f.DirID where f.FileID IN (', file_ids, ')');
939-
940-
PREPARE stmt FROM @sql;
941-
EXECUTE stmt;
942-
DEALLOCATE PREPARE stmt;
943-
944-
945-
-- delete the entry from the FC_Replicas table
946-
SET @sql = CONCAT('DELETE FROM FC_Replicas WHERE FileID IN (', file_ids, ')');
937+
SET @sql = CONCAT('DELETE FROM FC_Replicas WHERE FileID IN (', file_ids, ')');
947938
PREPARE stmt FROM @sql;
948939
EXECUTE stmt;
949940
DEALLOCATE PREPARE stmt;
@@ -966,60 +957,39 @@ CREATE PROCEDURE ps_delete_files
966957
(IN file_ids MEDIUMTEXT)
967958
BEGIN
968959

960+
969961
DECLARE exit handler for sqlexception
970962
BEGIN
971963
ROLLBACK;
972964
RESIGNAL;
973965
END;
974966

975-
START TRANSACTION;
976-
977-
-- Store the name of the tmp table once for all
978-
979-
SET @tmpTableName = CONCAT('tmpDirUsageDelFile_',CONNECTION_ID());
980-
981-
-- We create the table if it does not exist
982-
SET @sql = CONCAT('CREATE TEMPORARY TABLE IF NOT EXISTS ',@tmpTableName ,' (DirID INT, t_size BIGINT UNSIGNED, t_file INT, INDEX(DirID))');
983-
984-
PREPARE stmt FROM @sql;
985-
EXECUTE stmt;
986-
DEALLOCATE PREPARE stmt;
987-
988-
989-
-- Insert into it the values we will have to substract later on
990-
SET @sql = CONCAT('INSERT INTO ', @tmpTableName, '(DirID,t_size, t_file) SELECT d1.DirID, SUM(f.Size) as t_size, count(*) as t_file
991-
FROM FC_DirectoryList d1, FC_Files f
992-
WHERE f.DirID = d1.DirID AND f.FileID IN (', file_ids, ') GROUP BY d1.DirID');
993-
994-
PREPARE stmt FROM @sql;
995-
EXECUTE stmt;
996-
DEALLOCATE PREPARE stmt;
997-
998-
-- perform the update
999-
SET @sql = CONCAT('UPDATE FC_DirectoryUsage d, ',@tmpTableName,' t set d.`SESize` = d.`SESize` - t.t_size, d.`SEFiles` = d.`SEFiles` - t.t_file where d.DirID = t.DirID and d.`SEID`= 1');
1000-
1001-
PREPARE stmt FROM @sql;
1002-
EXECUTE stmt;
1003-
DEALLOCATE PREPARE stmt;
1004-
967+
START TRANSACTION;
1005968

969+
SET @sql = CONCAT('UPDATE FC_DirectoryUsage d,
970+
(SELECT d1.DirID, SUM(f.Size) as t_size, count(*) as t_file
971+
FROM FC_DirectoryList d1, FC_Files f
972+
where f.DirID = d1.DirID
973+
AND f.FileID IN (', file_ids, ')
974+
GROUP BY d1.DirID ) t
975+
SET d.SESize = d.SESize - t.t_size,
976+
d.SEFiles = d.SEFiles - t.t_file
977+
WHERE d.DirID = t.DirID
978+
AND d.SEID = 1' );
1006979

1007-
-- delete the entries from the temporary table
1008-
SET @sql = CONCAT('DELETE t FROM ',@tmpTableName, ' t JOIN FC_Files f ON t.DirID = f.DirID where f.FileID IN (', file_ids, ')');
980+
PREPARE stmt FROM @sql;
981+
EXECUTE stmt;
982+
DEALLOCATE PREPARE stmt;
1009983

1010-
PREPARE stmt FROM @sql;
1011-
EXECUTE stmt;
1012-
DEALLOCATE PREPARE stmt;
984+
SET @sql = CONCAT('DELETE FROM FC_Files WHERE FileID IN (', file_ids, ')');
985+
PREPARE stmt FROM @sql;
986+
EXECUTE stmt;
987+
DEALLOCATE PREPARE stmt;
1013988

989+
COMMIT;
1014990

1015-
-- delete the entry from the File table
1016-
SET @sql = CONCAT('DELETE FROM FC_Files WHERE FileID IN (', file_ids, ')');
1017-
PREPARE stmt FROM @sql;
1018-
EXECUTE stmt;
1019-
DEALLOCATE PREPARE stmt;
991+
SELECT 0, 'OK';
1020992

1021-
COMMIT;
1022-
SELECT 0, 'OK';
1023993
END //
1024994
DELIMITER ;
1025995

0 commit comments

Comments
 (0)