Skip to content

Commit 1206d17

Browse files
fix: opt for FindIntersection
1 parent 8c196ef commit 1206d17

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

sqle/api/controller/v1/sql_audit_record.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ func GetSQLAuditRecordsV1(c echo.Context) error {
704704
if !canViewProject && viewQuickAuditRecordPermission != nil {
705705
rangeUids := viewQuickAuditRecordPermission.RangeUids
706706
if req.FilterInstanceId != 0 {
707-
rangeUids = utils.FindIntersection(rangeUids, strconv.FormatUint(req.FilterInstanceId, 10))
707+
rangeUids = utils.FindIntersection(rangeUids, []string{strconv.FormatUint(req.FilterInstanceId, 10)})
708708
}
709709
data["filter_instance_ids"] = fmt.Sprintf("\"%s\"", strings.Join(rangeUids, "\",\""))
710710
data["check_user_can_access"] = false

sqle/utils/util.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -490,14 +490,23 @@ func GenerateSSHKeyPair() (privateKeyStr, publicKeyStr string, err error) {
490490
return string(privatePEM), publicKeyStr, nil
491491
}
492492

493-
func FindIntersection(elements []string, target string) []string {
494-
elementMap := make(map[string]bool)
495-
for _, elem := range elements {
496-
elementMap[elem] = true
493+
func FindIntersection(slice1, slice2 []string) []string {
494+
map1 := make(map[string]bool)
495+
map2 := make(map[string]bool)
496+
497+
// 填充第一个 map
498+
for _, item := range slice1 {
499+
map1[item] = true
497500
}
498-
// 检查 target 是否存在于 map 中
499-
if elementMap[target] {
500-
return []string{target}
501+
502+
// 填充第二个 map 并查找交集
503+
var intersection []string
504+
for _, item := range slice2 {
505+
if map1[item] && !map2[item] {
506+
intersection = append(intersection, item)
507+
}
508+
map2[item] = true
501509
}
502-
return []string{}
510+
511+
return intersection
503512
}

0 commit comments

Comments
 (0)