datainfo.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package importer
  2. import (
  3. "regexp"
  4. "strings"
  5. "sync"
  6. "time"
  7. "git.wecise.com/wecise/cgimport/schema"
  8. "git.wecise.com/wecise/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. }
  60. }
  61. return
  62. }
  63. func (odbci *ODBCImporter) reload() error {
  64. if odbci.client != nil {
  65. for i := len(schema.ClassNames) - 1; i >= 0; i-- {
  66. classname := schema.ClassNames[i]
  67. ci := schema.ClassInfos.GetIFPresent(classname)
  68. if ci == nil {
  69. continue
  70. }
  71. e := odbci.dropclass(ci.Classfullname)
  72. if e != nil {
  73. return e
  74. }
  75. }
  76. e := odbci.InitLocalDB(true)
  77. if e != nil {
  78. return e
  79. }
  80. }
  81. return nil
  82. }
  83. func (odbci *ODBCImporter) init() error {
  84. e := odbci.InitLocalDB(false)
  85. if e != nil {
  86. return e
  87. }
  88. return nil
  89. }
  90. func (odbci *ODBCImporter) dropclass(classnames ...string) error {
  91. for _, classname := range classnames {
  92. for retry := 2; retry >= 0; retry-- {
  93. _, e := odbci.client.Query(`delete from /matrix/tagdir where tags='` + classname + `'`).Do()
  94. _ = e
  95. _, e = odbci.client.Query(`delete from "` + classname + `" with version`).Do()
  96. _ = e
  97. _, e = odbci.client.Query(`drop class if exists "` + classname + `"`).Do()
  98. if e != nil {
  99. matchstr := regexp.MustCompile(`refer by ([^,]+)`).FindStringSubmatch(e.Error())
  100. if len(matchstr) >= 2 {
  101. e = odbci.dropclass(matchstr[1])
  102. if e != nil {
  103. return e
  104. }
  105. } else {
  106. matchstr := regexp.MustCompile(`has children \[([^\]]+)\]`).FindStringSubmatch(e.Error())
  107. if len(matchstr) >= 2 {
  108. e = odbci.dropclass(strings.Split(matchstr[1], ",")...)
  109. if e != nil {
  110. return e
  111. }
  112. }
  113. }
  114. if retry > 0 {
  115. continue
  116. }
  117. return e
  118. }
  119. }
  120. logger.Info("drop class " + classname)
  121. }
  122. return nil
  123. }