123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- package graph
- import (
- "encoding/json"
- "sync"
- "git.wecise.com/wecise/cgimport/schema"
- "git.wecise.com/wecise/util/deepcopy"
- "github.com/spf13/cast"
- "github.com/wecisecode/util/merrs"
- )
- type EdgeInfo struct {
- FromOid string
- EdgeType string
- ToOid string
- Attributes map[string]string
- }
- var edgeinfosmutex sync.RWMutex
- var edgeinfos = map[string]map[string]map[string][]string{} // fromid - edgetype - toid - relationinfo
- func CacheEdgeInfo(data map[string]any) (err error) {
- ei, e := ParseEdgeInfo(data)
- if e != nil {
- return e
- }
- edgeinfosmutex.Lock()
- defer edgeinfosmutex.Unlock()
- eitypes := edgeinfos[ei.FromOid]
- if eitypes == nil {
- eitypes = map[string]map[string][]string{}
- } else {
- eitypes = deepcopy.DeepCopy(eitypes).(map[string]map[string][]string)
- }
- edgeinfos[ei.FromOid] = eitypes
- eis := eitypes[ei.EdgeType]
- if eis == nil {
- eis = map[string][]string{}
- eitypes[ei.EdgeType] = eis
- }
- eis["_all"] = append(eis["_all"], ei.ToOid)
- eabs, _ := json.Marshal(ei.Attributes)
- eis[ei.ToOid] = append(eis[ei.ToOid], string(eabs))
- return
- }
- func GetEdgeInfo(fromoid string) map[string]map[string][]string {
- edgeinfosmutex.RLock()
- defer edgeinfosmutex.RUnlock()
- return edgeinfos[fromoid]
- }
- func ParseEdgeInfo(data map[string]any) (ei *EdgeInfo, err error) {
- extraattr := map[string]string{}
- fromuid := ""
- touid := ""
- edgetype := ""
- for k, v := range data {
- switch k {
- case "FROMUNIQUEID":
- fromuid = cast.ToString(v)
- case "TOUNIQUEID":
- touid = cast.ToString(v)
- case "EDGETYPE":
- edgetype = cast.ToString(v)
- default:
- extraattr[k] = cast.ToString(v)
- }
- }
- if fromuid == "" {
- databs, _ := json.MarshalIndent(data, "", " ")
- return nil, merrs.NewError("not found valid fromuniqueid in data ", merrs.SSMap{"data": string(databs)})
- }
- if touid == "" {
- databs, _ := json.MarshalIndent(data, "", " ")
- return nil, merrs.NewError("not found valid touniqueid in data ", merrs.SSMap{"data": string(databs)})
- }
- if edgetype == "" {
- databs, _ := json.MarshalIndent(data, "", " ")
- return nil, merrs.NewError("not found valid edgetype in data ", merrs.SSMap{"data": string(databs)})
- }
- edgetype = schema.Relations[edgetype]
- if edgetype == "" {
- databs, _ := json.MarshalIndent(data, "", " ")
- return nil, merrs.NewError("not found valid edgetype in data ", merrs.SSMap{"data": string(databs)})
- }
- foid := ToNodeId("level1", fromuid)
- toid := ToNodeId("level1", touid)
- return &EdgeInfo{
- FromOid: foid,
- ToOid: toid,
- EdgeType: edgetype,
- Attributes: extraattr,
- }, nil
- }
|