Unit KeyU

Classes

TKey -

Functions

Register - A description of the properties and methods follows.

Types

IDSectorInfo
KeyData
KeyHeader
KeySector
Locator
SectorFile
SectorInfo
SectorInfoArray
TRecsPerKey

Constants

NumLocs
Offset

Variables


Functions


procedure Register;

A description of the properties and methods follows.} {ActiveKeys: The number of keys in the file at the present time. Read only. FileName: The name of the disk file holding the keys. KeyLength: The maximum length of a key string. Read only but visible. RecsPerKey: Can have one of three states: None: File not attached yet. No key operations while in this state. One: One record id per key. Use this when the key string can be used to uniquely identify the record with the given id. Many: Many records per key. Use this when there can be many records involving the given key string. This property is set when the file is created and canoot be changed after that. AttachFile: This method MUST be called before any other methods can be executed. This method hooks the key data set with disk name FileName to the TKey. DetachFile: This method needs to be called only if you wish to use a TKey component with another key data set after using it with another one. For example suppose the program is running with TKey attached to 'FIRST.KEY'. To switch to 'SECOND.KEY' do this AKey.DetachFile; AKey.FileName := 'SECOND.KEY'; AKey.AttachFile; Create: Obvious; ExistKey: Returns TRUE if the given key exists and FALSE if it does not. FindKey: This routine looks for the key sought in the file. If it is present it just returns the string sought. If the key sought is not in the file but some next key after sought is in the file then it returns that key. If there is no sought key or following key it returns the null string. FirstKey: Returns the lowest or first key in the file or the null string if there are no keys. LastKey: Returns the highest or last key in the file or the null string if there are no keys. NextKey: Returns the key in the file next after the given key or the null sting if there is none. To go through all the keys code something like this. CurrentKey := ThisKey.FirstKey; while CurrentKey <> '' do begin ... do things with the key record, such as fetch a record ... CurrentKey := ThisKey.NextKey(CurrentKey); end; end; PrevKey: Returns the key in the file just before the given key or the null string if there is none. InsertKey: Attempts to insert the key with string newkey into the file. If the key newkey is not already in the file it will insert it and return TRUE. You can get the KeyID associated with the newkey by calling GetKeyID with newkey as parameter. If newkey is already in the file this method returns FALSE and does nothing else. DeleteKey: Attemps to delete the key with string killkey from the file. First the methods looks to see if the key killkey exists. If it does not then it returns FALSE and does nothing else. Assuming killkey exists the method next looks at the parameter IfEmpty. If FALSE the method deletes the key killkey and returns TRUE. If IfEmpty is TRUE then the key killkey will be deleted only if there are no associated record id's in which case TRUE is returned. If there are associated record id's then FALSE is returned and nothing else happens. In all cases when TRUE is returned the key killkey was deleted and if FALSE was returned it was not. ChangeKey: This method changes the key string from its present value, oldkey, to a new value, newkey. Any associated id(s) are preserved. If either oldkey does not already exist or newkey already does exist the routine returns FALSE and the change does not occur. Otherwise the routine returns TRUE and the change does occur. GetKeyID: This returns the key id associated with the string key. This longint can be stored in place of the much longer string. If the key does not exist, then -1 is returned to indicate -No such key-. GetKey: This returns the key string associated with the given key id kid. If kid does not refer to a valid key then the null string is returned. Get RecIDCount: This returns the number of record id's assigned to the given key. If the RecsPerKey property has the value One, this value can only be 0 or 1. IncludeRecID: This attempts to add the given record id rid to the key with string key. The method first sees if the key exists. If it does not, it returns FALSE and nothing else happens. Assuming that the key exists, the method next looks at the RecsPerKey property. If this property has the value One then only one record id is permitted. If RecsPerKey is One and there is already an assigned record id, the method returns FALSE and nothing else happens. If RecsPerKey is One and no record id is currently assigned, then rid is assigned and TRUE is returned. If the key exists AND RecsPerKey is Many then rid will be included among the assigned id's whether or not is was already present. In this case TRUE is returned. In general, if TRUE was returned then the given key exists and the given rid is among its assigned record id's. RemoveRecID: Attempts to remove the record id rid from the assigned id's for the key with the given string. If the key does not exist, FALSE is returned and nothing else happens. Assuming that the key exists then the given rid will be removed from the assigned record id's whether is was there or not and TRUE is returned. In any case, when TRUE is returned, the key exists and rid is not part of its assigned record id's. GetRecID: This method returns the record id in position n of the assigned record id's for the key with string key. The parameter n is 1 based (unlike most things in Delphi) so that to get the first (or only) id use n=1. If the record id does not exist for any reason, then -1 is returned. This can happen if the key does not exist, or it does but the index n is greater or equal to the number of assigned record ids. If the RecsPerKey property is One, the n parameter is ignored and the returned longint is either -1 or the single record id.

Types


IDSectorInfo = record
SecNo : word;
ItemNo : byte;
end;

KeyData = record
kjd : longint;
rjd : longint;
num : word;
end;

KeyHeader = record
NextID : longint;
Active : longint;
KL : word;
RecsPerKey : TRecsPerKey;
RootLevel : byte;
SendNumber : byte;
KeepNumber : byte;
RootSector : word;
FirstSector : word;
end;
See below for the use of each of these fields.
KeySector = record
KH : KeyHeader;
Level : byte;
Count : byte;
Follower : word;
Previous : word;
C : array [0..2023] of byte;
end;

Locator = record
at : longint;
num : word;
end;
number of record id's
SectorFile = file of KeySector

SectorInfo = record
SecNo : word;
ItemNo : byte;
end;

SectorInfoArray = array [0..9] of SectorInfo;

TRecsPerKey = (None, One, Many);

Constants

NumLocs = 193

Make sure these records are packed

Offset = 145


Variables