-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml.go
193 lines (159 loc) · 4.12 KB
/
xml.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package input
import (
"bufio"
"encoding/xml"
"fmt"
"github.com/antchfx/xmlquery"
"github.com/fsnotify/fsnotify"
"github.com/rodb-io/rodb/pkg/input/record"
"github.com/rodb-io/rodb/pkg/parser"
"github.com/rodb-io/rodb/pkg/util"
"io"
"os"
"sync"
"time"
)
var xmlParserOptions = xmlquery.ParserOptions{
Decoder: &xmlquery.DecoderOptions{
Strict: false,
},
}
type Xml struct {
config *XmlConfig
reader io.ReadSeeker
readerBuffer *bufio.Reader
readerLock sync.Mutex
xmlFile *os.File
xmlParser *xmlquery.StreamParser
parsers parser.List
watcher *fsnotify.Watcher
}
type xmlTempRecordNode struct {
XMLName xml.Name
Attr []xml.Attr `xml:",any,attr"`
InnerXML []byte `xml:",innerxml"`
}
func NewXml(
config *XmlConfig,
parsers parser.List,
) (*Xml, error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
xmlInput := &Xml{
config: config,
readerLock: sync.Mutex{},
watcher: watcher,
parsers: parsers,
}
util.StartFilesystemWatchProcess(
xmlInput.watcher,
xmlInput.config.ShouldDieOnInputChange(),
xmlInput.config.Logger,
)
file, err := os.Open(xmlInput.config.Path)
if err != nil {
return nil, err
}
xmlInput.xmlFile = file
xmlInput.reader = io.ReadSeeker(file)
xmlInput.readerBuffer = bufio.NewReader(xmlInput.reader)
xmlInput.xmlParser, err = xmlquery.CreateStreamParserWithOptions(
xmlInput.readerBuffer,
xmlParserOptions,
xmlInput.config.RecordXPath,
)
if err != nil {
return nil, err
}
if err := xmlInput.watcher.Add(config.Path); err != nil {
return nil, err
}
return xmlInput, nil
}
func (xmlInput *Xml) Name() string {
return xmlInput.config.Name
}
func (xmlInput *Xml) Get(position record.Position) (record.Record, error) {
xmlInput.readerLock.Lock()
defer xmlInput.readerLock.Unlock()
if err := util.SetBufferedReaderOffset(xmlInput.reader, xmlInput.readerBuffer, position); err != nil {
return nil, err
}
node, err := xmlInput.xmlParser.Read()
if err == io.EOF {
return nil, fmt.Errorf("Did not find an XML record at position %v", position)
} else if err != nil {
return nil, fmt.Errorf("Cannot read xml data: %w", err)
}
return NewXmlRecord(xmlInput.config, node, xmlInput.parsers, position)
}
func (xmlInput *Xml) Size() (int64, error) {
fileInfo, err := os.Stat(xmlInput.config.Path)
if err != nil {
return 0, err
}
return fileInfo.Size(), nil
}
func (xmlInput *Xml) ModTime() (time.Time, error) {
fileInfo, err := os.Stat(xmlInput.config.Path)
if err != nil {
return time.Time{}, err
}
return fileInfo.ModTime(), nil
}
func (xmlInput *Xml) IterateAll() (record.Iterator, func() error, error) {
file, err := os.Open(xmlInput.config.Path)
if err != nil {
return nil, nil, err
}
reader := io.ReadSeeker(file)
readerBuffer := bufio.NewReader(reader)
xmlParser, err := xmlquery.CreateStreamParserWithOptions(
readerBuffer,
xmlParserOptions,
xmlInput.config.RecordXPath,
)
if err != nil {
return nil, nil, err
}
position := int64(0)
iterator := func() (record.Record, error) {
// The returned position is actually the end of the opening tag of the previous element
// We cannot easily get a more precise position, but since we can get the next record
// reliably from this position, it works
position, err = util.GetBufferedReaderOffset(reader, readerBuffer)
if err != nil {
return nil, fmt.Errorf("Error when getting xml offset: %v", err)
}
node, err := xmlParser.Read()
if err == io.EOF {
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("Cannot read xml data: %w", err)
}
record, err := NewXmlRecord(xmlInput.config, node, xmlInput.parsers, position)
if err != nil {
return nil, fmt.Errorf("Error when creating record after position %v: %v", position, err)
}
return record, nil
}
end := func() error {
return file.Close()
}
return iterator, end, nil
}
func (xmlInput *Xml) Close() error {
if err := xmlInput.watcher.Remove(xmlInput.config.Path); err != nil {
return err
}
if err := xmlInput.watcher.Close(); err != nil {
return err
}
if err := xmlInput.xmlFile.Close(); err != nil {
return err
}
return nil
}