Use table cache_waypoint_pool to generate a list of unused waypoints and assign waypoints to new caches from this list.

This commit is contained in:
ocoliver
2012-11-04 22:16:39 +01:00
parent b298bfccc0
commit d6ae786829
7 changed files with 332 additions and 43 deletions

View File

@@ -86,43 +86,26 @@
// set a unique waypoint to this cache
function setCacheWaypoint($cacheid)
{
global $oc_waypoint_prefix;
global $opt;
// The following code runs into trouble, if multiple users/threads request a new
// waypoint synchronously (-> RT ticket #4071). We will hack around this problem by
// repeating the operation with random delays. Another solution would be a table-lock.
// cleanup previous assignments failures
sql("DELETE FROM `cache_waypoint_pool` WHERE `cache_id`='&1'", $cacheid);
// 2012-08-23 following
// changes: removed 4 hexdigits limit; added random delay;
// increased maxloop 10 -> 20; simplified loop
// reserve a waypoint
sql("UPDATE `cache_waypoint_pool` SET `cache_id`='&1' ORDER BY WPTODEC(`wp_oc`, '&2') ASC LIMIT 1", $cacheid, $opt['logic']['waypoint_pool']['prefix']);
$nLoop = 20;
// TODO: cronjob for waypoint pool generation may not run on development systems
// add a fix to generate a new waypoints on demand (insert this new waypoint to cache_waypoint_pool with reserved cache_id
// and follow standard assignment code to prevent race conditions with cronjob)
do
{
// add zero to convert CONV's string result to a sortable number
$rs = sql("SELECT MAX(0 + CONV(SUBSTR(wp_oc,3),16,10)) maxwp FROM `caches` WHERE wp_oc!='OCGC77'");
$r = sql_fetch_assoc($rs);
mysql_free_result($rs);
// assign reserved waypoint to the cache
// for the moment, we use IGNORE to catch duplicate keys that occur in any failure case
// the cache keeps without a waypoint in this case. Later we change field caches.wp_oc to NOT NULL and assign waypoint in BEFORE INSERT trigger
sql("UPDATE IGNORE `caches` INNER JOIN `cache_waypoint_pool` ON `caches`.`cache_id`=`cache_waypoint_pool`.`cache_id` SET `caches`.`wp_oc`=`cache_waypoint_pool`.`wp_oc` WHERE `caches`.`cache_id`='&1'", $cacheid);
if ($r['maxwp'] == null)
$nNext = 1; // first cache in database
else
$nNext = $r['maxwp'] + 1;
$nNext = dechex($nNext);
while (mb_strlen($nNext) < 4)
$nNext = '0' . $nNext;
$sWP = $oc_waypoint_prefix . mb_strtoupper($nNext);
if (sql("UPDATE IGNORE `caches` SET `wp_oc`='&1' WHERE `cache_id`='&2' AND ISNULL(`wp_oc`)", $sWP, $cacheid))
$nLoop = 0;
else
usleep(rand(0,50000));
} while (--$nLoop > 0);
}
// cleanup
sql("DELETE FROM `cache_waypoint_pool` WHERE `cache_id`='&1'", $cacheid);
}
function setLastFound($cacheid)
{