Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Best way to store object in db

by bessarabov (Acolyte)
on Mar 06, 2013 at 16:55 UTC ( #1022037=perlquestion: print w/replies, xml ) Need Help??
bessarabov has asked for the wisdom of the Perl Monks concerning the following question:

I'm now writing a simple module. This module represents one object. I need the ablitily to store the state of this object in the database and to restore it from the database.

To sole this task I want to add 2 methods to the object:

  • serialize() it will return scalar with some string that represents the object state
  • spawn() this is a constractor, it recieves the scalar that I got from serialize() method and will create the object

I'm planning to use YAML to serialize some object internals that are needed to recreate the object, and I'm going to save in the serialized string some number to make it possible to chagne serialazation format, but to store the compatibility.

What do you think? Is this aproach good, or should I do something else to solve my task of storing object in the databse.

UPDATE: I've created and released module Time::ETA that does the exact serialization that I have described in this post.

Replies are listed 'Best First'.
Re: Best way to store object in db
by moritz (Cardinal) on Mar 06, 2013 at 18:18 UTC

    Just a few random thoughts:

    • Storing objects as blobs in a database makes it (nearly) impossible to do useful searches in them. Since the power of SQL queries is the main selling point of relation database systems, that's really a pity, and you might want to consider other storage systems ("nosql")
    • If all your objects have similar structure, storing them in a proper schema would improve searchability
    • YAML doesn't capture all features of Perl 5 data structures (for example weak references).
    • Storable captures more features, but you'll generally have problems changing to a different version of Storable (or Perl, for that matter).
    • If you plan to support multiple serialization formats, store the format in addition to the actual data.

      Thank your for your points!

      I'm writing a module that calculated estimated time of accomplishment. After it is ready I'm planning to release it as Time::ETA. It is not complete, but you can look at the current version at github.</>

      With this module I'm solving a hi level task. I need to create web interface for long lasting tasks. I'm plannig to store serialized version of Time::ETA in database to make it simple to output current status. I think it is good solution I hide some logic in CPAN module.

      You are right that it will not be possible to search through this data, but I here I don't need it.

      You are right that YAML can't serialize a lot of thing. But some things can't be serialized in any way (for example open filehandle). In the module I'm going to serialize several scalars, so YAML will do it's job fine.

      I'm not doing to support multiple serialization formats I just want to have backward compatibility.

Re: Best way to store object in db
by daxim (Chaplain) on Mar 06, 2013 at 17:07 UTC
      Wow. I never new about this module. Thank you! This is a very interesting thing, but for my task it is too complex.
Re: Best way to store object in db
by tmharish (Friar) on Mar 06, 2013 at 17:22 UTC

    Will not apply directly to your problem but Storable might be of interest.

    Considering the hype around NoSQL unless you have specific need for a relational storage structure you might want to look at file storage.

      Thank you! I was thinking about Storable, but It does not suit me. I don't want to store the whole object, but only the parts that are necessary for its recreation. I also want to store version number in the serialized object to make it possible to change the internal format with backward compatibility.

      For now I have the task to store the MySQL, but I want to write the module so it serializes to some text that can be stored anywhere.

Re: Best way to store object in db
by tobyink (Abbot) on Mar 06, 2013 at 22:03 UTC

    Take a look at Sereal before you make up your mind on the format.

    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1022037]
Approved by marto
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2017-05-26 01:56 GMT
Find Nodes?
    Voting Booth?