-
Notifications
You must be signed in to change notification settings - Fork 801
/
Copy pathrule.go
118 lines (95 loc) · 2.65 KB
/
rule.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
package river
import (
"strings"
"github.com/siddontang/go-mysql/schema"
)
// Rule is the rule for how to sync data from MySQL to ES.
// If you want to sync MySQL data into elasticsearch, you must set a rule to let use know how to do it.
// The mapping rule may thi: schema + table <-> index + document type.
// schema and table is for MySQL, index and document type is for Elasticsearch.
type Rule struct {
Schema string `toml:"schema"`
Table string `toml:"table"`
Index string `toml:"index"`
Type string `toml:"type"`
Parent string `toml:"parent"`
ID []string `toml:"id"`
// Default, a MySQL table field name is mapped to Elasticsearch field name.
// Sometimes, you want to use different name, e.g, the MySQL file name is title,
// but in Elasticsearch, you want to name it my_title.
FieldMapping map[string]string `toml:"field"`
// MySQL table information
TableInfo *schema.Table
//only MySQL fields in filter will be synced , default sync all fields
Filter []string `toml:"filter"`
// Elasticsearch pipeline
// To pre-process documents before indexing
Pipeline string `toml:"pipeline"`
// shield ddl command
ShieldDDL string `toml:"shield_ddl"`
}
func newDefaultRule(schema string, table string) *Rule {
r := new(Rule)
r.Schema = schema
r.Table = table
lowerTable := strings.ToLower(table)
r.Index = lowerTable
r.Type = lowerTable
r.FieldMapping = make(map[string]string)
return r
}
func (r *Rule) prepare() error {
if r.FieldMapping == nil {
r.FieldMapping = make(map[string]string)
}
if len(r.Index) == 0 {
r.Index = r.Table
}
if len(r.Type) == 0 {
r.Type = r.Index
}
// ES must use a lower-case Type
// Here we also use for Index
r.Index = strings.ToLower(r.Index)
r.Type = strings.ToLower(r.Type)
return nil
}
// CheckFilter checkers whether the field needs to be filtered.
func (r *Rule) CheckFilter(field string) bool {
if r.Filter == nil {
return true
}
for _, f := range r.Filter {
if f == field {
return true
}
}
return false
}
// CheckHasShieldCommands Check Has Shield commands
func (r *Rule) CheckHasShieldCommands() bool {
return len(r.ShieldDDL) > 0
}
// CheckSkipShieldCommand check continue command
func (r *Rule) CheckSkipShieldCommand(action string) bool {
if len(r.ShieldDDL) < 1 {
return false
}
// get command []string
shieldDDLs := r.getManyShieldCommands()
for _, v := range shieldDDLs {
// action == shieldDDL
if v == action {
return true
}
}
return false
}
func (r *Rule) getManyShieldCommands() []string {
// not have shield DDL command
if len(r.ShieldDDL) < 0 {
return []string{}
}
// command example delete,update
return strings.Split(r.ShieldDDL, ",")
}