www.pudn.com > GOOGLE.rar > 2_1.pas, change:2002-01-14,size:2886b


type 
	TaaBtreePage = class 
		private 
			FDiskPage  : PChar; 
			FIsLeaf	   : boolean; 
			FKeyCount  : integer; 
			FKeyLen    : integer; 
			FKeys      : TStringList; 
			FOrder     : integer; 
			FPageLinks : array of integer; 
			FPageNum   : integer; 
			FRecStrm   : TaaRecordStream; 
			FRecLinks  : array of integer; 
		protected 
			procedure bpRead; 
			procedure bpWrite; 
		public 
			constructor Create( aRecordStream : TaaRecordStream; 
				aKeyLen : integer; aPageNum : integer ); 
			constructor CreateNew( aRecordStream : TaaRecordStream; 
				aKeyLen : integer; aAsLeaf : boolean ); 
			destructor Destroy : override; 
			procedure AddKey( const aKey : string; aRecLink : integer; 
				aPageLink : integer; aLeftLink : integer = -1 ); 
			function FindKey( const aKey : string; aRecLink : integer; 
				var aLink : longint ) : boolean; 
			function IsFull : boolean; 
			procedure Split( aNewPage : TaaBtreePage; var aMidKey : string;  
				var aMidRecLink : longint ); 
			property IsLeaf : boolean read FIsLeaf; 
			property KeyCount : integer read FKeyCount; 
			property PageNumber : integer read FPageNum; 
	end; 
 
constructor TaaBtreePage.Create( aRecordStream : TaaRecordStream; 
	aKEyLen : integer; aPageNum : integer ); 
begin 
	{create the ancestor} 
	inherited Create; 
	{save the parameters} 
	Assert( aRecordStream <> nil , 
		'TaaBtreePage.Create : record stream cannot be nil' ); 
	FRecStrm := aRecordStream; 
	Assert( aPageNum >= 0, 
		'TaaBtreePage.Create : page number cannot be negative' ); 
	FPageNum := aPageNum; 
	Assert( aKeyLen > 0 , 
		'TaaBtreePage.Create : key length cannot be zero or negative' ); 
	FKeyLen := aKeyLen; 
	{calculate the oeder of the page} 
	FOrder := (FRecStrm.RecordLength + aKeyLen) div (8 + aKeyLen); 
	if odd(FOrder) then 
		dec(FOrder); 
	{create the disk page image} 
	FDiskPage := AllocMem( aRecordStream.RecodLength ); 
	{create the lists} 
	FKeys := TStringList.Create; 
	SetLength( FPageLinks , FOrder ); 
	SetLength( FRecLinks , FOrder ); 
	{read the page from the stream} 
	bpRead; 
end; 
 
constructor TaaBtreePage.CreateNew( aRecordStream : TaaRecordStream; 
	aKeyLen : integer; aAsLeaf : boolean ); 
var 
	EmptyPage : PChar; 
	PageNum   : integer; 
begin 
	{initialize a new btree page and add it to record stream} 
	Assert( aRecordStream <> nil , 
		'TaaBtreePage.Create : record stream cannot be nil ' ); 
	EmptyPage := AllocMem(aRecordStream.RecordLength); 
	try 
		if not aAsLeaf then 
			EmptyPage[2] := char(true); 
		PageNum := aRecordStream.Add(EmptyPAge^); 
	finally 
		FreeMem(EmptyPage); 
	end; 
	{now initialize the object properly} 
	Create(aRecordStream , aKeyLen , PageNum); 
end; 
 
destructor TaaBtreePage.Destroy; 
begin 
	{free the list} 
	FKeys.Free; 
	{free the disk page image} 
	if (FDiskPage <> nil) then 
		FreeMem(FDiskPage); 
	{destroy the ancestor} 
	inherited Destroy; 
end;