home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-386-Vol-2of3.iso / b / btvcal10.zip / BTVCALC.PAS next >
Pascal/Delphi Source File  |  1993-02-09  |  7KB  |  185 lines

  1.  
  2.          {*********************************************************}
  3.          {*                  BTVCALC.PAS 1.00                     *}
  4.          {*        Copyright (c) TurboPower Software 1993.        *}
  5.          {*                 All rights reserved.                  *}
  6.          {*********************************************************}
  7.  
  8. program BTVCalc;  {-Calculate posible section lengths for VREC}
  9.  
  10.  {
  11.    Using the VRec B-Tree Filer unit to support variable sized records
  12.    requires you to choose a section size that VRec will use when accessing
  13.    your data. Several B-Tree users have requested an automated method for
  14.    determining this size. This program won't completely automate this
  15.    process, but it will releive you from performing the calculations. You
  16.    will still need to decide which of the proposed section sizes best fits
  17.    your applications needs (space savings, speed, etc.).
  18.  
  19.    Execute BTVCalc followed by the minimum size of your record and then the
  20.    maximum size of your record. Optionally, you can force BTVCalc to display
  21.    non-standard section sizes by providing a third parameter, (-2). By
  22.    default, BTVCalc will display section sizes that are powers of two.
  23.  
  24.    If the display scrolls or you want a permanent record of the BTVCalc's
  25.    output, you can use DOS redirection to redirect the output of BTVCalc.
  26.  
  27.    Example: BTVCALC 115 364 > TEMP.TXT    (send to a file)
  28.             BTVCALC 115 364 > PRN         (send to a printer)
  29.             BTVCALC 115 354 | MORE        (pipe to more program)
  30.  
  31.    If you execute BTVCalc with no parameters, a short help screen is displayed.
  32.  
  33.  
  34.    Rules: (From the B-Tree Filer manual)
  35.  
  36.     1. The section length should be kept as small as is consistant with the
  37.        remaining rules
  38.     2. The section length should typically be a multiple of 2
  39.     3. The largest record shouldn't be much larger than about 8 sections
  40.     4. A section shouldn't be much larger than the smallest version of the
  41.        record
  42.     5. The section length should account for the fact that VREC uses 6 bytes
  43.        of the first section and 7 bytes of each subsequent section
  44.  }
  45.  
  46. uses
  47.   Crt;
  48.  
  49. var
  50.   i                : Integer;
  51.   L, Sec, Pwr      : LongInt;
  52.   UsedSections     : LongInt;
  53.   UnusedBytes,
  54.   SectionSize,
  55.   LastSecSize      : LongInt;
  56.   MinRecSize,
  57.   MaxRecSize       : LongInt;
  58.   UsePowerOfTwo    : Boolean;
  59.   UnusedAvg        : Real;
  60.  
  61. procedure Help;
  62. begin
  63.   WriteLn;
  64.   WriteLn(' BTVCALC - Calculate section sizes for B-Tree Filer''s');
  65.   WriteLn('           variable length record unit, VREC.');
  66.   WriteLn;
  67.   WriteLn('   Syntax: BTVCALC MinRecSize MaxRecSize [-2]');
  68.   WriteLn;
  69.   WriteLn('    Where: MinRecSize is the fixed part of the record.');
  70.   WriteLn('           MaxRecSize is the largest record size.');
  71.   WriteLn('           -2 means NOT to use power of 2 section sizes.');
  72.   WriteLn;
  73.   WriteLn(' DOS redirection can be used to send the output to a file.');
  74.   Halt;
  75. end;
  76.  
  77. function Power(X,Y: Real): Real;
  78. {-Return X raised to the power of Y}
  79. begin
  80.   Power := Exp(Y*Ln(X));
  81. end;
  82.  
  83. function OverHead(SectionsUsed: LongInt): LongInt;
  84. {-Calculate the VRec Filer overhead for variable length record managemnt}
  85. begin
  86.   OverHead := 6 + (SectionsUsed-1) * 7;
  87. end;
  88.  
  89. function BytesUnused(SectionSize,SectionsUsed,RecordSize: LongInt): LongInt;
  90. {-Calculate the number of bytes that are not used}
  91. begin
  92.   BytesUnused := (SectionsUsed*SectionSize)-RecordSize-OverHead(SectionsUsed);
  93. end;
  94.  
  95.  
  96. begin  {BTVCalc}
  97.   {get min and max record sizes}
  98.   if ParamCount < 2 then Help;
  99.   Val(ParamStr(1),MinRecSize,i);
  100.   if (i <> 0) or (MinRecSize < 1) then Help;
  101.   Val(ParamStr(2),MaxRecSize,i);
  102.   if (i <> 0) or (MaxRecSize < MinRecSize) then Help;
  103.  
  104.   if ParamCount > 2 then
  105.     UsePowerOfTwo := ParamStr(3) <> '-2'
  106.   else
  107.     UsePowerOfTwo := True;
  108.  
  109.   {Used to bypass dupicate section sizes}
  110.   LastSecSize := 0;
  111.  
  112.   {Open standard output - so output can be redirected}
  113.   Assign(Output,'');
  114.   ReWrite(Output);
  115.  
  116.   WriteLn;
  117.   WriteLn(' BTVCALC - Possible section sizes for B-Tree Filer''s');
  118.   WriteLn('           variable length record unit, VREC.');
  119.   WriteLn;
  120.   WriteLn(' Minimum record size: ',MinRecSize);
  121.   WriteLn(' Maximum record size: ',MaxRecSize);
  122.   WriteLn;
  123.  
  124.   {calculate section sizes for 2 to 16 sections}
  125.   for Sec := 2 to 16 do begin
  126.     SectionSize := (MaxRecSize + OverHead(Sec)) div Sec;
  127.  
  128.     {work with even sized sections}
  129.     if Odd(SectionSize) then Inc (SectionSize);
  130.  
  131.     {Find closest power of 2 if not turned off at the command line}
  132.     if UsePowerOfTwo then begin
  133.       Pwr := 2;
  134.       while Round(Power(2,Pwr)) < SectionSize do Inc(Pwr);
  135.       SectionSize := Round(Power(2,Pwr));
  136.     end;
  137.  
  138.     {Skip sections sizes already displayed and those > 65536}
  139.     if (SectionSize <> LastSecSize) and (SectionSize <= 65536) then begin
  140.       LastSecSize := SectionSize;  {update our flag}
  141.  
  142.       WriteLn(' Sections of ',SectionSize,' Bytes each.');
  143.  
  144.       {Calculate sections used and unused bytes for MinRecSize}
  145.       UsedSections :=  MinRecSize div SectionSize;
  146.       {adjust used sections to account for overhead}
  147.       while (UsedSections * SectionSize) <
  148.             (MinRecSize + OverHead(UsedSections)) do Inc(UsedSections);
  149.       UnusedBytes := BytesUnused(SectionSize, UsedSections, MinRecSize);
  150.       WriteLn('    Minimum number of sections: ',UsedSections:2,
  151.               '    Bytes unused: ',UnusedBytes);
  152.  
  153.       {Calculate sections used and unused bytes for MaxRecSize}
  154.       UsedSections :=  MaxRecSize div SectionSize;
  155.       {adjust used sections to account for VRec maintenance overhead}
  156.       while (UsedSections * SectionSize) <
  157.             (MaxRecSize + OverHead(UsedSections)) do Inc(UsedSections);
  158.       UnusedBytes := BytesUnused(SectionSize, UsedSections, MaxRecSize);
  159.       WriteLn('    Maximum number of sections: ',UsedSections:2,
  160.               '    Bytes unused: ',UnusedBytes);
  161.  
  162.       {Average is computed assuming an equal usage distibution of the
  163.        variable part of each record throughout your data file}
  164.       UnusedAvg := 0;
  165.       for L := MinRecSize to MaxRecSize do begin
  166.         UsedSections :=  L div SectionSize;
  167.         {adjust used sections to account for overhead}
  168.         while (UsedSections * SectionSize) <
  169.               (L + OverHead(UsedSections)) do Inc(UsedSections);
  170.         UnusedAvg := UnusedAvg + BytesUnused(SectionSize,UsedSections,L);
  171.       end;
  172.       UnusedAvg := Round(UnusedAvg / (MaxRecSize-MinRecSize+1));
  173.       WriteLn('     Average unused Bytes for an equal distibution: ',
  174.               UnusedAvg:0:0);
  175.       WriteLn;
  176.     end;
  177.   end;
  178.   Close(Output);
  179.  
  180.   AssignCrt(Output);
  181.   ReWrite(Output);
  182.   WriteLn('  Done.');
  183. end.  {BTVCalc}
  184.  
  185.