#!/usr/bin/perl -w

use strict;
use Benchmark;
use DBI;
use MLDBM qw(DB_File Storable);
use Storable;
use Fcntl;

my($i_pub, $i_size, $i_type, $i_ip);
my(%s_crr, $rs_tmp, %s_tmp);
my(%s_disp_hash, $r_hash);
my($r_flatfile);
my($dbh, $r_postgres, @row_ary);
my($r_storable, %s_disp_hash_storable);
my($r_mldbm, %s_disp_hash_mldbm);

my($i_count) = 20;
my($i_inner_loop) = 2;
my($s_delimiter) = '|';

%s_crr = ("0", 0,
		"1", 1,
		"2", 2,
		"3", 3,
		"4", 4,
		"5", 5,
		"6", 6,
		"7", 7,
		"8", 8,
		"9", 9 );

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

$dbh = DBI->connect("dbi:Pg:dbname=opticlik_smg", "postgres",
		"postgres", { RaiseError => 1, AutoCommit => 1 });	# Postgres

foreach $i_pub (0..99) {

#	mkdir "flatfile/pub$i_pub", 0777;	# flat file	
	
	foreach $i_size (0..2) {
	
#		mkdir "flatfile/pub$i_pub/size$i_size", 0777;	# flat file
	
		foreach $i_type (0..3) {
		
#			mkdir "flatfile/pub$i_pub/size$i_size/type$i_type", 0777;

			foreach $i_ip (0..19) {

				#----- flat file -----

#				open(FH, ">flatfile/pub$i_pub/size$i_size/type$i_type/ip$i_ip");

#				print FH qq("0",0,"1",1,"2",2,"3",3,"4",4,"5",5,"6",6,"7",7,"8",8,"9",9);

#				close(FH);
			
				#----- hash -----
				
				$s_disp_hash{join($s_delimiter,
						"pub$i_pub","size$i_size","type$i_type","ip$i_ip")} =
						\%s_crr;	

				#----- postgres -----
		
#				$dbh->do(qq{insert into benchmark values ('pub$i_pub',
#						'size$i_size', 'type$i_type', 'ip$i_ip',
#						'"0",0,"1",1,"2",2,"3",3,"4",4,"5",5,"6",6,"7",7,"8",8,"9",9')});						
			}
		}
	}
}

#----- storable -----

#store \%s_disp_hash, 'storable.dat';


#----- MLDBM -----

#tie %s_disp_hash, 'MLDBM', "mldbm.dat", O_CREAT|O_RDWR, 00644;
#untie %s_disp_hash;

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

#----- r_hash -----

$r_hash = sub {
	foreach (1..$i_inner_loop) {
		$i_pub = int(rand(100));
		$i_size = int(rand(3));
		$i_type = int(rand(4));
		$i_ip = int(rand(20));

		$rs_tmp = $s_disp_hash{join($s_delimiter,
				"pub$i_pub","size$i_size","type$i_type","ip$i_ip")};
	}
};

#----- flat file -----

$r_flatfile = sub {
	foreach (1..$i_inner_loop) {
		$i_pub = int(rand(100));
		$i_size = int(rand(3));
		$i_type = int(rand(4));
		$i_ip = int(rand(20));

		open(FH, "<flatfile/pub$i_pub/size$i_size/type$i_type/ip$i_ip");
		%s_tmp = (eval(<FH>));
		close(FH);
	}
};

#----- Postgres -----

$r_postgres = sub {
	foreach (1..$i_inner_loop) {
		$i_pub = int(rand(100));
		$i_size = int(rand(3));
		$i_type = int(rand(4));
		$i_ip = int(rand(20));

		@row_ary = $dbh->selectrow_array("select crr from benchmark where
				rtrim(pub) = 'pub$i_pub' and rtrim(size) = 'size$i_size' and
				rtrim(type) = 'type$i_type' and rtrim(ip) = 'ip$i_ip'");
	}
};

#----- Storable -----

$r_storable = sub {
	foreach (1..$i_inner_loop) {
        $i_pub = int(rand(100));
	    $i_size = int(rand(3));
	    $i_type = int(rand(4));
	    $i_ip = int(rand(20));

		$rs_tmp = retrieve('storable.dat');
		%s_disp_hash_storable = %{$rs_tmp}; 
		
		$rs_tmp = $s_disp_hash_storable{join($s_delimiter,
				"pub$i_pub","size$i_size","type$i_type","ip$i_ip")};
	}	
};

#----- MLDBM -----

$r_mldbm = sub {
	foreach (1..$i_inner_loop) {
		$i_pub = int(rand(100));
		$i_size = int(rand(3));
		$i_type = int(rand(4));
		$i_ip = int(rand(20));

		tie(%s_disp_hash_mldbm, 'MLDBM', 'mldbm.dat', O_RDONLY,
				00444);
				
		$rs_tmp = $s_disp_hash_mldbm{join($s_delimiter,
				"pub$i_pub","size$i_size","type$i_type","ip$i_ip")};

		untie %s_disp_hash_mldbm;
	}
};

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

foreach (1..2) {
	print "hash\n";
	print timestr(timeit($i_count, $r_hash)), "\n"; 
	print "flatfile\n";
	print timestr(timeit($i_count, $r_flatfile)), "\n";
	print "postgres\n";
	print timestr(timeit($i_count, $r_postgres)), "\n";
	print "storable\n";
	print timestr(timeit($i_count, $r_storable)), "\n";
	print "mldbm\n";
	print timestr(timeit($i_count, $r_mldbm)), "\n";
}

$dbh->disconnect();
