| 1 | package Data::ObjectDriver::Driver::SimplePartition; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use base qw( Data::ObjectDriver::Driver::Partition ); | ||
| 5 | |||
| 6 | use Carp qw( croak ); | ||
| 7 | use Data::Dumper; | ||
| 8 | use Data::ObjectDriver::Driver::MultiPartition; | ||
| 9 | |||
| 10 | sub init { | ||
| 11 | my $driver = shift; | ||
| 12 | my %param = @_; | ||
| 13 | my $class = delete $param{using} or croak "using is required"; | ||
| 14 | my @extra = %param; | ||
| 15 | $param{get_driver} = _make_get_driver($class, \@extra); | ||
| 16 | $driver->SUPER::init(%param); | ||
| 17 | return $driver; | ||
| 18 | } | ||
| 19 | |||
| 20 | sub _make_get_driver { | ||
| 21 | my($class, $extra) = @_; | ||
| 22 | $extra ||= []; | ||
| 23 | |||
| 24 | ## Make sure we've loaded the parent class that contains information | ||
| 25 | ## about our partitioning scheme. | ||
| 26 | croak "Bogus classname." unless $class =~ /^[\w:]+$/; | ||
| 27 | "eval $class; 1;" or die "Failed to load parent class: $@\n"; | ||
| 28 | |||
| 29 | my $col = $class->primary_key_tuple->[0]; | ||
| 30 | my $get_driver = $class->properties->{partition_get_driver} | ||
| 31 | or croak "Partitioning driver not defined for $class"; | ||
| 32 | |||
| 33 | my $number = $class->properties->{number_of_partitions}; | ||
| 34 | my $mp_driver = Data::ObjectDriver::Driver::MultiPartition->new( | ||
| 35 | partitions => [ map { $get_driver->($_, @$extra) } (1 .. $number) ] | ||
| 36 | ); | ||
| 37 | |||
| 38 | return sub { | ||
| 39 | my($terms, $args) = @_; | ||
| 40 | my $parent_id; | ||
| 41 | if (ref($terms) eq 'HASH') { | ||
| 42 | $parent_id = $terms->{ $col }; | ||
| 43 | } elsif (ref($terms) eq 'ARRAY') { | ||
| 44 | ## An array ref could either be a multiple-column primary key OR | ||
| 45 | ## a list of primary keys. With a multiple-column primary key, the | ||
| 46 | ## $id is an array ref, where the first column is always the | ||
| 47 | ## parent_id. | ||
| 48 | $parent_id = ref($terms->[0]) eq 'ARRAY' ? | ||
| 49 | $terms->[0][0] : $terms->[0]; | ||
| 50 | } | ||
| 51 | if ($parent_id) { | ||
| 52 | my $parent = $class->driver->lookup($class, $parent_id) | ||
| 53 | or croak "Member of $class with ID $parent_id not found"; | ||
| 54 | return $get_driver->( $parent->partition_id, @$extra ); | ||
| 55 | } else { | ||
| 56 | unless($args->{multi_partition}) { | ||
| 57 | croak "Cannot extract $col from terms ", Dumper($terms); | ||
| 58 | } | ||
| 59 | return $mp_driver; | ||
| 60 | } | ||
| 61 | }; | ||
| 62 | } | ||
| 63 | |||
| 64 | 1; | ||
| 65 | |||
| 66 | __END__ |