-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathitem_reader.go
150 lines (140 loc) · 4.92 KB
/
item_reader.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
package gobatch
import (
"context"
"fmt"
errors "github.com/pkg/errors"
"reflect"
)
const (
// Key for storing the list of item keys in StepContext
ItemReaderKeyList = "gobatch.ItemReader.key.list"
// Key for tracking the current position in the key list during step execution
ItemReaderCurrentIndex = "gobatch.ItemReader.current.index"
// Key for storing the total number of keys in the list
ItemReaderMaxIndex = "gobatch.ItemReader.max.index"
)
// ItemReader handles batch data loading from a data source (e.g. database) in chunk-oriented steps.
// The process involves two phases:
// 1. Initial loading of all data keys via ReadKeys()
// 2. Subsequent loading of individual items by key during chunk processing
type ItemReader interface {
// ReadKeys retrieves all keys for the target dataset
ReadKeys() ([]interface{}, error)
// ReadItem loads a single item using the provided key
ReadItem(key interface{}) (interface{}, error)
}
type defaultChunkReader struct {
itemReader ItemReader
}
func (reader *defaultChunkReader) Open(execution *StepExecution) BatchError {
stepCtx := execution.StepContext
executionCtx := execution.StepExecutionContext
keyList := stepCtx.Get(ItemReaderKeyList)
if keyList != nil {
kind := reflect.TypeOf(keyList).Kind()
if kind != reflect.Slice {
return NewBatchError(ErrCodeGeneral, "the type of key list in context must be slice, but the actual is: %v", kind)
}
} else {
keys, err := reader.itemReader.ReadKeys()
if err != nil {
return NewBatchError(ErrCodeGeneral, "ReadKeys() err", err)
}
stepCtx.Put(ItemReaderKeyList, keys)
executionCtx.Put(ItemReaderCurrentIndex, 0)
executionCtx.Put(ItemReaderMaxIndex, len(keys))
}
return nil
}
func (reader *defaultChunkReader) Read(chunkCtx *ChunkContext) (r interface{}, e BatchError) {
defer func() {
if err := recover(); err != nil {
e = NewBatchError(ErrCodeGeneral, "panic on Read() in item reader, err:%v", err)
}
}()
stepCtx := chunkCtx.StepExecution.StepContext
executionCtx := chunkCtx.StepExecution.StepExecutionContext
keyList := stepCtx.Get(ItemReaderKeyList)
currentIndex, _ := executionCtx.GetInt(ItemReaderCurrentIndex)
maxIndex, _ := executionCtx.GetInt(ItemReaderMaxIndex)
if currentIndex < maxIndex {
key := reflect.ValueOf(keyList).Index(currentIndex).Interface()
var item interface{}
var err error
if reader.itemReader != nil {
item, err = reader.itemReader.ReadItem(key)
} else {
err = errors.New("no ItemReader is specified")
}
if err != nil {
return nil, NewBatchError(ErrCodeGeneral, "read item error", err)
}
executionCtx.Put(ItemReaderCurrentIndex, currentIndex+1)
return item, nil
}
return nil, nil
}
func (reader *defaultChunkReader) Close(execution *StepExecution) BatchError {
return nil
}
func (reader *defaultChunkReader) GetPartitioner(minPartitionSize, maxPartitionSize uint) Partitioner {
return &defaultPartitioner{
itemReader: reader.itemReader,
minPartitionSize: minPartitionSize,
maxPartitionSize: maxPartitionSize,
}
}
type defaultPartitioner struct {
itemReader ItemReader
minPartitionSize uint
maxPartitionSize uint
}
func (p *defaultPartitioner) Partition(execution *StepExecution, partitions uint) (subExecutions []*StepExecution, e BatchError) {
defer func() {
if err := recover(); err != nil {
e = NewBatchError(ErrCodeGeneral, "panic on Partition in defaultPartitioner, err:%v", err)
}
}()
keys, err := p.itemReader.ReadKeys()
if err != nil {
return nil, NewBatchError(ErrCodeGeneral, "ReadKeys() err", err)
}
subExecutions = make([]*StepExecution, 0)
count := len(keys)
if count == 0 {
return subExecutions, nil
}
partitionSize := uint(count) / partitions
if partitionSize > p.maxPartitionSize {
partitionSize = p.maxPartitionSize
}
if partitionSize < p.minPartitionSize {
partitionSize = p.minPartitionSize
}
i := 0
for start, end := 0, int(partitionSize); start < count; start, end = end, end+int(partitionSize) {
if end > count {
end = count
}
partitionName := fmt.Sprintf("%s:%04d", execution.StepName, i)
partitionKeys := keys[start:end]
subExecution := execution.deepCopy()
subExecution.StepName = partitionName
subExecution.StepContextId = 0
subExecution.StepContext.Put(ItemReaderKeyList, partitionKeys)
subExecution.StepExecutionContext.Put(ItemReaderCurrentIndex, 0)
subExecution.StepExecutionContext.Put(ItemReaderMaxIndex, len(partitionKeys))
subExecutions = append(subExecutions, subExecution)
i++
}
logger.Info(context.Background(), "partition step:%v, total count:%v, partitions:%v, partitionSize:%v, subExecutions:%v", execution.StepName, count, partitions, partitionSize, len(subExecutions))
return subExecutions, nil
}
func (p *defaultPartitioner) GetPartitionNames(execution *StepExecution, partitions uint) []string {
names := make([]string, 0)
for i := uint(0); i < partitions; i++ {
partitionName := fmt.Sprintf("%s:%04d", execution.StepName, i)
names = append(names, partitionName)
}
return names
}