/
file_parser.go
93 lines (85 loc) · 1.68 KB
/
file_parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package file
import (
"bufio"
"fmt"
"io"
"os"
. "github.com/rainmyy/easyDB/library/common"
. "github.com/rainmyy/easyDB/library/strategy"
)
/**
* 解析数据,将数据解析成树形结构进行存储
*/
func (this *File) Parser(objType int) error {
err := this.readFile()
if err != nil {
return err
}
return nil
}
//file size 1GB
var defaultSize int64 = 1 << 30
func (this *File) readFile() error {
fileName := this.fileAbs
fi, err := os.Open(fileName)
defer fi.Close()
if err != nil {
return err
}
fileSize := this.size
if fileSize == 0 {
fiStat, err := fi.Stat()
if err != nil {
return err
}
fileSize = fiStat.Size()
}
//if the file larger than 1GB,concurrently read and parse files
if fileSize > defaultSize {
return this.readFileByConcurrent(fi)
} else {
return this.readFileByGeneral(fi)
}
}
//
func (this *File) readFileByGeneral(fileObj *os.File) error {
if fileObj == nil {
return fmt.Errorf("file is nil")
}
r := bufio.NewReader(fileObj)
b := make([]byte, this.size)
for {
_, err := r.Read(b)
if err != nil && err == io.EOF {
break
}
}
tree, err := parserDataFunc(this, b)
if err != nil {
return err
}
this.content = tree
return nil
}
/**
* 并发读取,所有字符串按行分割, 暂不支持多行关联行数据
*/
func (this *File) readFileByConcurrent(fileObj *os.File) error {
return nil
}
/**
* 所有的数
*/
func parserDataFunc(file *File, data []byte) ([]*TreeStruct, error) {
var objType = file.GetDataType()
switch objType {
case IniType:
return ParserIniContent(data)
case YamlType:
return ParserYamlContent(data)
case JsonType:
return ParserJsonContent(data)
default:
return ParserContent(data)
}
}