-
Notifications
You must be signed in to change notification settings - Fork 25
/
simstore.go
295 lines (224 loc) · 6.21 KB
/
simstore.go
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Package simstore implements a storage layer for simhash locality-sensitive hashes.
/*
This package is an implementation of section 3 of "Detecting Near-Duplicates
for Web Crawling" by Manku, Jain, and Sarma,
http://www2007.org/papers/paper215.pdf
It is hard-coded for hamming distance 3 or 6.
*/
package simstore
import (
"runtime"
"sort"
"sync"
"github.com/dgryski/go-bits"
)
type entry struct {
hash uint64
docid uint64
}
type table []entry
func (t table) Len() int { return len(t) }
func (t table) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
func (t table) Less(i, j int) bool { return t[i].hash < t[j].hash }
const mask3 = 0xfffffff000000000
func (t table) find(sig uint64) []uint64 {
i := sort.Search(len(t), func(i int) bool { return t[i].hash >= sig })
var ids []uint64
for i < len(t) && t[i].hash == sig {
ids = append(ids, t[i].docid)
i++
}
return ids
}
func NewU64Slice(hashes int) u64store {
u := make(u64slice, 0, hashes)
return &u
}
type u64store interface {
add(hash uint64)
find(sig uint64, mask uint64, d int) []uint64
finish()
}
// a store for uint64s
type u64slice []uint64
func (u u64slice) Len() int { return len(u) }
func (u u64slice) Less(i int, j int) bool { return u[i] < u[j] }
func (u u64slice) Swap(i int, j int) { u[i], u[j] = u[j], u[i] }
func (u u64slice) find(sig, mask uint64, d int) []uint64 {
prefix := sig & mask
i := sort.Search(len(u), func(i int) bool { return u[i] >= prefix })
var ids []uint64
for i < len(u) && u[i]&mask == prefix {
if distance(u[i], sig) <= d {
ids = append(ids, u[i])
}
i++
}
return ids
}
func (u *u64slice) add(p uint64) {
*u = append(*u, p)
}
func (u u64slice) finish() {
sort.Sort(u)
}
// Store is a storage engine for 64-bit hashes
type Store struct {
docids table
rhashes []u64store
}
// New3 returns a Store for searching hamming distance <= 3
func New3(hashes int, newStore func(int) u64store) *Store {
s := Store{}
s.rhashes = make([]u64store, 16)
if hashes != 0 {
s.docids = make(table, 0, hashes)
for i := range s.rhashes {
s.rhashes[i] = newStore(hashes)
}
}
return &s
}
// Add inserts a signature and document id into the store
func (s *Store) Add(sig uint64, docid uint64) {
var t int
s.docids = append(s.docids, entry{hash: sig, docid: docid})
for i := 0; i < 4; i++ {
p := sig
s.rhashes[t].add(p)
t++
p = (sig & 0xffff000000ffffff) | (sig & 0x0000fff000000000 >> 12) | (sig & 0x0000000fff000000 << 12)
s.rhashes[t].add(p)
t++
p = (sig & 0xffff000fff000fff) | (sig & 0x0000fff000000000 >> 24) | (sig & 0x0000000000fff000 << 24)
s.rhashes[t].add(p)
t++
p = (sig & 0xffff000ffffff000) | (sig & 0x0000fff000000000 >> 36) | (sig & 0x0000000000000fff << 36)
s.rhashes[t].add(p)
t++
sig = (sig << 16) | (sig >> (64 - 16))
}
}
func (*Store) unshuffle(sig uint64, t int) uint64 {
const m2 = 0x0000fff000000000
t4 := t % 4
shift := 12 * uint64(t4)
m3 := uint64(m2 >> shift)
m1 := ^uint64(0) &^ (m2 | m3)
sig = (sig & m1) | (sig & m2 >> shift) | (sig & m3 << shift)
sig = (sig >> (16 * (uint64(t) / 4))) | (sig << (64 - (16 * (uint64(t) / 4))))
return sig
}
func (s *Store) unshuffleList(sigs []uint64, t int) []uint64 {
for i := range sigs {
sigs[i] = s.unshuffle(sigs[i], t)
}
return sigs
}
type limiter chan struct{}
func (l limiter) enter() { l <- struct{}{} }
func (l limiter) leave() { <-l }
// Finish prepares the store for searching. This must be called once after all
// the signatures have been added via Add().
func (s *Store) Finish() {
// empty store
if len(s.docids) == 0 {
return
}
l := make(limiter, runtime.GOMAXPROCS(0))
var wg sync.WaitGroup
sort.Sort(s.docids)
for i := range s.rhashes {
l.enter()
wg.Add(1)
go func(i int) {
s.rhashes[i].finish()
l.leave()
wg.Done()
}(i)
}
wg.Wait()
}
// Find searches the store for all hashes hamming distance 3 or less from the
// query signature. It returns the associated list of document ids.
func (s *Store) Find(sig uint64) []uint64 {
// empty store
if len(s.docids) == 0 {
return nil
}
var ids []uint64
// TODO(dgryski): search in parallel
var t int
for i := 0; i < 4; i++ {
p := sig
ids = append(ids, s.unshuffleList(s.rhashes[t].find(p, mask3, 3), t)...)
t++
p = (sig & 0xffff000000ffffff) | (sig & 0x0000fff000000000 >> 12) | (sig & 0x0000000fff000000 << 12)
ids = append(ids, s.unshuffleList(s.rhashes[t].find(p, mask3, 3), t)...)
t++
p = (sig & 0xffff000fff000fff) | (sig & 0x0000fff000000000 >> 24) | (sig & 0x0000000000fff000 << 24)
ids = append(ids, s.unshuffleList(s.rhashes[t].find(p, mask3, 3), t)...)
t++
p = (sig & 0xffff000ffffff000) | (sig & 0x0000fff000000000 >> 36) | (sig & 0x0000000000000fff << 36)
ids = append(ids, s.unshuffleList(s.rhashes[t].find(p, mask3, 3), t)...)
t++
sig = (sig << 16) | (sig >> (64 - 16))
}
ids = unique(ids)
var docids []uint64
for _, v := range ids {
docids = append(docids, s.docids.find(v)...)
}
return docids
}
// SmallStore3 is a simstore for distance k=3 with smaller memory requirements
type SmallStore3 struct {
tables [4][1 << 16]table
}
func New3Small(hashes int) *SmallStore3 {
return &SmallStore3{}
}
func (s *SmallStore3) Add(sig uint64, docid uint64) {
for i := 0; i < 4; i++ {
prefix := (sig & 0xffff000000000000) >> (64 - 16)
s.tables[i][prefix] = append(s.tables[i][prefix], entry{hash: sig, docid: docid})
sig = (sig << 16) | (sig >> (64 - 16))
}
}
func (s *SmallStore3) Find(sig uint64) []uint64 {
var ids []uint64
for i := 0; i < 4; i++ {
prefix := (sig & 0xffff000000000000) >> (64 - 16)
t := s.tables[i][prefix]
for i := range t {
if distance(t[i].hash, sig) <= 3 {
ids = append(ids, t[i].docid)
}
}
sig = (sig << 16) | (sig >> (64 - 16))
}
return unique(ids)
}
func (s *SmallStore3) Finish() {
for i := range s.tables {
for p := range s.tables[i] {
sort.Sort(s.tables[i][p])
}
}
}
func unique(ids []uint64) []uint64 {
// dedup ids
uniq := make(map[uint64]struct{})
for _, id := range ids {
uniq[id] = struct{}{}
}
ids = ids[:0]
for k := range uniq {
ids = append(ids, k)
}
return ids
}
// distance returns the hamming distance between v1 and v2
func distance(v1 uint64, v2 uint64) int {
return int(bits.Popcnt(v1 ^ v2))
}