1. Use record#1 for "housekeeping" and store the address of the first vacated
record place which can therefore be re-used.
2. No housekeeping record, on first opening determine first vacant spot.
3. Move all following records up one position to fill the gap. Keep track of
last valid record#. Leaves a gap at the end. When adding any more records,
correct position is evident.
4. Rewrite the file and reclaim the vacant space.
Both 1 & 2 require that record processing handles vacant (null) records
properly, which I see as a distinct drawback - requires extra code.
Method 3 saves the rewrite (which in itself is trivial - copy/delete/rename).
Which method do you guys use or favour? (VB-DOS std)
I think the most common method is to put a -1 or some other flag at the
beginning of deleted records. You could then either re-use those records, or
just leave them abandoned until the database is compacted. Compacting
(sliding records forward) could be done manually by the user once in a
while, or automatically every 100th time the program is quit. I think this
is what Outlook Express does - you can compact the folders manually, or let
the program do that when it deems it necessary. A variant on this is to
track the number of deleted records in an ini file (or 1st record), and when
the number of deleted records is significant the program can pop up a dialog
suggesting the user compact now.
Also, when sliding records forward you can truncate the file after the last
valid record with a DOS call to relinquish the space at the end. This is
better than leaving the space in the file. Likewise, I much prefer sliding
records versus rewriting the file. It's much more efficient, and when files
are very large it avoids needing twice as much total disk space as the file
size requires.
--Ethan
Well, I always added a one-character field called D or Deleted to RANDOM
files where I put an * if the record is marked as deleted, or leave it as
null or space (CHR$(32)) if it is alive. If I need to mantain a history of
records, then I leave the record alive just with the asterisk so I can view
it again (if I needed to), or I do a Pack by the User request or
automatically every fifth time the program is used. If the program needs
many RANDOM files to do its work, then I mantain a separated file that lists
the filename(s) that has deletions on it, and I process only those that
deserved a packing process. This is so quick in modern (highly cached)
computers that is difficult that the user sees any perceivable delay on the
process. To be clear, a 16-bit application runs in just the cache of the
processor, and many of its data.
I hope that my technique can help you with your doubts.
--
From the guts of progralogics and mechatronics
Tron.BAS