From d4f1723fc39b60a0ae056abf4103d0c9be6ffa77 Mon Sep 17 00:00:00 2001 From: Roman Khafizianov Date: Thu, 7 May 2020 19:07:52 +0300 Subject: [PATCH] jsonpatcher: sort events by ts in the reducer (#334) Signed-off-by: requilence --- jsonpatcher/jsonpatcher.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/jsonpatcher/jsonpatcher.go b/jsonpatcher/jsonpatcher.go index 2549ee3c..0fe5b410 100644 --- a/jsonpatcher/jsonpatcher.go +++ b/jsonpatcher/jsonpatcher.go @@ -5,6 +5,7 @@ import ( "encoding/binary" "errors" "fmt" + "sort" "time" jsonpatch "github.com/evanphx/json-patch" @@ -104,6 +105,17 @@ func (jp *jsonPatcher) Reduce( } defer txn.Discard() + sort.Slice(events, func(i, j int) bool { + ei, oki := events[i].(patchEvent) + ej, okj := events[j].(patchEvent) + + if !(oki && okj) { + return false + } + + return ei.Timestamp.Before(ej.Timestamp) + }) + actions := make([]core.ReduceAction, len(events)) for i, e := range events { je, ok := e.(patchEvent)