File Coverage

blib/lib/MorboDB.pm
Criterion Covered Total %
statement 12 15 80.0
branch 1 2 50.0
condition 1 3 33.3
subroutine 4 6 66.6
pod 3 3 100.0
total 21 29 72.4


line stmt bran cond sub pod time code
1             package MorboDB;
2              
3             # ABSTRACT: In-memory database, mostly-compatible clone of MongoDB
4              
5 4     4   98568 use Moo;
  4         93774  
  4         26  
6 4     4   8110 use Carp;
  4         7  
  4         324  
7 4     4   2335 use MorboDB::Database;
  4         89  
  4         1056  
8              
9             our $VERSION = "1.000000";
10             $VERSION = eval $VERSION;
11              
12             =head1 NAME
13              
14             MorboDB - In-memory database, mostly-compatible clone of MongoDB
15              
16             =head1 VERSION
17              
18             version 1.000000
19              
20             =head1 SYNOPSIS
21              
22             use MorboDB;
23              
24             # MorboDB usage is meant to refelect MongoDB usage
25              
26             my $morbo = MorboDB->new;
27             my $database = $morbo->get_database('my_database');
28             my $collection = $database->get_collection('users');
29              
30             my $id = $collection->insert({
31             username => 'someguy98',
32             password => 's3cr3t',
33             email => 'email at address dot com',
34             });
35              
36             ...
37              
38             =head1 DESCRIPTION
39              
40             MorboDB is an in-memory database, meant to be a mostly-compatible clone
41             of Perl's L driver, in such a way that it can be used to replace
42             or even supplement MongoDB in applications where it might be useful.
43              
44             =head2 USE CASES
45              
46             An in-memory database can be useful for many purposes. A common use case
47             is testing purposes, where using a "physical" database might be onerous.
48             You can already find a few in-memory databases on CPAN, such as L,
49             L (has optional support for in-memory
50             databases) and L (which has an in-memory hash serializer).
51             I'm sure there are others more.
52              
53             I decided to develop MorboDB for two main purposes:
54              
55             =over
56              
57             =item * B: at work I am currently developing a
58             very critical application that uses MongoDB (with replica-sets setup) as
59             a database backend. This application cannot afford to suffer downtimes.
60             The application's database has some constant data (not too much) that shouldn't change
61             which is completely required for it to work. Most of the data, however,
62             is dynamically written due to user's work and is not as important, so it
63             wouldn't matter if the database won't be able to take such writes for some time.
64              
65             Therefore, I have decided to build a fail-safe: when the application is launched (actually
66             I haven't decided yet if on launch or not), the constant data is loaded into
67             MorboDB, which silently waits in the background. If for some reason the
68             MongoDB database crashes, the application switches to MorboDB and the application
69             continues to work - the users don't even notice something happend. Since
70             MorboDB provides mostly the same syntax as MongoDB, this isn't very far-fetched
71             codewise.
72              
73             =item * B: I am also working on a content management
74             system in which I want to allow users to undo changes for a certain duration
75             (say 30 seconds) after the changes have been made. MorboDB can work as
76             a bridge between the application and the actual MongoDB database (or whatever
77             actually). So the data will only live in MorboDB for 30 seconds, and if the user decides
78             to undo, the data is removed and nothing happens. Otherwise, the data is
79             moved to MongoDB after the 30 seconds are over.
80              
81             =back
82              
83             =head2 MOSTLY-COMPATIBLE?
84              
85             As I've mentioned, MorboDB is I with L. First
86             of all, a lot of things that are relevant for MongoDB are not relevant for
87             in-memory database. Some things aren't supported and probably never will,
88             like GridFS for example. Otherwise, the syntax is almost completely the
89             same (by relying on L), apart for some changes detailed in both
90             L and L.
91              
92             I have provided most methods provided by relevant MongoDB modules, even where
93             they're not really implemented (in which case they either return 1 or an
94             undefined value). Read the documentation of MorboDB's different modules
95             for information on every method and whether it's implemented or not. These
96             methods are only provided to make it possible to use MorboDB as a drop-in
97             replacement of MongoDB where appropriate (so you don't get "undefined subroutine"
98             errors). Please let me know if there are methods you need (even unimplemented)
99             that I haven't provided.
100              
101             Note that autoloading of database and collection objects has been deprecated
102             since version 1.0.0, in accordance with MongoDB.
103              
104             =head2 STATUS
105              
106             This module is beta software, not suitable for production use yet. Feel
107             free to test it and let me know how it works for you (of course, not on
108             production), I'd be happy to receive any bug reports, requests, ideas, etc.
109              
110             =cut
111              
112             has '_dbs' => (is => 'ro', default => sub { {} });
113              
114             =head1 OBJECT METHODS
115              
116             =head2 database_names()
117              
118             Returns a list with the names of all existing databases.
119              
120             =cut
121              
122 0     0 1 0 sub database_names { sort keys %{$_[0]->_dbs} }
  0         0  
123              
124             =head2 get_database( $name )
125              
126             Returns a L object with the given name:
127              
128             my $morbodb = MorboDB->new;
129            
130             my $db = $morbodb->get_database('mydb');
131              
132             =cut
133              
134             sub get_database {
135 3     3 1 1585 my ($self, $name) = @_;
136              
137 3 50       16 confess "You must provide the name of the database to get."
138             unless $name;
139              
140 3   33     49 return $self->_dbs->{$name} ||= MorboDB::Database->new(_top => $self, name => $name);
141             }
142              
143             =head2 get_master()
144              
145             Not implemented, simply returns a true value here.
146              
147             =cut
148              
149 0     0 1   sub get_master { 1 } # not implemented
150              
151             =head1 CAVEATS
152              
153             Currently (not sure if this will change), MorboDB does not work in shared
154             memory, so if your application is multi-threaded, every thread will have
155             its own MorboDB container completely separate and unaware of other threads.
156              
157             =head1 DIAGNOSTICS
158              
159             This module throws the following errors:
160              
161             =over
162              
163             =item C<< "You must provide the name of the database to get." >>
164              
165             Thrown by C if you don't provide it with the name of the
166             database you want to get/create.
167              
168             =back
169              
170             =head1 CONFIGURATION AND ENVIRONMENT
171            
172             MorboDB requires no configuration files or environment variables.
173              
174             =head1 DEPENDENCIES
175              
176             MorboDB depends on the following CPAN modules:
177              
178             =over
179              
180             =item * L
181              
182             =item * L
183              
184             =item * L
185              
186             =item * L
187              
188             =item * L
189              
190             =item * L
191              
192             =back
193              
194             =head1 INCOMPATIBILITIES WITH MONGODB
195              
196             While I hope to make MorboDB as much of a clone of the L driver
197             as possible (syntax and usage-wise), some changes are inevitable. Currently,
198             only the most essential features of the MongoDB distribution are implemented.
199             That means you can insert documents as you would with MongoDB, update
200             documents and remove documents. You can find documents and work with cursor
201             pretty much the same, including sorting and other cursor modifications.
202              
203             Syntaxwise, any differences between MorboDB and MongoDB stem from the usage
204             of L as the parser, so read L
205             for a list of differences.
206              
207             Another difference worth noting is with OIDs. In MongoDB, OIDs (the automatic
208             ones at least) are 24 characters long hexadecimal strings, and are created
209             by the L module. In MorboDB, however, OIDs (also, only the
210             automatic ones) are 36 characters long UUIDs. This alone limits your ability
211             to use MorboDB alongside MongoDB in an application if you perform queries
212             on the C<_id> attribute with known MongoDB::OID objects. Other than that,
213             this shouldn't really be a problem.
214              
215             Featurewise, most differences should be missing or unimplemented methods
216             (and a few missing classes). I have taken some care not to miss any methods provided by the MongoDB
217             distribution, but I may have missed some. Where methods are unimplemented,
218             MorboDB will simply return a true or false value (as appropriate). Please
219             read the documentation of each MorboDB module to learn what to expect from
220             unimplemented methods (and implemented methods of course).
221              
222             Some features that are native to MongoDB itself (and not just the L
223             distribution on CPAN) will never be implemented in MorboDB (most of them
224             don't even make sense in an in-memory database).
225              
226             Here's a (probably incomplete) list of MongoDB features missing from MongoDB:
227              
228             =over
229              
230             =item * Authentication
231              
232             =item * Indexing (may be supported in the future, don't think so though)
233              
234             =item * Replication
235              
236             =item * Sharding
237              
238             =item * Map/Reduce
239              
240             =item * GridFS
241              
242             =back
243              
244             =head1 INCOMPATIBILITIES WITH OTHER MODULES
245              
246             None reported.
247              
248             =head1 BUGS AND LIMITATIONS
249              
250             No bugs have been reported.
251              
252             Please report any bugs or feature requests to
253             C, or through the web interface at
254             L.
255              
256             =head1 SEE ALSO
257              
258             L, L, L, L.
259              
260             =head1 AUTHOR
261              
262             Ido Perlmuter
263              
264             =head1 LICENSE AND COPYRIGHT
265              
266             Copyright (c) 2011-2013, Ido Perlmuter C<< ido@ido50.net >>.
267              
268             This module is free software; you can redistribute it and/or
269             modify it under the same terms as Perl itself, either version
270             5.8.1 or any later version. See L
271             and L.
272              
273             The full text of the license can be found in the
274             LICENSE file included with this module.
275              
276             =head1 DISCLAIMER OF WARRANTY
277              
278             BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
279             FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
280             OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
281             PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
282             EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
283             WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
284             ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
285             YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
286             NECESSARY SERVICING, REPAIR, OR CORRECTION.
287              
288             IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
289             WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
290             REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
291             LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
292             OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
293             THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
294             RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
295             FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
296             SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
297             SUCH DAMAGES.
298              
299             =cut
300              
301             __PACKAGE__->meta->make_immutable;
302             __END__