blob: 93f19bdaa8886435fec328d91e3132023e93dca4 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package datasource
import (
pb "github.com/go-chassis/cari/discovery"
)
// @titile SchemasAnalysis
// @description schemasanlysis decide the schema to be deleted,updated or added
// @param schemas []*pb.Schema "the schemas from request"
// @param schemasFromDb []*pb.Schema "the schemas from database by the serviceID"
// @param schemaIDsInService []string "the schema field in service struct"
func SchemasAnalysis(schemas []*pb.Schema, schemasFromDb []*pb.Schema, schemaIDsInService []string) (
[]*pb.Schema, []*pb.Schema, []*pb.Schema, []string) {
needUpdateSchemas := make([]*pb.Schema, 0, len(schemas))
needAddSchemas := make([]*pb.Schema, 0, len(schemas))
needDeleteSchemas := make([]*pb.Schema, 0, len(schemasFromDb))
nonExistSchemaIds := make([]string, 0, len(schemas))
duplicate := make(map[string]struct{})
for _, schema := range schemas {
if _, ok := duplicate[schema.SchemaId]; ok {
continue
}
duplicate[schema.SchemaId] = struct{}{}
exist := false
for _, schemaFromDb := range schemasFromDb {
if schema.SchemaId == schemaFromDb.SchemaId {
needUpdateSchemas = append(needUpdateSchemas, schema)
exist = true
break
}
}
if !exist {
needAddSchemas = append(needAddSchemas, schema)
}
exist = false
for _, schemaID := range schemaIDsInService {
if schema.SchemaId == schemaID {
exist = true
}
}
if !exist {
nonExistSchemaIds = append(nonExistSchemaIds, schema.SchemaId)
}
}
for _, schemaFromDb := range schemasFromDb {
exist := false
for _, schema := range schemas {
if schema.SchemaId == schemaFromDb.SchemaId {
exist = true
break
}
}
if !exist {
needDeleteSchemas = append(needDeleteSchemas, schemaFromDb)
}
}
return needUpdateSchemas, needAddSchemas, needDeleteSchemas, nonExistSchemaIds
}