-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathsync_message.go
More file actions
133 lines (111 loc) · 2.63 KB
/
sync_message.go
File metadata and controls
133 lines (111 loc) · 2.63 KB
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
package message
import (
"slices"
"github.com/apache/arrow-go/v18/arrow"
"github.com/cloudquery/plugin-sdk/v4/schema"
)
type syncBaseMessage struct {
}
func (*syncBaseMessage) IsSyncMessage() bool {
return true
}
type SyncMessage interface {
GetTable() *schema.Table
IsSyncMessage() bool
}
type SyncMigrateTable struct {
syncBaseMessage
Table *schema.Table
}
func (m SyncMigrateTable) GetTable() *schema.Table {
return m.Table
}
type SyncInsert struct {
syncBaseMessage
Record arrow.Record
}
func (m *SyncInsert) GetTable() *schema.Table {
table, err := schema.NewTableFromArrowSchema(m.Record.Schema())
if err != nil {
panic(err)
}
return table
}
type SyncMessages []SyncMessage
type SyncMigrateTables []*SyncMigrateTable
type SyncInserts []*SyncInsert
func (messages SyncMessages) InsertItems() int64 {
items := int64(0)
for _, msg := range messages {
if m, ok := msg.(*SyncInsert); ok {
items += m.Record.NumRows()
}
}
return items
}
func (messages SyncMessages) GetInserts() SyncInserts {
inserts := make(SyncInserts, 0, len(messages))
for _, msg := range messages {
if m, ok := msg.(*SyncInsert); ok {
inserts = append(inserts, m)
}
}
return slices.Clip(inserts)
}
func (m SyncMigrateTables) Exists(tableName string) bool {
for _, table := range m {
if table.Table.Name == tableName {
return true
}
}
return false
}
func (m SyncInserts) Exists(tableName string) bool {
for _, insert := range m {
md := insert.Record.Schema().Metadata()
tableNameMeta, ok := md.GetValue(schema.MetadataTableName)
if !ok {
continue
}
if tableNameMeta == tableName {
return true
}
}
return false
}
func (m SyncInserts) GetRecords() []arrow.Record {
res := make([]arrow.Record, len(m))
for i := range m {
res[i] = m[i].Record
}
return res
}
// Get all records for a single table
func (m SyncInserts) GetRecordsForTable(table *schema.Table) []arrow.Record {
res := make([]arrow.Record, 0, len(m))
for _, insert := range m {
md := insert.Record.Schema().Metadata()
tableNameMeta, ok := md.GetValue(schema.MetadataTableName)
if !ok || tableNameMeta != table.Name {
continue
}
res = append(res, insert.Record)
}
return slices.Clip(res)
}
type SyncDeleteRecord struct {
syncBaseMessage
// TODO: Instead of using this struct we should derive the DeletionKeys and parent/child relation from the schema.Table itself
DeleteRecord
}
func (m SyncDeleteRecord) GetTable() *schema.Table {
return &schema.Table{Name: m.TableName}
}
type SyncError struct {
syncBaseMessage
TableName string
Error string
}
func (e SyncError) GetTable() *schema.Table {
return &schema.Table{Name: e.TableName}
}