-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathset.go
110 lines (86 loc) · 2.59 KB
/
set.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2018-2019 The vogo Authors. All rights reserved.
// author: wongoo
// since: 2018/12/27
//
package zkclient
import (
"github.com/samuel/go-zookeeper/zk"
"github.com/vogo/logger"
)
// SetValue set value in zookeeper
func (cli *Client) SetValue(path string, obj interface{}, codec Codec) error {
bytes, err := codec.Encode(obj)
if err != nil {
return err
}
return cli.SetRawValue(path, bytes)
}
// SetRawValue set raw value in zookeeper
func (cli *Client) SetRawValue(path string, bytes []byte) error {
logger.Debugf("zk set node [%s]", path)
if err := cli.EnsurePath(path); err != nil {
return err
}
if _, err := cli.Conn().Set(path, bytes, -1); err != nil {
return err
}
return nil
}
// SetString in zookeeper
func (cli *Client) SetString(path, s string) error {
return cli.SetRawValue(path, []byte(s))
}
// SetJSON in zookeeper
func (cli *Client) SetJSON(path string, obj interface{}) error {
return cli.SetValue(path, obj, jsonEncodeCodec)
}
// SetMapValue set map value in zookeeper
func (cli *Client) SetMapValue(path, key string, obj interface{}, codec Codec) error {
childPath := PathJoin(path, key)
bytes, err := codec.Encode(obj)
if err != nil {
return err
}
return cli.SetRawValue(childPath, bytes)
}
// SetMapStringValue in zookeeper
func (cli *Client) SetMapStringValue(path, key, s string) error {
return cli.SetRawValue(PathJoin(path, key), []byte(s))
}
// SetMapJSONValue in zookeeper
func (cli *Client) SetMapJSONValue(path, key string, obj interface{}) error {
return cli.SetMapValue(path, key, obj, jsonEncodeCodec)
}
// CreateTempRawValue create temp raw value in zookeeper
func (cli *Client) CreateTempRawValue(path string, bytes []byte) error {
_, err := cli.Conn().Create(path, bytes, zk.FlagEphemeral, zk.WorldACL(zk.PermAll))
return err
}
// SetTempRawValue set temp raw value in zookeeper
func (cli *Client) SetTempRawValue(path string, bytes []byte) error {
err := cli.CreateTempRawValue(path, bytes)
if err == nil {
return nil
}
if err != zk.ErrNodeExists {
return err
}
_, err = cli.Conn().Set(path, bytes, -1)
return err
}
// SetTempString in zookeeper
func (cli *Client) SetTempString(path, s string) error {
return cli.SetTempRawValue(path, []byte(s))
}
// SetTempValue set temp value in zookeeper
func (cli *Client) SetTempValue(path string, obj interface{}, codec Codec) error {
bytes, err := codec.Encode(obj)
if err != nil {
return err
}
return cli.SetTempRawValue(path, bytes)
}
// SetTempJSON in zookeeper
func (cli *Client) SetTempJSON(path string, obj interface{}) error {
return cli.SetTempValue(path, obj, jsonEncodeCodec)
}