From ace6694c197a7acf0be34658d973e6e45ed62a9d Mon Sep 17 00:00:00 2001 From: Anton Nekipelov <226657+anton-107@users.noreply.github.com> Date: Thu, 8 May 2025 14:59:31 +0200 Subject: [PATCH 1/3] sort files when instantiating EventChanges --- libs/sync/event.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/libs/sync/event.go b/libs/sync/event.go index 643cdb39cc5..e5f41aaec14 100644 --- a/libs/sync/event.go +++ b/libs/sync/event.go @@ -48,6 +48,19 @@ type EventChanges struct { Delete []string `json:"delete,omitempty"` } +// creates EventChanges with filenames in Put and Delete sorted alphabetically +func newEventChanges(put, delete []string) *EventChanges { + putCopy := make([]string, len(put)) + copy(putCopy, put) + deleteCopy := make([]string, len(delete)) + copy(deleteCopy, delete) + + sort.Strings(putCopy) + sort.Strings(deleteCopy) + + return &EventChanges{Put: putCopy, Delete: deleteCopy} +} + func (e *EventChanges) IsEmpty() bool { return len(e.Put) == 0 && len(e.Delete) == 0 } @@ -55,11 +68,9 @@ func (e *EventChanges) IsEmpty() bool { func (e *EventChanges) String() string { var changes []string if len(e.Put) > 0 { - sort.Strings(e.Put) changes = append(changes, "PUT: "+strings.Join(e.Put, ", ")) } if len(e.Delete) > 0 { - sort.Strings(e.Delete) changes = append(changes, "DELETE: "+strings.Join(e.Delete, ", ")) } return strings.Join(changes, ", ") @@ -81,7 +92,7 @@ func (e *EventStart) String() string { func newEventStart(seq int, put, delete []string, dryRun bool) Event { return &EventStart{ EventBase: newEventBase(seq, EventTypeStart, dryRun), - EventChanges: &EventChanges{Put: put, Delete: delete}, + EventChanges: newEventChanges(put, delete), } } @@ -141,7 +152,7 @@ func (e *EventSyncComplete) String() string { func newEventComplete(seq int, put, delete []string, dryRun bool) Event { return &EventSyncComplete{ EventBase: newEventBase(seq, EventTypeComplete, dryRun), - EventChanges: &EventChanges{Put: put, Delete: delete}, + EventChanges: newEventChanges(put, delete), } } From 88d03b5d9d3a96e0fad5d28754478b27562e3343 Mon Sep 17 00:00:00 2001 From: Anton Nekipelov <226657+anton-107@users.noreply.github.com> Date: Thu, 8 May 2025 15:28:41 +0200 Subject: [PATCH 2/3] use slices.Clone instead of copy --- libs/sync/event.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/libs/sync/event.go b/libs/sync/event.go index e5f41aaec14..94e2022c5dd 100644 --- a/libs/sync/event.go +++ b/libs/sync/event.go @@ -3,6 +3,7 @@ package sync import ( "context" "fmt" + "slices" "sort" "strings" "time" @@ -50,10 +51,8 @@ type EventChanges struct { // creates EventChanges with filenames in Put and Delete sorted alphabetically func newEventChanges(put, delete []string) *EventChanges { - putCopy := make([]string, len(put)) - copy(putCopy, put) - deleteCopy := make([]string, len(delete)) - copy(deleteCopy, delete) + putCopy := slices.Clone(put) + deleteCopy := slices.Clone(delete) sort.Strings(putCopy) sort.Strings(deleteCopy) From 64cc4272ec62f66d64cca9027d176d2b1d929477 Mon Sep 17 00:00:00 2001 From: Anton Nekipelov <226657+anton-107@users.noreply.github.com> Date: Thu, 8 May 2025 15:54:37 +0200 Subject: [PATCH 3/3] use slices.Sorted instead of slices.Clone --- libs/sync/event.go | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/libs/sync/event.go b/libs/sync/event.go index 94e2022c5dd..3be6e74653c 100644 --- a/libs/sync/event.go +++ b/libs/sync/event.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "slices" - "sort" "strings" "time" ) @@ -51,13 +50,7 @@ type EventChanges struct { // creates EventChanges with filenames in Put and Delete sorted alphabetically func newEventChanges(put, delete []string) *EventChanges { - putCopy := slices.Clone(put) - deleteCopy := slices.Clone(delete) - - sort.Strings(putCopy) - sort.Strings(deleteCopy) - - return &EventChanges{Put: putCopy, Delete: deleteCopy} + return &EventChanges{Put: slices.Sorted(slices.Values(put)), Delete: slices.Sorted(slices.Values(delete))} } func (e *EventChanges) IsEmpty() bool {