Add helper function to sort children nodes by the sequenced postfix

main
Nikita Tokarchuk 10 months ago
parent 2f7f597ab0
commit df5dae77c6
Signed by: mainnika
GPG Key ID: A595FB7E3E56911C
  1. 26
      helpers.go
  2. 3
      nodesync.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
}

@ -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) {

Loading…
Cancel
Save