Repository: milvus-io/milvus
File: internal/datacoord/util.go (Go)

Code before:
			indexed := extractSegmentsWithVectorIndex(vecFieldID, resp.GetSegmentInfo())
			if len(indexed) > 0 {
				indexedSegmentCh <- indexed
			}
			return nil
		})
	}
	err := group.Wait()
	if err != nil {
		return nil, err
	}

	close(indexedSegmentCh)

	indexedSegments := make([]*SegmentInfo, 0)
	for segments := range indexedSegmentCh {
		for _, segment := range segments {
			if info, ok := segmentMap[segment]; ok {
				delete(segmentMap, segment)
				indexedSegments = append(indexedSegments, info)
			}
		}
	}

	return indexedSegments, nil
}

func extractSegmentsWithVectorIndex(vecFieldID map[int64]int64, segentIndexInfo map[int64]*indexpb.SegmentInfo) []int64 {
	indexedSegments := make(typeutil.UniqueSet)
	for _, indexInfo := range segentIndexInfo {
		for _, index := range indexInfo.GetIndexInfos() {
			if index.GetFieldID() == vecFieldID[indexInfo.GetCollectionID()] {
				indexedSegments.Insert(indexInfo.GetSegmentID())
				break
			}
		}
	}
	return indexedSegments.Collect()
}

func getZeroTime() time.Time {
	var t time.Time
	return t
}

// getCollectionTTL returns ttl if collection's ttl is specified, or return global ttl
func getCollectionTTL(properties map[string]string) (time.Duration, error) {
	v, ok := properties[common.CollectionTTLConfigKey]
	if ok {
		ttl, err := strconv.Atoi(v)
		if err != nil {

Review comment (suggestion):
follow the new error handling way:
```
err := merr.CheckRPCCall(resp, err)
if err != nil {
// ...
}
```

Code after:
			if info, ok := segmentMap[segment]; ok {
				delete(segmentMap, segment)
				indexedSegments = append(indexedSegments, info)
			}
		}
	}

	return indexedSegments, nil
}

func getIndexInfos(indexCoord types.IndexCoord, collectionID int64, segmentIDs []int64) (map[int64]*indexpb.SegmentInfo, error) {
	ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) // TODO: make it configurable
	defer cancel()
	resp, err := indexCoord.GetIndexInfos(ctx, &indexpb.GetIndexInfoRequest{
		CollectionID: collectionID,
		SegmentIDs:   segmentIDs,
	})
	if err != nil {
		log.Warn("failed to get index of collection",
			zap.Int64("collection", collectionID),
			zap.Int64s("segments", segmentIDs),
			zap.Error(err),
		)
		return nil, err
	}
	if resp.GetStatus().GetErrorCode() != commonpb.ErrorCode_Success {
		log.Warn("indexcoord return error when get index",
			zap.Int64("collection", collectionID),
			zap.Int64s("segments", segmentIDs),
			zap.String("status", resp.GetStatus().String()),
			zap.String("reason", resp.GetStatus().GetReason()),
		)
		return nil, fmt.Errorf("GetIndex failed for segments, status: %s, reason: %s",
			resp.GetStatus().GetErrorCode().String(), resp.GetStatus().GetReason())
	}
	return resp.GetSegmentInfo(), nil
}

func extractSegmentsWithVectorIndex(vecFieldID map[int64]int64, segentIndexInfo map[int64]*indexpb.SegmentInfo) []int64 {
	indexedSegments := make(typeutil.UniqueSet)
	for _, indexInfo := range segentIndexInfo {
		for _, index := range indexInfo.GetIndexInfos() {
			if index.GetFieldID() == vecFieldID[indexInfo.GetCollectionID()] {
				indexedSegments.Insert(indexInfo.GetSegmentID())
				break
			}
		}
	}
	return indexedSegments.Collect()
}