Author: david
Date: Mon Dec  6 16:20:52 2010
New Revision: 8874

Log:
MySQL primary key uniqueness is case-insensitive.  Use only lower-case letters 
in permalink hash.

Modified:
   trunk/lib/model/QubitSlug.php

Modified: trunk/lib/model/QubitSlug.php
==============================================================================
--- trunk/lib/model/QubitSlug.php       Mon Dec  6 00:02:21 2010        (r8873)
+++ trunk/lib/model/QubitSlug.php       Mon Dec  6 16:20:52 2010        (r8874)
@@ -23,22 +23,20 @@
   {
     $slug = null;
 
-    $alphabet = 
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
+    $alphabet = '0123456789abcdefghijklmnopqrstuvwxyz';
 
-    // Probability of collision is approximately the number of rows in slug
-    // table over getrandmax(), so the probability increases with the number
-    // of rows
+    // Probability of generating a collision can be found using this formula
+    // 
http://en.wikipedia.org/wiki/Birthday_paradox#Cast_as_a_collision_problem
     //
-    // More accurately, probability is number of slugs containing only digits
-    // and letters over getrandmax() - but this is most slugs
-    //
-    // For constant probability and increasing slug length, COUNT() the slug
-    // table, but then it'd be better to use an SQL expression for the slug
+    // For getrandmax() = 2^31-1 (32-bit signed integer max) probability of
+    // collision is >50% when we reach approx. 50,000 records
     $rand = rand();
-    while (62 < $rand)
+
+    // Convert $rand to base36 hash 
+    while (36 < $rand)
     {
-      $slug .= $alphabet[$rand % 62];
-      $rand /= 62;
+      $slug .= $alphabet[$rand % 36];
+      $rand /= 36;
     }
 
     return $slug;

-- 
You received this message because you are subscribed to the Google Groups 
"Qubit Toolkit Commits" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/qubit-commits?hl=en.

Reply via email to