| 1 | 4 | ▶ | use strictures 1; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 2 | package Mojito::Collection::CRUD::Elasticsearch; | ||||||||||||||||||||||||||||||||||
| 3 | 4 | $Mojito::Collection::CRUD::Elasticsearch::VERSION = '0.25'; | |||||||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use MongoDB::OID; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use 5.010; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use List::Util qw/first/; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Syntax::Keyword::Junction qw/ any /; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use Data::Dumper::Concise; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||
| 11 | 4 | with('Mojito::Role::DB::Elasticsearch'); | |||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||
| 13 | 4 | has base_url => ( is => 'rw', ); | |||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||
| 15 | =head1 Methods | ||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||
| 17 | =head2 create | ||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||
| 19 | Create a list of document ids in the collection named 'collection'. | ||||||||||||||||||||||||||||||||||
| 20 | The funny name is due to the fact we're storing document ids from | ||||||||||||||||||||||||||||||||||
| 21 | the notes collection in individual documents of the collection collection. | ||||||||||||||||||||||||||||||||||
| 22 | i.e. we are creating (ordered) sets of documents identified with | ||||||||||||||||||||||||||||||||||
| 23 | a document name. | ||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||
| 25 | The motivation for collections of docs collection is so that we can group | ||||||||||||||||||||||||||||||||||
| 26 | documents together form display purposes. Form example, say I'm working | ||||||||||||||||||||||||||||||||||
| 27 | on a project with multiple documents. I'd like to be able to "identify/tag" | ||||||||||||||||||||||||||||||||||
| 28 | them to the project so I can narrow my viewing focus to just those documents. | ||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||
| 30 | =cut | ||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | sub create { | ||||||||||||||||||||||||||||||||
|
Subroutine
create: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 33 | 0 | my ( $self, $params ) = @_; | |||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||
| 35 | # We don't need to store the form submit value | ||||||||||||||||||||||||||||||||||
| 36 | 0 | delete $params->{collect}; | |||||||||||||||||||||||||||||||||
| 37 | 0 | my $page_ids = $params->{collected_page_ids}; | |||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||
| 39 | # NOTE: The $page_ids input can come in two different forms: | ||||||||||||||||||||||||||||||||||
| 40 | # - String: "$page_id1,$page_id2,...$page_id_n"; | ||||||||||||||||||||||||||||||||||
| 41 | # - ArrayRef: [$page_id1,$page_id2,..., $page_id_n]; | ||||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | $params->{collected_page_ids} = [split ',', $page_ids] if (!ref($page_ids)); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||
| 44 | # add save time as last_modified and created | ||||||||||||||||||||||||||||||||||
| 45 | 0 | $params->{last_modified} = $params->{created} = time(); | |||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||
| 47 | 0 | my $collection_struct = $self->collection->{hits}{hits}; | |||||||||||||||||||||||||||||||||
| 48 | 0 | my @collections = map { $_->{_source} } @{$collection_struct}; | |||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | my $collection = first { $_->{collection_name} eq $params->{collection_name} } @collections; | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 50 | $params->{id} = | ||||||||||||||||||||||||||||||||||
| 51 | $collection->{_id} | ||||||||||||||||||||||||||||||||||
| 52 | || $collection->{id} | ||||||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | || $self->generate_mongo_like_oid; | ||||||||||||||||||||||||||||||||
|
Condition0%0/6
$collection->{'_id'} || $collection->{'id'} || $self->generate_mongo_like_oid
$collection->{"_id"} or $collection->{"id"}
MC/DC0%0/3
$collection->{'_id'} || $collection->{'id'} || $self->generate_mongo_like_oid
$collection->{"_id"}
$collection->{"id"}
$self->generate_mongo_like_oid
Truth table0%0/4
A or B || C
A= $collection->{"_id"}B= $collection->{"id"}C= $self->generate_mongo_like_oid
| |||||||||||||||||||||||||||||||||||
| 54 | $self->db->index( | ||||||||||||||||||||||||||||||||||
| 55 | index => $self->db_name, | ||||||||||||||||||||||||||||||||||
| 56 | type => $self->collection_name, | ||||||||||||||||||||||||||||||||||
| 57 | id => $params->{id}, | ||||||||||||||||||||||||||||||||||
| 58 | 0 | body => $params, | |||||||||||||||||||||||||||||||||
| 59 | ); | ||||||||||||||||||||||||||||||||||
| 60 | 0 | return $params->{id}; | |||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||
| 63 | =head2 read | ||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||
| 65 | Read a collection from the database. | ||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||
| 67 | =cut | ||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | sub read { | ||||||||||||||||||||||||||||||||
|
Subroutine
read: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 70 | 0 | my ( $self, $id ) = @_; | |||||||||||||||||||||||||||||||||
| 71 | 0 | my $doc = $self->db->get_source( | |||||||||||||||||||||||||||||||||
| 72 | index => $self->db_name, | ||||||||||||||||||||||||||||||||||
| 73 | type => $self->collection_name, | ||||||||||||||||||||||||||||||||||
| 74 | id => $id, | ||||||||||||||||||||||||||||||||||
| 75 | ); | ||||||||||||||||||||||||||||||||||
| 76 | 0 | return $doc; | |||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||
| 79 | =head2 update | ||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||
| 81 | Update a collection in the database. | ||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||
| 83 | =cut | ||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | sub update { | ||||||||||||||||||||||||||||||||
|
Subroutine
update: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 86 | 0 | my ( $self, $id, $params) = @_; | |||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||
| 88 | 0 | $params->{last_modified} = time(); | |||||||||||||||||||||||||||||||||
| 89 | 0 | $self->db->update( | |||||||||||||||||||||||||||||||||
| 90 | index => $self->db_name, | ||||||||||||||||||||||||||||||||||
| 91 | type => $self->collection_name, | ||||||||||||||||||||||||||||||||||
| 92 | id => $id, | ||||||||||||||||||||||||||||||||||
| 93 | body => { doc => $params }, | ||||||||||||||||||||||||||||||||||
| 94 | ); | ||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||
| 97 | =head2 delete | ||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||
| 99 | Delete a collection from the database. | ||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||
| 101 | =cut | ||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | sub delete { | ||||||||||||||||||||||||||||||||
|
Subroutine
delete: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 104 | 0 | my ( $self, $id ) = @_; | |||||||||||||||||||||||||||||||||
| 105 | 0 | $self->collection->delete($id); | |||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||
| 108 | =head2 get_all | ||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||
| 110 | Get all pages in the notes collection. | ||||||||||||||||||||||||||||||||||
| 111 | Returns a MongoDB cursor one can iterate over. | ||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||
| 113 | =cut | ||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | sub get_all { | ||||||||||||||||||||||||||||||||
|
Subroutine
get_all: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 116 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 117 | 0 | return $self->collection->{hits}{hits}; | |||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||
| 120 | =head2 collection_for_page | ||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||
| 122 | Get all the collection ids for which this page is a member of. | ||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||
| 124 | =cut | ||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | sub collections_for_page { | ||||||||||||||||||||||||||||||||
|
Subroutine
collections_for_page: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 127 | 0 | my ( $self, $page_id ) = @_; | |||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||
| 129 | 0 | ▶ | $page_id //= ''; | ||||||||||||||||||||||||||||||||
|
Condition0%0/2
$page_id //= ""
MC/DC0%0/1
$page_id //= ""
$page_id
Truth table0%0/2
A
A= $page_id
| |||||||||||||||||||||||||||||||||||
| 130 | 0 | my @collection_ids = (); | |||||||||||||||||||||||||||||||||
| 131 | 0 | my @collections = map { $_->{_source} } @{$self->get_all}; | |||||||||||||||||||||||||||||||||
| 132 | 0 | foreach my $collection (@collections) { | |||||||||||||||||||||||||||||||||
| 133 | 0 | ▶ | if ($page_id eq any(@{$collection->{collected_page_ids}})) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 134 | 0 | push @collection_ids, $collection->{id}; | |||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||
| 138 | 0 | return @collection_ids; | |||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||
| 141 | 0 | ▶ | sub update_collection_membership { | ||||||||||||||||||||||||||||||||
|
Subroutine
update_collection_membership: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 142 | 0 | my ($self, $params) = @_; | |||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||
| 144 | 0 | my $collection_ids = $params->{collection_select}; | |||||||||||||||||||||||||||||||||
| 145 | # Want to coerce (single select) SCALAR into an ArrayRef (happens w/ Dancer params) | ||||||||||||||||||||||||||||||||||
| 146 | 0 | ▶ | if (ref($collection_ids) ne 'ARRAY') { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 147 | 0 | ▶ | warn "Coercing collection select params into an ArrefRef" if $ENV{MOJITO_DEBUG}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 148 | 0 | $collection_ids = [$collection_ids]; | |||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||
| 151 | my $scroll = $self->db->scroll_helper( | ||||||||||||||||||||||||||||||||||
| 152 | index => $self->db_name, | ||||||||||||||||||||||||||||||||||
| 153 | type => $self->collection_name, | ||||||||||||||||||||||||||||||||||
| 154 | body => {query => {term => {collected_page_ids => $params->{mongo_id}}}}, | ||||||||||||||||||||||||||||||||||
| 155 | 0 | ); | |||||||||||||||||||||||||||||||||
| 156 | 0 | my %HAVE; | |||||||||||||||||||||||||||||||||
| 157 | 0 | ▶ | while (my $collection = $scroll->next) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 158 | 0 | $HAVE{$collection->{_source}{id}} = 1; | |||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||
| 160 | 0 | my %WANT = map { $_ => 1 } @{$collection_ids}; | |||||||||||||||||||||||||||||||||
| 161 | 0 | foreach my $collection_id (keys %WANT) { | |||||||||||||||||||||||||||||||||
| 162 | 0 | ▶ | if (not $HAVE{$collection_id}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 163 | # add page_id to the collection | ||||||||||||||||||||||||||||||||||
| 164 | 0 | my $collection = $self->read($collection_id); | |||||||||||||||||||||||||||||||||
| 165 | 0 | push @{$collection->{collected_page_ids}}, $params->{mongo_id}; | |||||||||||||||||||||||||||||||||
| 166 | 0 | $self->update($collection_id, $collection); | |||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||
| 169 | 0 | foreach my $collection_id (keys %HAVE) { | |||||||||||||||||||||||||||||||||
| 170 | 0 | ▶ | if (not $WANT{$collection_id}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 171 | # remove the page_id from the collection | ||||||||||||||||||||||||||||||||||
| 172 | 0 | my $collection = $self->read($collection_id); | |||||||||||||||||||||||||||||||||
| 173 | 0 | my @collected_page_ids = grep { $_ ne $params->{mongo_id} } | |||||||||||||||||||||||||||||||||
| 174 | 0 | @{$collection->{collected_page_ids}}; | |||||||||||||||||||||||||||||||||
| 175 | 0 | $self->update( | |||||||||||||||||||||||||||||||||
| 176 | $collection_id, | ||||||||||||||||||||||||||||||||||
| 177 | {collected_page_ids => \@collected_page_ids}, | ||||||||||||||||||||||||||||||||||
| 178 | ); | ||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||
| 182 | 0 | return; | |||||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||||
| 184 | =head2 BUILD | ||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||
| 186 | Set the collection we want to work with. | ||||||||||||||||||||||||||||||||||
| 187 | In this case it's the collection named 'collection'. | ||||||||||||||||||||||||||||||||||
| 188 | It's a bit meta is why the funny naming. | ||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||
| 190 | =cut | ||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||
| 192 | 1 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 193 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 194 | 1 | $self->collection_name('collection'); | |||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||
| 197 | 4 | 1 | |||||||||||||||||||||||||||||||||