This repository was archived by the owner on Apr 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathClient.php
More file actions
234 lines (197 loc) · 7.79 KB
/
Client.php
File metadata and controls
234 lines (197 loc) · 7.79 KB
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
<?php
// vim: fenc=utf-8:ft=php:ai:si:ts=4:sw=4:et:
/**
* Hashmark_Client
*
* @filesource
* @copyright Copyright (c) 2008-2011 David Smith
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* @package Hashmark
* @subpackage Base
* @version $Id$
*/
/**
* Interface for client applications.
*
* Enables scalar value reads/writes, sample writes, using scalar names as keys.
*
* @package Hashmark
* @subpackage Base
*/
class Hashmark_Client extends Hashmark_Module_DbDependent
{
/**
* @var boolean If true, incr()/decr() will create the named scalar
* if it doesn't exist.
*/
protected $_createScalarIfNotExists = false;
/**
* Set the value of a scalar identified by name.
*
* @param string $scalarName
* @param mixed $value Integers/floats OK if precision if PHP can accurately
* convert to string. If not, supply as string.
* @param boolean $newSample If true, a new sample partition row is inserted
* `scalars` is updated.
* @return boolean True on success.
* @throws Exception On query error or non-string $scalarName.
*/
public function set($scalarName, $value, $newSample = false)
{
if (!is_string($scalarName)) {
throw new Exception('Cannot look up scalar with a non-string name.',
HASHMARK_EXCEPTION_VALIDATION);
}
if ($newSample) {
$sql = "UPDATE {$this->_dbName}`scalars` "
. 'SET `value` = ?, '
. '`last_inline_change` = UTC_TIMESTAMP() '
. 'WHERE `name` = ?';
$stmt = $this->_db->query($sql, array($value, $scalarName));
if (!$stmt->rowCount()) {
return false;
}
unset($stmt);
$sql = 'INSERT INTO ~samples '
. '(`value`, `end`) '
. 'VALUES (?, UTC_TIMESTAMP())';
$scalarId = $this->getModule('Core')->getScalarIdByName($scalarName);
$partition = $this->getModule('Partition');
$stmt = $partition->queryCurrent($scalarId, $sql, array($value));
} else {
$sql = "UPDATE {$this->_dbName}`scalars` "
. 'SET `value` = ?, '
. '`last_inline_change` = UTC_TIMESTAMP() '
. 'WHERE `name` = ?';
$stmt = $this->_db->query($sql, array($value, $scalarName));
}
return (1 == $stmt->rowCount());
}
/**
* Get the value of a scalar identified by name or ID.
*
* @param mixed $scalarNameOrId Uses string/int type check.
* @return mixed Scalar value; null on miss.
* @throws Exception On query error or non-string $scalarName.
*/
public function get($scalarNameOrId)
{
if (is_string($scalarNameOrId)) {
$sql = 'SELECT `value` '
. "FROM {$this->_dbName}`scalars` "
. 'WHERE `name` = ? '
. 'LIMIT 1';
} else if (is_int($scalarNameOrId)) {
$sql = 'SELECT `value` '
. "FROM {$this->_dbName}`scalars` "
. 'WHERE `id` = ? '
. 'LIMIT 1';
} else {
throw new Exception('Cannot look up scalar with a non-string name.',
HASHMARK_EXCEPTION_VALIDATION);
}
$rows = $this->_db->fetchAll($sql, array($scalarNameOrId));
if (!$rows) {
return false;
}
return $rows[0]['value'];
}
/**
* Increment a scalar identified by name.
*
* @param string $name
* @param string $amount
* @param boolean $newSample If true, a new sample partition row is inserted
* `scalars` is updated.
* @return boolean True on success.
* @throws Exception On query error or non-string $scalarName.
*/
public function incr($scalarName, $amount = '1', $newSample = false)
{
if (!is_string($scalarName)) {
throw new Exception('Cannot look up scalar with a non-string name.',
HASHMARK_EXCEPTION_VALIDATION);
}
if (!preg_match('/[0-9.-]+/', $amount)) {
throw new Exception('Cannot increment/decrement a scalar an invalid amount.',
HASHMARK_EXCEPTION_VALIDATION);
}
if ($this->_createScalarIfNotExists) {
$core = $this->getModule('Core');
$scalarId = $core->getScalarIdByName($scalarName);
if (!$scalarId) {
$fields = array('name' => $scalarName, 'type' => 'decimal',
'value' => $amount,
'description' => 'Auto-created by client');
$scalarId = $core->createScalar($fields);
if (!$scalarId) {
throw new Exception("Scalar '{$scalarName}' was not auto-created",
HASHMARK_EXCEPTION_SQL);
}
if ($newSample) {
$sql = 'INSERT INTO ~samples '
. '(`value`, `end`) '
. "VALUES ({$amount}, UTC_TIMESTAMP())";
$partition = $this->getModule('Partition');
$partition->queryCurrent($scalarId, $sql);
}
return true;
}
}
$dbHelperConfig = Hashmark::getConfig('DbHelper');
$sum = 'CONVERT(`value`, DECIMAL'
. "({$dbHelperConfig['decimal_total_width']},{$dbHelperConfig['decimal_right_width']})) + "
. $this->escape($amount);
if ($newSample) {
$sql = "UPDATE {$this->_dbName}`scalars` "
. "SET `value` = {$sum}, "
. '`last_inline_change` = UTC_TIMESTAMP() '
. 'WHERE `name` = ? '
. 'AND `type` = "decimal"';
$stmt = $this->_db->query($sql, array($scalarName));
if (!$stmt->rowCount()) {
return false;
}
unset($stmt);
$currentScalarValue = "SELECT `value` FROM {$this->_dbName}`scalars` WHERE `name` = ? LIMIT 1";
$sql = 'INSERT INTO ~samples '
. '(`value`, `end`) '
. "VALUES (({$currentScalarValue}), UTC_TIMESTAMP())";
$scalarId = $this->getModule('Core')->getScalarIdByName($scalarName);
$partition = $this->getModule('Partition');
$stmt = $partition->queryCurrent($scalarId, $sql, array($scalarName));
} else {
$sql = "UPDATE {$this->_dbName}`scalars` "
. "SET `value` = {$sum}, "
. '`last_inline_change` = UTC_TIMESTAMP() '
. 'WHERE `name` = ? '
. 'AND `type` = "decimal"';
$stmt = $this->_db->query($sql, array($scalarName));
}
return (1 == $stmt->rowCount());
}
/**
* Decrement a scalar identified by name.
*
* @param string $name
* @param string $amount Unsigned.
* @param boolean $newSample If true, a new sample partition row is inserted
* `scalars` is updated.
* @return boolean True on success.
* @throws Exception On query error or non-string $scalarName.
*/
public function decr($scalarName, $amount = '1', $newSample = false)
{
return $this->incr($scalarName, "-({$amount})", $newSample);
}
/**
* Public write access to $_createScalarIfNotExists.
*
* @param boolean $newValue
* @return void
*/
public function createScalarIfNotExists($newValue)
{
$this->_createScalarIfNotExists = $newValue;
}
}