-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRiiak.php
278 lines (247 loc) · 6.74 KB
/
Riiak.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
namespace riiak;
use \CApplicationComponent,
\CJSON,
\Yii;
/**
* The Riiak object holds information necessary to connect to
* Riak. The Riak API uses HTTP, so there is no persistent
* connection, and the Riiak object is extremely lightweight.
* @package riiak
*
* @property-read bool $isAlive
* @property-read bool $isMultiBackendSupport
* @property-read bool $isSecondaryIndexSupport
* @property-read array $configuration Riak server configuration
* @property-read \riiak\Transport $transport Riiak Transport layer
* @property-read \riiak\MapReduce $mapReduce Map/Reduce object
*/
class Riiak extends CApplicationComponent {
/**
* Hostname or IP address
*
* @var string Default: '127.0.0.1'
*/
public $host = '127.0.0.1';
/**
* Port number
*
* @var int Default: 8098
*/
public $port = 8098;
/**
* Whether SSL is enabled
*
* @var bool Default: false
*/
public $ssl = false;
/**
* Interface prefix
*
* @var string Default: 'riak'
*/
public $prefix = 'riak';
/**
* Riak key stream url prefix
*
* @var string Default: 'buckets'
*/
public $bucketPrefix = 'buckets';
/**
* Riak key for secondary indexes implementation
*
* @var string Default: 'index'
*/
public $indexPrefix = 'index';
/**
* Riak key stream prefix
*
* @var string
*/
public $keyPrefix = 'keys';
/**
* MapReduce prefix
*
* @var string Default: 'mapred'
*/
public $mapredPrefix = 'mapred';
public $pingPrefix = 'ping';
public $statsPrefix = 'stats';
/**
* The clientID for this Riak client instance.
* Only specify if you know what you're doing.
*
* @var string
*/
public $clientId;
/**
* R-Value setting for client.
* Used by other Riiak class components as fallback value.
*
* @var int Default: 2
*/
public $r = 2;
/**
* W-Value setting for client.
* Used by other Riiak class components as fallback value.
*
* @var int Default: 2
*/
public $w = 2;
/**
* DW-Value setting for client.
* Used by other Riiak class components as fallback value.
*
* @var int Default: 2
*/
public $dw = 2;
/**
* When enabled, profiles requests using Yii::beginProfile && Yii::endProfile
*
* @var bool
*/
public $enableProfiling = false;
/**
* @var \riiak\MapReduce
*/
protected $_mr;
/**
* Riak configuration details
*
* @var array
*/
protected $_config;
/**
* Transport layer object
*
* @var \riiak\Transport
*/
protected $_transport;
/**
* Define transport layer protocol
*
* @var string Default: http
*/
public $protocol = 'HTTP';
/**
* Define transport layer processing method.
*
* @var string Default: Curl
*/
public $protocolMethod = 'CURL';
/**
* Initialise Riiak
*/
public function init() {
parent::init();
/**
* Default the value of clientId if not already specified
*/
if (empty($this->clientId))
$this->clientId = 'php_' . base64_encode(rand(1, 1073741824));
}
/**
* Get bucket by name
*
* @param string $name
* @return \riiak\Bucket
*/
public function bucket($name) {
return new Bucket($this, $name);
}
/**
* Return array of Bucket objects
*
* @return array
*/
public function buckets() {
Yii::log('Bucket listing is a very intensive operation, and should never occur in production!', \CLogger::LEVEL_WARNING);
Yii::trace('Fetching list of buckets', 'ext.riiak.Riiak');
return array_map(array($this, 'bucket'), $this->transport->listBuckets());
}
/**
* Check if Riak server is alive
*
* @return bool
*/
public function getIsAlive() {
Yii::trace('Pinging Riak server', 'ext.riiak.Riiak');
return $this->transport->getIsAlive();
}
/**
* Returns the Transport instance (created if not exists)
*
* @return \riiak\Transport
*/
public function getTransport() {
if (!($this->_transport instanceof Transport)) {
switch ($this->protocol) {
default:
case 'HTTP':
/**
* Get processing method object for HTTP protocol.
*/
switch ($this->protocolMethod) {
default:
case 'CURL':
$this->_transport = new \riiak\transport\http\Curl($this);
break;
case 'FOPEN':
break;
case 'PHPSTREAM':
break;
}
break;
case 'PBC':
/**
* Protocol Buffer Transport layer class object.
*/
break;
}
}
return $this->_transport;
}
/**
* Returns riak server configuration
*
* @return array
*/
public function getConfiguration() {
if (!is_array($this->_config))
$this->_config = $this->getTransport()->getRiakConfiguration();
return $this->_config;
}
/**
* Returns the MapReduce instance (created if not exists)
*
* @param bool $reset Whether to create a new MapReduce instance
* @return \riiak\MapReduce
*/
public function getMapReduce($reset = false) {
if ($reset || !($this->_mr instanceof MapReduce))
$this->_mr = new MapReduce($this);
return $this->_mr;
}
/**
* Check whether riak supports multi-backend or not.
*
* @todo Find better solution for this
*
* @return bool
*/
public function getIsMultiBackendSupport() {
Yii::trace('Checking multi-backend support', 'ext.riiak.Riiak');
return $this->transport->getIsMultiBackendSupport();
}
/**
* Check whether riak supports secondary index or not.
*
* @todo Find better solution for this
*
* @return bool
*/
public function getIsSecondaryIndexSupport() {
Yii::trace('Checking Secondary Index support', 'ext.riiak.Riiak');
return $this->transport->getIsSecondaryIndexSupport();
}
}