Commit 8b222bcf authored by jingbo.wang's avatar jingbo.wang

变更缓存文件的序列化协议,把json改成gob。原因是json的int、float傻傻分不清

parent 39ae40ec
......@@ -85,7 +85,7 @@ func main() {
}
}()
//初始化数据库(创建DB)
//初始化数据库(创建DB,创建连续查询
dao.DbInit()
//处理消息
......
package dao
import (
"bytes"
"context"
"encoding/json"
"encoding/gob"
"fmt"
"git.quantgroup.cn/DevOps/enoch/pkg/global"
"git.quantgroup.cn/DevOps/enoch/pkg/glog"
"github.com/influxdata/influxdb/client/v2"
......@@ -93,11 +95,17 @@ func (d *Dao) flashFileCache() {
}
cachePointList := make([]CachePoint, 0)
if err := json.Unmarshal(data, &cachePointList); err != nil {
glog.Error("can not unmarshal file:", filePath)
buf := bytes.NewBuffer(data)
dec := gob.NewDecoder(buf)
if err := dec.Decode(&cachePointList); err != nil {
glog.Error("can not decode file:", filePath, err)
continue
}
for _, v := range cachePointList {
fmt.Println(v)
}
pointList := make([]*client.Point, 0)
for _, cachePoint := range cachePointList {
point, err := client.NewPoint(cachePoint.Name, cachePoint.Tags, cachePoint.Fields, cachePoint.Time)
......@@ -108,10 +116,10 @@ func (d *Dao) flashFileCache() {
}
if err := d.writeDb(pointList); err != nil {
glog.Warn("flash file cache: can not write db")
glog.Warn("flash file cache: can not write db", err)
continue
} else {
glog.Info("文件缓存写入db成功:", filePath)
glog.Info("文件缓存写入influxdb成功:", filePath)
}
if err := os.Remove(filePath); err != nil {
......@@ -223,10 +231,12 @@ func (d *Dao) writeFile(pointList []*client.Point) error {
cachePointList = append(cachePointList, NewPoint(point))
}
data, err := json.Marshal(cachePointList)
if err != nil {
buf := new(bytes.Buffer)
enc := gob.NewEncoder(buf)
if err := enc.Encode(cachePointList); err != nil {
return err
}
data := buf.Bytes()
fileName := time.Now().Format("2006-01-02T15:04:05.999999999")
writingFileName := path.Join(d.cacheFileDir, filePrefixWriting+":"+fileName)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment