|
The following C/C++ example demonstrates the use of the FATS  indexing commands:
/* FATS 02.30 (c) GCS Software, Udo Gertz 1993-1998 Test program (Microsoft C/C++, Visual C) Reindex the test data file customer.dat. 19-03-2009 U.Gertz */ #include <stdio.h> #include <io.h> #include <fcntl.h> #include <sys\types.h> #include <sys\stat.h> #include <stdlib.h> #include <string.h> #include <memory.h> #include "FATS.h" struct custdat { char DELETEDMARK; char ID[6]; char NAME[26]; char JOB[26]; char STREET[26]; char ZIP[6]; char CITY[21]; }; char szCmnd[256]; char szFATSkey[256]; unsigned short uFATSError; unsigned long dwFATSRecno; struct custdat cust; main () { char cChar; int hCustomer; int nIOresult; unsigned long dwRecords = 0L; printf( "\nThis Test Program was designed for Microsoft C\n\n" ); printf( "To compile and link the test program, use ONE of the following methods:\n\n" ); printf( " 1. METHOD: Linking FATS to the program\n\n" ); printf( " CL /c /A[?] /Ot REBUILD.C\n" ); printf( " LINK REBUILD FATS_MC[?].OBJ FATS.OBJ;\n" ); printf( " [?] memory model: S (small) , M (medium), C (compact), L (large)\n\n" ); printf( " 2. METHOD: Calling the Workstation Engine\n\n" ); printf( " CL /c /A[?] /Ot REBUILD.c\n" ); printf( " LINK REBUILD FATSMC[?]R.LIB; \t(to call FATS_WE.EXE)\n\n" ); printf( " or if you own the extended version of FATS:\n\n" ); printf( " LINK REBUILD FATSXC[?]R.LIB; \t(to call FATSXWE.EXE)\n\n" ); printf( "Please press the [ENTER] key ...\n" ); cChar = getchar(); /* -------> Activation of FATS Cache By default the cache functionality is deactivated. It can be activated with the "Y" Auto Refresh command. If you want to reorganize a index file for example, you can activate the cache algorithm with the commands "Y\5" or "Y\106", then build the entire file and finally restore the original status (e.g. "Y\0", "Y\2"). The operation can be speed up for 30 times. FATS does not perform writes to the operation system until its cache is full and the least-recently-used algorithm controlling the I/O buffer cache selects a buffer for reuse. FATS never writes to the index file unless the cache buffers are entirely filled during a single command. In addition, FATS does not perform the reset disk operations that cause the operation system to flush its cache buffers to the disk. It does not close and reopen the file each time it physically expands in order to flush the directory structure. When using this command, you cannot assume that any of your updates have been written to the disk until you either perform a close operation or execute the command Write Page Map (W). Use "Y\106" instead of "Y\5" in an network environment. */ dwFATSRecno = FATSCALL( "Y\\5", &uFATSError, szFATSkey ); /* -------> Open data file */ hCustomer = open( "CUSTOMER.DAT", O_RDWR ); setmode( hCustomer, O_BINARY ); /* -------> Create index file */ dwFATSRecno = FATSCALL( "C\\CUSTOMER.KEY\\5;25\\4\\A\\1", &uFATSError, szFATSkey ); /* ======================================================================== Re-Indexing ======================================================================== */ do { nIOresult = read( hCustomer, &cust, sizeof( cust ) ); if ( nIOresult != sizeof( cust ) ) { break; } dwRecords++; if ( !cust.DELETEDMARK ) { /* "BK" Build Keys This command inserts the primary keys of the specified data record ("RecNo") into the index file. The number of keys included in the command string must equal the number of primary keys you specified in the Create Indexfile (C) command. This command corresponds to the command Insert Keys (IK) except that FATS does not perform writes to the operation system until its cache is full and the least-recently-used algorithm controlling the I/O buffer cache selects a buffer for reuse. FATS never writes to the index file unless the cache buffers are entirely filled during the operation. All other FATS commands will update the index file before returning to the calling program (if this feature had not been disabled with the Auto Refresh command). The syntax of the command string: szCmnd = "BK\{RecNo}\{FileNo}\{KeyStr1}[\{KeyStr2}[\{KeyStr3}]]" RecNo Record number FileNo File number KeyStr# Key value */ sprintf( szCmnd, "BK\\%lu\\1\\%s\\%s\\%s\\%s%s", dwRecords, cust.ID, cust.NAME, cust.JOB, cust.ZIP, cust.CITY ); } else { /* "DL" Manipulate Deleted List The record numbers of deleted data records are taken up by FATS automatically with the command Delete Record (D) to a list of the data records that already have been deleted, so that the command Insert Record (I) can possibly reuse these, before the data file must be extended. This list is administered according to the principle last in, first out, i.e. the record deleted last is reused as next. This command allows you to manipulate this list by inserting and deleting of record numbers. Because free space in the data and index files is reclaimed and reused by FATS automatically as records are deleted and added, this command is normally not needed, but it can be useful for reindexing data files containing deleted records. Note that using this command in conjunction with the commands "Insert Record" and "Delete Record" can result in corrupted data files, incorrect query results, and program failures. The syntax of the command string: szCmnd = "DL\{RecNo}\{GetSetFlag}\{FileNo}" RecNo Record number GetSetFlag Possible values: G Remove a record number from the list of deleted records. If there are no entries in the list this command will return the number of the next available record in the data file. If there are entries in the list, the record number of the last deleted record will be returned. This command is automatically called by FATS when the Insert Record (I) command is used. S Add a record number ("RecNo") to the list of deleted records. This command is automatically called by FATS when the Delete Record (D) command is used. FileNo File number */ sprintf( szCmnd, "DL\\%lu\\S\\1", dwRecords ); } dwFATSRecno = FATSCALL( szCmnd, &uFATSError, szFATSkey ); } while ( !uFATSError ); /* --------> Close index file */ dwFATSRecno = FATSCALL( "K\\1", &uFATSError, szFATSkey ); close( hCustomer ); }
© 2008 GCS Software, Udo Gertz