Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you persist data to disk from .NET?

I have a variety of rich data structures (primarily trees) that I would like to persist to disk, meaning I not only want to write them to disk but I want a guarantee that the data has been fully written and will survive a power-down.

Others seem to design ways to encode rich data structures in flat database tables as lookup tables from parent to child nodes. This facilitates running SQL queries against the data but I have no need for that: I just want to save and load my trees.

The obvious solution is to store everything as a blob in the data base: a single entry perhaps containing a long string. Is that an abuse of the database or a recommended practice? Another solution might be to use an XML database? Are there any alternatives to databases that I should be considering?

Finally, I'm doing this from F# so a turnkey solution for persisting data from .NET would be ideal...

EDIT: Please note that formatting (e.g. serialization) is irrelevant as I can trivially convert between formats with F#. This is about getting an acknowledgement that a write has been completed all the way down to the non-volatile store (i.e. the disk platter) and no part of the written data is still being held in volatile store (e.g. an RAM cache) so that I can continue safe in that knowledge (e.g. by deleting the old version of the data from disk).

like image 567
J D Avatar asked Jan 19 '11 19:01

J D


2 Answers

Some of the constructors for .NET's FileStream class take a parameter of type FileOptions. One of the values for FileOptions is WriteThrough, which "Indicates that the system should write through any intermediate cache and go directly to disk."

This should ensure that by the time your write operation (to a new file) returns, the data is committed to disk and you can safely delete the old file.

like image 63
Gavi Lock Avatar answered Nov 08 '22 04:11

Gavi Lock


This can be done via Serialization.

The .NET Framework includes many built-in options for serializing your data to disk, including using binary or XML-based formats. Detailed How-To articles are provided in the MSDN Documentation.

like image 6
Reed Copsey Avatar answered Nov 08 '22 03:11

Reed Copsey