From c6ec481690e3b810ed7b0b8eae3b26ca24ba40df Mon Sep 17 00:00:00 2001 From: Carlo Landmeter Date: Mon, 18 Apr 2011 10:59:17 +0200 Subject: run only one db query when importing --- apk_browser.module | 107 +++++++++++++++++++---------------------------------- 1 file changed, 38 insertions(+), 69 deletions(-) diff --git a/apk_browser.module b/apk_browser.module index a5ec0ac..0efd154 100644 --- a/apk_browser.module +++ b/apk_browser.module @@ -49,7 +49,7 @@ function apk_browser_menu() { $items['admin/config/apk_browser/import'] = array( 'title' => 'Import packages', 'description' => 'Import APK packages', - 'page callback' => 'apk_browser_import_apk', + 'page callback' => 'apk_browser_importer', 'access arguments' => array('access apk browser'), ); return $items; @@ -79,7 +79,7 @@ function apk_browser_settings($form) { } function apk_browser_cron() { - apk_browser_import_apk(); + apk_browser_importer(); //cleanup old packages ones in 24h if ((time() - variable_get('apk_browser_cleanup', '0')) > '86400') { apk_browser_cleanup(); @@ -218,56 +218,50 @@ function apk_browser_repositories_list() { } } -function apk_browser_import_apk() { +function apk_browser_importer() { $repos = variable_get('apk_repositories', NULL); - $debug = variable_get('apk_import_debug', '0'); - $updated = array(); - $added = array(); - $skipped = '0'; foreach ($repos as $id => $repo) { $apk_string = file_get_contents($repo['url']); - //no need to update if apkindex is not changed $checksum = md5($apk_string); $checksum_db = variable_get('apk_import_checksum_' . $id, ''); - if (($debug == '1') || ($checksum != $checksum_db)) { - if ($debug == '1') { - watchdog('apk', 'APK importer running in debug mode!', array(), WATCHDOG_WARNING, NULL); - } - $packages = apk_browser_apkindex_reader($apk_string); - foreach ($packages as $package) { - $exist = apk_browser_apk_check($package['P'], $repo['arch'], $repo['repo']); - if ($exist) { - //check if both fields are set - if (count($exist) == '2') { - //if checksum has changed we set nid and update node - if ($exist['csum'] != $package['C']) { - $package['update'] = $exist['nid']; - $updated[] = apk_browser_add_apk($package, $repo['arch'], $repo['repo']); - //log to db if we are debugging - if ($debug == '1') { - watchdog('apk', 'Package @apk updated', array('@apk' => $package['P']), WATCHDOG_INFO, NULL); - } - } else { - if ($debug == '1') { - watchdog('apk', 'Package @apk already in database', array('@apk' => $package['P']), WATCHDOG_INFO, NULL); - } - $skipped++; - } + if ($checksum != $checksum_db) { + $repo_apks = apk_browser_apkindex_reader($apk_string); + $skipped = '0'; + $updated = array(); + $added = array(); + $db_apks = db_query(" + SELECT node.nid, node.title, + csum.apk_checksum_value as csum + FROM {node}, + {field_data_taxonomy_apk_arch} AS arch, + {field_data_taxonomy_apk_repo} AS repo, + {field_data_apk_checksum} AS csum + WHERE node.nid = arch.entity_id + AND node.nid = repo.entity_id + AND node.status = '1' + AND node.nid = csum.entity_id + AND arch.taxonomy_apk_arch_tid = :arch + AND repo.taxonomy_apk_repo_tid = :repo", array( + ':arch' => $repo['arch'], + ':repo' => $repo['repo'] + ) + )->fetchAllAssoc('title'); + foreach ($repo_apks as $title => $repo_apk) { + if (isset($db_apks[$title])) { + if ($repo_apk['C'] != $db_apks[$title]->csum) { + $repo_apk['update'] = $db_apks[$title]->nid; + $updated[] = apk_browser_add_apk($repo_apk, $repo['arch'], $repo['repo']); } else { - watchdog('apk', 'Package @apk has issues', array('@apk' => $package['P']), WATCHDOG_ERROR, NULL); + $skipped++; } - // this is a new package } else { - $added[] = apk_browser_add_apk($package, $repo['arch'], $repo['repo']); - if ($debug == '1') { - watchdog('apk', 'Package @apk added', array('@apk' => $package['P']), WATCHDOG_INFO, NULL); - } + $added[] = apk_browser_add_apk($repo_apk, $repo['arch'], $repo['repo']); } } watchdog('apk', 'Added @added and updated @updated and skipped @skipped packages', array('@added' => count($added), '@updated' => count($updated), '@skipped' => $skipped), WATCHDOG_INFO, NULL); variable_set('apk_import_checksum_' . $id, $checksum); } else { - watchdog('apk', 'APK index is up-to-date', array(), WATCHDOG_INFO, NULL); + watchdog('apk', 'APK index @repo is up-to-date', array('@repo' => $id), WATCHDOG_INFO, NULL); } } } @@ -365,10 +359,10 @@ function apk_browser_apkindex_reader($apk_string) { } } } - /* - * this should take care of having multiple packages with different - * version in repositories - */ + /* + * this should take care of having multiple packages with different + * version in repositories + */ foreach ($result as $pkg) { //check if package name already exit in result array if (isset($output[$pkg['P']])) { @@ -377,7 +371,7 @@ function apk_browser_apkindex_reader($apk_string) { //if version is higher we overwrite package $output[$pkg['P']] = $pkg; } - //all not matching packages we just write them + //all not matching packages we just write them } else { $output[$pkg['P']] = $pkg; } @@ -386,31 +380,6 @@ function apk_browser_apkindex_reader($apk_string) { return $output; } -function apk_browser_apk_check($title, $arch, $repo) { - /* query using 4 tables to fetch nid and checksum - * used to check if apk exist and return checksum - * to check if its updated - */ - return db_query(" - SELECT node.nid, - csum.apk_checksum_value as csum - FROM {node}, - {field_data_taxonomy_apk_arch} AS arch, - {field_data_taxonomy_apk_repo} AS repo, - {field_data_apk_checksum} AS csum - WHERE node.nid = arch.entity_id - AND node.nid = repo.entity_id - AND node.nid = csum.entity_id - AND node.title = :title - AND arch.taxonomy_apk_arch_tid = :arch - AND repo.taxonomy_apk_repo_tid = :repo", array( - ':title' => $title, - ':arch' => $arch, - ':repo' => $repo - ) - )->fetchAssoc(); -} - //this is needed for auto views integration function apk_browser_views_api() { return array('api' => 3.0); -- cgit v1.2.3