home *** CD-ROM | disk | FTP | other *** search
- (* EXAMPLE1.PAS *)
-
- program driver;
- {$R+}
-
- uses
- Btree,
- Compare,
- FileBuff,
- FileDecs,
- Files,
- LRecList,
- Numbers,
- Page,
- Logical;
-
- const
- TOTALRECORDS = 100; (* number of records in this example *)
- TESTSTRINGSIZE = 10; (* size of strings (part of test record) *)
-
- type
- TestString = String[TESTSTRINGSIZE];
-
- TestRecord = record (* record definition for data file *)
- randByte : Byte;
- randString : TestString;
- end;
-
- var
- dataFile,
- indexFile1,
- indexFile2 : FnString; (* holds file name strings *)
-
- testRec : TestRecord; (* variable to hold a data record *)
-
- recCnt : 1 .. TOTALRECORDS;
-
- lrNum : LrNumber;
-
-
- (* This routine should be called before anything else happens. It calls
- the various routines to set up parameters according to values applicable
- to your particular application. *)
-
- procedure SetUp;
-
- begin
- SetMaxBufferPages(100); (* a call to the PAGE unit to set up the number
- of pages in the buffer. If this is not done a
- default of one page is used. This will cause
- poor performance but will not cause any
- runtime errors *)
-
- SetMaxOpenFiles(10); (* a call to the FILEBUFF unit to set the number
- of files the FILEBUFF unit can have open at
- once. See FILEBUFF unit for details. *)
-
- SetImmediateDiskWrite(FALSE); (* changed and newly created pages will be
- buffered in the page buffer and will only
- written to disk when they are swapped out
- or explicitly written out by a user
- request *)
-
- end;
-
-
- (* This procedure creates the one data file and the two corresponding
- index files. A couple of things should be noted. First, the files
- have no extensions! The extensions will be appended during creation
- and the file name variable will be modified. This will ensure that the
- correct extensions are used. The extensions are necessary to later
- determine file type and therefore should not be altered. Secondly,
- the data file needs to be created before the index file because the
- data file name is stored as part of the index. Presently this feature
- is not used, but I may need it later and decided to include it now. If
- you declare the data file after the index file, the name stored will not
- have the appropriate extension. Thirdly, the user does not create the
- bitmap files for the data and index files. As a matter of fact, the
- user NEVER gets involved with bitmap files directly. Ladtly, notice the
- use of SizeOf to pass in the size of TestString. This will alleviate
- problems with incorrectly calculation size of data types, especially
- strings. *)
-
- procedure InitFiles;
-
- begin
-
- dataFile := 'myFile1';
- CreateFile(dataFile,DATA);
-
- indexFile1 := 'testByte';
- CreateIndex(indexFile1,SizeOf(Byte),dataFile,BYTEVALUE);
-
- indexfile2 := 'testStrg';
- CreateIndex(indexFile2,SizeOf(TestString),dataFile,STRINGVALUE);
-
- end;
-
-
- (* This routine creates a random string and is used for creating strings to
- demonstrate the handling of strings in indexes *)
-
- procedure CreateRandomString(var str : TestString);
-
- var
- chrCnt : 1 .. TESTSTRINGSIZE;
- tss : Byte;
-
- begin
- str := '';
- for chrCnt := 1 to TESTSTRINGSIZE do
- begin
- str[chrCnt] := Chr(Random(25) + 65);
- end;
- tss := TESTSTRINGSIZE;
- Move(tss,str,1);
- end;
-
-
-
- begin
-
- Writeln('Initializing Parameters ...'); (* just a note so you can
- follow along *)
-
- SetUp; (* set file open buffer size and page buffer size limits *)
- (* set immediate disk write parameter as well *)
-
- Writeln('Creating Files ...'); (* just a note so you can
- follow along *)
-
-
- InitFiles; (* create the files and indexes *)
-
- Randomize; (* needed only for the random string creation
- routine and has nothing to do with
- BTree10 *)
-
- Writeln('The number of levels in the string index is -> ',
- NumberOfBTreeLevels(indexFile2));
- Writeln;
-
- Writeln('Creating and storing data ... this may take a minute ...');
- (* just a note so you can
- follow along *)
-
- (* the following loop will create 100 records which will be inserted into
- the data file. The appropriate values are also inserted into the
- two index files. *)
-
- for recCnt := 1 to TOTALRECORDS do
- begin
-
- testRec.randByte := Random(MAXBYTE + 1); (* random byte from 1 .. 255 *)
- CreateRandomString(testRec.randString); (* random string of 10 letters *)
-
- lrNum := StoreNewLogicalRecord(dataFile,testRec,SizeOf(testRec));
- (* insert
- in data
- file *)
-
- InsertValueInBTree(indexFile1,lrNum,testRec.randByte);
- InsertValueInBTree(indexFile2,lrNum,testRec.randString);
-
- end;
-
- Writeln;
- Writeln('The number of levels in the string index is -> ',
- NumberOfBTreeLevels(indexFile2));
- Writeln;
-
- Writeln('Writing Records To Disk ...'); (* just a note so you can
- follow along *)
-
- WriteEntireBufferToDisk; (* very important step!! Before leaving the
- program the buffer must be written to disk
- or some changes will be lost. This will
- cause major problems and lost data!!! *)
-
- Writeln('Closing Files ...'); (* just a note so you can
- follow along *)
-
- CloseAllFiles; (* Close the files to clean up. It is
- important to realize that CloseAllFiles
- DOES NOT write the buffer to disk, it only
- deals with the files open list. *)
-
- end.