Skip to content

Instantly share code, notes, and snippets.

@kawa-
Created July 24, 2014 13:54
Show Gist options
  • Save kawa-/4bf9be39f28f5e9402ea to your computer and use it in GitHub Desktop.
Save kawa-/4bf9be39f28f5e9402ea to your computer and use it in GitHub Desktop.
MySQLにアクセスして、ユニークなuserid, followerid, timestampを生成する。
<?php
# usage: $ php generate_unique_pair.php 10000 > unique_pairs.txt
define('NUM_PAIRS', $argv[1]);
define('MAX_USERS', 5000 * 10000);
$dbname = 'db01';
$host = 'localhost';
$user = 'root';
$password = 'root';
$table = 't01';
$pairs = array();
$pdo = new PDO(
'mysql:dbname=' . $dbname . ';host=' . $host . ';charset=latin1', $user, $password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_PERSISTENT => true)
);
while (TRUE) {
if (count($pairs) >= NUM_PAIRS) {
break;
}
$userid = rand(1, MAX_USERS);
$followerid = rand(1, MAX_USERS);
if (select($pdo, $table, $userid, $followerid) === FALSE) {
$pairs[] = array($userid, $followerid);
}
}
foreach ($pairs as $pair) {
echo $pair[0] . "\t" . $pair[1] . "\t" . (time()+rand(-100000000, 100000000)) . "\n";
}
/* $iと$fの組み合わせが存在してればTRUE, 存在しなければFALSE */
function select($pdo, $tablename, $i, $f) {
try {
$stmt = $pdo->prepare("select f from " . $tablename . " where i = :i and f = :f");
$stmt->bindValue(':i', $i, PDO::PARAM_INT);
$stmt->bindValue(':f', $f, PDO::PARAM_INT);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if ($row === FALSE) {
return FALSE;
}
return TRUE;
} catch (PDOException $e) {
die("ERROR. " . $e->getMessage());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment