#!/usr/bin/perl -w

use strict;
use DBI;
use MongoDB;
use Data::Dump qw/dump/;

$|++;

my $debug = @ARGV ? 1 : 0;

my $database = 'reblog';

my $dbi = "DBI:mysql:database=$database";
$dbi .= ";host=127.0.0.1;port=13306";	# XXX over ssh

my $dbh = DBI->connect($dbi,"","",{ RaiseError => 1 });

my $sql = qq{
	select
		md5(link) as _id,
		items.*
	from items
	where id > ?
	order by id asc
	limit 100000
};

my $sql_tags = qq{
select
	items_userdata.item_id,
	value_long as tags,
	timestamp
from items_userdata
join published_items p
	on items_userdata.item_id = p.item_id and label='tags'
where
	items_userdata.item_id > ?
order by items_userdata.item_id asc
};

my $conn  = MongoDB::Connection->new;
my $db    = $conn->get_database( $database );
$db->drop if $debug;
my $items = $db->get_collection( 'items' );

$items->ensure_index( { id => 1 } );

# > db.items.find().sort({item_id:-1}).limit(1);
my $last = $items->query()->sort({ 'id' => -1 })->limit(1)->next;
warn dump( $last );
my $last_item_id = $last->{id} || 0;

print "Fetching items from $dbi id > $last_item_id\n";

my $sth = $dbh->prepare($sql);
$sth->execute( $last_item_id );

warn dump( $sth->{NAME} );

print "found ",$sth->rows," items to process...\n";

while (my $row = $sth->fetchrow_hashref() ) {

	map { $row->{$_} * 1 } grep { m/id/ && $row->{$_} =~ /^\d+$/ } keys %$row;
	$items->insert( $row );
}

__END__

my $sth_tags = $dbh->prepare($sql_tags);
$sth_tags->execute( $last_item_id );
print "found ",$sth_tags->rows, " tags found...\n";

my $count = 0;

my $row_tags = $sth_tags->fetchrow_hashref();

my @join = ( 'id' => 'item_id' );

