FATS - Fast Access Tree System
Table of Contents
Programming Interfaces
IBM-Pascal, MS-Pascal, Quick Pascal


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

(*

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

   Test program (Microsoft Pascal, IBM Pascal)
 
   Reindex the test data file customer.dat.

   19-03-2009 U.Gertz
*) 

program REBUILD (input, output);


type

  custrec = record
      DELETEDMARK: char;
      ID: lstring(5);
      NAME: lstring(25);
      JOB: lstring(25);
      STREET: lstring(25);
      ZIP: lstring(5);
      CITY: lstring(20);
   end;


const

  fn_cust = 'CUSTOMER.DAT';


var
  hCustomer: file of custrec;
  cbuffer: custrec;
  szCmnd: lstring(255);
  szFATSkey: lstring(255);
  szRecno: lstring(8);
  uFATSError: word;
  dwFATSRecno: integer4;
  dwRecords: integer4;
  cChar: char;


     function FATSCALL (vars szCmnd: lstring; vars nErrorcode: word;
                        vars szReturnKey: lstring) : integer4; extern;



begin

  writeln;
  writeln ('To compile and link the test program, use ONE of the following methods:');
  writeln;
  writeln ('  1. METHOD: Linking FATS to the program');
  writeln;
  writeln ('     PAS1 REBUILD');
  writeln ('     PAS2');
  writeln ('     LINK REBUILD FATS.OBJ FATS_MSP.OBJ');
  writeln;
  writeln ('  2. METHOD: Calling the Workstation Engine');
  writeln;
  writeln ('     PAS1 REBUILD');
  writeln ('     PAS2');
  writeln ('     LINK REBUILD FATSMSPR.LIB   (FATS Standard Version FATS_WE.EXE)');
  writeln;
  writeln ('     or if you own the extended version of FATS:');
  writeln;
  writeln ('     LINK REBUILD FATSXMPR.LIB   (FATS Extended Version FATSXWE.EXE)');
  writeln;
  writeln ('Please press the [ENTER] key ...');
  writeln;
  read(cChar);


  (*
     -------> 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:=FATSCALL(szCmnd, uFATSError, szFATSkey);


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

  assign  (hCustomer, fn_cust);
  hCustomer.mode:=direct;
  reset (hCustomer);


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

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


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

  dwRecords:=0;

  repeat

    seek(hCustomer, dwRecords + 1);
    get(hCustomer);
    cbuffer:=hCustomer^;

    If EOF( hCustomer ) Then Begin

      break;
    End;

    dwRecords:=dwRecords + 1;

    If cbuffer.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\';
      If encode(szRecno, dwRecords:8) Then concat(szCmnd, szRecno);
      concat (szCmnd, '\1\');
      concat(szCmnd, cbuffer.ID);
      concat(szCmnd, '\');
      concat(szCmnd, cbuffer.NAME);
      concat(szCmnd, '\');
      concat(szCmnd, cbuffer.JOB);
      concat(szCmnd, '\');
      concat(szCmnd, cbuffer.ZIP);
      concat(szCmnd, cbuffer.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\';
      If encode(szRecno, dwRecords:8) Then concat(szCmnd, szRecno);
      concat (szCmnd, '\S\1');
    End;

    dwFATSRecno:=FATSCALL(szCmnd, uFATSError, szFATSkey);

  until uFATSError <> 0;

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

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

  close(hCustomer);

end.

© 2008  GCS Software, Udo Gertz