城轨repo接口定义及管理功能实现

This commit is contained in:
walker 2023-12-28 14:35:11 +08:00
parent 2bff91955a
commit 375d0a86a3
4 changed files with 57 additions and 31 deletions

View File

@ -1,33 +1,57 @@
package repo
// type repoManager struct {
// repoMap map[string]CgRepo
// lock sync.Mutex
// }
import "sync"
// var defaultManager = &repoManager{
// repoMap: make(map[string]CgRepo),
// }
// Repo管理
type repoManager struct {
repoMap map[string]CgRepo
lock sync.Mutex
}
// // 获取或构建模型仓库
// func GetOrBuildRepo(id string, dc func(errRecord *ErrorRecord) *dto.CgRepo) (CgRepo, *ErrorRecord) {
// manager := defaultManager
// manager.lock.Lock()
// defer manager.lock.Unlock()
// r, ok := manager.repoMap[id]
// errRecord := NewErrorRecord()
// if !ok {
// // 所需protobuf数据转换
// msgs := dc(errRecord)
// // 数据转换出错直接返回
// if errRecord.HasError() {
// return nil, errRecord
// }
// // 构建模型Repo
// r, errRecord = BuildFrom(msgs)
// if r != nil {
// manager.repoMap[id] = r
// }
// }
// return r, errRecord
// }
var defaultManager = &repoManager{
repoMap: make(map[string]CgRepo),
}
// 获取Repo
func GetRepo(id string) CgRepo {
manager := defaultManager
manager.lock.Lock()
defer manager.lock.Unlock()
r, ok := manager.repoMap[id]
if !ok {
return nil
}
return r
}
// 保存Repo
func SaveRepo(r CgRepo) {
manager := defaultManager
manager.lock.Lock()
defer manager.lock.Unlock()
manager.repoMap[r.Id()] = r
}
// 移除Repo
func RemoveRepo(id string) {
manager := defaultManager
manager.lock.Lock()
defer manager.lock.Unlock()
delete(manager.repoMap, id)
}
// 获取或构建Repo,若存在则返回已存在的,若不存在则构建保存并返回
func GetOrBuildRepo(id string, builder func() (CgRepo, error)) (CgRepo, error) {
r := GetRepo(id)
if r == nil {
// 构建模型Repo
r, err := builder()
// 构建出错直接返回
if err != nil {
return nil, err
}
// 保存
SaveRepo(r)
}
return r, nil
}

View File

@ -9,7 +9,7 @@ type Station interface {
IsEcs() bool
}
// 设备集中站
// 设备集中站(Equipment centralized station)
type Ecs interface {
Station
// 获取所有道岔

View File

@ -5,6 +5,8 @@ import "joylink.club/rtsssimulation/cgrepo/model"
type CgRepo interface {
// 模型仓库id
Id() string
// 获取所有设备集中站
Ecses() []model.Ecs
// 获取所有道岔
Turnouts() []model.Turnout
// 通过uid查询模型对象

@ -1 +1 @@
Subproject commit bd947baa4edadb6b7bd059f6e324fb20f8171f8f
Subproject commit e83feb89dd84e3456aafebb7d6dbe179675344e8