【修改地图数据缓存结构】【数据版本处理】

This commit is contained in:
weizhihong 2023-12-15 17:02:42 +08:00
parent bd9542fd8d
commit e0085c4513
6 changed files with 74 additions and 57 deletions

View File

@ -62,7 +62,7 @@ func (ms *IbpMs) collectStationIbpState(station *graphicData.Station) (*state.Pu
}
sid := memory.GetMapElementId(station.Common)
stationUid := memory.QueryUidByMidAndComId(ms.mapId, sid, &graphicData.Station{})
ibpMapId, ibpStorage := memory.GetStorageIBPMapData(station.RefIbpMapCode)
ibpMapId, ibpStorage := memory.QueryGiDataByName[*graphicData.IBPGraphicStorage](station.RefIbpMapCode)
ibpUidsMap := memory.QueryUidStructure[*memory.IBPUidStructure](ibpMapId)
buttonStates, err := ms.collectIBPButtonState(stationUid, ibpUidsMap, ibpStorage.IbpButtons)
if err != nil {

View File

@ -72,8 +72,7 @@ func (p *PslMs) collectGateBoxPSLState(box *graphicData.GatedBox) (*state.Pushed
mkx := component.MkxType.Get(mkxEntry)
var buttonStateArr []*state.ButtonState
if ok {
pslMapId := memory.QueryGiId(box.RefGatedBoxMapCode)
pslStorage := memory.QueryGiData[*graphicData.PslGraphicStorage](pslMapId)
_, pslStorage := memory.QueryGiDataByName[*graphicData.PslGraphicStorage](box.RefGatedBoxMapCode)
btnUidMap := make(map[string]uint32, len(pslStorage.PslButtons))
for _, button := range pslStorage.PslButtons {
btnUidMap[boxUid+"_"+button.Code] = memory.GetMapElementId(button.Common)

View File

@ -62,7 +62,7 @@ func ListQueryPublished(req *dto.PublishedListReqDto) []*dto.PublishedDto {
// 项目启动时查询发布地图信息
func ListAllPublished() []*dto.PublishedDto {
p, pv := dbquery.Published, dbquery.PublishedVersion
where := p.Select(p.ID, p.Code.As("name"), p.Type, p.Category, pv.Proto).
where := p.Select(p.ID, p.Code.As("name"), p.Type, p.Category, pv.Proto, pv.Version).
LeftJoin(pv, p.DataID.EqCol(pv.ID)).
Where(p.Status.Eq(1), p.Type.Neq(trainDataType)).Order(pv.PublishAt)
var records []*dto.PublishedDto
@ -76,7 +76,7 @@ func ListAllPublished() []*dto.PublishedDto {
// 查询详细信息
func GetPublishedById(id int32) *dto.PublishedDto {
p, pv := dbquery.Published, dbquery.PublishedVersion
where := p.Select(p.ID, p.Code.As("name"), p.Type, p.Category, pv.Proto).
where := p.Select(p.ID, p.Code.As("name"), p.Type, p.Category, pv.Proto, pv.Version).
LeftJoin(pv, p.DataID.EqCol(pv.ID)).
Where(p.ID.Eq(id), p.Status.Eq(1))
var record dto.PublishedDto

View File

@ -54,5 +54,6 @@ func getIbpUidByMapIdAndStationId(mapId int32, stationId uint32) (*IBPUidStructu
if station.RefIbpMapCode == "" {
return nil, fmt.Errorf("车站【%s】未关联IBP地图", station.StationName)
}
return QueryUidStructure[*IBPUidStructure](QueryGiId(station.RefIbpMapCode)), nil
ibpMapId, _ := QueryGiDataByName[*graphicData.IBPGraphicStorage](station.RefIbpMapCode)
return QueryUidStructure[*IBPUidStructure](ibpMapId), nil
}

View File

@ -18,16 +18,17 @@ import (
"joylink.club/bj-rtsts-server/ts/protos/graphicData"
)
var (
giTypeMap sync.Map
giNameMap sync.Map
giDataMap sync.Map
)
var giPublishData sync.Map
type giStoreData struct {
name string
giType graphicData.PictureType
message proto.Message
version int32
}
// 将发布的地图数据放入内存中
func PublishMapVerifyStructure(graphic *dto.PublishedDto) {
giTypeMap.Store(graphic.ID, graphicData.PictureType(graphic.Type))
giNameMap.Store(graphic.Name, graphic.ID)
var message proto.Message
switch graphicData.PictureType(graphic.Type) {
case graphicData.PictureType_StationLayout:
@ -43,13 +44,18 @@ func PublishMapVerifyStructure(graphic *dto.PublishedDto) {
if err != nil {
panic(&dto.ErrorDto{Code: dto.LogicError, Message: fmt.Sprintf("[id:%d]proto数据反序列化失败:%s", graphic.ID, err)})
}
giDataMap.Store(graphic.ID, message)
// 缓存数据
giPublishData.Store(graphic.ID, &giStoreData{
name: graphic.Name,
giType: graphicData.PictureType(graphic.Type),
message: message,
version: graphic.Version,
})
// 初始化地图结构
switch graphicData.PictureType(graphic.Type) {
case graphicData.PictureType_StationLayout:
graphicStorage := message.(*graphicData.RtssGraphicStorage)
// 处理掉其他线路的设备
filterOtherLineDevice(graphicStorage)
filterOtherLineDevice(graphicStorage) // 处理掉其他线路的设备
giUidMap.Store(graphic.ID, initStationUid(graphicStorage))
case graphicData.PictureType_RelayCabinetLayout:
graphicStorage := message.(*graphicData.RelayCabinetGraphicStorage)
@ -71,57 +77,67 @@ func GenerateElementUid(city, lineId string, stationIndexList []string, code str
// 移除内存中的地图信息
func DeleteMapVerifyStructure(mapId int32) {
giTypeMap.Delete(mapId)
giDataMap.Delete(mapId)
giPublishData.Delete(mapId)
giUidMap.Delete(mapId)
var name string
giNameMap.Range(func(key, value any) bool {
id := value.(int32)
if id == mapId {
name = key.(string)
return true
}
return false
})
if name != "" {
giNameMap.Delete(name)
}
}
// 获取地图类型
func QueryGiType(mapId int32) graphicData.PictureType {
value, ok := giTypeMap.Load(mapId)
value, ok := giPublishData.Load(mapId)
if !ok {
graphic := service.GetPublishedById(mapId)
PublishMapVerifyStructure(graphic)
return graphicData.PictureType(graphic.Type)
}
return value.(graphicData.PictureType)
d, ok := value.(*giStoreData)
if !ok {
panic(sys_error.New(fmt.Sprintf("地图【%d】缓存数据结构错误", mapId)))
}
return d.giType
}
// 获取地图版本
func QueryGiVersion(mapId int32) int32 {
value, ok := giPublishData.Load(mapId)
if !ok {
panic(sys_error.New(fmt.Sprintf("地图【%d】缓存数据丢失", mapId)))
}
d, ok := value.(*giStoreData)
if !ok {
panic(sys_error.New(fmt.Sprintf("地图【%d】缓存数据结构错误", mapId)))
}
return d.version
}
// 根据地图ID获取地图数据
func QueryGiData[T proto.Message](mapId int32) T {
value, _ := giDataMap.Load(mapId)
return value.(T)
value, ok := giPublishData.Load(mapId)
if !ok {
panic(sys_error.New(fmt.Sprintf("地图【%d】缓存数据丢失", mapId)))
}
d, ok := value.(*giStoreData)
if !ok {
panic(sys_error.New(fmt.Sprintf("地图【%d】缓存数据结构错误", mapId)))
}
return d.message.(T)
}
func QueryGiId(name string) int32 {
value, _ := giNameMap.Load(name)
return value.(int32)
}
func GetStorageIBPMapData(mapCode string) (int32, *graphicData.IBPGraphicStorage) {
// 处理关联的IBP盘信息
if mapCode == "" {
return 0, nil
}
ibpId, ok := giNameMap.Load(mapCode)
if !ok {
return 0, nil
}
ibpMapData, ok := giDataMap.Load(ibpId)
if !ok {
return ibpId.(int32), nil
}
return ibpId.(int32), ibpMapData.(*graphicData.IBPGraphicStorage)
// 根据地图名称获取地图数据
func QueryGiDataByName[T proto.Message](name string) (int32, T) {
var (
id int32
giData T
)
giPublishData.Range(func(key, value any) bool {
d, _ := value.(*giStoreData)
if d.name == name {
id = key.(int32)
giData = d.message.(T)
return false
}
return true
})
return id, giData
}
// 转换成统一坐标公里标

View File

@ -515,11 +515,13 @@ func (s *VerifySimulation) EvnWorld() ecs.World {
func (s *VerifySimulation) initRepository() error {
// 构建Repository
var mapIdStrSlice []string
var mapVersion []string
for _, id := range s.MapIds {
mapIdStrSlice = append(mapIdStrSlice, strconv.Itoa(int(id)))
mapVersion = append(mapVersion, strconv.Itoa(int(QueryGiVersion(id))))
}
repoId := strings.Join(mapIdStrSlice, "|")
repoVersion := "0.1"
repoVersion := strings.Join(mapVersion, ".")
repo := repository.FindRepository(repoId, repoVersion)
if repo == nil {
protoRepo, err := buildProtoRepository(s.MapIds)
@ -1150,8 +1152,7 @@ func fillProtoRepository(repo *proto.Repository, storage *graphicData.RtssGraphi
PsdId: uidsMap.PsdIds[data.RefScreenDoor].Uid,
}
repo.Mkxs = append(repo.Mkxs, mkx)
pslMapId := QueryGiId(data.RefGatedBoxMapCode)
pslStorage := QueryGiData[*graphicData.PslGraphicStorage](pslMapId)
_, pslStorage := QueryGiDataByName[*graphicData.PslGraphicStorage](data.RefGatedBoxMapCode)
for _, button := range pslStorage.PslButtons {
repoButton := &proto.Button{
Id: boxUidInfo.Uid + "_" + button.Code,
@ -1205,7 +1206,7 @@ func fillProtoRepository(repo *proto.Repository, storage *graphicData.RtssGraphi
// 将IBP的设备关联到车站中
func handlerIBPDeviceToStation(station *proto.Station, repo *proto.Repository, ibpMapCode string) {
mapId, storage := GetStorageIBPMapData(ibpMapCode)
mapId, storage := QueryGiDataByName[*graphicData.IBPGraphicStorage](ibpMapCode)
if storage == nil {
return
}