From 7b80d6e7365bf914b27cf831f642114bd3c5de61 Mon Sep 17 00:00:00 2001 From: walker Date: Wed, 27 Dec 2023 15:59:46 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4repo=E5=8C=85=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- {repo => cgrepo}/check_test.go | 0 {repo => cgrepo}/dto/cg_repo.pb.go | 0 {repo => cgrepo}/error_record.go | 0 {repo => cgrepo}/idmapping_build.go | 2 +- cgrepo/manage.go | 33 +++++++++++ {repo => cgrepo}/model/common.go | 0 {repo => cgrepo}/model/link.go | 0 {repo => cgrepo}/model/section.go | 0 {repo => cgrepo}/model/station.go | 0 {repo => cgrepo}/model/turnout.go | 0 .../model/impl => cgrepo/model_impl}/link.go | 4 +- .../model_impl}/physical_section.go | 2 +- .../model_impl}/section_check_device.go | 0 .../impl => cgrepo/model_impl}/turnout.go | 2 +- cgrepo/repo.go | 56 +++++++++++++++++++ repo/api.go | 17 ------ repo/manage.go | 39 ------------- repo/repo.go | 44 --------------- 18 files changed, 94 insertions(+), 105 deletions(-) rename {repo => cgrepo}/check_test.go (100%) rename {repo => cgrepo}/dto/cg_repo.pb.go (100%) rename {repo => cgrepo}/error_record.go (100%) rename {repo => cgrepo}/idmapping_build.go (99%) create mode 100644 cgrepo/manage.go rename {repo => cgrepo}/model/common.go (100%) rename {repo => cgrepo}/model/link.go (100%) rename {repo => cgrepo}/model/section.go (100%) rename {repo => cgrepo}/model/station.go (100%) rename {repo => cgrepo}/model/turnout.go (100%) rename {repo/model/impl => cgrepo/model_impl}/link.go (92%) rename {repo/model/impl => cgrepo/model_impl}/physical_section.go (93%) rename {repo/model/impl => cgrepo/model_impl}/section_check_device.go (100%) rename {repo/model/impl => cgrepo/model_impl}/turnout.go (89%) create mode 100644 cgrepo/repo.go delete mode 100644 repo/api.go delete mode 100644 repo/manage.go delete mode 100644 repo/repo.go diff --git a/repo/check_test.go b/cgrepo/check_test.go similarity index 100% rename from repo/check_test.go rename to cgrepo/check_test.go diff --git a/repo/dto/cg_repo.pb.go b/cgrepo/dto/cg_repo.pb.go similarity index 100% rename from repo/dto/cg_repo.pb.go rename to cgrepo/dto/cg_repo.pb.go diff --git a/repo/error_record.go b/cgrepo/error_record.go similarity index 100% rename from repo/error_record.go rename to cgrepo/error_record.go diff --git a/repo/idmapping_build.go b/cgrepo/idmapping_build.go similarity index 99% rename from repo/idmapping_build.go rename to cgrepo/idmapping_build.go index a390ace..9f07b16 100644 --- a/repo/idmapping_build.go +++ b/cgrepo/idmapping_build.go @@ -6,7 +6,7 @@ import ( "strconv" "strings" - "joylink.club/rtsssimulation/repo/dto" + "joylink.club/rtsssimulation/cgrepo/dto" ) // 城轨uid diff --git a/cgrepo/manage.go b/cgrepo/manage.go new file mode 100644 index 0000000..15f8f68 --- /dev/null +++ b/cgrepo/manage.go @@ -0,0 +1,33 @@ +package repo + +// type repoManager struct { +// repoMap map[string]CgRepo +// lock sync.Mutex +// } + +// var defaultManager = &repoManager{ +// repoMap: make(map[string]CgRepo), +// } + +// // 获取或构建模型仓库 +// 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 +// } diff --git a/repo/model/common.go b/cgrepo/model/common.go similarity index 100% rename from repo/model/common.go rename to cgrepo/model/common.go diff --git a/repo/model/link.go b/cgrepo/model/link.go similarity index 100% rename from repo/model/link.go rename to cgrepo/model/link.go diff --git a/repo/model/section.go b/cgrepo/model/section.go similarity index 100% rename from repo/model/section.go rename to cgrepo/model/section.go diff --git a/repo/model/station.go b/cgrepo/model/station.go similarity index 100% rename from repo/model/station.go rename to cgrepo/model/station.go diff --git a/repo/model/turnout.go b/cgrepo/model/turnout.go similarity index 100% rename from repo/model/turnout.go rename to cgrepo/model/turnout.go diff --git a/repo/model/impl/link.go b/cgrepo/model_impl/link.go similarity index 92% rename from repo/model/impl/link.go rename to cgrepo/model_impl/link.go index 6ad6a9c..be889c9 100644 --- a/repo/model/impl/link.go +++ b/cgrepo/model_impl/link.go @@ -4,8 +4,8 @@ import ( "fmt" "sync/atomic" - "joylink.club/rtsssimulation/repo/dto" - "joylink.club/rtsssimulation/repo/model" + "joylink.club/rtsssimulation/cgrepo/dto" + "joylink.club/rtsssimulation/cgrepo/model" ) // link生成uid基础值 diff --git a/repo/model/impl/physical_section.go b/cgrepo/model_impl/physical_section.go similarity index 93% rename from repo/model/impl/physical_section.go rename to cgrepo/model_impl/physical_section.go index 32d8072..008f365 100644 --- a/repo/model/impl/physical_section.go +++ b/cgrepo/model_impl/physical_section.go @@ -1,7 +1,7 @@ package impl import ( - "joylink.club/rtsssimulation/repo/model" + "joylink.club/rtsssimulation/cgrepo/model" ) type PhysicalSection struct { diff --git a/repo/model/impl/section_check_device.go b/cgrepo/model_impl/section_check_device.go similarity index 100% rename from repo/model/impl/section_check_device.go rename to cgrepo/model_impl/section_check_device.go diff --git a/repo/model/impl/turnout.go b/cgrepo/model_impl/turnout.go similarity index 89% rename from repo/model/impl/turnout.go rename to cgrepo/model_impl/turnout.go index d2d41b1..a465f0e 100644 --- a/repo/model/impl/turnout.go +++ b/cgrepo/model_impl/turnout.go @@ -1,6 +1,6 @@ package impl -import "joylink.club/rtsssimulation/repo/model" +import "joylink.club/rtsssimulation/cgrepo/model" type Turnout struct { uid string diff --git a/cgrepo/repo.go b/cgrepo/repo.go new file mode 100644 index 0000000..4053076 --- /dev/null +++ b/cgrepo/repo.go @@ -0,0 +1,56 @@ +package repo + +import "joylink.club/rtsssimulation/cgrepo/model" + +type CgRepo interface { + // 模型仓库id + Id() string + // 获取所有道岔 + Turnouts() []model.Turnout + // 通过uid查询模型对象 + FindByUid(uid string) model.Model +} + +type IdMap interface { + // 获取数据元素id + DeId() string + // 获取uid + Uid() string +} + +// type repo struct { +// id string +// idMapping *IdMapping // id映射 +// modelMap map[string]model.Model // 模型map,key为uid +// linkMap map[string]*impl.Link // 链路map,key为uid +// physicalSectionMap map[string]*impl.PhysicalSection // 物理区段map,key为uid +// turnoutMap map[string]*impl.Turnout // 道岔map,key为uid +// } + +// func BuildFrom(msgs *dto.CgRepo) (CgRepo, *ErrorRecord) { +// errRecord := NewErrorRecord() +// idMapping := BuildIdMapping(msgs, errRecord) +// if errRecord.HasError() { +// return nil, errRecord +// } +// repo := &repo{ +// id: msgs.Id, +// idMapping: idMapping, +// modelMap: make(map[string]model.Model, 1024), +// linkMap: make(map[string]*impl.Link, 256), +// physicalSectionMap: make(map[string]*impl.PhysicalSection, 256), +// turnoutMap: make(map[string]*impl.Turnout, 128), +// } + +// return repo, errRecord +// } + +// // 模型仓库id +// func (r *repo) Id() string { +// return r.id +// } + +// // 通过uid查询模型对象 +// func (r *repo) FindByUid(uid string) model.Model { +// return nil +// } diff --git a/repo/api.go b/repo/api.go deleted file mode 100644 index 0281e87..0000000 --- a/repo/api.go +++ /dev/null @@ -1,17 +0,0 @@ -package repo - -import "joylink.club/rtsssimulation/repo/model" - -type CgRepo interface { - // 模型仓库id - Id() string - // 通过uid查询模型对象 - FindByUid(uid string) model.Model -} - -type IdMap interface { - // 获取数据元素id - DeId() string - // 获取uid - Uid() string -} diff --git a/repo/manage.go b/repo/manage.go deleted file mode 100644 index 4b36369..0000000 --- a/repo/manage.go +++ /dev/null @@ -1,39 +0,0 @@ -package repo - -import ( - "sync" - - "joylink.club/rtsssimulation/repo/dto" -) - -type repoManager struct { - repoMap map[string]CgRepo - lock sync.Mutex -} - -var defaultManager = &repoManager{ - repoMap: make(map[string]CgRepo), -} - -// 获取或构建模型仓库 -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 -} diff --git a/repo/repo.go b/repo/repo.go deleted file mode 100644 index 62a91d1..0000000 --- a/repo/repo.go +++ /dev/null @@ -1,44 +0,0 @@ -package repo - -import ( - "joylink.club/rtsssimulation/repo/dto" - "joylink.club/rtsssimulation/repo/model" - "joylink.club/rtsssimulation/repo/model/impl" -) - -type repo struct { - id string - idMapping *IdMapping // id映射 - modelMap map[string]model.Model // 模型map,key为uid - linkMap map[string]*impl.Link // 链路map,key为uid - physicalSectionMap map[string]*impl.PhysicalSection // 物理区段map,key为uid - turnoutMap map[string]*impl.Turnout // 道岔map,key为uid -} - -func BuildFrom(msgs *dto.CgRepo) (CgRepo, *ErrorRecord) { - errRecord := NewErrorRecord() - idMapping := BuildIdMapping(msgs, errRecord) - if errRecord.HasError() { - return nil, errRecord - } - repo := &repo{ - id: msgs.Id, - idMapping: idMapping, - modelMap: make(map[string]model.Model, 1024), - linkMap: make(map[string]*impl.Link, 256), - physicalSectionMap: make(map[string]*impl.PhysicalSection, 256), - turnoutMap: make(map[string]*impl.Turnout, 128), - } - - return repo, errRecord -} - -// 模型仓库id -func (r *repo) Id() string { - return r.id -} - -// 通过uid查询模型对象 -func (r *repo) FindByUid(uid string) model.Model { - return nil -}