This repository has been archived by the owner on May 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathaggregate.php
171 lines (156 loc) · 6.95 KB
/
aggregate.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
<?php
################################################################################
# Copyright 2013-2015 Aerospike, Inc.
#
# Licensed 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.
################################################################################
require_once(realpath(__DIR__ . '/../examples_util.php'));
function parse_args() {
$shortopts = "";
$shortopts .= "h::"; /* Optional host */
$shortopts .= "p::"; /* Optional port */
$shortopts .= "p::"; /* Optional port */
$shortopts .= "a"; /* Optionally annotate output with code */
$shortopts .= "c"; /* Optionally clean up before leaving */
$longopts = array(
"host::", /* Optional host */
"port::", /* Optional port */
"clean", /* Optionally clean up before leaving */
"annotate", /* Optionally annotate output with code */
"help", /* Usage */
);
$options = getopt($shortopts, $longopts);
return $options;
}
$args = parse_args();
if (isset($args["help"])) {
echo("php simple.php [-h<HOST IP ADDRESS>|--host=<HOST IP ADDRESS> -p<HOST PORT NUMBER>|--port=<HOST PORT NUMBER> -a|--annotate -c|--clean]\n");
exit(1);
}
$HOST_ADDR = (isset($args["h"])) ? (string) $args["h"] : ((isset($args["host"])) ? (string) $args["host"] : "localhost");
$HOST_PORT = (isset($args["p"])) ? (integer) $args["p"] : ((isset($args["port"])) ? (string) $args["port"] : 3000);
echo colorize("Connecting to the host ≻", 'black', true);
$start = __LINE__;
$config = array("hosts" => array(array("addr" => $HOST_ADDR, "port" => $HOST_PORT)));
$db = new Aerospike($config, false);
if (!$db->isConnected()) {
echo fail("Could not connect to host $HOST_ADDR:$HOST_PORT [{$db->errorno()}]: {$db->error()}");
exit(1);
}
echo success();
if (isset($args['a']) || isset($args['annotate'])) display_code(__FILE__, $start, __LINE__);
echo colorize("Registering the UDF module ≻", 'black', true);
$start = __LINE__;
$copied = copy(__DIR__.'/lua/example_aggregate_udf.lua', ini_get('aerospike.udf.lua_user_path').'/example_aggregate_udf.lua');
if (!$copied) {
echo fail("Could not copy the local lua/example_aggregate_udf.lua to ". ini_get('aerospike.udf.lua_user_path'));
}
$status = $db->register(ini_get('aerospike.udf.lua_user_path').'/example_aggregate_udf.lua', "example_aggregate_udf.lua");
if ($status == Aerospike::OK) {
echo success();
} elseif ($status == Aerospike::ERR_UDF_NOT_FOUND) {
echo fail("Could not find the udf file ".ini_get('aerospike.udf.lua_user_path').'/example_aggregate_udf.lua');
} else {
echo standard_fail($db);
}
if (isset($args['a']) || isset($args['annotate'])) display_code(__FILE__, $start, __LINE__);
// display the Lua code of the UDF module
$lua = file_get_contents(ini_get('aerospike.udf.lua_user_path').'/example_aggregate_udf.lua');
echo colorize($lua, 'purple', false);
echo colorize("Creating a secondary index on the 'age' bin of test.characters ≻", 'black', true);
$start = __LINE__;
$status = $db->addIndex("test", "characters", "age", "age_index", Aerospike::INDEX_TYPE_DEFAULT, Aerospike::INDEX_NUMERIC);
if ($status == Aerospike::OK) {
echo success();
} else {
echo standard_fail($db);
}
if (isset($args['a']) || isset($args['annotate'])) display_code(__FILE__, $start, __LINE__);
echo colorize("Writing records in test.characters with PKs=4-10 ≻", 'black', true);
$start = __LINE__;
$characters = array("Dr. Perceptron" => array('HAL Institute', 162),
"Philip J Fry" => array('Planet Express', 40),
"Tarunga Leela" => array('Planet Express', 39),
"Dr. Amy Wong" => array('Planet Express', 34),
"Prof. Hubert J Farnsworth" => array('Planet Express', 173),
"Bender Bending Rodriguez" => array('Planet Express', 1055));
$i = 4;
foreach ($characters as $name => $info) {
$key = $db->initKey("test", "characters", $i++);
$put_vals = array("name" => $name, "workplace" => $info[0], "age" => $info[1]);
$status = $db->put($key, $put_vals);
if ($status == Aerospike::OK) {
echo success();
} else {
echo standard_fail($db);
}
}
if (isset($args['a']) || isset($args['annotate'])) display_code(__FILE__, $start, __LINE__);
echo colorize("Applying a stream UDF to records with ages between 0 and 200 using aggregate()≻", 'black', true);
$start = __LINE__;
$where = $db->predicateBetween("age", 0, 200);
$status = $db->aggregate("test", "characters", $where, "example_aggregate_udf", "group_count", array("workplace"), $result);
if($status !== Aerospike::OK) {
echo standard_fail($db);
} else {
echo success();
echo "Counts of characters in their first two centuries, grouped by workplace:\n";
var_dump($result);
}
if (isset($args['a']) || isset($args['annotate'])) display_code(__FILE__, $start, __LINE__);
echo colorize("Showing characters WHERE ages between 20 and 200 AND workplace='Planet Express' ≻", 'black', true);
$start = __LINE__;
$where = $db->predicateBetween("age", 20, 200);
$status = $db->aggregate("test", "characters", $where, "example_aggregate_udf", "ages", array('workplace', 'Planet Express'), $result);
if($status !== Aerospike::OK) {
echo standard_fail($db);
} else {
echo success();
echo "The characters expressed as name => age pairs:\n";
var_dump($result);
}
if (isset($args['a']) || isset($args['annotate'])) display_code(__FILE__, $start, __LINE__);
if (isset($args['a']) || isset($args['clean'])) {
$start = __LINE__;
echo colorize("Removing the records ≻", 'black', true);
for ($i = 4; $i <= 10; $i++) {
$key = $db->initKey("test", "characters", $i);
$db->remove($key);
}
if ($status == Aerospike::OK) {
echo success();
} else {
echo standard_fail($db);
}
if (isset($args['a']) || isset($args['annotate'])) display_code(__FILE__, $start, __LINE__);
$start = __LINE__;
echo colorize("Dropping the index ≻", 'black', true);
$status = $db->dropIndex("test", "age_index");
if ($status == Aerospike::OK) {
echo success();
} else {
echo standard_fail($db);
}
if (isset($args['a']) || isset($args['annotate'])) display_code(__FILE__, $start, __LINE__);
echo colorize("De-Registering the UDF module ≻", 'black', true);
$start = __LINE__;
$status = $db->deregister("example_aggregate_udf.lua");
if ($status == Aerospike::OK) {
echo success();
} else {
echo standard_fail($db);
}
if (isset($args['a']) || isset($args['annotate'])) display_code(__FILE__, $start, __LINE__);
}
$db->close();
?>