130 likes | 314 Views
Comp 335 File Structures. Reclaiming and Reusing File Space Techniques for File Maintenance. Considerations for File Modification. Modifications take place in three forms: Record Addition Record Deletion Record Update ADDITIONS
E N D
Comp 335File Structures Reclaiming and Reusing File Space Techniques for File Maintenance
Considerations for File Modification • Modifications take place in three forms: • Record Addition • Record Deletion • Record Update • ADDITIONS • If a file only has additions, no problems, just continue to append to the end of file. • DELETIONS • Theses leave “holes” in the file. You do not want to always “pack” the file. We must come up with a way to use these holes. • UPDATING • Only a problem if variable length records are used. What if the modification results in a smaller record? Larger record?
Reclaiming File Space • DELETIONS and UPDATES can lead to a severely fragmented file. • FILE COMPACTION is an option. Records can be marked as deleted and then the OS can run a compaction on the file. The compaction can be done in two ways: a) move records up into unused space or b) a file copy which simply skips over deleted records. • Considering large files with millions of records, file compactions are usually done once a day or week.
Dynamic Space Reclamations • It would be nice if file space could be reclaimed on the fly and just simply reuse the space (holes) without having to do file compaction. • This can be done fairly efficiently with both fixed and variable length record files using a stack.
Fixed length Record FilesDynamically Reusing Space • One way would be to “logically” delete records. A “logical” deletion is a flag kept in a record header which simply marks whether or not this record is deleted. The records could be searched and the first record found which is deleted could simply be reused. • This could be EXTREMELY SLOW! O(n) algorithm. Not to mention the possibility of MANY SEEKS!
Fixed length Record FilesDynamically Reusing Space • It would be nice to find immediately where a space (hole) is in the file ( if indeed there is available space) and go directly to this location. • A very fast technique to use is to link all available holes together and to implement this list as a stack.
Fixed length Record FilesDynamically Reusing Space • Add a “top” pointer to the file header. This will point to the top of the linked list of available spaces in the file. • Each record will need to include a header which will contain a delete flag and a next pointer.
Variable length Record FilesDynamically Reusing Space • Same basic technique will be utilized but there are some problems which arise with VL record files that do not occur with FL record files. • Problems: • Cannot use RRN to get to a “hole”, must use actual address • Size of the holes are different • Fragmentation is a problem, holes can get so small that they cannot be reused.
Variable length Record FilesDynamically Reusing Space • Ordering of the “avail” (available space) list becomes an issue. • Three Techniques could be used: • First Fit • Best Fit • Worst Fit
First Fit Strategy • Adding a new “hole” to list: • The new hole is added to front of list – O(1) • Reusing space • Find first space which fits (searching an unordered list) – O(n)
Best Fit Strategy • List must be ordered in ascending order. • Adding a new “hole” to the list: • Insertion into an ordered linked list – O(n) • Reusing Space • Searching an ordered list but is not in an array – O(n) • This technique can also lead to fast fragmentation. Since we using the best or closest fit, any unused space could possibly be too small to be reused.
Worst Fit Strategy • List must be placed in descending order. • Adding a new “hole” to the list: • Insertion into an ordered list not in an array – O(n) • Reusing space • Take the top of the avail list because this is either the worst fit or no fit. O(1) • This technique is frequently used because of two reasons: 1) fragmentation is usually delayed because unused space in a reclamation can probably be reused and 2) reusing the space is fast.
Other Points about File Maintenance • How do you handle updates to records in files? • Fixed length records – no problem, just reuse that same space. Variable length records – handle as a deletion and then an add. • Coalescing – to delay fragmentation, coalescing can be used; this technique searches the file for places in the file where there are two or more “adjacent” holes and will combine them into one hole.