Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Give RowsEvent a method to inspect the underlying event type. #1016

Merged
12 changes: 12 additions & 0 deletions replication/row_event.go
Original file line number Diff line number Diff line change
@@ -1120,6 +1120,18 @@ func (e *RowsEvent) Decode(data []byte) error {
return e.DecodeData(pos, data)
}

func (e *RowsEvent) IsInsert() bool {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes currently RowsEvent has no public type information. You can know the type from EventHeader

Copy link
Contributor Author

@proton-lisandro-pin proton-lisandro-pin Mar 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed. The problem is mostly that once casted to RowsEvent, it is impossible to inspect an event type - this unnecessarily complicates code needing to handle rows events based on operation type.
It also means consumers need to deal with raw event types (f.ex. UPDATE_ROWS_EVENTv01 vs UPDATE_ROWS_EVENTv2) which should be kept an implementation detail.

The type data is available thou, we just need to cleanly make it public.

return e.eventType == WRITE_ROWS_EVENTv0 || e.eventType == WRITE_ROWS_EVENTv1 || e.eventType == WRITE_ROWS_EVENTv2 || e.eventType == MARIADB_WRITE_ROWS_COMPRESSED_EVENT_V1
}

func (e *RowsEvent) IsUpdate() bool {
return e.eventType == UPDATE_ROWS_EVENTv0 || e.eventType == UPDATE_ROWS_EVENTv1 || e.eventType == UPDATE_ROWS_EVENTv2 || e.eventType == MARIADB_UPDATE_ROWS_COMPRESSED_EVENT_V1
}

func (e *RowsEvent) IsDelete() bool {
return e.eventType == DELETE_ROWS_EVENTv0 || e.eventType == DELETE_ROWS_EVENTv1 || e.eventType == DELETE_ROWS_EVENTv2 || e.eventType == MARIADB_DELETE_ROWS_COMPRESSED_EVENT_V1
}

func isBitSet(bitmap []byte, i int) bool {
return bitmap[i>>3]&(1<<(uint(i)&7)) > 0
}
35 changes: 35 additions & 0 deletions replication/row_event_test.go
Original file line number Diff line number Diff line change
@@ -1176,6 +1176,41 @@ func TestRowsDataExtraData(t *testing.T) {
}
}

func TestRowsEventTypoe(t *testing.T) {
testcases := []struct {
eventType EventType
isInsert bool
isUpdate bool
isDelete bool
}{
{WRITE_ROWS_EVENTv0, true, false, false},
{WRITE_ROWS_EVENTv1, true, false, false},
{WRITE_ROWS_EVENTv2, true, false, false},
{MARIADB_WRITE_ROWS_COMPRESSED_EVENT_V1, true, false, false},
{UPDATE_ROWS_EVENTv0, false, true, false},
{UPDATE_ROWS_EVENTv1, false, true, false},
{UPDATE_ROWS_EVENTv2, false, true, false},
{MARIADB_UPDATE_ROWS_COMPRESSED_EVENT_V1, false, true, false},
{DELETE_ROWS_EVENTv0, false, false, true},
{DELETE_ROWS_EVENTv1, false, false, true},
{DELETE_ROWS_EVENTv2, false, false, true},
{MARIADB_DELETE_ROWS_COMPRESSED_EVENT_V1, false, false, true},

// Whoops, these are not rows events at all
{EXEC_LOAD_EVENT, false, false, false},
{HEARTBEAT_EVENT, false, false, false},
}

for _, tc := range testcases {
rev := new(RowsEvent)
rev.eventType = tc.eventType

require.Equal(t, tc.isInsert, rev.IsInsert())
require.Equal(t, tc.isUpdate, rev.IsUpdate())
require.Equal(t, tc.isDelete, rev.IsDelete())
}
}

func TestTableMapHelperMaps(t *testing.T) {
/*
CREATE TABLE `_types` (