optimize ParseIndexes func.

This commit is contained in:
daheige 2021-06-14 11:23:25 +08:00
parent 9653eaf0fe
commit bdf3eea560

View File

@ -27,33 +27,29 @@ type IndexOption struct {
// ParseIndexes parse schema indexes // ParseIndexes parse schema indexes
func (schema *Schema) ParseIndexes() map[string]Index { func (schema *Schema) ParseIndexes() map[string]Index {
var indexes = map[string]Index{} indexes := make(map[string]Index, len(schema.Fields)+1)
for _, field := range schema.Fields { for _, field := range schema.Fields {
if field.TagSettings["INDEX"] != "" || field.TagSettings["UNIQUEINDEX"] != "" { if field.TagSettings["INDEX"] != "" || field.TagSettings["UNIQUEINDEX"] != "" {
for _, index := range parseFieldIndexes(field) { for _, index := range parseFieldIndexes(field) {
idx := indexes[index.Name] idx, ok := indexes[index.Name]
idx.Name = index.Name if !ok {
if idx.Class == "" { indexes[index.Name] = Index{
idx.Class = index.Class Name: index.Name,
} Class: index.Class,
if idx.Type == "" { Type: index.Type,
idx.Type = index.Type Where: index.Where,
} Comment: index.Comment,
if idx.Where == "" { Option: index.Option,
idx.Where = index.Where Fields: make([]IndexOption, 0, 10), // Fields cap default is 10
} }
if idx.Comment == "" {
idx.Comment = index.Comment
}
if idx.Option == "" {
idx.Option = index.Option
} }
idx.Fields = append(idx.Fields, index.Fields...) idx.Fields = append(idx.Fields, index.Fields...)
sort.Slice(idx.Fields, func(i, j int) bool { if len(idx.Fields) >= 2 {
return idx.Fields[i].priority < idx.Fields[j].priority sort.Slice(idx.Fields, func(i, j int) bool {
}) return idx.Fields[i].priority < idx.Fields[j].priority
})
}
indexes[index.Name] = idx indexes[index.Name] = idx
} }