Add helper function to sort children nodes by the sequenced postfix

This commit is contained in:
2022-04-13 23:47:58 +02:00
parent 2f7f597ab0
commit df5dae77c6
2 changed files with 29 additions and 0 deletions
+26
View File
@@ -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
}
+3
View File
@@ -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) {