From 2f7f597ab07234a4e18e30677347631f6319a338 Mon Sep 17 00:00:00 2001 From: Nikita Tokarchuk Date: Wed, 13 Apr 2022 23:46:51 +0200 Subject: [PATCH] Implement functions to work with saved data --- nodesync.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/nodesync.go b/nodesync.go index 680ab67..412ce60 100644 --- a/nodesync.go +++ b/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 +} \ No newline at end of file