| 1 | use strictures 1; | ||
| 2 | package Mojito::Model::Transform; | ||
| 3 | $Mojito::Model::Transform::VERSION = '0.25'; | ||
| 4 | use Moo; | ||
| 5 | use MooX::Types::MooseLike::Base qw/Object ArrayRef/; | ||
| 6 | use Mojito::Page::CRUD::Mongo; | ||
| 7 | use Mojito::Model::Config; | ||
| 8 | use DBM::Deep; | ||
| 9 | use Data::Dumper::Concise; | ||
| 10 | use Search::Elasticsearch | ||
| 11 | |||
| 12 | # Move records from Mongodb to Elasticsearch or DBM::Deep | ||
| 13 | has transfer_type => ( | ||
| 14 | is => 'lazy', | ||
| 15 | builder => sub { 'elasticsearch' }, | ||
| 16 | ); | ||
| 17 | has db_host => ( | ||
| 18 | is => 'lazy', | ||
| 19 | builder => sub { 'localhost:9200' }, | ||
| 20 | ); | ||
| 21 | has db_file => ( | ||
| 22 | is => 'rw', | ||
| 23 | lazy => 1, | ||
| 24 | default => sub { Mojito::Model::Config->new->config->{dbm_deep_filepath} }, | ||
| 25 | ); | ||
| 26 | has editer => ( | ||
| 27 | is => 'ro', | ||
| 28 | isa => Object, | ||
| 29 | lazy => 1, | ||
| 30 | default => sub { Mojito::Page::CRUD::Mongo->new }, | ||
| 31 | ); | ||
| 32 | has frigo => ( | ||
| 33 | is => 'lazy', | ||
| 34 | isa => Object, | ||
| 35 | builder => sub { | ||
| 36 | my $self = shift; | ||
| 37 | my $frigo; | ||
| 38 | my $transfer_type = $self->transfer_type; | ||
| 39 | if ($transfer_type eq 'deep') { | ||
| 40 | $frigo = DBM::Deep->new($_[0]->db_file); | ||
| 41 | } | ||
| 42 | elsif ($transfer_type eq 'elasticsearch') { | ||
| 43 | $frigo = Search::Elasticsearch->new( | ||
| 44 | nodes => [$self->db_host], | ||
| 45 | ); | ||
| 46 | } | ||
| 47 | return $frigo; | ||
| 48 | }, | ||
| 49 | ); | ||
| 50 | has collections => ( | ||
| 51 | is => 'ro', | ||
| 52 | isa => ArrayRef, | ||
| 53 | lazy => 1, | ||
| 54 | default => sub { [qw/notes collection users/] }, | ||
| 55 | ); | ||
| 56 | |||
| 57 | sub transfer_records { | ||
| 58 | my ($self, $collection, @ids) = @_; | ||
| 59 | foreach my $doc_id (@ids) { | ||
| 60 | $self->transfer_record($collection, $doc_id); | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | # Use mongo id as the key, but remove it's object from the doc | ||
| 65 | sub transfer_record { | ||
| 66 | my ($self, $collection, $doc_id) = @_; | ||
| 67 | my $doc = $self->get_mongo_record($collection, $doc_id); | ||
| 68 | # Remove the MongoDB::OID object, and replace it with just the oid string for the id key's value | ||
| 69 | delete $doc->{_id}; | ||
| 70 | $doc->{id} = $doc_id; | ||
| 71 | $self->set_record($collection, $doc_id, $doc); | ||
| 72 | } | ||
| 73 | sub get_mongo_record { | ||
| 74 | my ($self, $collection, $id) = @_; | ||
| 75 | $self->set_collection_name($collection); | ||
| 76 | my $doc = $self->editer->read($id); | ||
| 77 | } | ||
| 78 | sub set_record { | ||
| 79 | my ($self, $collection, $id, $record) = @_; | ||
| 80 | print "SETTING Doc entitled: ", $record->{title}, "\n" if $record->{title}; | ||
| 81 | print "SETTING Collection named: ", $record->{collection_name}, "\n" if $record->{collection_name}; | ||
| 82 | print "SETTING User named: ", $record->{username}, "\n" if $record->{username}; | ||
| 83 | my $transfer_type = $self->transfer_type; | ||
| 84 | if ($transfer_type eq 'deep') { | ||
| 85 | $self->frigo->{$collection}->{$id} = $record; | ||
| 86 | } | ||
| 87 | elsif ($transfer_type eq 'elasticsearch') { | ||
| 88 | $self->frigo->index( | ||
| 89 | id => $id, | ||
| 90 | type => $collection, | ||
| 91 | index => Mojito::Model::Config->new->config->{es_index} || 'mojito', | ||
| 92 | body => $record, | ||
| 93 | ); | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | sub list_mongo_ids { | ||
| 98 | my ($self, $collection) = @_; | ||
| 99 | |||
| 100 | # Clear collection since it's lazy and we want it rebuilt | ||
| 101 | # to ensure we're using the collection passed instead of any default | ||
| 102 | $self->set_collection_name($collection); | ||
| 103 | my $cursor = $self->editer->get_all; | ||
| 104 | |||
| 105 | my @ids; | ||
| 106 | while (my $doc = $cursor->next) { | ||
| 107 | my $id = $doc->{_id}->value; | ||
| 108 | push @ids, $id; | ||
| 109 | } | ||
| 110 | return @ids; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub set_collection_name { | ||
| 114 | my ($self, $collection) = @_; | ||
| 115 | $self->editer->clear_collection_name; | ||
| 116 | $self->editer->clear_collection; | ||
| 117 | $self->editer->collection_name($collection); | ||
| 118 | } | ||
| 119 | |||
| 120 | |||
| 121 | =head2 BUILD | ||
| 122 | |||
| 123 | Set a test DB when RELEASE_TESTING | ||
| 124 | |||
| 125 | =cut | ||
| 126 | |||
| 127 | sub BUILD { | ||
| 128 | my ($self) = (shift); | ||
| 129 | if ($ENV{RELEASE_TESTING}) { | ||
| 130 | my $test_deep_db_file = '/home/hunter/mojito_test.db'; | ||
| 131 | my $test_mongo_db_name = 'mojito_test'; | ||
| 132 | $self->editer->db_name($test_mongo_db_name); | ||
| 133 | $self->db_file($test_deep_db_file); | ||
| 134 | } | ||
| 135 | } | ||
| 136 | |||
| 137 | 1; |