FATS - Fast Access Tree System
Table of Contents
Programming Interfaces
Turbo Pascal, Borland Pascal for Windows 3.x


The following Borland Pascal example demonstrates the use of the FATS  indexing commands:

(*

   FATS 02.30
   (c) GCS Software, Udo Gertz 1993-1998

   Test program (Turbo Pascal / Win 3.x)
 
   Reindex the test data file customer.dat.

   19-03-2009 U.Gertz
*) 

program REBUILD;

uses WinCrt,
     WinProcs;


     function FATSLibInit(nSize: integer; nSign: integer): Pointer;
              far; external 'FATS_WIN';

     function FATSLibExit(lpFATSdata: Pointer): Pointer;
              far; external 'FATS_WIN';


     function FATSLibCall(var szCmnd: string; var uErrorCode: word;
              var szFATSkey: string; lpFATSdata: Pointer): longint;
              far; external 'FATS_WIN';

const

  fn_cust = 'CUSTOMER.DAT';


type

  custrec = record
    DELETEDMARK: char;
    ID: string[5];
    NAME: string[25];
    JOB: string[25];
    STREET: string[25];
    ZIP: string[5];
    CITY: string[20];
  end;


var
  hCustomer: file of custrec;
  custdata: custrec;
  lpFATSdata: Pointer;
  szCmnd: string;
  szFATSkey: string;
  szRecno: string[8];
  uFATSError: word;
  dwFATSRecno: longint;
  dwRecords: longint;


label exitbuild;


begin

  (*
     Before you can use the FATS commands in your programs, you must
     assign a data area to FATS:
  *) 

  lpFATSdata := FATSLibInit( 0, 1 );
  if lpFATSdata = nil then exit;

  (*
     The first parameter determines the size of the FATS data area.
     If you indicate a zero here, then the minimum needed storage
     space is reserved (approx. 18-20 KB). The second parameter intends
     the assigned programming language. The address of the data area
     is returned in the variable lpFatsdata.
  *) 

  (*
     -------> 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.
   

  *)  

  szCmnd:='Y\5';
  dwFATSRecno:=FATSLibCall(szCmnd, uFATSError, szFATSkey, lpFATSdata);


  (*
     -------> Open data file
  *) 

  assign  (hCustomer, fn_cust);
  reset (hCustomer);


  (*
     -------> Create index file
  *) 

  szCmnd:='C\CUSTOMER.KEY\5;25\4\A\1';
  dwFATSRecno:=FATSLibCall(szCmnd, uFATSError, szFATSkey, lpFATSdata);


  (*
     ========================================================================
               Re-Indexing
     ========================================================================
  *) 


  dwRecords:=0;

  repeat

    If EOF( hCustomer ) Then Begin

      goto exitbuild;
    End;

    seek(hCustomer, dwRecords);
    read(hCustomer, custdata);

    dwRecords:=dwRecords + 1;

    str(dwRecords, szRecno);

    If custdata.DELETEDMARK = ' ' Then Begin

      (*
          "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

      *)  

      szCmnd:='BK\' + szRecno + '\1';
      szCmnd:=szCmnd + '\' + custdata.ID;
      szCmnd:=szCmnd + '\' + custdata.NAME;
      szCmnd:=szCmnd + '\' + custdata.JOB;
      szCmnd:=szCmnd + '\' + custdata.ZIP + custdata.CITY;

    End Else Begin

      (*
          "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

      *)  

      szCmnd:='DL\' + szRecno + '\S\1';
    End;

      dwFATSRecno:=FATSLibCall(szCmnd, uFATSError, szFATSkey, lpFATSdata);

  until uFATSError <> 0;

  exitbuild:
  (*
     --------> Close index file
  *) 

  szCmnd:='K\1';
  dwFATSRecno:=FATSLibCall(szCmnd, uFATSError, szFATSkey, lpFATSdata);

  close(hCustomer);


  (*
     Before terminating the application program you should release the
     storage area allocated by FATSLibInit. You can do this by calling
     the function FATSLibExit:
  *) 

  lpFATSdata := FATSLibExit( lpFATSdata );

  (*
     Although the storage area would be released automatically by the
     operating system, this instruction is useful, since it closes
     all FATS files that are still opened.
  *) 

end.

© 2008  GCS Software, Udo Gertz