datainfo.go 3.2 KB

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