forked from chararch/gobatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_writer.go
110 lines (105 loc) · 3.29 KB
/
file_writer.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
package batch
import (
"fmt"
"github.com/supreness/batch/file"
"strings"
)
const (
fileItemWriterHandleKey = "batch.FileItemWriter.handle"
fileItemWriterFileNameKey = "batch.FileItemWriter.fileName"
)
type fileWriter struct {
fd file.FileObjectModel
writer file.FileItemWriter
checkumer file.ChecksumFlusher
merger file.FileMerger
}
func (w *fileWriter) Open(execution *StepExecution) BatchError {
stepName := execution.StepName
//get actual file name
fd := w.fd //copy fd
fp := &FilePath{fd.FileName}
fileName, err := fp.Format(execution)
if err != nil {
return NewBatchError(ErrCodeGeneral, "get real file path:%v err", fd.FileName, err)
}
fd.FileName = fileName
if strings.Index(stepName, ":") > 0 { //may be a partitioned step
fd.FileName = fmt.Sprintf("%v.%v", fd.FileName, strings.ReplaceAll(stepName, ":", "."))
}
handle, e := w.writer.Open(fd)
if e != nil {
return NewBatchError(ErrCodeGeneral, "open file writer:%v err", fd.FileName, e)
}
execution.StepExecutionContext.Put(fileItemWriterHandleKey, handle)
execution.StepExecutionContext.Put(fileItemWriterFileNameKey, fd.FileName)
return nil
}
func (w *fileWriter) Write(items []interface{}, chunkCtx *ChunkContext) BatchError {
executionCtx := chunkCtx.StepExecution.StepExecutionContext
handle := executionCtx.Get(fileItemWriterHandleKey)
fileName := executionCtx.Get(fileItemWriterFileNameKey)
for _, item := range items {
e := w.writer.WriteItem(handle, item)
if e != nil {
return NewBatchError(ErrCodeGeneral, "write item to file:%v err", fileName, e)
}
}
return nil
}
func (w *fileWriter) Close(execution *StepExecution) BatchError {
executionCtx := execution.StepExecutionContext
handle := executionCtx.Get(fileItemWriterHandleKey)
fileName := executionCtx.Get(fileItemWriterFileNameKey)
executionCtx.Remove(fileItemWriterHandleKey)
e := w.writer.Close(handle)
if e != nil {
return NewBatchError(ErrCodeGeneral, "close file writer:%v err", fileName, e)
}
//generate file checksum
if w.fd.Checksum != "" {
fd := w.fd
fd.FileName = fileName.(string)
checksumer := file.GetChecksumer(fd.Checksum)
if checksumer != nil {
err := checksumer.Checksum(fd)
if err != nil {
return NewBatchError(ErrCodeGeneral, "generate file checksum:%v err", fd, err)
}
}
}
return nil
}
func (w *fileWriter) Aggregate(execution *StepExecution, subExecutions []*StepExecution) BatchError {
if w.merger != nil {
subFiles := make([]file.FileObjectModel, 0)
for _, subExecution := range subExecutions {
fileName := subExecution.StepExecutionContext.Get(fileItemWriterFileNameKey)
fd := w.fd
fd.FileName = fileName.(string)
subFiles = append(subFiles, fd)
}
fd := w.fd
fp := &FilePath{fd.FileName}
fileName, err := fp.Format(execution)
if err != nil {
return NewBatchError(ErrCodeGeneral, "get real file path:%v err", fd.FileName, err)
}
fd.FileName = fileName
err = w.merger.Merge(subFiles, fd)
if err != nil {
return NewBatchError(ErrCodeGeneral, "aggregate file:%v err", fd.FileName, err)
}
//generate file checksum
if fd.Checksum != "" {
checksumer := file.GetChecksumer(fd.Checksum)
if checksumer != nil {
err = checksumer.Checksum(fd)
if err != nil {
return NewBatchError(ErrCodeGeneral, "generate file checksum:%v err", fd, err)
}
}
}
}
return nil
}