I'm trying to implement a file storage mechanism which holds a number of variably-sized records in a sin开发者_运维问答gle file with the guarantee that the set of records will always be recoverable into a consistent state, even if the system failed at a hardware level.
So far, every scheme I've come up with pivots on writing data sequentially. Some piece of data would be appended to the end of each record which confirms that the write succeeded. However, if the data is not necessarily written to the disk sequentially when flushed then it would be possible for the confirmation data to be written before the content data.
There are two obvious ways around this, but both are undesirable:
- Flush the content, then write the confirmation and flush it. Adding the extra flush may degrade performance.
- Include a checksum in the confirmation (would require reading the content to confirm that it is valid).
I'm using C# on Windows (32 and 64-bit) and .Net 4.0's memory mapped file implementation
This is too low level and OS specific for C#. try using Windows APIs from C, and read very carefully the API specifiations.
Have you tried using the FileOptions.WriteThrough on the underlying filestream? That might help since it disables buffering. Other ideas would be to keep a separate file containing the confirmations as the last written offset, if it doesnt match your filesize (due to power outage for example ) you can simply truncate it do that size
精彩评论