| 1 | package DBIx::NoSQL::Model::Index; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use Any::Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 7 | ▶ | use Clone qw/ clone /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 7 | ▶ | use Digest::SHA qw/ sha1_hex /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 7 | ▶ | use DBIx::NoSQL::Search; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 7 | has model => qw/ is ro required 1 weak_ref 1 /, handles => [qw/ store storage /]; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 7 | has prepared => qw/ is rw isa Bool default 0 /; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 7 | has key_column => qw/ is rw isa Str lazy_build 1 /; | |||||||||||||||||||||||||||
| 16 | 9 | ▶ | sub _build_key_column { 'key' } | ||||||||||||||||||||||||||
|
Subroutine
_build_key_column: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 7 | has [qw/ create_statement drop_statement schema_digest /] => qw/ is rw isa Maybe[Str] /; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 7 | has result_class_scaffold => qw/ is ro lazy_build 1 /; | |||||||||||||||||||||||||||
| 21 | 9 | ▶ | sub _build_result_class_scaffold { return DBIx::NoSQL::ClassScaffold->new->become_ResultClass } | ||||||||||||||||||||||||||
|
Subroutine
_build_result_class_scaffold: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 7 | has result_class => qw/ is ro lazy_build 1 /; | |||||||||||||||||||||||||||
| 23 | 9 | ▶ | sub _build_result_class { return shift->result_class_scaffold->package } | ||||||||||||||||||||||||||
|
Subroutine
_build_result_class: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | sub search { | ||||||||||||||||||||||||||||
| 26 | 16 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
search: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 16 | $self->prepare; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 16 | my $search = DBIx::NoSQL::Search->new( model => $self->model ); | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 16 | ▶ | if ( @_ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 7 | $search->_where( $_[0] ); | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 16 | return $search; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | sub update { | ||||||||||||||||||||||||||||
| 40 | 23 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
update: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 41 | 23 | my $key = shift; | |||||||||||||||||||||||||||
| 42 | 23 | my $target = shift; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 23 | $self->prepare; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 23 | my $model = $self->model; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 23 | my $data = $target; | |||||||||||||||||||||||||||
| 49 | 23 | ▶ | if ( $data && ! ref $data ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$data and not ref $data
MC/DC50%1/2
$data and not ref $data
$data
not ref $data
Truth table66%2/3
A and not(B)
A= $dataB= not ref $data
| |||||||||||||||||||||||||||||
| 50 | 8 | $data = $model->deserialize( $target ); | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 23 | my %set; | |||||||||||||||||||||||||||
| 54 | 23 | $set{ $self->key_column } = $key; | |||||||||||||||||||||||||||
| 55 | 23 | ▶ | while( my ( $field, $column ) = each %{ $model->_field2column_map } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 23 | $set{ $column } = $data->{ $field }; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 23 | $self->store->schema->resultset( $self->model->name )->update_or_create( | |||||||||||||||||||||||||||
| 60 | \%set, { key => 'primary' }, | ||||||||||||||||||||||||||||
| 61 | ); | ||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | sub delete { | ||||||||||||||||||||||||||||
| 65 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
delete: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 66 | 1 | my $key = shift; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 1 | $self->prepare; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 1 | my $result = $self->store->schema->resultset( $self->model->name )->find( | |||||||||||||||||||||||||||
| 71 | { $self->key_column => $key }, | ||||||||||||||||||||||||||||
| 72 | { key => 'primary' } | ||||||||||||||||||||||||||||
| 73 | ); | ||||||||||||||||||||||||||||
| 74 | 1 | ▶ | if ( $result ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 1 | $result->delete; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | sub prepare { | ||||||||||||||||||||||||||||
| 80 | 40 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
prepare: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 40 | ▶ | return if $self->prepared; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 10 | $self->register_result_class; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 10 | ▶ | if ( ! $self->exists ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 87 | 8 | $self->deploy; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | elsif ( ! $self->same ) { | ||||||||||||||||||||||||||||
| 90 | 1 | if ( 1 ) { | |||||||||||||||||||||||||||
| 91 | 1 | $self->redeploy; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | else { | ||||||||||||||||||||||||||||
| 94 | my $model = $self->model->name; | ||||||||||||||||||||||||||||
| 95 | die "Unable to prepare index for model ($model) because index already exists (and is different)"; | ||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 10 | $self->prepared( 1 ); | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | sub register_result_class { | ||||||||||||||||||||||||||||
| 103 | 15 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
register_result_class: called CC=9 SCAR=22.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 15 | my $model = $self->model; | |||||||||||||||||||||||||||
| 106 | 15 | my $store = $self->store; | |||||||||||||||||||||||||||
| 107 | 15 | my $schema = $store->schema; | |||||||||||||||||||||||||||
| 108 | 15 | my $name = $self->model->name; | |||||||||||||||||||||||||||
| 109 | 15 | my $result_class = $self->result_class; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 15 | ▶ | $schema->unregister_source( $name ) if $schema->source_registrations->{ $name }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | { | ||||||||||||||||||||||||||||
| 114 | 15 | ▶ | unless ( $result_class->can( 'result_source_instance' ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 9 | $result_class->table( $name ); | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 15 | my $key_column = $self->key_column; | |||||||||||||||||||||||||||
| 119 | 15 | ▶ | unless( $result_class->has_column( $key_column ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 9 | $result_class->add_column( $key_column => { | |||||||||||||||||||||||||||
| 121 | data_type => 'text' | ||||||||||||||||||||||||||||
| 122 | } ); | ||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | 15 | ▶ | unless( $result_class->primary_columns ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 9 | $result_class->set_primary_key( $key_column ); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 15 | for my $field ( values %{ $model->field_map } ) { | |||||||||||||||||||||||||||
| 129 | 27 | ▶ | next unless $field->index; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 27 | ▶ | unless( $result_class->has_column( $field->name ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 12 | $field->install_index( $model, $result_class ); | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 15 | $schema->register_class( $name => $result_class ); | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 15 | my $table = $result_class->table; | |||||||||||||||||||||||||||
| 139 | 15 | my $deployment_statements = $schema->build_deployment_statements; | |||||||||||||||||||||||||||
| 140 | 15 | my @deployment_statements = split m/;\n/, $deployment_statements; | |||||||||||||||||||||||||||
| 141 | 15 | my ( $create ) = grep { m/(?:(?i)CREATE\s+TABLE\s+)$table/ } @deployment_statements; | |||||||||||||||||||||||||||
| 142 | 15 | my ( $drop ) = grep { m/(?:(?i)DROP\s+TABLE\s+.*)$table/ } @deployment_statements; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 15 | s/^\s*//, s/\s*$// for $create, $drop; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 15 | $self->create_statement( $create ); | |||||||||||||||||||||||||||
| 147 | 15 | $self->drop_statement( $drop ); | |||||||||||||||||||||||||||
| 148 | 15 | $self->schema_digest( sha1_hex $create ); | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | sub stash_schema_digest { | ||||||||||||||||||||||||||||
| 152 | 15 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
stash_schema_digest: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 153 | 15 | my $model = $self->model->name; | |||||||||||||||||||||||||||
| 154 | 15 | return $self->store->stash->value( "model.$model.index.schema_digest", @_ ); | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | sub exists { | ||||||||||||||||||||||||||||
| 158 | 19 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
exists: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 19 | return $self->storage->table_exists( $self->model->name ); | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | sub same { | ||||||||||||||||||||||||||||
| 164 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
same: called CC=3 SCAR=11.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 3 | ▶ | return unless my $stash_schema_digest = $self->stash_schema_digest; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 3 | ▶ | return unless my $schema_digest = $self->schema_digest; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 3 | return $schema_digest eq $stash_schema_digest; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | sub deploy { | ||||||||||||||||||||||||||||
| 172 | 8 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
deploy: called CC=3 SCAR=17.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 8 | ▶ | if ( $self->exists ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | 0 | ▶ | if ( $self->same ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 0 | return; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | else { | ||||||||||||||||||||||||||||
| 179 | 0 | my $model = $self->model->name; | |||||||||||||||||||||||||||
| 180 | 0 | warn "Index schema mismatch for model ($model)"; | |||||||||||||||||||||||||||
| 181 | 0 | return; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 8 | $self->_deploy; | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | sub _deploy { | ||||||||||||||||||||||||||||
| 189 | 12 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_deploy: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 190 | 12 | $self->store->storage->do( $self->create_statement ); | |||||||||||||||||||||||||||
| 191 | 12 | $self->stash_schema_digest( $self->schema_digest ); | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | sub undeploy { | ||||||||||||||||||||||||||||
| 195 | 4 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
undeploy: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 196 | 4 | $self->store->storage->do( $self->drop_statement ); | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | sub redeploy { | ||||||||||||||||||||||||||||
| 200 | 4 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
redeploy: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 201 | 4 | my %options = @_; | |||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 4 | ▶ | exists $options{ $_ } or $options{ $_ } = 1 for qw/ register /; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 4 | ▶ | $self->register_result_class if $options{ register }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | 4 | $self->undeploy; | |||||||||||||||||||||||||||
| 207 | 4 | $self->_deploy; | |||||||||||||||||||||||||||
| 208 | 4 | $self->reload; | |||||||||||||||||||||||||||
| 209 | 4 | $self->prepared( 1 ); | |||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | sub reload { | ||||||||||||||||||||||||||||
| 213 | 4 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
reload: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 4 | my @result = $self->model->_store_set->search( { __model__ => $self->model->name } )->all; | |||||||||||||||||||||||||||
| 216 | 4 | for my $result ( @result ) { | |||||||||||||||||||||||||||
| 217 | 8 | $self->update( $result->get_column( '__key__' ), $result->get_column( '__value__' ) ); | |||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | sub reset { | ||||||||||||||||||||||||||||
| 222 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
reset: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 223 | 1 | $self->register_result_class; | |||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 3 | ▶ | sub reindex { return shift->redeploy( @_ ) } | ||||||||||||||||||||||||||
|
Subroutine
reindex: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 227 | 0 | ▶ | sub migrate { return shift->redeploy( @_ ) } | ||||||||||||||||||||||||||
|
Subroutine
migrate: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | 7 | 1; | |||||||||||||||||||||||||||