sheharyarn / memento

Simple + Powerful interface to the Mnesia Distributed Database 💾
http://hexdocs.pm/memento/
MIT License
734 stars 23 forks source link

How to persist data on disk? #3

Closed yokujin closed 5 years ago

yokujin commented 5 years ago

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.

sheharyarn commented 5 years ago

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.

yokujin commented 5 years ago

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

yokujin commented 5 years ago

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

sheharyarn commented 5 years ago

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.

yokujin commented 5 years ago

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

yokujin commented 5 years ago

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

sheharyarn commented 5 years ago

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).

sheharyarn commented 5 years ago

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:

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

sheharyarn commented 5 years ago

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