-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdupfinder.js
141 lines (127 loc) · 3.63 KB
/
dupfinder.js
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Поиск дубликатов для сайта Pastvu.com
// База для хранения хэшей картинок
/*
CREATE image_hash (
name VARCHAR(200) not null,
`hash` bigint(20) unsigned NOT NULL,
`b0` tinyint(3) unsigned NOT NULL,
`b1` tinyint(3) unsigned NOT NULL,
`b2` tinyint(3) unsigned NOT NULL,
`b3` tinyint(3) unsigned NOT NULL,
`b4` tinyint(3) unsigned NOT NULL,
`b5` tinyint(3) unsigned NOT NULL,
`b6` tinyint(3) unsigned NOT NULL,
`b7` tinyint(3) unsigned NOT NULL,
`b8` tinyint(3) unsigned NOT NULL,
`b9` tinyint(3) unsigned NOT NULL,
UNIQUE KEY `name` (`name`),
KEY `b0` (`b0`),
KEY `b1` (`b1`),
KEY `b2` (`b2`),
KEY `b3` (`b3`),
KEY `b4` (`b4`),
KEY `b5` (`b5`),
KEY `b6` (`b6`),
KEY `b7` (`b7`),
KEY `b8` (`b8`),
KEY `b9` (`b9`)
ENGINE=InnoDB
);
*/
const mysql = require('mysql')
const combinations = [
[0, 1], [0, 2], [0, 3], [0, 4], [0, 5], [0, 6], [0, 7], [0, 8], [0, 9],
[1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7], [1, 8], [1, 9],
[2, 3], [2, 4], [2, 5], [2, 6], [2, 7], [2, 8], [2, 9],
[3, 4], [3, 5], [3, 6], [3, 7], [3, 8], [3, 9],
[4, 5], [4, 6], [4, 7], [4, 8], [4, 9],
[5, 6], [5, 7], [5, 8], [5, 9],
[6, 7], [6, 8], [6, 9],
[7, 8], [7, 9], [8, 9]
]
class DupFinder {
/**
* @param {string} host - name of the mysql host
* @param {string} user - user of mysql database
* @param {string} password - password of the user
* @param {string} database - name of the database
*/
constructor(host, user, password, database)
{
this.db = mysql.createConnection({host, user, password, database})
this.db.connect();
}
/**
* @param {string} name - identifier of the image
* @param {string} hash - hash of the image
* @returns {Promise} - Promise object for await
*/
async add(name, hash)
{
const chunks = this.#splitHash(hash).map( (el,i) => `b${i}=0x${el}` ).join(", ")
// console.log(`INSERT INTO image_hash SET name=?, hash=0x${hash}, ${chunks}`, name)
await this.#query(`INSERT INTO image_hash SET name=?, hash=0x${hash}, ${chunks}`, name)
}
/**
* @param {string} hash - image hash calculated by dhash
* @param {number} [limit=0] - number of returned results
* @returns {Promise} - Promise object represents results of search
*/
async match(hash, limit=0)
{
const chunks = this.#splitHash(hash)
const where0 = combinations.map(([m,n]) => `(b${m}=${chunks[m]} AND b${n}=${chunks[n]})`).join(" OR ")
const where = `(${where0}) AND (BIT_COUNT(hash ^ 0x${hash}) <= 8)`
let q = `SELECT name, BIT_COUNT(hash ^ 0x${hash}) AS distance FROM image_hash WHERE ${where} ORDER BY distance`
if (limit)
{
q += ` LIMIT ${limit}`
}
console.log(q)
const results = await this.#query(q)
return results
}
#query(qstr, values)
{
return new Promise( (resolve, reject) =>
{
this.db.query(qstr, values, (error, results, fields) =>
{
if (error)
{
reject(error)
}
else
{
resolve(results)
}
})
})
}
#digits = {
"0": "0000",
"1": "0001",
"2": "0010",
"3": "0011",
"4": "0100",
"5": "0101",
"6": "0110",
"7": "0111",
"8": "1000",
"9": "1001",
"A": "1010",
"B": "1011",
"C": "1100",
"D": "1101",
"E": "1110",
"F": "1111",
}
#splitHash(hash)
{
const bin = hash.toUpperCase().split("").map(d => this.#digits[d]).join("")
const chunks = bin.match(/^(.{7})(.{7})(.{7})(.{7})(.{6})(.{6})(.{6})(.{6})(.{6})(.{6})/)
chunks.shift()
return chunks.map( chunk => parseInt(chunk,2) )
}
}
module.exports = DupFinder