1 Star 0 Fork 0

fishjam/gitee-esm

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
file.go 3.15 KB
一键复制 编辑 原始数据 按行查看 历史
fishjam 提交于 2024-01-05 11:01 +08:00 . add sync function for incremental update (#84)
/*
Copyright 2016 Medcl (m AT medcl.net)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"bufio"
"encoding/json"
"github.com/cheggaaa/pb"
log "github.com/cihub/seelog"
"io"
"os"
"strings"
"sync"
)
func checkFileIsExist(filename string) bool {
var exist = true
if _, err := os.Stat(filename); os.IsNotExist(err) {
exist = false
}
return exist
}
func (m *Migrator) NewFileReadWorker(pb *pb.ProgressBar, wg *sync.WaitGroup) {
log.Debug("start reading file")
f, err := os.Open(m.Config.DumpInputFile)
if err != nil {
log.Error(err)
return
}
defer f.Close()
r := bufio.NewReader(f)
lineCount := 0
for {
line, err := r.ReadString('\n')
if io.EOF == err || nil != err {
break
}
lineCount += 1
js := map[string]interface{}{}
err = DecodeJson(line, &js)
if err != nil {
log.Error(err)
continue
}
m.DocChan <- js
pb.Increment()
}
defer f.Close()
log.Debug("end reading file")
close(m.DocChan)
wg.Done()
}
func (c *Migrator) NewFileDumpWorker(pb *pb.ProgressBar, wg *sync.WaitGroup) {
var f *os.File
var err1 error
if checkFileIsExist(c.Config.DumpOutFile) {
flag := os.O_WRONLY
if c.Config.TruncateOutFile {
flag |= os.O_TRUNC
} else {
flag |= os.O_APPEND
}
f, err1 = os.OpenFile(c.Config.DumpOutFile, flag, os.ModeAppend)
if err1 != nil {
log.Error(err1)
return
}
} else {
f, err1 = os.Create(c.Config.DumpOutFile)
if err1 != nil {
log.Error(err1)
return
}
}
w := bufio.NewWriter(f)
skipFields := make([]string, 0)
if len(c.Config.SkipFields) > 0 {
//skip fields
if !strings.Contains(c.Config.SkipFields, ",") {
skipFields = append(skipFields, c.Config.SkipFields)
} else {
fields := strings.Split(c.Config.SkipFields, ",")
for _, field := range fields {
skipFields = append(skipFields, field)
}
}
}
READ_DOCS:
for {
docI, open := <-c.DocChan
// this check is in case the document is an error with scroll stuff
if status, ok := docI["status"]; ok {
if status.(int) == 404 {
log.Error("error: ", docI["response"])
continue
}
}
// sanity check
for _, key := range []string{"_index", "_type", "_source", "_id"} {
if _, ok := docI[key]; !ok {
break READ_DOCS
}
}
for _, key := range skipFields {
if _, found := docI[key]; found {
delete(docI, key)
}
}
jsr, err := json.Marshal(docI)
log.Trace(string(jsr))
if err != nil {
log.Error(err)
}
n, err := w.WriteString(string(jsr))
if err != nil {
log.Error(n, err)
}
w.WriteString("\n")
pb.Increment()
// if channel is closed flush and gtfo
if !open {
goto WORKER_DONE
}
}
WORKER_DONE:
w.Flush()
f.Close()
wg.Done()
log.Debug("file dump finished")
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/fishjam/gitee-esm.git
git@gitee.com:fishjam/gitee-esm.git
fishjam
gitee-esm
gitee-esm
master

搜索帮助