123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- package cgf
- import (
- "encoding/json"
- "fmt"
- "io"
- "os"
- "path/filepath"
- "sync"
- "sync/atomic"
- "git.wecise.com/wecise/cgimport/cgf/reader"
- "git.wecise.com/wecise/cgimport/odbc"
- "git.wecise.com/wecise/util/filewalker"
- "git.wecise.com/wecise/util/merrs"
- "git.wecise.com/wecise/util/rc"
- )
- var mcfg = odbc.Config
- var logger = odbc.Logger
- func ImportDir(datapath string, parallel int) (filescount, recordscount int64, err error) {
- // 遍历文件目录
- var cgirc = rc.NewRoutinesController("", parallel)
- var wg sync.WaitGroup
- fw, e := filewalker.NewFileWalker([]string{datapath}, ".*")
- if e != nil {
- err = e
- return
- }
- e = fw.List(func(basedir string, fpath string) bool {
- if err != nil {
- return false
- }
- filename := filepath.Join(basedir, fpath)
- wg.Add(1)
- cgirc.ConcurCall(1,
- func() {
- defer wg.Done()
- records, e := ImportFile(filename)
- if e != nil {
- err = e
- return
- }
- atomic.AddInt64(&filescount, 1)
- atomic.AddInt64(&recordscount, int64(records))
- },
- )
- return true
- })
- wg.Wait()
- if e != nil {
- err = e
- return
- }
- return
- }
- func ImportFile(filepath string) (blockcount int, err error) {
- f, e := os.Open(filepath)
- if e != nil {
- return blockcount, merrs.NewError(e, merrs.SSMaps{{"filename": filepath}})
- }
- defer f.Close()
- return importReader(filepath, f)
- }
- var parserc = rc.NewRoutinesController("", 1000)
- func importReader(filename string, buf io.Reader) (blockcount int, err error) {
- br, e := reader.NewBlockReader(filename, buf)
- if e != nil {
- return blockcount, merrs.NewError(e, merrs.SSMaps{{"filename": filename}})
- }
- var wg sync.WaitGroup
- defer wg.Wait()
- for {
- if err != nil {
- break
- }
- block, linecount, e := br.ReadBlock()
- if e != nil {
- return blockcount, merrs.NewError(e, merrs.SSMaps{{"filename": filename}, {"line": fmt.Sprint(linecount)}})
- }
- if block == nil {
- return
- }
- wg.Add(1)
- e = parserc.ConcurCall(1, func() {
- defer wg.Done()
- e = importBlock(block, filename, linecount)
- if e != nil {
- err = merrs.NewError(e, merrs.SSMaps{{"filename": filename}, {"line": fmt.Sprint(linecount)}})
- return
- }
- blockcount++
- })
- if e != nil {
- return blockcount, merrs.NewError(e, merrs.SSMaps{{"filename": filename}, {"line": fmt.Sprint(linecount)}})
- }
- }
- return
- }
- func importBlock(block map[string]any, filename string, linecount int) (err error) {
- bs, e := json.MarshalIndent(block, "", " ")
- if e != nil {
- return e
- }
- logger.Debug(fmt.Sprint("import ", filename, "[", linecount, "]:", string(bs)))
- return
- }
|