From fd8208683889e484523cf99e44d54728386c122c Mon Sep 17 00:00:00 2001 From: Robin Tang Date: Wed, 18 Sep 2024 19:33:38 -0700 Subject: [PATCH] Move where we hash. --- models/event/event.go | 25 +++++++++------------ models/event/event_test.go | 46 ++++++++------------------------------ 2 files changed, 20 insertions(+), 51 deletions(-) diff --git a/models/event/event.go b/models/event/event.go index 88833e144..2b8bf293b 100644 --- a/models/event/event.go +++ b/models/event/event.go @@ -35,6 +35,16 @@ type Event struct { mode config.Mode } +func hashData(data map[string]any, tc kafkalib.TopicConfig) map[string]any { + for _, columnToHash := range tc.ColumnsToHash { + if value, isOk := data[columnToHash]; isOk { + data[columnToHash] = cryptography.HashValue(value) + } + } + + return data +} + func ToMemoryEvent(event cdc.Event, pkMap map[string]any, tc kafkalib.TopicConfig, cfgMode config.Mode) (Event, error) { cols, err := event.GetColumns() if err != nil { @@ -84,7 +94,7 @@ func ToMemoryEvent(event cdc.Event, pkMap map[string]any, tc kafkalib.TopicConfi ExecutionTime: event.GetExecutionTime(), OptionalSchema: optionalSchema, Columns: cols, - Data: evtData, + Data: hashData(evtData, tc), Deleted: event.DeletePayload(), }, nil } @@ -135,16 +145,6 @@ func (e *Event) PrimaryKeyValue() string { return key } -func (e *Event) hashData(tc kafkalib.TopicConfig) { - for _, columnToHash := range tc.ColumnsToHash { - if value, isOk := e.Data[columnToHash]; isOk { - e.Data[columnToHash] = cryptography.HashValue(value) - } - } - - return -} - // Save will save the event into our in memory event // It will return (flush bool, flushReason string, err error) func (e *Event) Save(cfg config.Config, inMemDB *models.DatabaseData, tc kafkalib.TopicConfig, message artie.Message) (bool, string, error) { @@ -152,9 +152,6 @@ func (e *Event) Save(cfg config.Config, inMemDB *models.DatabaseData, tc kafkali return false, "", errors.New("event not valid") } - // Are there any columns that need to be hashed? - e.hashData(tc) - // Does the table exist? td := inMemDB.GetOrCreateTableData(e.Table) td.Lock() diff --git a/models/event/event_test.go b/models/event/event_test.go index f6761bd32..c15797ff5 100644 --- a/models/event/event_test.go +++ b/models/event/event_test.go @@ -52,54 +52,26 @@ func (e *EventsTestSuite) TestEvent_IsValid() { } } -func (e *EventsTestSuite) TestEvent_HashData() { +func (e *EventsTestSuite) TestHashData() { { // No columns to hash - event := Event{ - Data: map[string]any{ - "foo": "bar", - "abc": "def", - }, - } - - event.hashData(kafkalib.TopicConfig{}) - assert.Equal(e.T(), map[string]any{"foo": "bar", "abc": "def"}, event.Data) + data := hashData(map[string]any{"foo": "bar", "abc": "def"}, kafkalib.TopicConfig{}) + assert.Equal(e.T(), map[string]any{"foo": "bar", "abc": "def"}, data) } { // There's a column to hash, but the event does not have any data - event := Event{ - Data: map[string]any{ - "foo": "bar", - "abc": "def", - }, - } - - event.hashData(kafkalib.TopicConfig{ColumnsToHash: []string{"super duper"}}) - assert.Equal(e.T(), map[string]any{"foo": "bar", "abc": "def"}, event.Data) + data := hashData(map[string]any{"foo": "bar", "abc": "def"}, kafkalib.TopicConfig{ColumnsToHash: []string{"super duper"}}) + assert.Equal(e.T(), map[string]any{"foo": "bar", "abc": "def"}, data) } { // Hash the column foo (value is set) - event := Event{ - Data: map[string]any{ - "foo": "bar", - "abc": "def", - }, - } - - event.hashData(kafkalib.TopicConfig{ColumnsToHash: []string{"foo"}}) - assert.Equal(e.T(), map[string]any{"foo": "fcde2b2edba56bf408601fb721fe9b5c338d10ee429ea04fae5511b68fbf8fb9", "abc": "def"}, event.Data) + data := hashData(map[string]any{"foo": "bar", "abc": "def"}, kafkalib.TopicConfig{ColumnsToHash: []string{"foo"}}) + assert.Equal(e.T(), map[string]any{"foo": "fcde2b2edba56bf408601fb721fe9b5c338d10ee429ea04fae5511b68fbf8fb9", "abc": "def"}, data) } { // Hash the column foo (value is nil) - event := Event{ - Data: map[string]any{ - "foo": nil, - "abc": "def", - }, - } - - event.hashData(kafkalib.TopicConfig{ColumnsToHash: []string{"foo"}}) - assert.Equal(e.T(), map[string]any{"foo": nil, "abc": "def"}, event.Data) + data := hashData(map[string]any{"foo": nil, "abc": "def"}, kafkalib.TopicConfig{ColumnsToHash: []string{"foo"}}) + assert.Equal(e.T(), map[string]any{"foo": nil, "abc": "def"}, data) } }