Pacific-Design.com

    
Home Index

1. MySQL

2. Permutation BerkeleyDB

MySQL / Permutation BerkeleyDB /

#!/usr/bin/perl
#-------------------------------------------------------------------------------------------#
use strict;
use warnings;
use BerkeleyDB;
use Time::HiRes qw(usleep sleep);
use English qw(-no_match_vars);
use DBI;
use vars qw( $dbh %h $tbl $total );
$| = 1;

#-------------------------------------------------------------------------------------------#
$dbh = DBI->connect(
            'DBI:mysql:mysql_socket=/var/lib/mysql/mysql.sock;database=engine7',
            'user', 'password', {RaiseError => 1, AutoCommit => 0})
  || die "Database connection not made: $DBI::errstr";

#-------------------------------------------------------------------------------------------#
$total = 0;
&permutation();
untie %h;
$dbh->disconnect();

#-------------------------------------------------------------------------------------------#
sub initialize
{
    my $table = shift;
    my $btree = "/data1/cache/engine7_" . $table . ".bdb";
    untie %h;
    tie %h, "BerkeleyDB::Btree",
      -Filename  => $btree,
      -Flags     => DB_CREATE,
      -Cachesize => 4000000000,
      or die "Cannot open file : $! $BerkeleyDB::Error\n";
}

#-------------------------------------------------------------------------------------------#
sub execute_sql
{
    my $table = shift;
    my $tmp = substr($table, 0, 1);
    if ($tbl eq $tmp) { }
    else
    {
        $tbl = $tmp;
        &initialize($tbl);
    }

    my $sql = qq/SELECT sha256url, url FROM part_$table;/;
    print "-----------------" . $sql . "-----------------\n";

    my $sth = $dbh->prepare($sql);
    $sth->execute();
    my ($sha256url, $url);

    $sth->bind_col(1, \$sha256url);
    $sth->bind_col(2, \$url);

    while ($sth->fetch())
    {
        $total++;
        print $total . "=" . $url . "\n" if (($total % 5000) == 0);
        $h{$url}++;
    }

    $sth->finish();
}

#---------------------------------------------------------------------------#
sub permutation
{
    for my $a ('0' .. '9', 'a' .. 'f')
    {
        for my $b ('0' .. '9', 'a' .. 'f')
        {
            for my $c ('0' .. '9', 'a' .. 'f')
            {
                my $table = $a . $b . $c;
                &execute_sql($table);
            }
        }
    }
}

#---------------------------------------------------------------------------#