datainfo.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package importer
  2. import (
  3. "regexp"
  4. "strings"
  5. "sync"
  6. "time"
  7. "git.wecise.com/wecise/cgimport/schema"
  8. "github.com/wecisecode/util/cmap"
  9. "github.com/wecisecode/util/merrs"
  10. )
  11. type classdatainfo struct {
  12. *schema.ClassInfo
  13. insertcount int64
  14. lastlogtime time.Time
  15. lastlogicount int64
  16. mutex sync.Mutex
  17. }
  18. var classdatainfos = cmap.NewSingle[string, *classdatainfo]()
  19. // 根据数据修正类定义
  20. func (odbci *ODBCImporter) ReviseClassStruct() (err error) {
  21. for _, classname := range schema.ClassNames {
  22. ci := schema.ClassInfos.GetIFPresent(classname)
  23. if ci == nil {
  24. return merrs.NewError("classinfo not found " + classname)
  25. }
  26. cdi, e := classdatainfos.GetWithNew(ci.Classaliasname, func() (cdi *classdatainfo, err error) {
  27. if odbci.client != nil {
  28. _, e := odbci.client.Query("select class,id from " + ci.Classfullname + " limit 1").Do()
  29. if e != nil {
  30. if !strings.Contains(e.Error(), "not find") {
  31. return nil, e
  32. }
  33. logger.Info("create class " + ci.Classfullname)
  34. _, e = odbci.client.Query(ci.Createmql).Do()
  35. if e != nil {
  36. return nil, e
  37. }
  38. // add graph tags
  39. _, e = odbci.client.Query(ci.Addtagmql, ci.Classaliasname, ci.Classaliasname, []string{ci.Classaliasname}).Do()
  40. if e != nil {
  41. return nil, e
  42. }
  43. }
  44. }
  45. cdi = &classdatainfo{ClassInfo: ci}
  46. return
  47. })
  48. if e != nil {
  49. return e
  50. }
  51. classdatainfos.Set(ci.Classfullname, cdi)
  52. }
  53. if odbci.client != nil {
  54. for _, createedgemql := range schema.CreateEdgeMqls {
  55. _, e := odbci.client.Query(createedgemql).Do()
  56. if e != nil && !strings.Contains(e.Error(), "already exist") {
  57. return e
  58. }
  59. logger.Info(createedgemql)
  60. }
  61. }
  62. return
  63. }
  64. func (odbci *ODBCImporter) reload() error {
  65. if odbci.client != nil {
  66. for i := len(schema.ClassNames) - 1; i >= 0; i-- {
  67. classname := schema.ClassNames[i]
  68. ci := schema.ClassInfos.GetIFPresent(classname)
  69. if ci == nil {
  70. continue
  71. }
  72. e := odbci.dropclass(ci.Classfullname)
  73. if e != nil {
  74. return e
  75. }
  76. }
  77. e := odbci.InitLocalDB(true)
  78. if e != nil {
  79. return e
  80. }
  81. }
  82. return nil
  83. }
  84. func (odbci *ODBCImporter) init() error {
  85. e := odbci.InitLocalDB(false)
  86. if e != nil {
  87. return e
  88. }
  89. return nil
  90. }
  91. func (odbci *ODBCImporter) dropclass(classnames ...string) error {
  92. for _, classname := range classnames {
  93. for retry := 2; retry >= 0; retry-- {
  94. _, e := odbci.client.Query(`delete from /matrix/tagdir where tags='` + classname + `'`).Do()
  95. _ = e
  96. _, e = odbci.client.Query(`delete from "` + classname + `" with version`).Do()
  97. _ = e
  98. _, e = odbci.client.Query(`drop class if exists "` + classname + `"`).Do()
  99. if e != nil {
  100. matchstr := regexp.MustCompile(`refer by ([^,]+)`).FindStringSubmatch(e.Error())
  101. if len(matchstr) >= 2 {
  102. e = odbci.dropclass(matchstr[1])
  103. if e != nil {
  104. return e
  105. }
  106. } else {
  107. matchstr := regexp.MustCompile(`has children \[([^\]]+)\]`).FindStringSubmatch(e.Error())
  108. if len(matchstr) >= 2 {
  109. e = odbci.dropclass(strings.Split(matchstr[1], ",")...)
  110. if e != nil {
  111. return e
  112. }
  113. }
  114. }
  115. if retry > 0 {
  116. continue
  117. }
  118. return e
  119. }
  120. }
  121. logger.Info("drop class " + classname)
  122. }
  123. return nil
  124. }