Skip to content

Commit

Permalink
Convert some pre-generics map.Keys/.Values to their generic version (#…
Browse files Browse the repository at this point in the history
…6575)

Very straightforward "replace custom code with stdlib".
Generics are great. I'm so glad we have them.
  • Loading branch information
Groxx authored Dec 23, 2024
1 parent 60ac355 commit e20443c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 199 deletions.
36 changes: 18 additions & 18 deletions service/history/execution/mutable_state_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1457,18 +1457,18 @@ func (e *mutableStateBuilder) CloseTransactionAsMutation(
ExecutionInfo: e.executionInfo,
VersionHistories: e.versionHistories,

UpsertActivityInfos: convertUpdateActivityInfos(e.updateActivityInfos),
DeleteActivityInfos: convertInt64SetToSlice(e.deleteActivityInfos),
UpsertTimerInfos: convertUpdateTimerInfos(e.updateTimerInfos),
DeleteTimerInfos: convertStringSetToSlice(e.deleteTimerInfos),
UpsertChildExecutionInfos: convertUpdateChildExecutionInfos(e.updateChildExecutionInfos),
DeleteChildExecutionInfos: convertInt64SetToSlice(e.deleteChildExecutionInfos),
UpsertRequestCancelInfos: convertUpdateRequestCancelInfos(e.updateRequestCancelInfos),
DeleteRequestCancelInfos: convertInt64SetToSlice(e.deleteRequestCancelInfos),
UpsertSignalInfos: convertUpdateSignalInfos(e.updateSignalInfos),
DeleteSignalInfos: convertInt64SetToSlice(e.deleteSignalInfos),
UpsertSignalRequestedIDs: convertStringSetToSlice(e.updateSignalRequestedIDs),
DeleteSignalRequestedIDs: convertStringSetToSlice(e.deleteSignalRequestedIDs),
UpsertActivityInfos: maps.Values(e.updateActivityInfos),
DeleteActivityInfos: maps.Keys(e.deleteActivityInfos),
UpsertTimerInfos: maps.Values(e.updateTimerInfos),
DeleteTimerInfos: maps.Keys(e.deleteTimerInfos),
UpsertChildExecutionInfos: maps.Values(e.updateChildExecutionInfos),
DeleteChildExecutionInfos: maps.Keys(e.deleteChildExecutionInfos),
UpsertRequestCancelInfos: maps.Values(e.updateRequestCancelInfos),
DeleteRequestCancelInfos: maps.Keys(e.deleteRequestCancelInfos),
UpsertSignalInfos: maps.Values(e.updateSignalInfos),
DeleteSignalInfos: maps.Keys(e.deleteSignalInfos),
UpsertSignalRequestedIDs: maps.Keys(e.updateSignalRequestedIDs),
DeleteSignalRequestedIDs: maps.Keys(e.deleteSignalRequestedIDs),
NewBufferedEvents: e.updateBufferedEvents,
ClearBufferedEvents: e.clearBufferedEvents,

Expand Down Expand Up @@ -1544,12 +1544,12 @@ func (e *mutableStateBuilder) CloseTransactionAsSnapshot(
ExecutionInfo: e.executionInfo,
VersionHistories: e.versionHistories,

ActivityInfos: convertPendingActivityInfos(e.pendingActivityInfoIDs),
TimerInfos: convertPendingTimerInfos(e.pendingTimerInfoIDs),
ChildExecutionInfos: convertPendingChildExecutionInfos(e.pendingChildExecutionInfoIDs),
RequestCancelInfos: convertPendingRequestCancelInfos(e.pendingRequestCancelInfoIDs),
SignalInfos: convertPendingSignalInfos(e.pendingSignalInfoIDs),
SignalRequestedIDs: convertStringSetToSlice(e.pendingSignalRequestedIDs),
ActivityInfos: maps.Values(e.pendingActivityInfoIDs),
TimerInfos: maps.Values(e.pendingTimerInfoIDs),
ChildExecutionInfos: maps.Values(e.pendingChildExecutionInfoIDs),
RequestCancelInfos: maps.Values(e.pendingRequestCancelInfoIDs),
SignalInfos: maps.Values(e.pendingSignalInfoIDs),
SignalRequestedIDs: maps.Keys(e.pendingSignalRequestedIDs),

TransferTasks: e.insertTransferTasks,
ReplicationTasks: e.insertReplicationTasks,
Expand Down
136 changes: 0 additions & 136 deletions service/history/execution/mutable_state_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,43 +50,6 @@ func (policy TransactionPolicy) Ptr() *TransactionPolicy {
return &policy
}

// NOTE: do not use make(type, len(input))
// since this will assume initial length being len(inputs)
// always use make(type, 0, len(input))

func convertPendingActivityInfos(
inputs map[int64]*persistence.ActivityInfo,
) []*persistence.ActivityInfo {

outputs := make([]*persistence.ActivityInfo, 0, len(inputs))
for _, item := range inputs {
outputs = append(outputs, item)
}
return outputs
}

func convertUpdateActivityInfos(
inputs map[int64]*persistence.ActivityInfo,
) []*persistence.ActivityInfo {

outputs := make([]*persistence.ActivityInfo, 0, len(inputs))
for _, item := range inputs {
outputs = append(outputs, item)
}
return outputs
}

func convertInt64SetToSlice(
inputs map[int64]struct{},
) []int64 {

outputs := make([]int64, 0, len(inputs))
for item := range inputs {
outputs = append(outputs, item)
}
return outputs
}

func convertSyncActivityInfos(
activityInfos map[int64]*persistence.ActivityInfo,
inputs map[int64]struct{},
Expand All @@ -107,105 +70,6 @@ func convertSyncActivityInfos(
return outputs
}

func convertPendingTimerInfos(
inputs map[string]*persistence.TimerInfo,
) []*persistence.TimerInfo {

outputs := make([]*persistence.TimerInfo, 0, len(inputs))
for _, item := range inputs {
outputs = append(outputs, item)
}
return outputs
}

func convertUpdateTimerInfos(
inputs map[string]*persistence.TimerInfo,
) []*persistence.TimerInfo {

outputs := make([]*persistence.TimerInfo, 0, len(inputs))
for _, item := range inputs {
outputs = append(outputs, item)
}
return outputs
}

func convertStringSetToSlice(
inputs map[string]struct{},
) []string {

outputs := make([]string, 0, len(inputs))
for item := range inputs {
outputs = append(outputs, item)
}
return outputs
}

func convertPendingChildExecutionInfos(
inputs map[int64]*persistence.ChildExecutionInfo,
) []*persistence.ChildExecutionInfo {

outputs := make([]*persistence.ChildExecutionInfo, 0, len(inputs))
for _, item := range inputs {
outputs = append(outputs, item)
}
return outputs
}

func convertUpdateChildExecutionInfos(
inputs map[int64]*persistence.ChildExecutionInfo,
) []*persistence.ChildExecutionInfo {

outputs := make([]*persistence.ChildExecutionInfo, 0, len(inputs))
for _, item := range inputs {
outputs = append(outputs, item)
}
return outputs
}

func convertPendingRequestCancelInfos(
inputs map[int64]*persistence.RequestCancelInfo,
) []*persistence.RequestCancelInfo {

outputs := make([]*persistence.RequestCancelInfo, 0, len(inputs))
for _, item := range inputs {
outputs = append(outputs, item)
}
return outputs
}

func convertUpdateRequestCancelInfos(
inputs map[int64]*persistence.RequestCancelInfo,
) []*persistence.RequestCancelInfo {

outputs := make([]*persistence.RequestCancelInfo, 0, len(inputs))
for _, item := range inputs {
outputs = append(outputs, item)
}
return outputs
}

func convertPendingSignalInfos(
inputs map[int64]*persistence.SignalInfo,
) []*persistence.SignalInfo {

outputs := make([]*persistence.SignalInfo, 0, len(inputs))
for _, item := range inputs {
outputs = append(outputs, item)
}
return outputs
}

func convertUpdateSignalInfos(
inputs map[int64]*persistence.SignalInfo,
) []*persistence.SignalInfo {

outputs := make([]*persistence.SignalInfo, 0, len(inputs))
for _, item := range inputs {
outputs = append(outputs, item)
}
return outputs
}

func convertWorkflowRequests(inputs map[persistence.WorkflowRequest]struct{}) []*persistence.WorkflowRequest {
outputs := make([]*persistence.WorkflowRequest, 0, len(inputs))
for key := range inputs {
Expand Down
45 changes: 0 additions & 45 deletions service/history/execution/mutable_state_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,51 +465,6 @@ func TestConvert(t *testing.T) {
assert.Equal(t, int64(1), outputs[0].(*persistence.SyncActivityTask).ScheduledID)
assert.Equal(t, int64(1), outputs[0].GetVersion())
})

t.Run("convertUpdateRequestCancelInfos", func(t *testing.T) {
key := int64(0)
inputs := map[int64]*persistence.RequestCancelInfo{key: {}}
outputs := convertUpdateRequestCancelInfos(inputs)
assert.NotNil(t, outputs)
assert.Equal(t, 1, len(outputs))
assert.Equal(t, inputs[key], outputs[0])
})

t.Run("convertPendingRequestCancelInfos", func(t *testing.T) {
key := int64(0)
inputs := map[int64]*persistence.RequestCancelInfo{key: {}}
outputs := convertPendingRequestCancelInfos(inputs)
assert.NotNil(t, outputs)
assert.Equal(t, 1, len(outputs))
assert.Equal(t, inputs[key], outputs[0])
})

t.Run("convertInt64SetToSlice", func(t *testing.T) {
key := int64(0)
inputs := map[int64]struct{}{key: {}}
outputs := convertInt64SetToSlice(inputs)
assert.NotNil(t, outputs)
assert.Equal(t, 1, len(outputs))
assert.Equal(t, key, outputs[0])
})

t.Run("convertUpdateChildExecutionInfos", func(t *testing.T) {
key := int64(0)
inputs := map[int64]*persistence.ChildExecutionInfo{key: {}}
outputs := convertUpdateChildExecutionInfos(inputs)
assert.NotNil(t, outputs)
assert.Equal(t, 1, len(outputs))
assert.Equal(t, inputs[key], outputs[0])
})

t.Run("convertUpdateSignalInfos", func(t *testing.T) {
key := int64(0)
inputs := map[int64]*persistence.SignalInfo{key: {}}
outputs := convertUpdateSignalInfos(inputs)
assert.NotNil(t, outputs)
assert.Equal(t, 1, len(outputs))
assert.Equal(t, inputs[key], outputs[0])
})
}

func TestScheduleDecision(t *testing.T) {
Expand Down

0 comments on commit e20443c

Please sign in to comment.