connect to search accelerator for 2-100x speedup on heavy queries

This commit is contained in:
Shish 2015-07-12 22:12:05 +01:00
parent 9f9f2735f4
commit 88fead6ba6
2 changed files with 70 additions and 5 deletions

View File

@ -158,11 +158,18 @@ class Image {
} }
} }
$querylet = Image::build_search_querylet($tags); $result = null;
$querylet->append(new Querylet(" ORDER BY ".($order_sql ?: "images.".$config->get_string("index_order")))); if(SEARCH_ACCEL) {
$querylet->append(new Querylet(" LIMIT :limit OFFSET :offset", array("limit"=>$limit, "offset"=>$start))); $result = Image::get_accelerated_result($tags, $start, $limit);
#var_dump($querylet->sql); var_dump($querylet->variables); }
$result = $database->execute($querylet->sql, $querylet->variables);
if(!$result) {
$querylet = Image::build_search_querylet($tags);
$querylet->append(new Querylet(" ORDER BY ".($order_sql ?: "images.".$config->get_string("index_order"))));
$querylet->append(new Querylet(" LIMIT :limit OFFSET :offset", array("limit"=>$limit, "offset"=>$start)));
#var_dump($querylet->sql); var_dump($querylet->variables);
$result = $database->execute($querylet->sql, $querylet->variables);
}
while($row = $result->fetch()) { while($row = $result->fetch()) {
$images[] = new Image($row); $images[] = new Image($row);
@ -171,6 +178,63 @@ class Image {
return $images; return $images;
} }
public function validate_accel($tags) {
$yays = 0;
$nays = 0;
foreach($tags as $tag) {
if(!preg_match("/^-?[a-zA-Z0-9_]+$/", $tag)) {
return false;
}
if($tag[0] == "-") $nays++;
else $yays++;
}
return ($yays > 1 || $nays > 0);
}
public function get_accelerated_result($tags, $offset, $limit) {
global $database;
$tags = Tag::resolve_aliases($tags);
if(!Image::validate_accel($tags)) {
return null;
}
$yays = array();
$nays = array();
foreach($tags as $tag) {
if($tag[0] == "-") {
$nays[] = substr($tag, 1);
}
else {
$yays[] = $tag;
}
}
$req = array(
"yays" => $yays,
"nays" => $nays,
"offset" => $offset,
"limit" => $limit,
);
$fp = fsockopen("127.0.0.1", 21212);
if (!$fp) {
return null;
}
fwrite($fp, json_encode($req));
$data = fgets($fp, 1024);
fclose($fp);
$response = json_decode($data);
$list = implode(",", $response);
if($list) {
$result = $database->execute("SELECT * FROM images WHERE id IN ($list) ORDER BY images.id DESC");
}
else {
$result = $database->execute("SELECT * FROM images WHERE 1=0 ORDER BY images.id DESC");
}
return $result;
}
/* /*
* Image-related utility functions * Image-related utility functions
*/ */

View File

@ -32,6 +32,7 @@ _d("COOKIE_PREFIX", 'shm'); // string if you run multiple galleries with non-
_d("SPEED_HAX", false); // boolean do some questionable things in the name of performance _d("SPEED_HAX", false); // boolean do some questionable things in the name of performance
_d("COMPILE_ELS", false); // boolean pre-build the list of event listeners _d("COMPILE_ELS", false); // boolean pre-build the list of event listeners
_d("NICE_URLS", false); // boolean force niceurl mode _d("NICE_URLS", false); // boolean force niceurl mode
_d("SEARCH_ACCEL", false); // boolean use search accelerator
_d("WH_SPLITS", 1); // int how many levels of subfolders to put in the warehouse _d("WH_SPLITS", 1); // int how many levels of subfolders to put in the warehouse
_d("VERSION", '2.5.4+'); // string shimmie version _d("VERSION", '2.5.4+'); // string shimmie version
_d("TIMEZONE", null); // string timezone _d("TIMEZONE", null); // string timezone