Skip to content

Commit

Permalink
Try::Tiny
Browse files Browse the repository at this point in the history
  • Loading branch information
nothingmuch committed Sep 17, 2009
1 parent 9f70867 commit 0096222
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 87 deletions.
1 change: 1 addition & 0 deletions Makefile.PL
Expand Up @@ -48,6 +48,7 @@ WriteMakefile(
'MooseX::YAML' => 0.02,

# misc dev shit
'Try::Tiny' => 0,
'Scalar::Util' => 0,
#'Devel::PartialDump' => '0.06',
'namespace::clean' => '0.08',
Expand Down
7 changes: 4 additions & 3 deletions bench/shootout.pl
Expand Up @@ -7,6 +7,7 @@
use Path::Class;
use Storable qw(nstore retrieve);
use Scalar::Util qw(blessed);
use Try::Tiny;

use KiokuDB;

Expand Down Expand Up @@ -38,10 +39,10 @@ sub bench {

$mxsd_sqlite->backend->deploy;

my $mxsd_mysql = eval { KiokuDB->connect("dbi:mysql:test", serializer => "storable") }; warn $@ if $@;
my $mxsd_mysql = try { KiokuDB->connect("dbi:mysql:test", serializer => "storable") } catch { warn @_ };
$mxsd_mysql && $mxsd_mysql->backend->deploy({ add_drop_table => 1, producer_args => { mysql_version => 5 } });

my $mxsd_pg = eval { KiokuDB->connect("dbi:Pg:dbname=test", serializer => "storable") }; warn $@ if $@;
my $mxsd_pg = try { KiokuDB->connect("dbi:Pg:dbname=test", serializer => "storable") } catch { warn $@ };
$mxsd_pg && $mxsd_pg->backend->deploy({ add_drop_table => 1 });

$dir->subdir("mxsd_bdb_dumb")->mkpath;
Expand All @@ -66,7 +67,7 @@ sub bench {
my $name = $ENV{KIOKU_COUCHDB_NAME} || "kioku-$$";

my $db = $couch->db($name);
eval { $db->drop };
try { $db->drop };

$db->create;

Expand Down
5 changes: 3 additions & 2 deletions bin/kioku
Expand Up @@ -4,6 +4,7 @@ use strict;
use warnings;

use KiokuDB;
use Try::Tiny;

unless ( try_run() ) {
if ( $INC{"KiokuDB/Cmd.pm"} ) {
Expand All @@ -23,15 +24,15 @@ exit 1;


sub try_run {
return unless eval { require KiokuDB::Cmd; 1 };
return unless try { require KiokuDB::Cmd; 1 };
return unless KiokuDB::Cmd->is_up_to_date;

KiokuDB::Cmd->run;
exit;
}

sub try_auto_install {
if (eval {
if (try {
require Module::AutoInstall;
require ExtUtils::MakeMaker;
1;
Expand Down
31 changes: 12 additions & 19 deletions lib/KiokuDB.pm
Expand Up @@ -18,6 +18,7 @@ use KiokuDB::Stream::Objects;

use Hash::Util::FieldHash::Compat qw(idhash);
use Carp qw(croak);
use Try::Tiny;

use namespace::clean -except => [qw(meta SERIAL_IDS)];

Expand All @@ -28,7 +29,7 @@ no warnings 'recursion';
our $REQUIRED_CMD_VERSION = "0.02";
sub cmd_is_up_to_date {
require KiokuDB::Cmd;
eval { KiokuDB::Cmd->VERSION($REQUIRED_CMD_VERSION); 1 };
try { KiokuDB::Cmd->VERSION($REQUIRED_CMD_VERSION); 1 };
}


Expand Down Expand Up @@ -271,27 +272,19 @@ sub lookup {

my $linker = $self->linker;

my ( $e, @objects );
try {
my @objects = $linker->get_or_load_objects(@ids);

eval {
local $@;
eval { @objects = $linker->get_or_load_objects(@ids) };
$e = $@;
};

if ( $e ) {
if ( ref($e) and $e->{missing} ) {
return;
if ( @ids == 1 ) {
return $objects[0];
} else {
return @objects;
}
} catch {
die $_ unless ref and $_->{missing};

die $e;
}

if ( @ids == 1 ) {
return $objects[0];
} else {
return @objects;
}
return;
};
}

sub search {
Expand Down
14 changes: 6 additions & 8 deletions lib/KiokuDB/Backend.pm
Expand Up @@ -4,6 +4,7 @@ package KiokuDB::Backend;
use Moose::Role;

use Moose::Util::TypeConstraints;
use Try::Tiny;

use namespace::clean -except => 'meta';

Expand All @@ -12,16 +13,13 @@ coerce ( __PACKAGE__,
my %p = %$_;
my $class = delete $p{class} || die "Can't coerce backend from hash without a 'class' parameter";

do {
local $@;
eval {
Class::MOP::load_class("KiokuDB::Backend::$class");
"KiokuDB::Backend::$class"->new(%p);
}
} or do {
try {
Class::MOP::load_class("KiokuDB::Backend::$class");
"KiokuDB::Backend::$class"->new(%p);
} catch {
Class::MOP::load_class($class);
$class->new(%p);
}
};
},
);

Expand Down
60 changes: 24 additions & 36 deletions lib/KiokuDB/Backend/Role/TXN.pm
Expand Up @@ -4,6 +4,7 @@ package KiokuDB::Backend::Role::TXN;
use Moose::Role;

use Carp qw(croak);
use Try::Tiny;

use namespace::clean -except => 'meta';

Expand All @@ -20,44 +21,31 @@ sub txn_do {

my @txn_args = $self->txn_begin;

my @result;

my $wantarray = wantarray; # gotta capture, eval { } has its own

my ( $success, $err ) = do {
local $@;

my $success = eval {
if ( $wantarray ) {
@result = $coderef->(@args);
} elsif( defined $wantarray ) {
$result[0] = $coderef->(@args);
} else {
$coderef->(@args);
}

$commit && $commit->();
$self->txn_commit(@txn_args);

1;
try {
my @ret;

if ( wantarray ) {
@ret = $coderef->(@args);
} elsif ( defined wantarray ) {
$ret[0] = $coderef->(@args);
} else {
$coderef->(@args);
}

$commit->() if $commit;
$self->txn_commit(@txn_args);

return wantarray ? @ret : $ret[0];
} catch {
my $err = $_;

try {
$self->txn_rollback(@txn_args);
$rollback->() if $rollback;
} catch {
croak "Transaction aborted: $err, rollback failed: $_";
};

( $success, $@ );
};

if ( $success ) {
return wantarray ? @result : $result[0];
} else {
my $rollback_exception = do {
local $@;
eval { $self->txn_rollback(@txn_args); $rollback && $rollback->() };
$@;
};

if ($rollback_exception) {
croak "Transaction aborted: $err, rollback failed: $rollback_exception";
}

die $err;
}
}
Expand Down
4 changes: 3 additions & 1 deletion lib/KiokuDB/Role/UUIDs.pm
Expand Up @@ -3,12 +3,14 @@
package KiokuDB::Role::UUIDs;
use Moose::Role;

use Try::Tiny;

use namespace::clean -except => 'meta';

if ( defined &KiokuDB::SERIAL_IDS and KiokuDB::SERIAL_IDS() ) {
with qw(KiokuDB::Role::UUIDs::SerialIDs);
} else {
my $have_libuuid = do { local $@; eval { require Data::UUID::LibUUID; 1 } };
my $have_libuuid = try { require Data::UUID::LibUUID; 1 };

my $backend = $have_libuuid ? "LibUUID" : "DataUUID";

Expand Down
7 changes: 4 additions & 3 deletions lib/KiokuDB/Test/Fixture/Concurrency.pm
Expand Up @@ -4,6 +4,7 @@ use Moose;
use Test::More;
use Test::Exception;

use Try::Tiny;
use List::Util qw(sum);
use Scope::Guard;
use POSIX qw(_exit :sys_wait_h);
Expand Down Expand Up @@ -121,7 +122,7 @@ sub check_consistency {
my ( $counter, @accounts );

attempt: foreach my $attempt ( 1 .. FORKS ) {
last attempt if eval {
last attempt if try {
$self->txn_do(sub {
my $s = $self->new_scope;

Expand Down Expand Up @@ -164,7 +165,7 @@ sub run_child {
my ( $self, $child ) = @_;

for ( 1 .. ITER ) {
eval {
try {
$self->txn_do(sub {
my $s = $self->new_scope;

Expand Down Expand Up @@ -199,7 +200,7 @@ sub run_child {
my $ok;

attempt: foreach my $attempt ( 1 .. FORKS*2 ) {
last attempt if eval {
last attempt if try {
$self->txn_do(sub {
my $s = $self->new_scope;

Expand Down
3 changes: 2 additions & 1 deletion lib/KiokuDB/Test/Fixture/TXN/Nested.pm
Expand Up @@ -5,6 +5,7 @@ use Moose;

use Test::More;
use Test::Exception;
use Try::Tiny;

use namespace::clean -except => 'meta';

Expand Down Expand Up @@ -39,7 +40,7 @@ sub verify {
my ( $db_entry ) = $self->backend->get( $self->joe );
is( $db_entry->data->{name}, "lalalala", "entry written to DB" );

eval {
try {
$self->txn_do(sub {
$joe->name("oi");
$self->update_ok($joe);
Expand Down
19 changes: 10 additions & 9 deletions lib/KiokuDB/Test/Fixture/TypeMap/Default.pm
Expand Up @@ -8,6 +8,7 @@ use Moose;
use Encode;
use Test::More;
use Test::Moose;
use Try::Tiny;

use KiokuDB::Test::Person;
use KiokuDB::Test::Employee;
Expand All @@ -18,15 +19,15 @@ use namespace::clean -except => 'meta';
use constant required_backend_roles => qw(TypeMap::Default);

use Tie::RefHash;
use constant HAVE_DATETIME => eval { require DateTime };
use constant HAVE_DATETIME_FMT => eval { require DateTime::Format::Strptime };
use constant HAVE_URI => eval { require URI };
use constant HAVE_URI_WITH_BASE => eval { require URI::WithBase };
use constant HAVE_AUTHEN_PASSPHRASE => eval { require Authen::Passphrase::SaltedDigest };
use constant HAVE_PATH_CLASS => eval { require Path::Class };
use constant HAVE_IXHASH => eval { require Tie::IxHash };
use constant HAVE_MX_TRAITS => eval { require MooseX::Traits };
use constant HAVE_MX_OP => eval { require MooseX::Object::Pluggable };
use constant HAVE_DATETIME => try { require DateTime };
use constant HAVE_DATETIME_FMT => try { require DateTime::Format::Strptime };
use constant HAVE_URI => try { require URI };
use constant HAVE_URI_WITH_BASE => try { require URI::WithBase };
use constant HAVE_AUTHEN_PASSPHRASE => try { require Authen::Passphrase::SaltedDigest };
use constant HAVE_PATH_CLASS => try { require Path::Class };
use constant HAVE_IXHASH => try { require Tie::IxHash };
use constant HAVE_MX_TRAITS => try { require MooseX::Traits };
use constant HAVE_MX_OP => try { require MooseX::Object::Pluggable };

{
package Some::Role;
Expand Down
11 changes: 6 additions & 5 deletions lib/KiokuDB/TypeMap.pm
Expand Up @@ -4,6 +4,7 @@ package KiokuDB::TypeMap;
use Moose;

use Carp qw(croak);
use Try::Tiny;

use KiokuDB::TypeMap::Entry;
use KiokuDB::TypeMap::Entry::Alias;
Expand Down Expand Up @@ -50,11 +51,11 @@ sub resolve {
unless ( $loaded{$class}++ ) {
( my $pmfile = $class . ".pm" ) =~ s{::}{/}g;

my $e = do { local $@; eval { require $pmfile }; $@ };

if ($e) {
croak $e unless $e =~ /^Can't locate \Q$pmfile\E in \@INC/;
}
try {
require $pmfile;
} catch {
croak $_ unless /^Can't locate \Q$pmfile\E in \@INC/;
};
}

# if this is an anonymous class, redo the lookup using a single named
Expand Down

0 comments on commit 0096222

Please sign in to comment.