From 88fead6ba656c1799d7eff7053a3ec942ec84870 Mon Sep 17 00:00:00 2001 From: Shish Date: Sun, 12 Jul 2015 22:12:05 +0100 Subject: [PATCH] connect to search accelerator for 2-100x speedup on heavy queries --- core/imageboard.pack.php | 74 +++++++++++++++++++++++++++++++++++++--- core/sys_config.inc.php | 1 + 2 files changed, 70 insertions(+), 5 deletions(-) diff --git a/core/imageboard.pack.php b/core/imageboard.pack.php index d56d561c..e3050d23 100644 --- a/core/imageboard.pack.php +++ b/core/imageboard.pack.php @@ -158,11 +158,18 @@ class Image { } } - $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); + $result = null; + if(SEARCH_ACCEL) { + $result = Image::get_accelerated_result($tags, $start, $limit); + } + + 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()) { $images[] = new Image($row); @@ -171,6 +178,63 @@ class Image { 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 */ diff --git a/core/sys_config.inc.php b/core/sys_config.inc.php index 63fa169a..dae1e161 100644 --- a/core/sys_config.inc.php +++ b/core/sys_config.inc.php @@ -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("COMPILE_ELS", false); // boolean pre-build the list of event listeners _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("VERSION", '2.5.4+'); // string shimmie version _d("TIMEZONE", null); // string timezone