-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule.sql
More file actions
60 lines (58 loc) · 2.04 KB
/
module.sql
File metadata and controls
60 lines (58 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
DELIMITER //
DROP FUNCTION IF EXISTS phash_distance //
CREATE FUNCTION phash_distance(p_hash1 CHAR(16), p_hash2 CHAR(16))
RETURNS FLOAT
DETERMINISTIC
BEGIN
DECLARE count INT;
DECLARE i INT;
DECLARE L CHAR(2);
DECLARE R CHAR(2);
DECLARE N INT;
SET i = 0;
SET count = 0;
REPEAT
SET L = SUBSTR(p_hash1, 1 + i * 2, 2);
SET R = SUBSTR(p_hash2, 1 + i * 2, 2);
SET N = CONV(L, 16, 10) ^ CONV(R, 16, 10);
SET count = count + bit_count(N);
SET i = i + 1;
UNTIL i = 8
END REPEAT;
RETURN 1 - count / 64.0;
END //
DROP PROCEDURE IF EXISTS get_post_per_distance //
CREATE PROCEDURE get_post_per_distance(p_hash CHAR(16), p_chat_id INTEGER, p_threshold FLOAT)
DETERMINISTIC
BEGIN
DROP TABLE IF EXISTS tmp_post_per_distance;
CREATE TEMPORARY TABLE tmp_post_per_distance AS
SELECT
*,
phash_distance(file_hash, p_hash) AS distance,
phash_distance(file_preview_hash, p_hash) AS distance_preview
FROM post
WHERE (chat_id = p_chat_id OR chat_id IS NULL) AND
(phash_distance(file_hash, p_hash) >= p_threshold OR
phash_distance(file_preview_hash, p_hash) >= p_threshold)
ORDER BY distance, distance_preview;
COMMIT;
END //
DROP PROCEDURE IF EXISTS get_repost_per_distance //
CREATE PROCEDURE get_repost_per_distance(p_hash CHAR(16), p_chat_id INTEGER, p_threshold FLOAT)
DETERMINISTIC
BEGIN
DROP TABLE IF EXISTS tmp_repost_per_distance;
CREATE TEMPORARY TABLE tmp_repost_per_distance AS
SELECT
*,
phash_distance(file_hash, p_hash) AS distance,
phash_distance(file_preview_hash, p_hash) AS distance_preview
FROM repost
WHERE (chat_id = p_chat_id OR chat_id) IS NULL AND
(phash_distance(file_hash, p_hash) >= p_threshold OR
phash_distance(file_preview_hash, p_hash) >= p_threshold)
ORDER BY distance, distance_preview;
COMMIT;
END //
DELIMITER ;