diff options
| author | Bharat Mediratta <bharat@menalto.com> | 2009-05-20 06:10:14 +0000 |
|---|---|---|
| committer | Bharat Mediratta <bharat@menalto.com> | 2009-05-20 06:10:14 +0000 |
| commit | 774e0b3737d51b70bb141f4e13c6c111208847c0 (patch) | |
| tree | 8f367c99a16c1d4d3419838aec420a29a320bc3b /modules/search/helpers | |
| parent | 54fe2c600e6930c2885f3f6048221710637a142f (diff) | |
Avoid complex inserts in available_tasks() to make admin/maintenance
run faster. This fixes ticket #235.
Incidentally, refactor exif and search to use the same patterns
overall so that if you understand one, you understand the other and
they generally use the same strings for localization.
Diffstat (limited to 'modules/search/helpers')
| -rw-r--r-- | modules/search/helpers/search.php | 24 | ||||
| -rw-r--r-- | modules/search/helpers/search_event.php | 14 | ||||
| -rw-r--r-- | modules/search/helpers/search_installer.php | 9 | ||||
| -rw-r--r-- | modules/search/helpers/search_task.php | 56 |
4 files changed, 50 insertions, 53 deletions
diff --git a/modules/search/helpers/search.php b/modules/search/helpers/search.php index 2842afbd..2c7be123 100644 --- a/modules/search/helpers/search.php +++ b/modules/search/helpers/search.php @@ -51,7 +51,8 @@ class search_Core { } static function check_index() { - if ($count = ORM::factory("search_record")->where("dirty", 1)->count_all()) { + list ($remaining) = search::stats(); + if ($remaining) { site_status::warning( t('Your search index needs to be updated. <a href="%url" class="gDialogLink">Fix this now</a>', array("url" => url::site("admin/maintenance/start/search_task::update_index?csrf=__CSRF__"))), @@ -59,8 +60,13 @@ class search_Core { } } - static function update_record($record) { + static function update($item) { $data = array(); + $record = ORM::factory("search_record")->where("item_id", $item->id)->find(); + if (!$record->loaded) { + $record->item_id = $item->id; + } + foreach (module::installed() as $module_name => $module_info) { $class_name = "{$module_name}_search"; if (method_exists($class_name, "item_index_data")) { @@ -71,4 +77,18 @@ class search_Core { $record->dirty = 0; $record->save(); } + + static function stats() { + $remaining = Database::instance() + ->select("items.id") + ->from("items") + ->join("search_records", "items.id", "search_records.item_id", "left") + ->where("search_records.item_id", null) + ->orwhere("search_records.dirty", 1) + ->get() + ->count(); + $total = ORM::factory("item")->count_all(); + $percent = round(100 * ($total - $remaining) / $total); + return array($remaining, $total, $percent); + } } diff --git a/modules/search/helpers/search_event.php b/modules/search/helpers/search_event.php index 50a968e7..b9657395 100644 --- a/modules/search/helpers/search_event.php +++ b/modules/search/helpers/search_event.php @@ -19,16 +19,11 @@ */ class search_event_Core { static function item_created($item) { - $record = ORM::factory("search_record"); - $record->item_id = $item->id; - search::update_record($record); + search::update($item); } static function item_updated($old_item, $new_item) { - $record = ORM::factory("search_record") - ->where("item_id", $new_item->id) - ->find(); - search::update_record($record); + search::update($new_item); } static function item_before_delete($item) { @@ -38,10 +33,7 @@ class search_event_Core { } static function item_related_update($item) { - $record = ORM::factory("search_record") - ->where("item_id", $item->id) - ->find(); - search::update_record($record); + search::update($item); } static function item_related_update_batch($sql) { diff --git a/modules/search/helpers/search_installer.php b/modules/search/helpers/search_installer.php index fe5a7359..a3d0f79e 100644 --- a/modules/search/helpers/search_installer.php +++ b/modules/search/helpers/search_installer.php @@ -35,14 +35,7 @@ class search_installer { // populate the index with dirty records $db->query("INSERT INTO {search_records} (`item_id`) SELECT `id` FROM {items}"); module::set_version("search", 1); - - if (ORM::factory("search_record")->count_all() < 10) { - foreach (ORM::factory("search_record")->where("dirty", 1)->find_all() as $record) { - search::update_record($record); - } - } else { - search::check_index(); - } + search::check_index(); } } diff --git a/modules/search/helpers/search_task.php b/modules/search/helpers/search_task.php index e3884d53..876661e4 100644 --- a/modules/search/helpers/search_task.php +++ b/modules/search/helpers/search_task.php @@ -25,21 +25,16 @@ class search_task_Core { "WHERE {search_records}.`item_id` NOT IN " . "(SELECT `id` FROM {items})"); - // Insert missing search_records - Database::instance()->query( - "INSERT INTO {search_records}(`item_id`) (" . - " SELECT {items}.`id` FROM {items} " . - " LEFT JOIN {search_records} ON ({search_records}.`item_id` = {items}.`id`) " . - " WHERE {search_records}.`id` IS NULL)"); - - list ($remaining, $total, $percent) = self::_get_stats(); + list ($remaining, $total, $percent) = search::stats(); return array(Task_Definition::factory() ->callback("search_task::update_index") ->name(t("Update Search Index")) - ->description($remaining ? - t("Search index is %percent% up-to-date", - array("percent" => $percent)) - : t("Search index is up to date")) + ->description( + $remaining + ? t2("1 photo or album needs to be scanned", + "%count (%percent%) of your photos and albums need to be scanned", + $remaining, array("percent" => (100 - $percent))) + : t("Search data is up-to-date")) ->severity($remaining ? log::WARNING : log::SUCCESS)); } @@ -47,34 +42,31 @@ class search_task_Core { $completed = $task->get("completed", 0); $start = microtime(true); - while (!$task->done && microtime(true) - $start < 1) { - foreach (ORM::factory("search_record")->where("dirty", 1)->limit(5)->find_all() as $record) { - search::update_record($record); - $completed++; + foreach (ORM::factory("item") + ->join("search_records", "items.id", "search_records.item_id", "left") + ->where("search_records.item_id", null) + ->orwhere("search_records.dirty", 1) + ->find_all() as $item) { + if (microtime(true) - $start > 1.5) { + break; } - list ($remaining, $total, $percent) = self::_get_stats(); - if ($remaining + $completed) { - $task->percent_complete = round(100 * $completed / ($remaining + $completed)); - $task->status = t("%done records updated, index is %percent% up-to-date", - array("done" => $completed, "percent" => $percent)); - } else { - $task->percent_complete = 100; - } + search::update($item); + $completed++; } + list ($remaining, $total, $percent) = search::stats(); $task->set("completed", $completed); - if ($remaining == 0) { + if ($remaining == 0 || !($remaining + $completed)) { $task->done = true; $task->state = "success"; site_status::clear("search_index_out_of_date"); + $task->percent_complete = 100; + } else { + $task->percent_complete = round(100 * $completed / ($remaining + $completed)); } - } - - private static function _get_stats() { - $remaining = ORM::factory("search_record")->where("dirty", 1)->count_all(); - $total = ORM::factory("search_record")->count_all(); - $percent = round(100 * ($total - $remaining) / $total); - return array($remaining, $total, $percent); + $task->status = t2("one record updated, index is %percent% up-to-date", + "%count records updated, index is %percent% up-to-date", + $completed, array("percent" => $percent)); } } |
