edgeinfo.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package graph
  2. import (
  3. "encoding/json"
  4. "sync"
  5. "git.wecise.com/wecise/cgimport/schema"
  6. "github.com/spf13/cast"
  7. "github.com/wecisecode/util/deepcopy"
  8. "github.com/wecisecode/util/merrs"
  9. )
  10. type EdgeInfo struct {
  11. FromOid string
  12. EdgeType string
  13. ToOid string
  14. Attributes map[string]string
  15. }
  16. var edgeinfosmutex sync.RWMutex
  17. var edgeinfos = map[string]map[string]map[string][]string{} // fromid - edgetype - toid - relationinfo
  18. func CacheEdgeInfo(data map[string]any) (err error) {
  19. ei, e := ParseEdgeInfo(data)
  20. if e != nil {
  21. return e
  22. }
  23. edgeinfosmutex.Lock()
  24. defer edgeinfosmutex.Unlock()
  25. eitypes := edgeinfos[ei.FromOid]
  26. if eitypes == nil {
  27. eitypes = map[string]map[string][]string{}
  28. } else {
  29. eitypes = deepcopy.DeepCopy(eitypes).(map[string]map[string][]string)
  30. }
  31. edgeinfos[ei.FromOid] = eitypes
  32. eis := eitypes[ei.EdgeType]
  33. if eis == nil {
  34. eis = map[string][]string{}
  35. eitypes[ei.EdgeType] = eis
  36. }
  37. eis["_all"] = append(eis["_all"], ei.ToOid)
  38. eabs, _ := json.Marshal(ei.Attributes)
  39. eis[ei.ToOid] = append(eis[ei.ToOid], string(eabs))
  40. return
  41. }
  42. func GetEdgeInfo(fromoid string) map[string]map[string][]string {
  43. edgeinfosmutex.RLock()
  44. defer edgeinfosmutex.RUnlock()
  45. return edgeinfos[fromoid]
  46. }
  47. func ParseEdgeInfo(data map[string]any) (ei *EdgeInfo, err error) {
  48. extraattr := map[string]string{}
  49. fromuid := ""
  50. touid := ""
  51. edgetype := ""
  52. for k, v := range data {
  53. switch k {
  54. case "FROMUNIQUEID":
  55. fromuid = cast.ToString(v)
  56. case "TOUNIQUEID":
  57. touid = cast.ToString(v)
  58. case "EDGETYPE":
  59. edgetype = cast.ToString(v)
  60. default:
  61. extraattr[k] = cast.ToString(v)
  62. }
  63. }
  64. if fromuid == "" {
  65. databs, _ := json.MarshalIndent(data, "", " ")
  66. return nil, merrs.NewError("not found valid fromuniqueid in data ", merrs.SSMap{"data": string(databs)})
  67. }
  68. if touid == "" {
  69. databs, _ := json.MarshalIndent(data, "", " ")
  70. return nil, merrs.NewError("not found valid touniqueid in data ", merrs.SSMap{"data": string(databs)})
  71. }
  72. if edgetype == "" {
  73. databs, _ := json.MarshalIndent(data, "", " ")
  74. return nil, merrs.NewError("not found valid edgetype in data ", merrs.SSMap{"data": string(databs)})
  75. }
  76. edgetype = schema.Relations[edgetype]
  77. if edgetype == "" {
  78. databs, _ := json.MarshalIndent(data, "", " ")
  79. return nil, merrs.NewError("not found valid edgetype in data ", merrs.SSMap{"data": string(databs)})
  80. }
  81. foid := ToNodeId("level1", fromuid)
  82. toid := ToNodeId("level1", touid)
  83. return &EdgeInfo{
  84. FromOid: foid,
  85. ToOid: toid,
  86. EdgeType: edgetype,
  87. Attributes: extraattr,
  88. }, nil
  89. }