-
Notifications
You must be signed in to change notification settings - Fork 3
/
ZookeeperClient.php
142 lines (134 loc) · 3.45 KB
/
ZookeeperClient.php
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
/**
* PHP Zookeeper
*
* PHP Version 5.3
*
* The PHP License, version 3.01
*
* @category Libraries
* @package PHP-Zookeeper
* @author Lorenzo Alberton <l.alberton@quipo.it>
* @copyright 2012 PHP Group
* @license http://www.php.net/license The PHP License, version 3.01
* @link https://github.com/andreiz/php-zookeeper
*/
/**
* Example interaction with the PHP Zookeeper extension
*
* @category Libraries
* @package PHP-Zookeeper
* @author Lorenzo Alberton <l.alberton@quipo.it>
* @copyright 2012 PHP Group
* @license http://www.php.net/license The PHP License, version 3.01
* @link https://github.com/andreiz/php-zookeeper
*/
class ZookeeperClient
{
/**
* @var Zookeeper
*/
private $zookeeper ;
/**
* Constructor
*
* @param string $address CSV list of host:port values (e.g. "host1:2181,host2:2181")
*/
public function __construct($address) {
$this->zookeeper = new Zookeeper($address);
}
/**
* Set a node to a value. If the node doesn't exist yet, it is created.
* Existing values of the node are overwritten
*
* @param string $path The path to the node
* @param mixed $value The new value for the node
*
* @return mixed previous value if set, or null
*/
public function set($path, $value) {
if (!$this->zookeeper->exists($path)) {
$this->makePath($path);
$this->makeNode($path, $value);
} else {
$this->zookeeper->set($path, $value);
}
}
/**
* Equivalent of "mkdir -p" on ZooKeeper
*
* @param string $path The path to the node
* @param string $value The value to assign to each new node along the path
*
* @return bool
*/
public function makePath($path, $value = '') {
$parts = explode('/', $path);
$parts = array_filter($parts);
$subpath = '';
while (count($parts) > 1) {
$subpath .= '/' . array_shift($parts);
if (!$this->zookeeper->exists($subpath)) {
$this->makeNode($subpath, $value);
}
}
}
/**
* Create a node on ZooKeeper at the given path
*
* @param string $path The path to the node
* @param string $value The value to assign to the new node
* @param array $params Optional parameters for the Zookeeper node.
* By default, a public node is created
*
* @return string the path to the newly created node or null on failure
*/
public function makeNode($path, $value, array $params = array()) {
if (empty($params)) {
$params = array(
array(
'perms' => Zookeeper::PERM_ALL,
'scheme' => 'world',
'id' => 'anyone',
)
);
}
return $this->zookeeper->create($path, $value, $params);
}
/**
* Get the value for the node
*
* @param string $path the path to the node
*
* @return string|null
*/
public function get($path) {
if (!$this->zookeeper->exists($path)) {
return null;
}
return $this->zookeeper->get($path);
}
/**
* List the children of the given path, i.e. the name of the directories
* within the current node, if any
*
* @param string $path the path to the node
*
* @return array the subpaths within the given node
*/
public function getChildren($path) {
if (strlen($path) > 1 && preg_match('@/$@', $path)) {
// remove trailing /
$path = substr($path, 0, -1);
}
return $this->zookeeper->getChildren($path);
}
/**
* Remove the zk node
* @param [string] $nodePath [the path for node]
* @return [type] [description]
*/
public function removeNode($nodePath){
return $this->zookeeper->delete($nodePath);
}
}