home *** CD-ROM | disk | FTP | other *** search
- (* TBTree13 Copyright (c) 1988 Dean H. Farwell II *)
-
- unit Btree;
-
- (*****************************************************************************)
- (* *)
- (* B T R E E R O U T I N E S *)
- (* *)
- (*****************************************************************************)
-
- (* These routines are responsible for handling the indexes. The scheme used
- is Knuth's variation of B-Trees discussed on page 64 of "Database Systems
- Volume I Fourth Edition" by C.J. Date. There are two different types of
- nodes in the tree. The upper level(s) are index nodes and fill the role of
- the normal B-Tree. The lowest level nodes are sequence nodes and represent
- dense index to the file. The entries in the sequence node are sorted and
- thus provide an alternate means to access information in the file. This
- allows rapid sorted retrievals without the need to sort.
-
- It is important to understand the operations can be performed on this BTree.
- Routines provided are used to add a value, delete a value (one or more
- occurences), create an index, and fetch a list of logical record numbers
- corresponding to a value. When an index file is created, the zeroth record,
- the root node, and one sequence node are created. Therefore, there will
- always be one root node and one sequence node for every index. The
- sequence node can, of course, be empty. *)
-
- (* Version Information
-
- Version 1.1 - Corrected error in InsertValueInBTree routine. I
- was not checking for duplicate entries. This has
- been resolved.
-
- - Added GetRangeFromBTree routine
-
- - Added GetSubstringFromBTree routine
-
- - Divided the source code into 3 source files - BTREE.PAS
- BTREE1.INC
- BTREE1.INC
-
- Version 1.2 - No Changes
-
- Version 1.3 - Added NumberOfBTreeLevels routine *)
-
- (*\*)
- (*////////////////////////// I N T E R F A C E //////////////////////////////*)
-
- interface
-
- uses
- Compare,
- FileDecs,
- Files,
- Numbers,
- LRecList,
- Page;
-
- const
- MAXVALSIZE = 494; (* max value size in index *)
-
- type
- VSizeType = 1 .. MAXVALSIZE; (* size range for index entries *)
-
-
- (* This routine will create an index file and set the zeroth record as
- appropriate. The root node will also be created. The root node will
- be empty.
-
- note - File name passed as parameter does not have a file extension. The
- extension will be added and returned.
-
- note - Extremely important - WARNING - for CHARVALUE indexes only - the
- valsize must be 1 greater than the number of characters of the longest
- string. This will allow 1 byte for the string length to be stored.
- for example - if 'abc' is the longest string then valSize = 4. *)
-
- procedure CreateIndex(var iFName : FnString;
- valSize : VSizeType;
- dataFName : FnString;
- valType : ValueType);
-
-
- (* This routine will insert a value and its associated logical record number
- into the given index file. This routine will guard against duplicate
- entries. An index should have no more than one occurence of any
- lrNum,paramValue pair (no two entries match on paramValue and lrNum). This
- routine assures this by calling DeleteValueFromBTree prior to performing the
- insert. This will get rid of a previous occurence if it exists. *)
-
- procedure InsertValueInBTree(iFName : FnString;
- lrNum : LRNumber;
- var paramValue);
-
- (*\*)
- (* This routine will delete a value and its associated logical record number
- from a given index file. If the logical record number passed in is zero
- then all records with a matching paramValue will be deleted. Otherwise,
- only the entry with the matching paramValue and the matching logical record
- number will be deleted. *)
-
- procedure DeleteValueFromBTree(iFName : FnString;
- lrNum : LrNumber;
- var paramValue);
-
-
- (* This routine will start at the root node and return the number of levels
- that exist in a BTree. The index file name is the only required input. *)
-
- function NumberOfBTreeLevels(iFName : FnString) : Byte;
-
-
- (* This routine locates all values within the index which meet a condition
- (cond) and returns a list of logical record numbers corresponding to these
- values.
-
- note - In the case of cond = EX (exists) all entries in the index will be
- returned. paramValue is not really used in this case so anything (a dummy)
- can be passed in as the parameter corresponding to paramValue. *)
-
- procedure GetValuesFromBTree(iFName : FnString;
- var paramValue;
- cond : Condition;
- var lrLst : LrList);
-
-
- (* This routine locates all values within the index within a given range and
- returns a list of logical record numbers corresponding to values within
- that range. The range is determined by paramValue1 and paramValue2.
- paramValue1 must be less than paramValue2. cond1 and cond2 are used to
- determine if the range is inclusive or exclusive. cond1 must be either
- GE or GT and cond2 must be either LE or LT for this to work. If any of
- the above conditions is not true then an empty list will be returned. *)
-
- procedure GetRangeFromBTree(iFName : FnString;
- var paramValue1;
- cond1 : Condition;
- var paramValue2;
- cond2 : Condition;
- var lrLst : LrList);
-
- (*\*)
- (* This routine locates partial string matches in an index of type
- STRINGVALUE. A partial string match occurs when the string passed in as
- paramValue is contained within a string entry in the index. You must
- specify where in the string the match must occur. You accomplish this by
- using cond. cond can be CO which stands for contains. In this case, a
- match occurs if the string passed in as paramValue is anywhere in the
- string in the index. Another option for cond is ST which stands for
- starts. A match will occur in this instance if paramValue is located at
- the start of the string in the index. The last option for cond is EN which
- stands for ends. A match here occurs if paramValue matches the last
- characters in the index string. Be aware that if paramValue and the string
- being checked are the same length and they match exactly, then a match
- would occur if any of the three options were selected (not earth shattering
- but true nonetheless).
-
- Otherwise, this works like any of the other retrieval routines. A list of
- logical record numbers will be returned.
-
- This is exceptionally useful for many applications. For example, a field
- might be a 7 digit code stored as a string (STRINGVALUE). The last two
- digits might mean something in particular (part category, state, or
- whatever). Using this you can look for all the matches on only the last
- two digits.
-
- One reality note here!! - For cond <> ST I have to look at every entry in
- the index for matches. Why this is true should be reasonably obvious.
- Anyway, for cond = EN or cond = CO it will be somewhat slower than for cond
- = ST. How much slower depends on the size of the index. It boils down to
- the difference between a O(LOG(n)) algorithm and a O(n) algorithm, the
- former being much faster for n = a large number. If any of the above is
- confusing ignore it and experiment. It is still better to use this routine
- than to grovel through all of the data records yourself!! *)
-
- procedure GetSubstringFromBTree(iFName : FnString;
- var paramValue; (* must be a string var *)
- cond : StringCondition;
- var lrLst : LrList);
-
-
- (*\*)
- (*///////////////////// I M P L E M E N T A T I O N /////////////////////////*)
-
- implementation
-
- {$I btree1.inc} (* most of the implementation part of the btree unit *)
-
- {$I btree2.inc} (* the rest of the implementation of the btree unit *)
-
- end. (* end of BTree unit *)