views:

274

answers:

8

We take text/csv like data over long periods (~days) from costly experiments and so file corruption is to be avoided at all costs.

Recently, a file was copied from the Explorer in XP whilst the experiment was in progress and the data was partially lost, presumably due to multiple access conflict.

What are some good techniques to avoid such loss? - We are using Delphi on Windows XP systems.

Some ideas we came up with are listed below - we'd welcome comments as well as your own input.

A: 

Write data to a buffer file in an obscure directory and copy the data to the 'public' data file periodically (every 10 points for instance), thereby reducing writes and also providing a backup

Brendan
Security through obscurity is rarely a good thing. You'll end up spending a lot of time positioning yourself for new writes to the public file if it very large.
tvanfosson
A: 

Write data points discretely, i.e. open and close the filehandle for every data point write - this reduces the amount of time the file is being accessed provided the time between data points is low

Brendan
You're just narrowing the window of opportunity, not eliminating it. You'll also have to deal with seeking to the end of the data each time to append.
tvanfosson
@tvanfosson: on a civilized system (of which XP might not be an example), you can open the file for append so that each write is automatically at the end of the file.
Jonathan Leffler
A: 

If a write fails, cache the result for a later write - so if a file is opened externally the data is still stored internally, or could even be stored to a disk

Brendan
If the write fails for a long period, then you loose the data stored in memory if the computer losses power.
stukelly
@stukelly, this is true however these ideas are not 'either/or' I envisage implementing more than one of them to make the system more robust.
Brendan
+8  A: 

Use a database as a secondary data storage mechanism and take advantage of the atomic transaction mechanisms

Brendan
A database would provide other benefits and could be used for generating reports.
stukelly
Transactions are what you need and the DB already has them.
tvanfosson
Introducing dependencies on another complex system is a really bad idea. Experiment handling computers should not have software on them with unpredictable processor and disk load characteristics.
Stephan Eggermont
Stephen has a valid point - one thing that I should emphasise is that the computers here are unlikely to have a professional 'administering' them, I was reluctant to implement a database because it adds a whole new layer. KISS seems to have a lot of weight in this environment...
Brendan
+4  A: 

How about splitting the large file into separate files, one for each day.

stukelly
Splitting the files up would be a good idea and relatively easy to implement - thanks!
Brendan
A: 

I think what you're looking for is the Win32 CreateFile API, with these flags:

FILE_FLAG_WRITE_THROUGH : Write operations will not go through any intermediate cache, they will go directly to disk.

FILE_FLAG_NO_BUFFERING : The file or device is being opened with no system caching for data reads and writes. This flag does not affect hard disk caching or memory mapped files. There are strict requirements for successfully working with files opened with CreateFile using the FILE_FLAG_NO_BUFFERING flag, for details see File Buffering.

PatrickvL
+1  A: 

If these machines are on a network: send a HTTP post with the logging data to a webserver. (sending UDP packets would be even simpler).

Make sure you only copy old data. If you have a timestamp on the filename with a 1 hour resolution, you can safely copy the data older than 1 hour.

Stephan Eggermont
A: 

Each experiment much use a 'work' file and a 'done' file. Work file is opened exclusively and done file copied to a place on the network. A application on the receiving machine would feed that files into a database. If explorer try to move or copy the work file, it will receive a 'Access denied' error.

'Work' file would become 'done' after a certain period (say, 6/12/24 hours or what ever period). So it create another work file (the name must contain the timestamp) and send the 'done' through the network ( or a human can do that, what is you are doing actually if I understand your text correctly).

Copying a file while in use is asking for it being corrupted.

Fabricio Araujo