forked from basho/riak-php-client
-
Notifications
You must be signed in to change notification settings - Fork 1
/
riak.php
2041 lines (1810 loc) · 57.2 KB
/
riak.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
This file is provided to you under the Apache License,
Version 2.0 (the "License"); you may not use this file
except in compliance with the License. You may obtain
a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
/**
* The Riak API for PHP allows you to connect to a Riak instance,
* create, modify, and delete Riak objects, add and remove links from
* Riak objects, run Javascript (and
* Erlang) based Map/Reduce operations, and run Linkwalking
* operations.
*
* See the unit_tests.php file for example usage.
*
* @author Rusty Klophaus (@rklophaus) (rusty@basho.com)
* @package RiakAPI
*/
/**
* The RiakClient object holds information necessary to connect to
* Riak. The Riak API uses HTTP, so there is no persistent
* connection, and the RiakClient object is extremely lightweight.
* @package RiakClient
*/
class RiakClient {
/**
* Construct a new RiakClient object.
* @param string $host - Hostname or IP address (default '127.0.0.1')
* @param int $port - Port number (default 8098)
* @param string $prefix - Interface prefix (default "riak")
* @param string $mapred_prefix - MapReduce prefix (default "mapred")
*/
function RiakClient($host='127.0.0.1', $port=8098, $prefix='riak', $mapred_prefix='mapred') {
$this->host = $host;
$this->port = $port;
$this->prefix = $prefix;
$this->mapred_prefix = $mapred_prefix;
$this->indexPrefix='buckets';
$this->clientid = 'php_' . base_convert(mt_rand(), 10, 36);
$this->r = 2;
$this->w = 2;
$this->dw = 2;
}
/**
* Get the R-value setting for this RiakClient. (default 2)
* @return integer
*/
function getR() {
return $this->r;
}
/**
* Set the R-value for this RiakClient. This value will be used
* for any calls to get(...) or getBinary(...) where where 1) no
* R-value is specified in the method call and 2) no R-value has
* been set in the RiakBucket.
* @param integer $r - The R value.
* @return $this
*/
function setR($r) {
$this->r = $r;
return $this;
}
/**
* Get the W-value setting for this RiakClient. (default 2)
* @return integer
*/
function getW() {
return $this->w;
}
/**
* Set the W-value for this RiakClient. See setR(...) for a
* description of how these values are used.
* @param integer $w - The W value.
* @return $this
*/
function setW($w) {
$this->w = $w;
return $this;
}
/**
* Get the DW-value for this ClientOBject. (default 2)
* @return integer
*/
function getDW() {
return $this->dw;
}
/**
* Set the DW-value for this RiakClient. See setR(...) for a
* description of how these values are used.
* @param integer $dw - The DW value.
* @return $this
*/
function setDW($dw) {
$this->dw = $dw;
return $this;
}
/**
* Get the clientID for this RiakClient.
* @return string
*/
function getClientID() {
return $this->clientid;
}
/**
* Set the clientID for this RiakClient. Should not be called
* unless you know what you are doing.
* @param string $clientID - The new clientID.
* @return $this
*/
function setClientID($clientid) {
$this->clientid = $clientid;
return $this;
}
/**
* Get the bucket by the specified name. Since buckets always exist,
* this will always return a RiakBucket.
* @return RiakBucket
*/
function bucket($name) {
return new RiakBucket($this, $name);
}
/**
* Get all buckets.
* @return array() of RiakBucket objects
*/
function buckets() {
$url = RiakUtils::buildRestPath($this);
$response = RiakUtils::httpRequest('GET', $url.'?buckets=true');
$response_obj = json_decode($response[1]);
$buckets = array();
foreach($response_obj->buckets as $name) {
$buckets[] = $this->bucket($name);
}
return $buckets;
}
/**
* Check if the Riak server for this RiakClient is alive.
* @return boolean
*/
function isAlive() {
$url = 'http://' . $this->host . ':' . $this->port . '/ping';
$response = RiakUtils::httpRequest('GET', $url);
return ($response != NULL) && ($response[1] == 'OK');
}
# MAP/REDUCE/LINK FUNCTIONS
/**
* Start assembling a Map/Reduce operation.
* @see RiakMapReduce::add()
* @return RiakMapReduce
*/
function add($params) {
$mr = new RiakMapReduce($this);
$args = func_get_args();
return call_user_func_array(array(&$mr, "add"), $args);
}
/**
* Start assembling a Map/Reduce operation. This command will
* return an error unless executed against a Riak Search cluster.
* @see RiakMapReduce::search()
* @return RiakMapReduce
*/
function search($params) {
$mr = new RiakMapReduce($this);
$args = func_get_args();
return call_user_func_array(array(&$mr, "search"), $args);
}
/**
* Start assembling a Map/Reduce operation.
* @see RiakMapReduce::link()
*/
function link($params) {
$mr = new RiakMapReduce($this);
$args = func_get_args();
return call_user_func_array(array(&$mr, "link"), $args);
}
/**
* Start assembling a Map/Reduce operation.
* @see RiakMapReduce::map()
*/
function map($params) {
$mr = new RiakMapReduce($this);
$args = func_get_args();
return call_user_func_array(array(&$mr, "map"), $args);
}
/**
* Start assembling a Map/Reduce operation.
* @see RiakMapReduce::reduce()
*/
function reduce($params) {
$mr = new RiakMapReduce($this);
$args = func_get_args();
return call_user_func_array(array(&$mr, "reduce"), $args);
}
}
/**
* The RiakMapReduce object allows you to build up and run a
* map/reduce operation on Riak.
* @package RiakMapReduce
*/
class RiakMapReduce {
/**
* Construct a Map/Reduce object.
* @param RiakClient $client - A RiakClient object.
* @return RiakMapReduce
*/
function RiakMapReduce($client) {
$this->client = $client;
$this->phases = array();
$this->inputs = array();
$this->input_mode = NULL;
$this->key_filters = array();
}
/**
* Add inputs to a map/reduce operation. This method takes three
* different forms, depending on the provided inputs. You can
* specify either a RiakObject, a string bucket name, or a bucket,
* key, and additional arg.
* @param mixed $arg1 - RiakObject or Bucket
* @param mixed $arg2 - Key or blank
* @param mixed $arg3 - Arg or blank
* @return RiakMapReduce
*/
function add($arg1, $arg2=NULL, $arg3=NULL) {
if (func_num_args() == 1) {
if ($arg1 instanceof RiakObject)
return $this->add_object($arg1);
else
return $this->add_bucket($arg1);
}
return $this->add_bucket_key_data($arg1, (string) $arg2, $arg3);
}
/**
* Private.
*/
private function add_object($obj) {
return $this->add_bucket_key_data($obj->bucket->name, $obj->key, NULL);
}
/**
* Private.
*/
private function add_bucket_key_data($bucket, $key, $data) {
if ($this->input_mode == "bucket")
throw new Exception("Already added a bucket, can't add an object.");
$this->inputs[] = array($bucket, $key, $data);
return $this;
}
/**
* Private.
* @return $this
*/
private function add_bucket($bucket) {
$this->input_mode = "bucket";
$this->inputs = $bucket;
return $this;
}
/**
* Begin a map/reduce operation using a Search. This command will
* return an error unless executed against a Riak Search cluster.
* @param string $bucket - The Bucket to search. @param string
* query - The Query to execute. (Lucene syntax.) @return
* RiakMapReduce
*/
function search($bucket, $query) {
$this->inputs = array("module"=>"riak_search", "function"=>"mapred_search", "arg"=>array($bucket, $query));
return $this;
}
/**
* Add a link phase to the map/reduce operation.
* @param string $bucket - Bucket name (default '_', which means all
* buckets)
* @param string $tag - Tag (default '_', which means all buckets)
* @param boolean $keep - Flag whether to keep results from this
* stage in the map/reduce. (default FALSE, unless this is the last
* step in the phase)
* @return $this
*/
function link($bucket='_', $tag='_', $keep=FALSE) {
$this->phases[] = new RiakLinkPhase($bucket, $tag, $keep);
return $this;
}
/**
* Add a map phase to the map/reduce operation.
* @param mixed $function - Either a named Javascript function (ie:
* "Riak.mapValues"), or an anonymous javascript function (ie:
* "function(...) { ... }" or an array ["erlang_module",
* "function"].
* @param array() $options - An optional associative array
* containing "language", "keep" flag, and/or "arg".
* @return $this
*/
function map($function, $options=array()) {
$language = is_array($function) ? "erlang" : "javascript";
$this->phases[] = new RiakMapReducePhase("map",
$function,
RiakUtils::get_value("language", $options, $language),
RiakUtils::get_value("keep", $options, FALSE),
RiakUtils::get_value("arg", $options, NULL));
return $this;
}
/**
* Add a reduce phase to the map/reduce operation.
* @param mixed $function - Either a named Javascript function (ie:
* "Riak.mapValues"), or an anonymous javascript function (ie:
* "function(...) { ... }" or an array ["erlang_module",
* "function"].
* @param array() $options - An optional associative array
* containing "language", "keep" flag, and/or "arg".
* @return $this
*/
function reduce($function, $options=array()) {
$language = is_array($function) ? "erlang" : "javascript";
$this->phases[] = new RiakMapReducePhase("reduce",
$function,
RiakUtils::get_value("language", $options, $language),
RiakUtils::get_value("keep", $options, FALSE),
RiakUtils::get_value("arg", $options, NULL));
return $this;
}
/**
* Add a key filter to the map/reduce operation. If there are already
* existing filters, an "and" condition will be used to combine them.
* Alias for key_filter_and
* @param array $filter - a key filter (ie:
* ->key_filter(
* array("tokenize", "-", 2),
* array("between", "20110601", "20110630")
* )
* @return $this
*/
function key_filter(array $filter /*. ,$filter .*/) {
$args = func_get_args();
array_unshift($args, 'and');
return call_user_func_array(array($this, 'key_filter_operator'), $args);
}
/**
* Add a key filter to the map/reduce operation. If there are already
* existing filters, an "and" condition will be used to combine them.
* @param array $filter - a key filter (ie:
* ->key_filter(
* array("tokenize", "-", 2),
* array("between", "20110601", "20110630")
* )
* @return $this
*/
function key_filter_and(array $filter) {
$args = func_get_args();
array_unshift($args, 'and');
return call_user_func_array(array($this, 'key_filter_operator'), $args);
}
/**
* Adds a key filter to the map/reduce operation. If there are already
* existing filters, an "or" condition will be used to combine with the
* existing filters.
* @param array $filter
* @return $this
*/
function key_filter_or(array $filter /*. ,$filter .*/) {
$args = func_get_args();
array_unshift($args, 'or');
return call_user_func_array(array($this, 'key_filter_operator'), $args);
}
/**
* Adds a key filter to the map/reduce operation. If there are already
* existing filters, the provided conditional operator will be used
* to combine with the existing filters.
* @param string $operator - Operator (usually "and" or "or")
* @param array $filter
* @return $this
*/
function key_filter_operator($operator, $filter /*. ,$filter .*/) {
$filters = func_get_args();
array_shift($filters);
if ($this->input_mode != 'bucket')
throw new Exception("Key filters can only be used in bucket mode");
if (count($this->key_filters) > 0) {
$this->key_filters = array(array(
$operator,
$this->key_filters,
$filters
));
} else {
$this->key_filters = $filters;
}
return $this;
}
/**
* Run the map/reduce operation. Returns an array of results, or an
* array of RiakLink objects if the last phase is a link phase.
* @param integer $timeout - Timeout in seconds.
* @return array()
*/
function run($timeout=NULL) {
$num_phases = count($this->phases);
$linkResultsFlag = FALSE;
# If there are no phases, then just echo the inputs back to the user.
if ($num_phases == 0) {
$this->reduce(array("riak_kv_mapreduce", "reduce_identity"));
$num_phases = 1;
$linkResultsFlag = TRUE;
}
# Convert all phases to associative arrays. Also,
# if none of the phases are accumulating, then set the last one to
# accumulate.
$keep_flag = FALSE;
$query = array();
for ($i = 0; $i < $num_phases; $i++) {
$phase = $this->phases[$i];
if ($i == ($num_phases - 1) && !$keep_flag)
$phase->keep = TRUE;
if ($phase->keep) $keep_flag = TRUE;
$query[] = $phase->to_array();
}
# Add key filters if applicable
if ($this->input_mode == 'bucket' && count($this->key_filters) > 0) {
$this->inputs = array(
'bucket' => $this->inputs,
'key_filters' => $this->key_filters
);
}
# Construct the job, optionally set the timeout...
$job = array("inputs"=>$this->inputs, "query"=>$query);
if ($timeout != NULL) $job["timeout"] = $timeout;
$content = json_encode($job);
# Do the request...
$url = "http://" . $this->client->host . ":" . $this->client->port . "/" . $this->client->mapred_prefix;
$response = RiakUtils::httpRequest('POST', $url, array(), $content);
$result = json_decode($response[1]);
# If the last phase is NOT a link phase, then return the result.
$linkResultsFlag |= (end($this->phases) instanceof RiakLinkPhase);
# If we don't need to link results, then just return.
if (!$linkResultsFlag) return $result;
# Otherwise, if the last phase IS a link phase, then convert the
# results to RiakLink objects.
$a = array();
foreach ($result as $r) {
$tag = isset($r[2]) ? $r[2] : null;
$link = new RiakLink($r[0], $r[1], $tag);
$link->client = $this->client;
$a[] = $link;
}
return $a;
}
}
/**
* The RiakMapReducePhase holds information about a Map phase or
* Reduce phase in a RiakMapReduce operation.
* @package RiakMapReducePhase
*/
class RiakMapReducePhase {
/**
* Construct a RiakMapReducePhase object.
* @param string $type - "map" or "reduce"
* @param mixed $function - string or array()
* @param string $language - "javascript" or "erlang"
* @param boolean $keep - True to return the output of this phase in
* the results.
* @param mixed $arg - Additional value to pass into the map or
* reduce function.
*/
function RiakMapReducePhase($type, $function, $language, $keep, $arg) {
$this->type = $type;
$this->language = $language;
$this->function = $function;
$this->keep = $keep;
$this->arg = $arg;
}
/**
* Convert the RiakMapReducePhase to an associative array. Used
* internally.
*/
function to_array() {
$stepdef = array("keep"=>$this->keep,
"language"=>$this->language,
"arg"=>$this->arg);
if ($this->language == "javascript" && is_array($this->function)) {
$stepdef["bucket"] = $this->function[0];
$stepdef["key"] = $this->function[1];
} else if ($this->language == "javascript" && is_string($this->function)) {
if (strpos($this->function, "{") == FALSE)
$stepdef["name"] = $this->function;
else
$stepdef["source"] = $this->function;
} else if ($this->language == "erlang" && is_array($this->function)) {
$stepdef["module"] = $this->function[0];
$stepdef["function"] = $this->function[1];
}
return array(($this->type)=>$stepdef);
}
}
/**
* The RiakLinkPhase object holds information about a Link phase in a
* map/reduce operation.
* @package RiakLinkPhase
*/
class RiakLinkPhase {
/**
* Construct a RiakLinkPhase object.
* @param string $bucket - The bucket name.
* @param string $tag - The tag.
* @param boolean $keep - True to return results of this phase.
*/
function RiakLinkPhase($bucket, $tag, $keep) {
$this->bucket = $bucket;
$this->tag = $tag;
$this->keep = $keep;
}
/**
* Convert the RiakLinkPhase to an associative array. Used
* internally.
*/
function to_array() {
$stepdef = array("bucket"=>$this->bucket,
"tag"=>$this->tag,
"keep"=>$this->keep);
return array("link"=>$stepdef);
}
}
/**
* The RiakLink object represents a link from one Riak object to
* another.
* @package RiakLink
*/
class RiakLink {
/**
* Construct a RiakLink object.
* @param string $bucket - The bucket name.
* @param string $key - The key.
* @param string $tag - The tag.
*/
function RiakLink($bucket, $key, $tag=NULL) {
$this->bucket = $bucket;
$this->key = $key;
$this->tag = $tag;
$this->client = NULL;
}
/**
* Retrieve the RiakObject to which this link points.
* @param integer $r - The R-value to use.
* @return RiakObject
*/
function get($r=NULL) {
return $this->client->bucket($this->bucket)->get($this->key, $r);
}
/**
* Retrieve the RiakObject to which this link points, as a binary.
* @param integer $r - The R-value to use.
* @return RiakObject
*/
function getBinary($r=NULL) {
return $this->client->bucket($this->bucket)->getBinary($this->key, $r);
}
/**
* Get the bucket name of this link.
* @return string
*/
function getBucket() {
return $this->bucket;
}
/**
* Set the bucket name of this link.
* @param string $name - The bucket name.
* @return $this
*/
function setBucket($name) {
$this->bucket = $bucket;
return $this;
}
/**
* Get the key of this link.
* @return string
*/
function getKey() {
return $this->key;
}
/**
* Set the key of this link.
* @param string $key - The key.
* @return $this
*/
function setKey($key) {
$this->key = $key;
return $this;
}
/**
* Get the tag of this link.
* @return string
*/
function getTag() {
if ($this->tag == null)
return $this->bucket;
else
return $this->tag;
}
/**
* Set the tag of this link.
* @param string $tag - The tag.
* @return $this
*/
function setTag($tag) {
$this->tag = $tag;
return $this;
}
/**
* Convert this RiakLink object to a link header string. Used internally.
*/
function toLinkHeader($client) {
$link = "</" .
$client->prefix . "/" .
urlencode($this->bucket) . "/" .
urlencode($this->key) . ">; riaktag=\"" .
urlencode($this->getTag()) . "\"";
return $link;
}
/**
* Return true if the links are equal.
* @param RiakLink $link - A RiakLink object.
* @return boolean
*/
function isEqual($link) {
$is_equal =
($this->bucket == $link->bucket) &&
($this->key == $link->key) &&
($this->getTag() == $link->getTag());
return $is_equal;
}
}
/**
* The RiakBucket object allows you to access and change information
* about a Riak bucket, and provides methods to create or retrieve
* objects within the bucket.
* @package RiakBucket
*/
class RiakBucket {
function RiakBucket($client, $name) {
$this->client = $client;
$this->name = $name;
$this->r = NULL;
$this->w = NULL;
$this->dw = NULL;
}
/**
* Get the bucket name.
*/
function getName() {
return $this->name;
}
/**
* Get the R-value for this bucket, if it is set, otherwise return
* the R-value for the client.
* @return integer
*/
function getR($r=NULL) {
if ($r != NULL) return $r;
if ($this->r != NULL) return $this->r;
return $this->client->getR();
}
/**
* Set the R-value for this bucket. get(...) and getBinary(...)
* operations that do not specify an R-value will use this value.
* @param integer $r - The new R-value.
* @return $this
*/
function setR($r) {
$this->r = $r;
return $this;
}
/**
* Get the W-value for this bucket, if it is set, otherwise return
* the W-value for the client.
* @return integer
*/
function getW($w) {
if ($w != NULL) return $w;
if ($this->w != NULL) return $this->w;
return $this->client->getW();
}
/**
* Set the W-value for this bucket. See setR(...) for more information.
* @param integer $w - The new W-value.
* @return $this
*/
function setW($w) {
$this->w = $w;
return $this;
}
/**
* Get the DW-value for this bucket, if it is set, otherwise return
* the DW-value for the client.
* @return integer
*/
function getDW($dw) {
if ($dw != NULL) return $dw;
if ($this->dw != NULL) return $this->dw;
return $this->client->getDW();
}
/**
* Set the DW-value for this bucket. See setR(...) for more information.
* @param integer $dw - The new DW-value
* @return $this
*/
function setDW($dw) {
$this->dw = $dw;
return $this;
}
/**
* Create a new Riak object that will be stored as JSON.
* @param string $key - Name of the key.
* @param object $data - The data to store. (default NULL)
* @return RiakObject
*/
function newObject($key, $data=NULL) {
$obj = new RiakObject($this->client, $this, $key);
$obj->setData($data);
$obj->setContentType('text/json');
$obj->jsonize = TRUE;
return $obj;
}
/**
* Create a new Riak object that will be stored as plain text/binary.
* @param string $key - Name of the key.
* @param object $data - The data to store.
* @param string $content_type - The content type of the object. (default 'text/json')
* @return RiakObject
*/
function newBinary($key, $data, $content_type='text/json') {
$obj = new RiakObject($this->client, $this, $key);
$obj->setData($data);
$obj->setContentType($content_type);
$obj->jsonize = FALSE;
return $obj;
}
/**
* Retrieve a JSON-encoded object from Riak.
* @param string $key - Name of the key.
* @param int $r - R-Value of the request (defaults to bucket's R)
* @return RiakObject
*/
function get($key, $r=NULL) {
$obj = new RiakObject($this->client, $this, $key);
$obj->jsonize = TRUE;
$r = $this->getR($r);
return $obj->reload($r);
}
/**
* Retrieve a binary/string object from Riak.
* @param string $key - Name of the key.
* @param int $r - R-Value of the request (defaults to bucket's R)
* @return RiakObject
*/
function getBinary($key, $r=NULL) {
$obj = new RiakObject($this->client, $this, $key);
$obj->jsonize = FALSE;
$r = $this->getR($r);
return $obj->reload($r);
}
/**
* Set the N-value for this bucket, which is the number of replicas
* that will be written of each object in the bucket. Set this once
* before you write any data to the bucket, and never change it
* again, otherwise unpredictable things could happen. This should
* only be used if you know what you are doing.
* @param integer $nval - The new N-Val.
*/
function setNVal($nval) {
return $this->setProperty("n_val", $nval);
}
/**
* Retrieve the N-value for this bucket.
* @return integer
*/
function getNVal() {
return $this->getProperty("n_val");
}
/**
* If set to true, then writes with conflicting data will be stored
* and returned to the client. This situation can be detected by
* calling hasSiblings() and getSiblings(). This should only be used
* if you know what you are doing.
* @param boolean $bool - True to store and return conflicting writes.
*/
function setAllowMultiples($bool) {
return $this->setProperty("allow_mult", $bool);
}
/**
* Retrieve the 'allow multiples' setting.
* @return Boolean
*/
function getAllowMultiples() {
return "true" == $this->getProperty("allow_mult");
}
/**
* Set a bucket property. This should only be used if you know what
* you are doing.
* @param string $key - Property to set.
* @param mixed $value - Property value.
*/
function setProperty($key, $value) {
return $this->setProperties(array($key=>$value));
}
/**
* Retrieve a bucket property.
* @param string $key - The property to retrieve.
* @return mixed
*/
function getProperty($key) {
$props = $this->getProperties();
if (array_key_exists($key, $props)) {
return $props[$key];
} else {
return NULL;
}
}
/**
* Set multiple bucket properties in one call. This should only be
* used if you know what you are doing.
* @param array $props - An associative array of $key=>$value.
*/
function setProperties($props) {
# Construct the URL, Headers, and Content...
$url = RiakUtils::buildRestPath($this->client, $this);
$headers = array('Content-Type: application/json');
$content = json_encode(array("props"=>$props));
# Run the request...
$response = RiakUtils::httpRequest('PUT', $url, $headers, $content);
# Handle the response...
if ($response == NULL) {
throw Exception("Error setting bucket properties.");
}
# Check the response value...
$status = $response[0]['http_code'];
if ($status != 204) {
throw Exception("Error setting bucket properties.");
}
}
/**
* Retrieve an associative array of all bucket properties.
* @return Array
*/
function getProperties() {
# Run the request...
$params = array('props' => 'true', 'keys' => 'false');
$url = RiakUtils::buildRestPath($this->client, $this, NULL, NULL, $params);
$response = RiakUtils::httpRequest('GET', $url);
# Use a RiakObject to interpret the response, we are just interested in the value.
$obj = new RiakObject($this->client, $this, NULL);
$obj->populate($response, array(200));
if (!$obj->exists()) {
throw Exception("Error getting bucket properties.");
}
$props = $obj->getData();
$props = $props["props"];
return $props;
}
/**
* Retrieve an array of all keys in this bucket.
* Note: this operation is pretty slow.
* @return Array
*/
function getKeys() {
$params = array('props'=>'false','keys'=>'true');
$url = RiakUtils::buildRestPath($this->client, $this, NULL, NULL, $params);
$response = RiakUtils::httpRequest('GET', $url);
# Use a RiakObject to interpret the response, we are just interested in the value.
$obj = new RiakObject($this->client, $this, NULL);
$obj->populate($response, array(200));
if (!$obj->exists()) {
throw Exception("Error getting bucket properties.");
}
$keys = $obj->getData();
return array_map("urldecode",$keys["keys"]);
}
/**
* Search a secondary index
* @author Eric Stevens <estevens@taglabsinc.com>
* @param string $indexName - The name of the index to search
* @param string $indexType - The type of index ('int' or 'bin')
* @param string|int $startOrExact
* @param string|int optional $end
* @param bool $dedupe - whether to eliminate duplicate entries if any
* @return array of RiakLinks
*/
function indexSearch($indexName, $indexType, $startOrExact, $end=NULL, $dedupe=false) {
$url = RiakUtils::buildIndexPath($this->client, $this, "{$indexName}_{$indexType}", $startOrExact, $end, NULL);
$response = RiakUtils::httpRequest('GET', $url);
$obj = new RiakObject($this->client, $this, NULL);
$obj->populate($response, array(200));
if (!$obj->exists()) {
throw Exception("Error searching index.");
}
$data = $obj->getData();
$keys = array_map("urldecode",$data["keys"]);
$seenKeys = array();