optimize the circuit diagram loading function
This commit is contained in:
parent
148ab704df
commit
c4965bed32
|
|
@ -1,13 +1,9 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"modelRT/orm"
|
||||
)
|
||||
|
||||
type ModelParseConfig struct {
|
||||
CircuitDiagramID int64
|
||||
CircuitDiagramInfos []orm.CircuitDiagram
|
||||
CircuitDiagramMap *sync.Map
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ package database
|
|||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"modelRT/config"
|
||||
|
|
@ -15,7 +14,7 @@ import (
|
|||
)
|
||||
|
||||
// LoadCircuitDiagramFromPostgresDB return the result of query circuit diagram info from postgresDB
|
||||
func LoadCircuitDiagramFromPostgresDB(ctx context.Context, pool *ants.PoolWithFunc, circuitDiagramsMap *sync.Map, logger *zap.Logger) error {
|
||||
func LoadCircuitDiagramFromPostgresDB(ctx context.Context, pool *ants.PoolWithFunc, logger *zap.Logger) error {
|
||||
var circuitDiagramOverviews []orm.CircuitDiagramOverview
|
||||
// ctx超时判断
|
||||
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
|
|
@ -37,9 +36,7 @@ func LoadCircuitDiagramFromPostgresDB(ctx context.Context, pool *ants.PoolWithFu
|
|||
return result.Error
|
||||
}
|
||||
pool.Invoke(config.ModelParseConfig{
|
||||
CircuitDiagramID: overview.ID,
|
||||
CircuitDiagramInfos: circuitDiagramInfos,
|
||||
CircuitDiagramMap: circuitDiagramsMap,
|
||||
})
|
||||
}
|
||||
return nil
|
||||
|
|
|
|||
|
|
@ -0,0 +1,23 @@
|
|||
package diagram
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync"
|
||||
|
||||
cmap "github.com/orcaman/concurrent-map/v2"
|
||||
)
|
||||
|
||||
var DiagramsOverview sync.Map
|
||||
|
||||
func GetCircuitDiagramMap(key string) (*cmap.ConcurrentMap[string, any], error) {
|
||||
value, ok := DiagramsOverview.Load(key)
|
||||
if !ok {
|
||||
newMap := cmap.New[any]()
|
||||
return &newMap, nil
|
||||
}
|
||||
paramsMap, ok := value.(*cmap.ConcurrentMap[string, any])
|
||||
if !ok {
|
||||
return nil, errors.New("")
|
||||
}
|
||||
return paramsMap, nil
|
||||
}
|
||||
|
|
@ -7,6 +7,7 @@ require (
|
|||
github.com/gofrs/uuid v4.4.0+incompatible
|
||||
github.com/json-iterator/go v1.1.12
|
||||
github.com/natefinch/lumberjack v2.0.0+incompatible
|
||||
github.com/orcaman/concurrent-map/v2 v2.0.1
|
||||
github.com/panjf2000/ants/v2 v2.10.0
|
||||
github.com/spf13/viper v1.19.0
|
||||
go.uber.org/zap v1.27.0
|
||||
|
|
|
|||
|
|
@ -76,6 +76,8 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G
|
|||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||
github.com/natefinch/lumberjack v2.0.0+incompatible h1:4QJd3OLAMgj7ph+yZTuX13Ld4UpgHp07nNdFX7mqFfM=
|
||||
github.com/natefinch/lumberjack v2.0.0+incompatible/go.mod h1:Wi9p2TTF5DG5oU+6YfsmYQpsTIOm0B1VNzQg9Mw6nPk=
|
||||
github.com/orcaman/concurrent-map/v2 v2.0.1 h1:jOJ5Pg2w1oeB6PeDurIYf6k9PQ+aTITr/6lP/L/zp6c=
|
||||
github.com/orcaman/concurrent-map/v2 v2.0.1/go.mod h1:9Eq3TG2oBe5FirmYWQfYO5iH1q0Jv47PLaNK++uCdOM=
|
||||
github.com/panjf2000/ants/v2 v2.10.0 h1:zhRg1pQUtkyRiOFo2Sbqwjp0GfBNo9cUY2/Grpx1p+8=
|
||||
github.com/panjf2000/ants/v2 v2.10.0/go.mod h1:7ZxyxsqE4vvW0M7LSD8aI3cKwgFhBHbxnlN8mDqHa1I=
|
||||
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
|
||||
|
|
|
|||
|
|
@ -13,4 +13,8 @@ func ModelLoad(ctx *gin.Context) {
|
|||
ctx.Writer.Write([]byte("Hi Boy"))
|
||||
pgClient := database.GetPostgresDBInstance(ctx, "")
|
||||
fmt.Println(pgClient)
|
||||
// TODO
|
||||
// step1 查询电路具体信息表 circuit_diagram_xxx获取所有 uuid
|
||||
// step2 根据 uuid 获取所有的电路图map 结构
|
||||
// step3 json化相关数据并返回结果
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ package main
|
|||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"modelRT/config"
|
||||
|
|
@ -36,7 +35,6 @@ var (
|
|||
modelRTConfig config.ModelRTConfig
|
||||
postgresDBClient *gorm.DB
|
||||
logger *zap.Logger
|
||||
circuitDiagramMap sync.Map
|
||||
)
|
||||
|
||||
// TODO 使用 wire 依赖注入
|
||||
|
|
@ -68,7 +66,7 @@ func main() {
|
|||
defer ants.Release()
|
||||
|
||||
// load circuit diagram from postgres
|
||||
err = database.LoadCircuitDiagramFromPostgresDB(ctx, pool, &circuitDiagramMap, logger)
|
||||
err = database.LoadCircuitDiagramFromPostgresDB(ctx, pool, logger)
|
||||
if err != nil {
|
||||
logger.Error("load circuit diagrams from postgres failed", zap.Error(err))
|
||||
panic(err)
|
||||
|
|
|
|||
|
|
@ -1,7 +1,10 @@
|
|||
package model
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"modelRT/config"
|
||||
"modelRT/diagram"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
|
||||
|
|
@ -14,20 +17,27 @@ var ParseFunc = func(parseConfig interface{}) {
|
|||
modelParseConfig, ok := parseConfig.(config.ModelParseConfig)
|
||||
if !ok {
|
||||
logger.Error("conversion model parse config type failed")
|
||||
return
|
||||
panic(errors.New("conversion model parse config type failed"))
|
||||
}
|
||||
|
||||
for _, circuitDiagram := range modelParseConfig.CircuitDiagramInfos {
|
||||
parseStruct := SelectModelByType(circuitDiagram.Type)
|
||||
err := jsoniter.Unmarshal([]byte(circuitDiagram.OtherParams), parseStruct)
|
||||
overviewKey := circuitDiagram.UUID
|
||||
unmarshalMap, err := diagram.GetCircuitDiagramMap(overviewKey)
|
||||
if err != nil {
|
||||
logger.Error("get circuit diagram map from overviewMap failed", zap.String("circuit_diagram_overview_key", overviewKey), zap.Error(err))
|
||||
panic(err)
|
||||
}
|
||||
|
||||
err = jsoniter.Unmarshal([]byte(circuitDiagram.OtherParams), unmarshalMap)
|
||||
if err != nil {
|
||||
logger.Error("parsing circuit diagram params failed", zap.Int64("circuit_diagram_id", circuitDiagram.ID), zap.Error(err))
|
||||
panic(err)
|
||||
}
|
||||
// TODO 利用多叉树结构存储解析后的电路图
|
||||
unmarshalMap.Set("id", circuitDiagram.ID)
|
||||
unmarshalMap.Set("parent_id", circuitDiagram.ParentID)
|
||||
unmarshalMap.Set("type", circuitDiagram.Type)
|
||||
unmarshalMap.Set("created_time", circuitDiagram.CreatedTime)
|
||||
unmarshalMap.Set("updated_time", circuitDiagram.UpdateTime)
|
||||
}
|
||||
key := modelParseConfig.CircuitDiagramID
|
||||
// TODO 形成 key-多叉树结构
|
||||
modelParseConfig.CircuitDiagramMap.Store(key, nil)
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,14 +1,12 @@
|
|||
// Package orm define database data struct
|
||||
package orm
|
||||
|
||||
import "time"
|
||||
|
||||
type CircuitDiagram struct {
|
||||
ID int64 `gorm:""`
|
||||
ParentID int64
|
||||
Type int
|
||||
UUID string
|
||||
OtherParams string
|
||||
CreatedTime time.Time
|
||||
UpdateTime time.Time
|
||||
CreatedTime int64
|
||||
UpdateTime int64
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue