github.com/milvus-io/milvus-sdk-go/v2@v2.4.1/test/testcases/compact_test.go (about) 1 //go:build L0 2 3 package testcases 4 5 import ( 6 "testing" 7 "time" 8 9 "github.com/stretchr/testify/require" 10 11 "github.com/milvus-io/milvus-sdk-go/v2/entity" 12 "github.com/milvus-io/milvus-sdk-go/v2/test/common" 13 ) 14 15 func TestCompact(t *testing.T) { 16 ctx := createContext(t, time.Second*common.DefaultTimeout) 17 // connect 18 mc := createMilvusClient(ctx, t) 19 20 // create collection with 1 shard 21 cp := CollectionParams{CollectionFieldsType: AllFields, AutoID: false, EnableDynamicField: true, 22 ShardsNum: 1, Dim: common.DefaultDim} 23 collName := createCollection(ctx, t, mc, cp) 24 25 // insert 26 for i := 0; i < 4; i++ { 27 dp := DataParams{CollectionName: collName, PartitionName: "", CollectionFieldsType: AllFields, 28 start: i * common.DefaultNb, nb: common.DefaultNb, dim: common.DefaultDim, EnableDynamicField: true, WithRows: false} 29 _, _ = insertData(ctx, t, mc, dp) 30 mc.Flush(ctx, collName, false) 31 } 32 33 // get persisted segments 34 segments, _ := mc.GetPersistentSegmentInfo(ctx, collName) 35 require.Len(t, segments, 4) 36 segIds := make([]int64, 0, 4) 37 for _, seg := range segments { 38 require.Equal(t, seg.NumRows, int64(common.DefaultNb)) 39 segIds = append(segIds, seg.ID) 40 } 41 42 indexHnsw, _ := entity.NewIndexHNSW(entity.L2, 8, 96) 43 indexBinary, _ := entity.NewIndexBinIvfFlat(entity.JACCARD, 64) 44 for _, field := range common.AllFloatVectorsFieldNames { 45 err := mc.CreateIndex(ctx, collName, field, indexHnsw, false) 46 common.CheckErr(t, err, true) 47 } 48 err := mc.CreateIndex(ctx, collName, common.DefaultBinaryVecFieldName, indexBinary, false) 49 common.CheckErr(t, err, true) 50 51 // compact 52 compactionID, errCompact := mc.Compact(ctx, collName, 100) 53 common.CheckErr(t, errCompact, true) 54 55 // get compaction states 56 errWait := mc.WaitForCompactionCompleted(ctx, compactionID) 57 common.CheckErr(t, errWait, true) 58 59 // get compaction plan 60 compactionState2, compactionPlan, errPlan := mc.GetCompactionStateWithPlans(ctx, compactionID) 61 common.CheckErr(t, errPlan, true) 62 require.Equal(t, compactionState2, entity.CompactionStateCompleted) 63 var targetSeg int64 64 for _, plan := range compactionPlan { 65 if plan.PlanType == entity.CompactionPlanMergeSegments { 66 require.ElementsMatch(t, segIds, plan.Source) 67 targetSeg = plan.Target 68 } 69 } 70 71 // get persisted segments 72 segments2, _ := mc.GetPersistentSegmentInfo(ctx, collName) 73 var actualSeg []int64 74 actualRows := 0 75 for _, s := range segments2 { 76 actualSeg = append(actualSeg, s.ID) 77 actualRows = int(s.NumRows) + actualRows 78 } 79 require.Equal(t, common.DefaultNb*4, actualRows) 80 require.ElementsMatch(t, []int64{targetSeg}, actualSeg) 81 } 82 83 // test compaction collection not exist 84 func TestCompactCollectionNotExist(t *testing.T) { 85 ctx := createContext(t, time.Second*common.DefaultTimeout) 86 // connect 87 mc := createMilvusClient(ctx, t) 88 89 _, err := mc.Compact(ctx, "coll", 0) 90 common.CheckErr(t, err, false, "collection coll does not exist") 91 } 92 93 // test compact empty collection 94 func TestCompactEmptyCollection(t *testing.T) { 95 ctx := createContext(t, time.Second*common.DefaultTimeout) 96 // connect 97 mc := createMilvusClient(ctx, t) 98 99 // create collection with 1 shard 100 collName := createDefaultCollection(ctx, t, mc, true, 1) 101 102 // compact 103 compactionID, err := mc.Compact(ctx, collName, 0) 104 common.CheckErr(t, err, true) 105 106 mc.WaitForCompactionCompleted(ctx, compactionID) 107 compactionState, compactionPlans, errPlans := mc.GetCompactionStateWithPlans(ctx, compactionID) 108 common.CheckErr(t, errPlans, true) 109 require.Equal(t, compactionState, entity.CompactionStateCompleted) 110 require.Len(t, compactionPlans, 0) 111 }