Home | Web Design | Programming | Fairlight CMI | Soap Box | Downloads | Links | Biography | About... | Site Map |
Record Recycling |
CA-Clipper | Opportunities | Tips and Tricks | Networking | Internal Errors | Source Code | CA-VO |
BACK TO CA-CLIPPER TIPS & TRICKS |
Because the pack command is a problem (see
The Pack Command), a replacement needs
to be found. Record recycling is the answer. Regardless of the dangers of pack , its purpose is to remove
deleted records. However, most data files grow over time and eventually
stabilize at a certain size as records are added and removed throughout
the course of normal activity. The principle behind record recycling is to re-use the deleted records, thus eliminating the need to pack them out. |
The Method
Instead of using the append command to add another
record to a table, the program should search first for a deleted
record and present it as the "new" record. This approach saves time
because the application does not need to ask the operating system to
allocate more space to the table. Record recycling requires two main functions, one to add records (here it's called AddRec() ) and one to remove records
(EraseRec() ). In this implementation, the EraseRec() function not only
deletes the record, but also blanks out all fields. This provides a
bit of security, but also makes it easier for the AddRec()
function to determine if a record is a candidate for recycling. Thus,
records which are only deleted (but not blanked out with
EraseRec() ) are not going to be recycled. The EraseRec() function is called wherever delete
is now used. The difference is that EraseRec() locks and
unlocks the record for you. The AddRec() function is called anytime you want a new record:
AddRec() will work most of the time, you need to
decide what to do if it fails. This depends on your application. The Code Let's look at the source code for the record recycling functions. In the following discussion, the program file (RECYCLE.PRG) is split up so that it can be examined in sections, but you can paste it back together before using it. Possible enhancements to the source code are given at the end. |
EraseRec() function). |
AddRec() function is one of the two primary functions for
record recycling, and it is responsible for finding available records. A
record is a candidate if it is deleted and all of the fields are empty. First, the function makes deleted records visible with set(_SET_DELETED, .F.) and saves the previous state of the
flag. Because blank values always appear first in an indexed table (unless the index is descending), the code checks to see if there is an active index. If so, the code moves to the top of the index where blank records are most likely to appear. To ensure that the record data remains consistent for the next few lines of code, the record is locked. For speed, the code then determines if the top-most record is deleted. This check is performed before the slower RecIsEmpty() function is
called. The RecIsEmpty() function is described below. If a deleted, blank record is found, then it is recalled and the change is commited to disk. The result flag is then set to indicate success. The last part of the function performs a standard append blank
if the record recycling was unsuccessful. If the append blank
fails, the record pointer is moved to end-of-file, otherwise the result
flag is set to indicate success. Finally, the deleted flag is set back to the state it had upon entry to the function. Note that the new record is left locked if the function was successful in finding or adding a new record. This copies the behaviour of the standard append blank command which adds and
locks a record. |
EraseRec() function blanks out every field in the record,
using a for..next loop to visit each field.
The EmptyType() function is described further
below. |
lResult is false each time through the loop. This causes
the for..next loop to stop as soon as a non-empty field
is encountered. |
do case statement. |
Enhancements
There are several alterations that can be made to the previous functions.
|
Home | Web Design | Programming | Fairlight CMI | Soap Box | Downloads | Links | Biography | About... | Site Map |
Send comments about this site to Greg at
gregh@ghservices.com All pages copyright © 1996-1999 GH Services Created 1997/08/21 Last updated 1999/09/30 All trademarks contained herein are the property of their respective owners |