Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Atomic delete for large amounts of files

Tags:

c++

c

file

io

atomic

I am trying to delete 10000+ files at once, atomically e.g. either all need to be deleted at once, or all need to stay in place.

Of course, the obvious answer is to move all the files into a temporary directory, and delete it recursively on success, but that doubles the amount of I/O required.

Compression doesn't work, because 1) I don't know which files will need to be deleted, and 2) the files need to be edited frequently.

Is there anything out there that can help reduce the I/O cost? Any platform will do.

EDIT: let's assume a power outage can happen anytime.

like image 634
György Andrasek Avatar asked Oct 26 '09 00:10

György Andrasek


3 Answers

Kibbee is correct: you're looking for a transaction. However, you needn't depend on either databases or special file system features if you don't want to. The essence of a transaction is this:

  1. Write out a record to a special file (often called the "log") that lists the files you are going to remove.
  2. Once this record is safely written, make sure your application acts just as if the files have actually been removed.
  3. Later on, start removing the files named in the transaction record.
  4. After all files are removed, delete the transaction record.

Note that, any time after step (1), you can restart your application and it will continue removing the logically deleted files until they're finally all gone.

Please note that you shouldn't pursue this path very far: otherwise you're starting to reimplement a real transaction system. However, if you only need a very few simple transactions, the roll-your-own approach might be acceptable.

like image 181
Dale Hagglund Avatar answered Sep 25 '22 21:09

Dale Hagglund


On *nix, moving files within a single filesystem is a very low cost operation, it works by making a hard link to the new name and then unlinking the original file. It doesn't even change any of the file times.

If you could move the files into a single directory, then you could rename that directory to get it out of the way as a truly atomic op, and then delete the files (and directory) later in a slower, non-atomic fashion.

Are you sure you don't just want a database? They all have transaction commit and rollback built-in.

like image 25
DigitalRoss Avatar answered Sep 22 '22 21:09

DigitalRoss


I think what you are really looking for is the ability to have a transaction. Because the disc can only write one sector at a time, you can only delete the files one at a time. What you need is the ability to roll back the previous deletions if one of the deletes doesn't happen successfully. Tasks like this are usually reserved for databases. Whether or not your file system can do transactions depends on which file system and OS you are using. NTFS on Windows Vista supports Transactional NTFS. I'm not too sure on how it works, but it could be useful.

Also, there is something called shadow copy for Windows, which in the Linux world is called an LVM Snapshot. Basically it's a snapshot of the disc at a point in time. You could take a snapshot directly before doing the delete, and on the chance that it isn't successfully, copy the files back out of the snapshot. I've created shadow copies using WMI in VBScript, I'm sure that similar apis exist for C/C++ also.

One thing about Shadow Copy and LVM Snapsots. The work on the whole partition. So you can't take a snapshot of just a single directory. However, taking a snapshot of the whole disk takes only a couple seconds. So you would take a snapshot. Delete the files, and then if unsucessful, copy the files back out of the snapshot. This would be slow, but depending on how often you plan to roll back, it might be acceptable. The other idea would be to restore the entire snapshot. This may or may not be good as it would roll back all changes on the entire disk. Not good if your OS or other important files are located there. If this partition only contains the files you want to delete, recovering the entire snapshot may be easier and quicker.

like image 41
Kibbee Avatar answered Sep 23 '22 21:09

Kibbee