-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlsh_test.go
107 lines (88 loc) · 2.35 KB
/
lsh_test.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
package lsh
import (
"context"
"fmt"
"slices"
"testing"
"github.com/agtabesh/lsh/interfaces"
"github.com/agtabesh/lsh/types"
"github.com/stretchr/testify/assert"
)
func TestAdd(t *testing.T) {
ctx := context.Background()
lsh, hashFamily, _, _, err := getInstance()
assert.Nil(t, err)
vectors := getSampleVectors()
for i, vector := range vectors {
vectorID := types.VectorID(fmt.Sprint(i))
signature := hashFamily.MinHash(vector)
err := lsh.Add(ctx, vectorID, vector)
assert.Nil(t, err)
storedSignature, err := lsh.store.GetSignatureByVectorID(ctx, vectorID)
assert.Nil(t, err)
assert.True(t, slices.Equal(signature, storedSignature))
}
}
func TestQueryByVectorID(t *testing.T) {
ctx := context.Background()
lsh, _, _, _, err := getInstance()
assert.Nil(t, err)
vectors := getSampleVectors()
for i, vector := range vectors {
vectorID := types.VectorID(fmt.Sprint(i))
err := lsh.Add(ctx, vectorID, vector)
assert.Nil(t, err)
}
for i := range vectors {
vectorID := types.VectorID(fmt.Sprint(i))
vectorsID, err := lsh.QueryByVectorID(ctx, vectorID, 100)
assert.Nil(t, err)
assert.Contains(t, vectorsID, vectorID)
}
}
func TestQueryByVector(t *testing.T) {
ctx := context.Background()
lsh, _, _, _, err := getInstance()
assert.Nil(t, err)
vectors := getSampleVectors()
for i, vector := range vectors {
vectorID := types.VectorID(fmt.Sprint(i))
err := lsh.Add(ctx, vectorID, vector)
assert.Nil(t, err)
}
for i, vector := range vectors {
vectorID := types.VectorID(fmt.Sprint(i))
vectorsID, err := lsh.QueryByVector(ctx, vector, 100)
assert.Nil(t, err)
assert.Contains(t, vectorsID, vectorID)
}
}
func getConfig() LSHConfig {
return LSHConfig{
SignatureSize: 128,
}
}
func getInstance() (
*LSH,
interfaces.HashFamily,
interfaces.SimilarityMeasure,
interfaces.Store,
error,
) {
config := getConfig()
hashFamily := NewXXHASH64HashFamily(config.SignatureSize)
similarityMeasure := NewHammingSimilarity()
store := NewInMemoryStore()
lsh, err := NewLSH(config, hashFamily, similarityMeasure, store)
if err != nil {
return nil, nil, nil, nil, err
}
return lsh, hashFamily, similarityMeasure, store, nil
}
func getSampleVectors() []types.Vector {
return []types.Vector{
{"feat1": 1, "feat2": 1, "feat3": 1},
{"feat1": 1, "feat4": 1, "feat5": 1},
{"feat1": 1, "feat2": 1, "feat6": 1, "feat7": 1},
}
}