From df5dae77c6adc00257da2648f85dadda316387b2 Mon Sep 17 00:00:00 2001 From: Nikita Tokarchuk Date: Wed, 13 Apr 2022 23:47:58 +0200 Subject: [PATCH] Add helper function to sort children nodes by the sequenced postfix --- helpers.go | 26 ++++++++++++++++++++++++++ nodesync.go | 3 +++ 2 files changed, 29 insertions(+) diff --git a/helpers.go b/helpers.go index 43cb45b..1c320fc 100644 --- a/helpers.go +++ b/helpers.go @@ -4,6 +4,8 @@ import ( "github.com/go-zookeeper/zk" "fmt" + "path" + "sort" "strings" ) @@ -46,3 +48,27 @@ func (s *NodeSync) createRecursively(path string, acl []zk.ACL) error { return nil } + +// sortChildren sorts sequenced zookeeper nodes by sequence id +// sequence id is a last part of node key and always consists of 10 digits +func sortChildren(children []string) []string { + + var sorted []string + for _, child := range children { + if len(child) <= sequenceLen { + continue + } + + sorted = append(sorted, child) + } + + sort.SliceStable(sorted, func(i, j int) bool { + + _, a := path.Split(sorted[i]) + _, b := path.Split(sorted[j]) + + return a[len(a)-sequenceLen:] < b[len(b)-sequenceLen:] + }) + + return sorted +} diff --git a/nodesync.go b/nodesync.go index 412ce60..f9932a0 100644 --- a/nodesync.go +++ b/nodesync.go @@ -16,6 +16,9 @@ type NodeSync struct { // Inside private environment keep everything open var defaultAcl = zk.WorldACL(zk.PermAll) +// The length of a postfix sequence, see http://zookeeper.apache.org/doc/current/zookeeperProgrammers.html#Sequence+Nodes+--+Unique+Naming +var sequenceLen = 10 + // New creates a new nodesync instance func New(zkconn *zk.Conn, rootPath string) (nodeSync *NodeSync, err error) {