Advertisement
rfv123

q34180258/generate-and-save-million-of-uniques-codes

Nov 2nd, 2016
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.77 KB | None | 0 0
  1. <?php // http://stackoverflow.com/questions/34180258/generate-and-save-million-of-uniques-codes/34183586#34183586
  2.  
  3. /**
  4.  *  This creates a database connction and initializes an ORM.
  5.  *  I use redbeanPHP but Doctrine can also be used
  6.  */
  7. include __DIR__ . '/__bootstrap__.php';
  8.  
  9. use pip\system\encryption\RandomData;
  10. use pip\system\ElapsedTiming;
  11.  
  12. \R::freeze(true); // table already exists - make it faster.
  13.  
  14. define('BATCH_ID_MIN',             100);
  15. define('BATCH_ID_MAX',             999);
  16. define('BATCH_ID_CNT',               2);   // 100 really
  17. define('BATCH_ID_MAX_TRIES',     50000);   // should be less than this ;-/
  18.  
  19.  
  20. define('BATCH_MIN',                  0);
  21. define('BATCH_MAX',          999999999);
  22. define('BATCH_CNT',                100);   // Ten thousand really
  23. define('BATCH_MAX_TRIES',    999999999);   // should be  a less than this ;-/
  24.  
  25.  
  26. // generate all the batch ids
  27. $batchIds = makeBatch(BATCH_ID_MIN, BATCH_ID_MAX, BATCH_ID_CNT, BATCH_ID_MAX_TRIES);
  28.  
  29.  
  30. // for each batch id generate all the unique 9 digit numbers
  31. foreach(array_keys($batchIds) as $id) {
  32.  
  33.     // takes approx 2 seconds per batch    
  34.     $batchNos = makeBatch(BATCH_MIN, BATCH_MAX, BATCH_CNT, BATCH_MAX_TRIES);
  35.    
  36.     // takes approx 30 seconds per 10000 batch - this can be improved :)    
  37.     saveBatch($id, array_keys($batchNos));
  38. }            
  39.  
  40. // Show output...
  41. $results = \R::getAll('select batchid, batchno from batchbean
  42.                       order by batchid, batchno', array());
  43.  
  44. foreach ($results as $number) {
  45.   echo 'Random: ', sprintf('%3d-%9d', $number['batchid'], $number['batchno']), PHP_EOL;
  46. }
  47.  
  48. exit();
  49.  
  50.  
  51.  
  52. // due to the low chance of collision then this doesn't need stats - whatever - collect them
  53. function makeBatch($batchMin, $batchMax, $required, $maxTries = BATCH_MAX_TRIES)
  54. {
  55.     $rand = new RandomData();
  56.     $batchIds   = array();
  57.     $created    = 0;
  58.        
  59.     while ($created < $required && $maxTries > 0) {
  60.         $batchId = $rand->int32($batchMin, $batchMax);
  61.        
  62.         $maxTries--;
  63.        
  64.         if (isset($batchIds[$batchId])) {
  65.             $batchIds[$batchId]++;
  66.             continue;  
  67.         }    
  68.        
  69.         $batchIds[$batchId] = 1;
  70.         $created++;
  71.     }
  72.     $GLOBALS['maxTries'] = $maxTries;
  73.    
  74.     ksort($batchIds);
  75.    
  76.     return $batchIds;
  77. }
  78.  
  79. /**
  80.  *  Insert each batch into a database table.
  81.  *
  82.  *  I use RedbeanPHP so it does all the work for me but will be a little slow
  83.  */  
  84.  function saveBatch($batchId, $batchNumbers)
  85.  {
  86.      \R::begin();
  87.      foreach($batchNumbers as $batchNo) {
  88.         $bean = \R::dispense('batchbean');
  89.         $bean->batchid = $batchId;        
  90.         $bean->batchno = $batchNo;
  91.        
  92.         \R::store($bean);        
  93.      }
  94.      \R::commit();
  95.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement