Implement functions to work with saved data

main
Nikita Tokarchuk 2 years ago
parent 8c9aefd6c1
commit 2f7f597ab0
Signed by: mainnika
GPG Key ID: A595FB7E3E56911C
  1. 32
      nodesync.go

@ -1,6 +1,8 @@
package sync
import (
"path"
"github.com/go-zookeeper/zk"
)
@ -26,3 +28,33 @@ func New(zkconn *zk.Conn, rootPath string) (nodeSync *NodeSync, err error) {
return
}
// Fetch returns node data
func (s *NodeSync) Fetch(where string, key string) (current []byte, err error) {
workingPath := path.Join(s.rootPath, where, key)
current, _, err = s.Zk.Get(workingPath)
return
}
// FetchAndSet replaces zookeeper key with new value bytes and returns old if exists
func (s *NodeSync) FetchAndSet(where string, key string, value []byte) (old []byte, err error) {
workingPath := path.Join(s.rootPath, where, key)
err = s.createRecursively(workingPath, defaultAcl)
if err != nil {
return
}
var ver int32 = -1
old, stat, err := s.Zk.Get(workingPath)
if err == nil {
ver = stat.Version
}
_, err = s.Zk.Set(workingPath, value, ver)
return old, err
}
Loading…
Cancel
Save