Skip to content

Commit edb8e78

Browse files
committed
adapt Mapping script to use ESConfig module
ESConfig knows how to find mapping data. Use it to find the mapping data as well as index configuration. The mapping data should be able to be moved into json files rather than json wrapped in a module. This can happen in the future.
1 parent 1489794 commit edb8e78

File tree

1 file changed

+29
-81
lines changed

1 file changed

+29
-81
lines changed

lib/MetaCPAN/Script/Mapping.pm

Lines changed: 29 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,11 @@ package MetaCPAN::Script::Mapping;
22

33
use Moose;
44

5-
use Cpanel::JSON::XS qw( decode_json );
6-
use DateTime ();
7-
use Log::Contextual qw( :log );
8-
use MetaCPAN::Script::Mapping::Contributor ();
9-
use MetaCPAN::Script::Mapping::Cover ();
10-
use MetaCPAN::Script::Mapping::CPAN::Author ();
11-
use MetaCPAN::Script::Mapping::CPAN::Distribution ();
12-
use MetaCPAN::Script::Mapping::CPAN::Favorite ();
13-
use MetaCPAN::Script::Mapping::CPAN::File ();
14-
use MetaCPAN::Script::Mapping::CPAN::Mirror ();
15-
use MetaCPAN::Script::Mapping::CPAN::Package ();
16-
use MetaCPAN::Script::Mapping::CPAN::Permission ();
17-
use MetaCPAN::Script::Mapping::CPAN::Release ();
18-
use MetaCPAN::Script::Mapping::CVE ();
19-
use MetaCPAN::Script::Mapping::DeployStatement ();
20-
use MetaCPAN::Script::Mapping::User::Account ();
21-
use MetaCPAN::Script::Mapping::User::Identity ();
22-
use MetaCPAN::Script::Mapping::User::Session ();
23-
use MetaCPAN::Types::TypeTiny qw( Bool Str );
5+
use Cpanel::JSON::XS qw( decode_json );
6+
use DateTime ();
7+
use Log::Contextual qw( :log );
8+
use MetaCPAN::ESConfig qw( es_config );
9+
use MetaCPAN::Types::TypeTiny qw( Bool Str );
2410

2511
use constant {
2612
EXPECTED => 1,
@@ -29,13 +15,6 @@ use constant {
2915

3016
with 'MetaCPAN::Role::Script', 'MooseX::Getopt';
3117

32-
has cpan_index => (
33-
is => 'ro',
34-
isa => Str,
35-
default => 'cpan_v1_01',
36-
documentation => 'real name for the cpan index',
37-
);
38-
3918
has arg_deploy_mapping => (
4019
init_arg => 'delete',
4120
is => 'ro',
@@ -285,11 +264,9 @@ sub update_index {
285264
die "update_index requires patch_mapping\n"
286265
unless $self->patch_mapping;
287266

288-
my $patch_mapping = decode_json $self->patch_mapping;
289-
my @patch_types = sort keys %{$patch_mapping};
290-
my $dep = $self->index->deployment_statement;
291-
my $existing_mapping = delete $dep->{mappings};
292-
my $mapping = +{ map { $_ => $patch_mapping->{$_} } @patch_types };
267+
my $patch_mapping = decode_json $self->patch_mapping;
268+
my @patch_types = sort keys %{$patch_mapping};
269+
my $mapping = +{ map { $_ => $patch_mapping->{$_} } @patch_types };
293270

294271
log_info {"Updating mapping for index: $name"};
295272

@@ -311,15 +288,14 @@ sub create_index {
311288
my $dst_idx = $self->arg_create_index;
312289
$self->_check_index_exists( $dst_idx, NOT_EXPECTED );
313290

314-
my $patch_mapping = decode_json $self->patch_mapping;
315-
my @patch_types = sort keys %{$patch_mapping};
316-
my $dep = $self->index->deployment_statement;
317-
delete $dep->{mappings};
318-
my $mapping = +{};
291+
my $patch_mapping = decode_json $self->patch_mapping;
292+
my @patch_types = sort keys %{$patch_mapping};
293+
my $index_settings = es_config->index_settings($dst_idx);
294+
my $mapping = +{};
319295

320296
# create the new index with the copied settings
321297
log_info {"Creating index: $dst_idx"};
322-
$self->es->indices->create( index => $dst_idx, body => $dep );
298+
$self->es->indices->create( index => $dst_idx, body => $index_settings );
323299

324300
# override with new type mapping
325301
if ( $self->patch_mapping ) {
@@ -489,51 +465,25 @@ sub show_info {
489465
}
490466

491467
sub _build_mapping {
492-
my $self = $_[0];
493-
return {
494-
$self->cpan_index => {
495-
author =>
496-
decode_json(MetaCPAN::Script::Mapping::CPAN::Author::mapping),
497-
distribution => decode_json(
498-
MetaCPAN::Script::Mapping::CPAN::Distribution::mapping),
499-
favorite => decode_json(
500-
MetaCPAN::Script::Mapping::CPAN::Favorite::mapping),
501-
file =>
502-
decode_json(MetaCPAN::Script::Mapping::CPAN::File::mapping),
503-
mirror =>
504-
decode_json(MetaCPAN::Script::Mapping::CPAN::Mirror::mapping),
505-
permission => decode_json(
506-
MetaCPAN::Script::Mapping::CPAN::Permission::mapping),
507-
package => decode_json(
508-
MetaCPAN::Script::Mapping::CPAN::Package::mapping),
509-
release => decode_json(
510-
MetaCPAN::Script::Mapping::CPAN::Release::mapping),
511-
},
468+
my $self = $_[0];
469+
my $docs = es_config->documents;
470+
my $mappings = {};
471+
for my $name ( sort keys %$docs ) {
472+
my $doc = $docs->{$name};
473+
my $index = $doc->{index}
474+
or die "no index defined for $name documents";
475+
my $type = $doc->{type}
476+
or die "no type defined for $name documents";
477+
my $mapping = es_config->mapping($name);
478+
$mappings->{$index}{$type} = $mapping;
479+
}
512480

513-
user => {
514-
account => decode_json(
515-
MetaCPAN::Script::Mapping::User::Account::mapping),
516-
identity => decode_json(
517-
MetaCPAN::Script::Mapping::User::Identity::mapping),
518-
session => decode_json(
519-
MetaCPAN::Script::Mapping::User::Session::mapping),
520-
},
521-
contributor => {
522-
contributor =>
523-
decode_json(MetaCPAN::Script::Mapping::Contributor::mapping),
524-
},
525-
cover => {
526-
cover => decode_json(MetaCPAN::Script::Mapping::Cover::mapping),
527-
},
528-
cve => {
529-
cve => decode_json(MetaCPAN::Script::Mapping::CVE::mapping),
530-
},
531-
};
481+
return $mappings;
532482
}
533483

534484
sub _build_aliases {
535485
my $self = $_[0];
536-
return { 'cpan' => $self->cpan_index };
486+
es_config->aliases;
537487
}
538488

539489
sub deploy_mapping {
@@ -546,18 +496,16 @@ sub deploy_mapping {
546496
# Deserialize the Index Mapping Structure
547497
my $rmappings = $self->_build_mapping;
548498

549-
my $deploy_statement
550-
= decode_json(MetaCPAN::Script::Mapping::DeployStatement::mapping);
551-
552499
my $es = $self->es;
553500

554501
# recreate the indices and apply the mapping
555502

556503
for my $idx ( sort keys %$rmappings ) {
557504
$self->_delete_index($idx) if $es->indices->exists( index => $idx );
505+
my $index_settings = es_config->index_settings($idx);
558506

559507
log_info {"Creating index: $idx"};
560-
$es->indices->create( index => $idx, body => $deploy_statement );
508+
$es->indices->create( index => $idx, body => $index_settings );
561509

562510
for my $type ( sort keys %{ $rmappings->{$idx} } ) {
563511
log_info {"Adding mapping: $idx/$type"};

0 commit comments

Comments
 (0)