?? myldbtypes.pas
字號:
procedure SetBit(BitNo: Integer; Value: Boolean);
// returns number of bit = 1 in FBits array by bit position
function GetBitNoByBitPosition(BitPosition: Integer): Integer;
// returns position of bit = 1 by bit no in FBits array
function GetBitPositionByBitNo(BitNo: Integer): Integer;
procedure SetAllBits;
function FindBit(Value: Boolean; var BitNo: Integer): Boolean;
function Find(Restart,GoForward: Boolean; CurBitNo: Integer; var FoundBitNo: Integer): Boolean;
public
property Size: Integer read FBitCount write SetSize;
property NonZeroBitCount: Integer read FNonZeroBitCount;
end;
////////////////////////////////////////////////////////////////////////////////
//
// Bits functions
//
////////////////////////////////////////////////////////////////////////////////
// return true if null flag is set (bit = 1)
function CheckNullFlag(
BitNo: Integer; // number of bit to check
NullFlags: PChar // pointer to bits of null flags
): Boolean;
// set or clear a null flag
procedure SetNullFlag(
ToSet: Boolean; // if true - set bit = 1, otherwise set bit = 0
BitNo: Integer; // number of bit to check
NullFlags: PChar // pointer to bits of null flags
);
////////////////////////////////////////////////////////////////////////////////
//
// Other functions
//
////////////////////////////////////////////////////////////////////////////////
// returns temporary name
function GetTemporaryName(Prefix: String): String;
function BracketFieldName(name: String): String;
implementation
uses Math, MYLDBExpressions, MYLDBCompression, MYLDBBTree, MYLDBBase;
////////////////////////////////////////////////////////////////////////////////
//
// TMYLDBIndexPositionCache
//
////////////////////////////////////////////////////////////////////////////////
//------------------------------------------------------------------------------
// Create
//------------------------------------------------------------------------------
constructor TMYLDBIndexPositionCache.Create;
begin
Position := TMYLDBKeyPath.Create;
IndexID := INVALID_OBJECT_ID;
TableState := -1;
end;// Create
//------------------------------------------------------------------------------
// Destroy
//------------------------------------------------------------------------------
destructor TMYLDBIndexPositionCache.Destroy;
begin
TMYLDBKeyPath(Position).Free;
end;// Destroy
////////////////////////////////////////////////////////////////////////////////
//
// TMYLDBScanSearchCondition
//
////////////////////////////////////////////////////////////////////////////////
//------------------------------------------------------------------------------
// ClearKeyRecordBuffer
//------------------------------------------------------------------------------
procedure TMYLDBScanSearchCondition.ClearKeyRecordBuffer;
begin
if (not FExternalKeyRecordBuffer) then
if (KeyRecordBuffer <> nil) then
MemoryManager.FreeAndNillMem(KeyRecordBuffer);
end;// ClearKeyRecordBuffer
//------------------------------------------------------------------------------
// Create
//------------------------------------------------------------------------------
constructor TMYLDBScanSearchCondition.Create(ExternalKeyRecordBuffer: Boolean);
begin
FExternalKeyRecordBuffer := ExternalKeyRecordBuffer;
KeyRecordBuffer := nil;
KeyRecordBufferSize := 0;
IndexID := INVALID_OBJECT_ID;
Expression := nil;
CreatedFromExpression := False;
ParentExpressions := TList.Create;
ParentExpressionNodes := TList.Create;
PartialCompare := False;
end;// Create
//------------------------------------------------------------------------------
// Destroy
//------------------------------------------------------------------------------
destructor TMYLDBScanSearchCondition.Destroy;
begin
ClearKeyRecordBuffer;
ParentExpressions.Free;
ParentExpressionNodes.Free;
inherited;
end;// Destroy
//------------------------------------------------------------------------------
// Assign
//------------------------------------------------------------------------------
procedure TMYLDBScanSearchCondition.Assign(ScanSearchCondition: TMYLDBScanSearchCondition);
var
i: Integer;
begin
Condition := ScanSearchCondition.Condition;
ClearKeyRecordBuffer;
if (ScanSearchCondition.KeyRecordBuffer <> nil) then
begin
KeyRecordBuffer := MemoryManager.AllocMem(ScanSearchCondition.KeyRecordBufferSize);
Move(ScanSearchCondition.KeyRecordBuffer^, KeyRecordBuffer^, ScanSearchCondition.KeyRecordBufferSize);
end
else
KeyRecordBuffer := nil;
FExternalKeyRecordBuffer := False;
KeyRecordBufferSize := ScanSearchCondition.KeyRecordBufferSize;
KeyFieldCount := ScanSearchCondition.KeyFieldCount;
IndexID := ScanSearchCondition.IndexID;
Expression := ScanSearchCondition.Expression;
CreatedFromExpression := ScanSearchCondition.CreatedFromExpression;
PartialCompare := ScanSearchCondition.PartialCompare;
// assign
ParentExpressions.Clear;
for i:=0 to ScanSearchCondition.ParentExpressions.Count-1 do
ParentExpressions.Add(ScanSearchCondition.ParentExpressions[i]);
// assign
ParentExpressionNodes.Clear;
for i:=0 to ScanSearchCondition.ParentExpressionNodes.Count-1 do
ParentExpressionNodes.Add(ScanSearchCondition.ParentExpressionNodes[i]);
end;// Assign
////////////////////////////////////////////////////////////////////////////////
//
// TMYLDBScanSearchConditionArray
//
////////////////////////////////////////////////////////////////////////////////
//------------------------------------------------------------------------------
// Create
//------------------------------------------------------------------------------
constructor TMYLDBScanSearchConditionArray.Create;
begin
Items := nil;
Count := 0;
end;// Create
//------------------------------------------------------------------------------
// Destroy
//------------------------------------------------------------------------------
destructor TMYLDBScanSearchConditionArray.Destroy;
var
i: Integer;
begin
for i := 0 to Count-1 do
Items[i].Free;
SetLength(Items, 0);
end;// Destroy
//------------------------------------------------------------------------------
// Delete
//------------------------------------------------------------------------------
procedure TMYLDBScanSearchConditionArray.Delete(ItemNo: Integer);
begin
if (ItemNo < 0) or (ItemNo >= Count) then
raise EMYLDBException.Create(10413,ErrorLInvalidItemNumber);
if (Items[ItemNo] = nil) then
raise EMYLDBException.Create(10414,ErrorLNilPointer);
Items[ItemNo].Free;
if (ItemNo < Count - 1) then
Move(Items[ItemNo+ 1 ],Items[ItemNo],
(Count - ItemNo-1) * SizeOf(TMYLDBScanSearchCondition));
Dec(Count);
SetLength(Items,Count);
end; // Delete
//------------------------------------------------------------------------------
// AddExpression
//------------------------------------------------------------------------------
procedure TMYLDBScanSearchConditionArray.AddExpression(Expression: Pointer);
begin
SetLength(Items, Count+1);
Items[Count] := TMYLDBScanSearchCondition.Create(False);
Items[Count].Expression := Expression;
Items[Count].Condition := scNone;
Items[Count].KeyRecordBuffer := nil;
Items[Count].KeyFieldCount := 0;
Items[Count].IndexID := INVALID_OBJECT_ID;
Inc(Count);
end;// AddExpression
//------------------------------------------------------------------------------
// AddCondition
//------------------------------------------------------------------------------
procedure TMYLDBScanSearchConditionArray.AddCondition(
ScanSearchCondition: TMYLDBScanSearchCondition
);
begin
if ((ScanSearchCondition.Expression = nil) and
(ScanSearchCondition.IndexID = INVALID_ID4)) then
raise EMYLDBException.Create(10388,ErrorLInvalidScanConditionNoIndex,
[ScanSearchCondition.IndexID,Count]);
SetLength(Items, Count+1);
Items[Count] := TMYLDBScanSearchCondition.Create(False);
Items[Count].Assign(ScanSearchCondition);
Inc(Count);
end;// AddCondition
//------------------------------------------------------------------------------
// TryToExtendMultiFieldIndexCondition
//------------------------------------------------------------------------------
procedure TMYLDBScanSearchConditionArray.TryToExtendMultiFieldIndexCondition(
ConditionNo: Integer; IndexDef: Pointer; Expressions: TList);
var
bExtendSucceeded: Boolean;
i: integer;
begin
if (TMYLDBIndexDef(IndexDef).ColumnCount > 1) then
begin
bExtendSucceeded := False;
repeat
for i := 0 to Expressions.Count-1 do
begin
bExtendSucceeded := TMYLDBExpression(Expressions[i]).ExtendMultiFieldIndexCondition(Items[ConditionNo], IndexDef);
if (bExtendSucceeded) then
break;
end;
until (not bExtendSucceeded);
end;
end;// TryToExtendMultiFieldIndexCondition
//------------------------------------------------------------------------------
// CreateIndexScanConditionsFromExpressions
//------------------------------------------------------------------------------
procedure TMYLDBScanSearchConditionArray.CreateIndexScanConditionsFromExpressions(IndexDefs: Pointer; SessionID: TMYLDBSessionID);
var
i: Integer;
Expressions: TList;
begin
// create all possible single-field index scan conditions
for i := 0 to Count-1 do
if (Items[i].Expression <> nil) then
TMYLDBExpression(Items[i].Expression).CreateIndexScanConditions(IndexDefs, Self, SessionID);
Expressions := TList.Create;
try
// collect all expressions
for i := 0 to Count-1 do
if (Items[i].Expression <> nil) then
Expressions.Add(Items[i].Expression);
if (Expressions.Count > 0) then
// try to extend multi-field index conditions
for i := 0 to Count-1 do
if (Items[i].Expression = nil) then
TryToExtendMultiFieldIndexCondition(i, TMYLDBIndexDefs(IndexDefs).GetDefByObjectId(Items[i].IndexID), Expressions);
finally
Expressions.Free;
end;
end;// CreateIndexScanConditionsFromExpressions
//------------------------------------------------------------------------------
// RemoveUnusedConditionsCreatedFromExpressions
//------------------------------------------------------------------------------
procedure TMYLDBScanSearchConditionArray.RemoveUnusedConditionsCreatedFromExpressions(
var ScanConditionNo,ScanEndConditionNo: Integer);
var
i: integer;
begin
i := 0;
while (i < Count-1) do
begin
if not (i in [ScanConditionNo, ScanEndConditionNo]) then
if (Items[i].CreatedFromExpression) then
begin
Delete(i);
if (ScanConditionNo > i) then
Dec(ScanConditionNo);
if (ScanEndConditionNo > i) then
Dec(ScanEndConditionNo);
i := 0;
continue;
end;
Inc(i);
end;
end;// RemoveUnusedConditionsCreatedFromExpressions
//------------------------------------------------------------------------------
// ExtractChosenConditionsFromExpressions
//------------------------------------------------------------------------------
procedure TMYLDBScanSearchConditionArray.ExtractChosenConditionsFromExpressions(ScanConditionNo, ScanEndConditionNo: Integer);
var
i: Integer;
begin
for i := 0 to Count-1 do
if (Items[i].Expression <> nil) then
TMYLDBExpression(Items[i].Expression).RemoveExtractedNodes(Self,ScanConditionNo,ScanEndConditionNo);
end;// ExtractChosenConditionsFromExpressions
//------------------------------------------------------------------------------
// ReturnExtractedConditionsToExpressions
//------------------------------------------------------------------------------
procedure TMYLDBScanSearchConditionArray.ReturnExtractedConditionsToExpressions(ScanConditionNo, ScanEndConditionNo: Integer);
var
i,j: Integer;
begin
for i := 0 to Count-1 do
if (i in [ScanConditionNo, ScanEndConditionNo]) then
for j := 0 to Items[i].ParentExpressions.Count-1 do
TMYLDBExpression(Items[i].ParentExpressions[j]).AddNode(Items[i].ParentExpressionNodes[j]);
end;// ReturnExtractedConditionsToExpressions
////////////////////////////////////////////////////////////////////////////////
//
// TMYLDBIntegerArray
//
////////////////////////////////////////////////////////////////////////////////
//------------------------------------------------------------------------------
// Construct array of specified size
//------------------------------------------------------------------------------
constructor TMYLDBIntegerArray.Create(
size: Integer;
DefaultAllocBy: Integer;
MaximumAllocBy: Integer
);
begin
AllocBy := DefaultAllocBy; // default alloc
deAllocBy := DefaultAllocBy; // default dealloc
MaxAllocBy := MaximumAllocBy; // max alloc
AllocItemCount := 0;
SetSize(size);
end;//TMYLDBIntegerArray.Create
//------------------------------------------------------------------------------
// Destruct array (free mem)
//------------------------------------------------------------------------------
destructor TMYLDBIntegerArray.Destroy;
begin
Items := nil;
inherited Destroy;
end;//TMYLDBIntegerArray.Destroy;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -