-
Notifications
You must be signed in to change notification settings - Fork 194
/
Copy pathESConfig.pm
223 lines (205 loc) · 6.84 KB
/
ESConfig.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
use v5.20;
use warnings;
use experimental qw( signatures postderef );
package MetaCPAN::ESConfig;
use Carp qw( croak );
use Const::Fast qw( const );
use Cpanel::JSON::XS ();
use Exporter qw( import );
use Hash::Merge::Simple qw( merge );
use MetaCPAN::Server::Config ();
use MetaCPAN::Types::TypeTiny qw( Defined HashRef );
use MetaCPAN::Util qw( root_dir );
use Module::Runtime qw( $module_name_rx require_module );
const my %config => merge(
{
documents => {
author => {
index => 'author',
type => 'author',
mapping => 'es/author/mapping.json',
settings => 'es/author/settings.json',
model => 'MetaCPAN::Document::Author',
},
cve => {
index => 'cve',
type => 'cve',
mapping => 'es/cve/mapping.json',
settings => 'es/cve/settings.json',
model => 'MetaCPAN::Document::CVE',
},
contributor => {
index => 'contributor',
type => 'contributor',
mapping => 'es/contributor/mapping.json',
settings => 'es/contributor/settings.json',
model => 'MetaCPAN::Document::Contributor',
},
cover => {
index => 'cover',
type => 'cover',
mapping => 'es/cover/mapping.json',
settings => 'es/cover/settings.json',
model => 'MetaCPAN::Document::Cover',
},
distribution => {
index => 'distribution',
type => 'distribution',
mapping => 'es/distribution/mapping.json',
settings => 'es/distribution/settings.json',
model => 'MetaCPAN::Document::Distribution',
},
favorite => {
index => 'favorite',
type => 'favorite',
mapping => 'es/favorite/mapping.json',
settings => 'es/favorite/settings.json',
model => 'MetaCPAN::Document::Favorite',
},
file => {
index => 'file',
type => 'file',
mapping => 'es/file/mapping.json',
settings => 'es/file/settings.json',
model => 'MetaCPAN::Document::File',
},
mirror => {
index => 'mirror',
type => 'mirror',
mapping => 'es/mirror/mapping.json',
settings => 'es/mirror/settings.json',
model => 'MetaCPAN::Document::Mirror',
},
package => {
index => 'package',
type => 'package',
mapping => 'es/package/mapping.json',
settings => 'es/package/settings.json',
model => 'MetaCPAN::Document::Package',
},
permission => {
index => 'permission',
type => 'permission',
mapping => 'es/permission/mapping.json',
settings => 'es/permission/settings.json',
model => 'MetaCPAN::Document::Permission',
},
release => {
index => 'release',
type => 'release',
mapping => 'es/release/mapping.json',
settings => 'es/release/settings.json',
model => 'MetaCPAN::Document::Release',
},
account => {
index => 'account',
type => 'account',
mapping => 'es/account/mapping.json',
settings => 'es/account/settings.json',
model => 'MetaCPAN::Model::User::Account',
},
session => {
index => 'session',
type => 'session',
mapping => 'es/session/mapping.json',
settings => 'es/session/settings.json',
model => 'MetaCPAN::Model::User::Session',
},
},
},
MetaCPAN::Server::Config::config()->{elasticsearch} || {},
)->%*;
{
use Moo;
}
has all_indexes => (
is => 'lazy',
default => sub ($self) {
my %seen;
[
sort
grep !$seen{$_}++,
map $_->{index},
values $self->documents->%*
];
},
);
my $DefinedHash = ( HashRef [Defined] )->plus_coercions(
HashRef,
=> sub ($hash) {
return {
map {
my $value = $hash->{$_};
defined $value ? ( $_ => $value ) : ();
} keys %$hash
};
},
);
has documents => (
is => 'ro',
isa => HashRef [$DefinedHash],
coerce => 1,
required => 1,
);
sub _load_es_data ( $location, $def_sub ) {
my $data;
if ( ref $location ) {
$data = $location;
}
elsif ( $location
=~ /\A($module_name_rx)(?:::([0-9a-zA-Z_]+)\(\)|->($module_name_rx))?\z/
)
{
my ( $module, $sub, $method ) = ( $1, $2, $3 );
require_module $module;
if ($method) {
$data = $module->$method;
}
else {
$sub ||= $def_sub;
no strict 'refs';
my $code = \&{"${module}::${sub}"};
die "can't find $location"
if !defined &$code;
$data = $code->();
}
}
else {
my $abs_path = File::Spec->rel2abs( $location, root_dir() );
open my $fh, '<', $abs_path
or die "can't open mapping file $abs_path: $!";
$data = do { local $/; <$fh> };
}
return $data
if ref $data;
return Cpanel::JSON::XS::decode_json($data);
}
sub mapping ( $self, $doc ) {
my $doc_data = $self->documents->{$doc}
or croak "unknown document $doc";
return _load_es_data( $doc_data->{mapping}, 'mapping' );
}
sub index_settings ( $self, $doc ) {
my $documents = $self->documents;
my $doc_data = exists $documents->{$doc} && $documents->{$doc}
or return {};
my $settings = exists $doc_data->{settings} && $doc_data->{settings}
or return {};
return _load_es_data( $settings, 'settings' );
}
sub doc_path ( $self, $doc ) {
my $doc_data = $self->documents->{$doc}
or croak "unknown document $doc";
return (
( $doc_data->{index} ? ( index => $doc_data->{index} ) : () ),
( $doc_data->{type} ? ( type => $doc_data->{type} ) : () ),
);
}
our @EXPORT_OK = qw(
es_config
es_doc_path
);
my $single = __PACKAGE__->new(%config);
sub es_config : prototype() {$single}
sub es_doc_path ($doc) { $single->doc_path($doc) }
1;