mirror of
https://github.com/mainnika/nodesync.git
synced 2026-05-23 16:23:34 +00:00
Add helper function to sort children nodes by the sequenced postfix
This commit is contained in:
+26
@@ -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) {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user