2020-12-28 16:55:50 +08:00
|
|
|
package indexbuilder
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-12-29 16:31:03 +08:00
|
|
|
"math/rand"
|
2020-12-28 16:55:50 +08:00
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-12-29 16:31:03 +08:00
|
|
|
IvfPq = "IVF_PQ"
|
|
|
|
BinFlat = "BIN_FLAT"
|
|
|
|
dim = 8
|
|
|
|
nlist = 100
|
|
|
|
m = 4
|
|
|
|
nbits = 8
|
|
|
|
L2 = "L2"
|
|
|
|
Jaccard = "JACCARD"
|
|
|
|
nb = 8 * 10000
|
2020-12-28 16:55:50 +08:00
|
|
|
)
|
|
|
|
|
2020-12-29 16:31:03 +08:00
|
|
|
type testCase struct {
|
|
|
|
indexType string
|
|
|
|
metricType string
|
|
|
|
isBinary bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateFloatVectorTestCases() []testCase {
|
|
|
|
return []testCase{
|
|
|
|
{IvfPq, L2, false},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateBinaryVectorTestCases() []testCase {
|
|
|
|
return []testCase{
|
|
|
|
{BinFlat, Jaccard, true},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateTestCases() []testCase {
|
|
|
|
return append(generateFloatVectorTestCases(), generateBinaryVectorTestCases()...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateParams(indexType, metricType string) (map[string]string, map[string]string) {
|
2020-12-28 16:55:50 +08:00
|
|
|
typeParams := make(map[string]string)
|
|
|
|
indexParams := make(map[string]string)
|
|
|
|
indexParams["index_type"] = indexType
|
|
|
|
indexParams["metric_type"] = metricType
|
2020-12-29 16:31:03 +08:00
|
|
|
if indexType == IvfPq {
|
|
|
|
indexParams["dim"] = strconv.Itoa(dim)
|
|
|
|
indexParams["nlist"] = strconv.Itoa(nlist)
|
|
|
|
indexParams["m"] = strconv.Itoa(m)
|
|
|
|
indexParams["nbits"] = strconv.Itoa(nbits)
|
|
|
|
} else if indexType == BinFlat {
|
|
|
|
indexParams["dim"] = strconv.Itoa(dim)
|
|
|
|
}
|
|
|
|
|
|
|
|
return typeParams, indexParams
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateFloatVectors() []float32 {
|
|
|
|
vectors := make([]float32, 0)
|
|
|
|
for i := 0; i < nb; i++ {
|
|
|
|
vectors = append(vectors, rand.Float32())
|
|
|
|
}
|
|
|
|
return vectors
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateBinaryVectors() []byte {
|
|
|
|
vectors := make([]byte, 0)
|
|
|
|
for i := 0; i < nb/8; i++ {
|
|
|
|
vectors = append(vectors, byte(rand.Intn(8)))
|
|
|
|
}
|
|
|
|
return vectors
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCIndex_New(t *testing.T) {
|
|
|
|
for _, c := range generateTestCases() {
|
|
|
|
typeParams, indexParams := generateParams(c.indexType, c.metricType)
|
|
|
|
|
|
|
|
index, err := NewCIndex(typeParams, indexParams)
|
|
|
|
assert.Equal(t, err, nil)
|
|
|
|
assert.NotEqual(t, index, nil)
|
|
|
|
|
|
|
|
err = index.Delete()
|
|
|
|
assert.Equal(t, err, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCIndex_BuildFloatVecIndexWithoutIds(t *testing.T) {
|
|
|
|
for _, c := range generateFloatVectorTestCases() {
|
|
|
|
typeParams, indexParams := generateParams(c.indexType, c.metricType)
|
|
|
|
|
|
|
|
index, err := NewCIndex(typeParams, indexParams)
|
|
|
|
assert.Equal(t, err, nil)
|
|
|
|
assert.NotEqual(t, index, nil)
|
|
|
|
|
|
|
|
vectors := generateFloatVectors()
|
|
|
|
err = index.BuildFloatVecIndexWithoutIds(vectors)
|
|
|
|
assert.Equal(t, err, nil)
|
|
|
|
|
|
|
|
err = index.Delete()
|
|
|
|
assert.Equal(t, err, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCIndex_BuildBinaryVecIndexWithoutIds(t *testing.T) {
|
|
|
|
for _, c := range generateBinaryVectorTestCases() {
|
|
|
|
typeParams, indexParams := generateParams(c.indexType, c.metricType)
|
|
|
|
|
|
|
|
index, err := NewCIndex(typeParams, indexParams)
|
|
|
|
assert.Equal(t, err, nil)
|
|
|
|
assert.NotEqual(t, index, nil)
|
|
|
|
|
|
|
|
vectors := generateBinaryVectors()
|
|
|
|
err = index.BuildBinaryVecIndexWithoutIds(vectors)
|
|
|
|
assert.Equal(t, err, nil)
|
|
|
|
|
|
|
|
err = index.Delete()
|
|
|
|
assert.Equal(t, err, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCIndex_Codec(t *testing.T) {
|
|
|
|
for _, c := range generateTestCases() {
|
|
|
|
typeParams, indexParams := generateParams(c.indexType, c.metricType)
|
|
|
|
|
|
|
|
index, err := NewCIndex(typeParams, indexParams)
|
|
|
|
assert.Equal(t, err, nil)
|
|
|
|
assert.NotEqual(t, index, nil)
|
|
|
|
|
|
|
|
if !c.isBinary {
|
|
|
|
vectors := generateFloatVectors()
|
|
|
|
err = index.BuildFloatVecIndexWithoutIds(vectors)
|
|
|
|
assert.Equal(t, err, nil)
|
|
|
|
} else {
|
|
|
|
vectors := generateBinaryVectors()
|
|
|
|
err = index.BuildBinaryVecIndexWithoutIds(vectors)
|
|
|
|
assert.Equal(t, err, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
blobs, err := index.Serialize()
|
|
|
|
assert.Equal(t, err, nil)
|
|
|
|
|
|
|
|
copyIndex, err := NewCIndex(typeParams, indexParams)
|
|
|
|
err = copyIndex.Load(blobs)
|
|
|
|
assert.Equal(t, err, nil)
|
|
|
|
copyBlobs, err := copyIndex.Serialize()
|
|
|
|
assert.Equal(t, err, nil)
|
|
|
|
assert.Equal(t, len(blobs), len(copyBlobs))
|
|
|
|
// TODO: check key, value and more
|
|
|
|
|
|
|
|
err = index.Delete()
|
|
|
|
assert.Equal(t, err, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCIndex_Delete(t *testing.T) {
|
|
|
|
for _, c := range generateTestCases() {
|
|
|
|
typeParams, indexParams := generateParams(c.indexType, c.metricType)
|
2020-12-28 16:55:50 +08:00
|
|
|
|
2020-12-29 16:31:03 +08:00
|
|
|
index, err := NewCIndex(typeParams, indexParams)
|
|
|
|
assert.Equal(t, err, nil)
|
|
|
|
assert.NotEqual(t, index, nil)
|
2020-12-28 16:55:50 +08:00
|
|
|
|
2020-12-29 16:31:03 +08:00
|
|
|
err = index.Delete()
|
|
|
|
assert.Equal(t, err, nil)
|
|
|
|
}
|
2020-12-28 16:55:50 +08:00
|
|
|
}
|