importer.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package importer
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "os"
  7. "path/filepath"
  8. "sync"
  9. "sync/atomic"
  10. "git.wecise.com/wecise/cgimport/odbc"
  11. "git.wecise.com/wecise/cgimport/reader"
  12. "git.wecise.com/wecise/util/filewalker"
  13. "git.wecise.com/wecise/util/merrs"
  14. "git.wecise.com/wecise/util/rc"
  15. )
  16. var mcfg = odbc.Config
  17. var logger = odbc.Logger
  18. type Importer struct {
  19. datapath string
  20. parallel int
  21. importrc *rc.RoutinesController
  22. odbcimporter *ODBCImporter
  23. }
  24. func ImportDir(datapath string, parallel int) (filescount, recordscount int64, err error) {
  25. importer := &Importer{
  26. datapath: datapath,
  27. parallel: parallel,
  28. importrc: rc.NewRoutinesController("", 1000),
  29. odbcimporter: NewODBCImporter(),
  30. }
  31. return importer.Import()
  32. }
  33. func (importer *Importer) Import() (filescount, recordscount int64, err error) {
  34. // 遍历文件目录
  35. var cgirc = rc.NewRoutinesController("", importer.parallel)
  36. var wg sync.WaitGroup
  37. fw, e := filewalker.NewFileWalker([]string{importer.datapath}, ".*")
  38. if e != nil {
  39. err = e
  40. return
  41. }
  42. e = fw.List(func(basedir string, fpath string) bool {
  43. if err != nil {
  44. return false
  45. }
  46. filename := filepath.Join(basedir, fpath)
  47. wg.Add(1)
  48. cgirc.ConcurCall(1,
  49. func() {
  50. defer wg.Done()
  51. records, e := importer.ImportFile(filename)
  52. if e != nil {
  53. err = e
  54. return
  55. }
  56. atomic.AddInt64(&filescount, 1)
  57. atomic.AddInt64(&recordscount, int64(records))
  58. },
  59. )
  60. return true
  61. })
  62. wg.Wait()
  63. if e != nil {
  64. err = e
  65. return
  66. }
  67. return
  68. }
  69. func (importer *Importer) ImportFile(filepath string) (blockcount int, err error) {
  70. f, e := os.Open(filepath)
  71. if e != nil {
  72. return blockcount, merrs.NewError(e, merrs.SSMaps{{"filename": filepath}})
  73. }
  74. defer f.Close()
  75. return importer.importReader(filepath, f)
  76. }
  77. func (importer *Importer) importReader(filename string, buf io.Reader) (blockcount int, err error) {
  78. br, e := reader.NewBlockReader(filename, buf)
  79. if e != nil {
  80. return blockcount, merrs.NewError(e, merrs.SSMaps{{"filename": filename}})
  81. }
  82. var wg sync.WaitGroup
  83. defer wg.Wait()
  84. for {
  85. if err != nil {
  86. break
  87. }
  88. block, linecount, e := br.ReadBlock()
  89. if e != nil {
  90. return blockcount, merrs.NewError(e, merrs.SSMaps{{"filename": filename}, {"line": fmt.Sprint(linecount)}})
  91. }
  92. if block == nil {
  93. return
  94. }
  95. wg.Add(1)
  96. e = importer.importrc.ConcurCall(1, func() {
  97. defer wg.Done()
  98. e = importer.importRecord(block, filename, linecount)
  99. if e != nil {
  100. err = merrs.NewError(e, merrs.SSMaps{{"filename": filename}, {"line": fmt.Sprint(linecount)}})
  101. return
  102. }
  103. blockcount++
  104. })
  105. if e != nil {
  106. return blockcount, merrs.NewError(e, merrs.SSMaps{{"filename": filename}, {"line": fmt.Sprint(linecount)}})
  107. }
  108. }
  109. return
  110. }
  111. func (importer *Importer) importRecord(record map[string]any, filename string, linecount int) (err error) {
  112. bs, e := json.MarshalIndent(record, "", " ")
  113. if e != nil {
  114. return e
  115. }
  116. logger.Debug(fmt.Sprint("import ", filename, "[", linecount, "]:", string(bs)))
  117. e = importer.odbcimporter.ReviseClassStruct(record)
  118. if e != nil {
  119. return e
  120. }
  121. e = importer.odbcimporter.InsertData(record)
  122. if e != nil {
  123. return e
  124. }
  125. return
  126. }