sheharyarn/memento

How to persist data on disk?

yokujin opened this issue · 9 comments

Maybe I did get something wrong, but how do I persist data on disk across restarts? I reread the documentation several times, tried to reuse :mnesia.create_table options... And nothing - data exists only in RAM or error is raised.

Hey @yokujin,
Thank you for pointing out the documentation issue. Setting up persistence in Mnesia is a bit weird, to say the least. It involves stopping the application, creating schemas on disk, restarting the application and then creating tables with certain options.

Here's a quick overview of the steps you need to take, but I'll make sure these are properly documented in the README and HexDocs:

# The nodes where you want to persist
nodes = [ node() ]

# Create the schema
Memento.stop
Memento.Schema.create(nodes)
Memento.start

# Create disc copies of the table
Memento.Table.create!(YourTable, disc_copies: nodes)

This just needs to be done once to create the disk schema. See the sample implementation in Que.

Yeah! Thank you! Got it.
I didn't realize I have to look around schema...

May be it is better to add disc_copies option to table definition?

May be it is better to add disc_copies option to table definition?

The Table definition already supports it, but it is recommended that you explicitly pass it when creating the schemas on disk.

How do you pass it to table definition? I tried some different methods and got only errors...

Is it possible to ignore disc_copies option when there is no schema created on disk? Or maybe warn about missing disk schema...

Is it possible to ignore disc_copies option when there is no schema created on disk? Or maybe warn about missing disk schema...

The default behaviour is to raise an exception when trying to create a Table with disc copies when there is no Schema on disk (raised directly from the exit signal thrown by the underlying Mnesia implementation).

How do you pass it to table definition? I tried some different methods and got only errors...

defmodule MyTable do
  use Memento.Table, attributes: [:key, :value], disc_copies: [node()]
end

Again, this is not recommended at all, and for good reasons:

  • You've now hard-coded the node name, which will be burned into the module at compile-time.
  • You still need to stop and restart the Memento/Mnesia application when creating schemas, and you'll have to specify the node names anyway, making it redundant or causing inconsistent node values in the two functions.

Better to explicitly specify this when you are creating it. You can have a setup function that does it for you.

@cpilka's comments moved to a new issue: Data is not always flushed to disk