-
Notifications
You must be signed in to change notification settings - Fork 499
/
data_convertor.go
138 lines (120 loc) · 3.98 KB
/
data_convertor.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
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package helper
import (
"database/sql"
"fmt"
"reflect"
"github.com/apache/incubator-devlake/models/common"
"github.com/apache/incubator-devlake/plugins/core"
)
// Accept row from source cursor, return list of entities that need to be stored
type DataConvertHandler func(row interface{}) ([]interface{}, error)
type DataConverterArgs struct {
RawDataSubTaskArgs
// Domain layer entity Id prefix, i.e. `jira:JiraIssue:1`, `github:GithubIssue`
InputRowType reflect.Type
// Cursor to a set of Tool Layer Records
Input *sql.Rows
Convert DataConvertHandler
BatchSize int
}
// DataConverter helps you convert Data from Tool Layer Tables to Domain Layer Tables
// It reads rows from specified Iterator, and feed it into `Convter` handler
// you can return arbitrary domain layer entities from this handler, ApiConverter would
// first delete old data by their RawDataOrigin information, and then perform a
// batch save operation for you.
type DataConverter struct {
*RawDataSubTask
args *DataConverterArgs
}
func NewDataConverter(args DataConverterArgs) (*DataConverter, error) {
rawDataSubTask, err := newRawDataSubTask(args.RawDataSubTaskArgs)
if err != nil {
return nil, err
}
// process args
if args.BatchSize == 0 {
args.BatchSize = 500
}
return &DataConverter{
RawDataSubTask: rawDataSubTask,
args: &args,
}, nil
}
func (converter *DataConverter) Execute() error {
// load data from database
db := converter.args.Ctx.GetDb()
// batch insertion divider
RAW_DATA_ORIGIN := "RawDataOrigin"
divider := NewBatchSaveDivider(db, converter.args.BatchSize)
divider.OnNewBatchSave(func(rowType reflect.Type) error {
// check if row type has RawDataOrigin
if rawDataOrigin, ok := rowType.Elem().FieldByName(RAW_DATA_ORIGIN); ok {
if (rawDataOrigin.Type != reflect.TypeOf(common.RawDataOrigin{})) {
return fmt.Errorf("type %s must nested RawDataOrigin struct", rowType.Name())
}
} else {
return fmt.Errorf("type %s must nested RawDataOrigin struct", rowType.Name())
}
// delete old data
return db.Delete(
reflect.New(rowType).Interface(),
"_raw_data_table = ? AND _raw_data_params = ?",
converter.table, converter.params,
).Error
})
// prgress
converter.args.Ctx.SetProgress(0, -1)
cursor := converter.args.Input
defer cursor.Close()
ctx := converter.args.Ctx.GetContext()
// iterate all rows
for cursor.Next() {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
inputRow := reflect.New(converter.args.InputRowType).Interface()
err := db.ScanRows(cursor, inputRow)
if err != nil {
return err
}
results, err := converter.args.Convert(inputRow)
if err != nil {
return err
}
for _, result := range results {
// get the batch operator for the specific type
batch, err := divider.ForType(reflect.TypeOf(result))
if err != nil {
return err
}
// set raw data origin field
reflect.ValueOf(result).Elem().FieldByName(RAW_DATA_ORIGIN).
Set(reflect.ValueOf(inputRow).Elem().FieldByName(RAW_DATA_ORIGIN))
// records get saved into db when slots were max outed
err = batch.Add(result)
if err != nil {
return err
}
}
converter.args.Ctx.IncProgress(1)
}
// save the last batches
return divider.Close()
}
var _ core.SubTask = (*DataConverter)(nil)