57
45
$this->default_db_group = $this->req->props['db_group'];
59
47
$this->default_db_group = false;
61
49
if (($this->default_db_server !== false)&&($this->default_db_name !== false)&&($this->default_db_group !== false)) {
62
50
$this->default_postfix = $this->GetCachePostfix();
65
$this->default_postfix = false;
52
$this->default_postfix = false;
56
static function ConnectDB() {
59
$dbh = mysql_connect($ADEI_DB['host'] . ($ADEI_DB['port']?(":" . $ADEI_DB['port']):""), $ADEI_DB['user'], $ADEI_DB['password']);
60
if (!$dbh) throw new ADEIException(translate("Connection to the Caching MySQL Server is failed"));
62
mysql_query("SET time_zone = '+0:00'");
64
if (!@mysql_select_db($ADEI_DB['database'], $dbh)) {
66
if (mysql_errno($dbh) == CACHEDB::MYSQL_ER_BAD_DB_ERROR) {
67
if (mysql_query("CREATE DATABASE " . $ADEI_DB['database'], $dbh)) {
68
if (!mysql_select_db($ADEI_DB['database'], $dbh))
69
throw new ADEIException(translate("Connection to the Caching MySQL Database is failed") . " (" . mysql_error($dbh) . ")");
71
throw new ADEIException(translate("Creation of the caching MySQL database is failed") . " (" . mysql_error($dbh) . ")");
75
throw new ADEIException(translate("Connection to the Caching MySQL Database is failed") . " (" . mysql_error($dbh) . ")");
81
function ParsePostfix($postfix, $prefix="", $allow_incomplete = false) {
82
if (preg_match("/^$prefix(__md5_(.*))$/", $postfix, $m)) {
88
$mres = @mysql_query("SELECT `postfix` FROM md5 WHERE hash = '" . $postfix . "'", $this->dbh);
89
if ($mres) $mrow = mysql_fetch_row($mres);
92
if (($mrow)&&(preg_match("/^__(.*)__(.*)__(.*)$/", $mrow[0], $m))) {
96
} else if ($allow_incomplete) {
101
throw new ADEIException(translate("Supplied MD5 postfix (%s) is not listed in md5 table", $postfix));
105
} else if (preg_match("/^$prefix(__(.*)__(.*)__(.*))$/", $postfix, $m)) {
113
} else if (preg_match("/^$prefix(__.*)$/", $postfix, $m)) {
114
if ($allow_incomplete) {
122
throw new ADEIException(translate("Unsupported postfix (%s) is supplied", $postfix));
126
throw new ADEIException(translate("Unsupported postfix is specified: %s", $string));
128
throw new ADEIException(translate("Postfix should be specified"));
133
'postfix' => $postfix,
137
'db_group' => $group,
138
'supported' => $supported
142
function CreateServerRequest(&$postfix_or_info) {
143
if (is_array($postfix_or_info))
144
$info = &$postfix_or_info;
145
else if ($postfix_or_info)
146
$info = $this->ParsePostfix($postfix_or_info);
148
$info = $this->ParsePostfix($this->default_postfix);
150
return new SERVERRequest($req = array(
151
'db_server' => $info['db_server']
156
function CreateGroupRequest(&$postfix_or_info) {
157
if (is_array($postfix_or_info))
158
$info = &$postfix_or_info;
159
else if ($postfix_or_info)
160
$info = $this->ParsePostfix($postfix_or_info);
162
$info = $this->ParsePostfix($this->default_postfix);
164
return new GROUPRequest($req = array(
165
'db_server' => $info['db_server'],
166
'db_name' => $info['db_name'],
167
'db_group' => $info['db_group']
171
function GetExtendedCacheInfo(&$postfix_or_row, $flags = 0) {
174
if (is_array($postfix_or_row)) {
176
$string = $postfix_or_row[0];
177
} else if ($postfix_or_row) {
179
$string = $postfix_or_row;
182
$string = $this->default_postfix;
185
$info = $this->ParsePostfix($string, $prefix, true);
187
$postfix = $info['postfix'];
188
$srv = $info['db_server'];
189
$db = $info['db_name'];
190
$group = $info['db_group'];
191
$supported = $info['supported'];
194
if ($flags&CACHE::NEED_REQUESTS) {
197
$req = $this->CreateGroupRequest($info);
199
$info['server'] = $READER_DB[$srv]['title'];
202
$reader = $req->CreateReader();
203
} catch (ADEIException $ae) {
204
$sreq = $this->CreateServerRequest($info);
205
$reader = $sreq->CreateReader();
208
$info['reader'] = get_class($reader);
210
if ($reader->server['disconnected'])
211
$info['disconnected'] = true;
213
$db_list = $reader->GetDatabaseList();
214
if (isset($db_list[$db])) {
215
$info['database'] = $db_list[$db]['name'];
218
$group_list = $reader->GetGroupList();
219
if (isset($group_list[$group])) {
220
$info['group'] = $group_list[$group]['name'];
229
} catch (ADEIException $ae) {
230
if ($info['server']) $info['disconnected'] = true;
238
if (($flags&CACHEDB::TABLE_INFO)&&($supported)) {
239
$info['info'] = $this->GetCacheInfo($postfix);
240
if (is_array($postfix_or_row)) {
241
$info['info']['records'] = $postfix_or_row[4];
242
$info['info']['dbsize'] = $postfix_or_row[6] + $postfix_or_row[8];
247
$mres = @mysql_query("SHOW TABLE STATUS LIKE 'cache%$postfix'", $this->dbh);
250
while ($mrow = mysql_fetch_row($mres)) {
251
$size += $mrow[6] + $mrow[8];
252
if (preg_match("/^cache(\d+)$postfix$/", $mrow[0], $m)) {
255
if (!$id) $info['info']['records'] = $mrow[4];
257
$tables[$id] = $this->GetTableInfo($id, $postfix);
258
if (!is_array($tables[$id])) $tables[$id] = array();
260
$tables[$id]['resolution'] = $id;
261
$tables[$id]['records'] = $mrow[4];
262
$tables[$id]['dbsize'] = $mrow[6] + $mrow[8];
268
$info['info']['dbsize'] = $size;
273
$info['info']['tables'] = $tables;
275
$info['info']['tables'] = array(
282
if (($flags&CACHEDB::ITEM_INFO)&&($supported)) {
283
$mres = mysql_query("SHOW COLUMNS FROM `cache0$postfix`", $this->dbh);
285
$mrow = mysql_fetch_row($mres);
286
if ($mrow[0] == 'time') {
287
$mrow = mysql_fetch_row($mres);
288
if ($mrow[0] == 'ns') {
289
$info['info']['ns'] = true;
293
$info['info']['ns'] = false;
298
while ($mrow = mysql_fetch_array($mres)) $lastrow = $mrow[0];
299
if (preg_match("/(\d+)/", $lastrow, $m)) {
300
$info['info']['width'] = $m[1] + 1;
302
$info['info']['width'] = 0;
306
$mrow = mysql_fetch_row($mres);
307
if ($mrow[0] != 'subcache') $item_number++;
309
while ($mrow = mysql_fetch_row($mres)) {
310
if ($mrow[0]) $item_number++;
312
$info['info']['width'] = $item_number;
316
# echo $postfix . " - " . $info['info']['width'] . "<br/>";
320
$info['info']['items'] = $reader->GetItemList();
327
function GetCacheList($flags = 0) {
330
if ($flags&CACHEDB::TABLE_INFO)
331
$res = @mysql_query("SHOW TABLE STATUS LIKE 'cache0\\_\\_%'", $this->dbh);
333
$res = @mysql_query("SHOW TABLES LIKE 'cache0\\_\\_%'", $this->dbh);
335
$postfixes = array();
337
while ($row = mysql_fetch_row($res)) {
338
$info = $this->GetExtendedCacheInfo($row, $flags);
339
array_push($list, $info);
341
array_push($postfixes, $info['postfix']);
344
if ($flags&CACHE::FIND_BROKEN) {
345
$res = @mysql_query("SHOW TABLES LIKE 'cache%\\_\\_%'", $this->dbh);
347
$all_postfixes = array();
348
while ($row = mysql_fetch_row($res)) {
349
if (preg_match("/^cache\d+(__.*)$/", $row[0], $m)) {
350
array_push($all_postfixes, $m[1]);
354
$incomplete = array_diff(array_unique($all_postfixes), $postfixes);
356
foreach ($incomplete as $postfix) {
357
$info = $this->GetExtendedCacheInfo($postfix, $flags);
358
$info['incomplete'] = true;
359
array_push($list, $info);
367
function CreateReader($postfix = false) {
368
if (!$postfix) $postfix = $this->default_postfix;
370
$req = $this->CreateGroupRequest($postfix);
371
return $req->CreateReader();
374
function Drop($postfix) {
375
$lock = new LOCK("cache");
376
$lock->Lock(LOCK::BLOCK|LOCK::ALL);
379
$resolutions = $this->ListResolutions($postfix);
382
foreach ($resolutions as $res) {
383
$table = $this->GetTableName($res, $postfix);
384
$query = "DROP TABLE `$table`;";
385
if (!mysql_query($query, $this->dbh)) {
386
throw new ADEIException(translate("Failed to drop CACHE (%s). Request '%s' is failed with error: %s", "cache*_$postfix", $query, mysql_error($this->dbh)));
389
} catch (ADEIException $ae) {
397
function Rewidth($postfix, $report = false) {
398
$lock = new LOCK("cache");
399
$lock->Lock(LOCK::BLOCK|LOCK::ALL);
402
$current = $this->GetCacheWidth($postfix);
404
$reader = $this->CreateReader($postfix);
405
$actual = $reader->GetGroupSize();
407
if ($current == $actual) {
413
echo translate("Resizing %s from %d to %d", "cache*_$postfix", $current, $actual);
414
if ($report === "html") echo "<br/>";
418
if ($current < $actual) {
419
for ($i = $current; $i < $actual; $i++) {
424
$spec0 = "ADD `v$i` DOUBLE";
425
$spec = "ADD `min" . $i . "` DOUBLE, ADD `max" . $i . "` DOUBLE, ADD `mean" . $i . "` DOUBLE";
428
for ($i = $actual; $i < $current; $i++) {
433
$spec0 = "DROP `v$i`";
434
$spec = "DROP `min" . $i . "`, DROP `max" . $i . "`, DROP `mean" . $i . "`";
438
$resolutions = $this->ListResolutions($postfix);
441
foreach ($resolutions as $res) {
443
$width = $this->GetTableWidth($postfix, $res);
444
if ($width != $current) {
445
if ($width == $actual) {
447
echo translate("Resolution %lu is already converted", $res);
448
if ($report === "html") echo "<br/>";
453
throw new ADEIException(translate("CACHE table (%s) have unexpected size: %d (converting from %d to %d)", "cache${res}${postfix}", $width, $current, $actual));
456
$query = "ALTER TABLE `cache${res}${postfix}` $spec";
458
$query = "ALTER TABLE `cache0$postfix` $spec0";
462
echo translate("Converting resolution %lu", $res);
463
if ($report === "html") echo "<br/>";
467
# echo $query . "<br/>\n";
468
if (!mysql_query($query, $this->dbh)) {
469
throw new ADEIException(translate("The CACHE (%s) resizing request is not complete. Request '%s' is failed with error: %s", "cache*_$postfix", $query, mysql_error($this->dbh)));
473
} catch (ADEIException $ae) {
68
482
function ListCachedGroups($db_name = false, $db_server = false) {