Menu

[r4761]: / branches / parsnip / Src / Main / CS.Database.SnippetsTable.pas  Maximize  Restore  History

Download this file

171 lines (139 with data), 3.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
{
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 2013, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Implements the snippets table at the core of the snippets database.
}
unit CS.Database.SnippetsTable;
interface
uses
Collections.Dictionaries,
Collections.Base,
CS.Database.Types,
CS.Database.Snippets,
UExceptions;
type
TDBSnippet = class(TSnippetBase)
public
class function CreateFrom(ASnippet: IEditableSnippet): TDBSnippet;
function CloneAsEditable: IEditableSnippet;
function CloneAsReadOnly: ISnippet;
function IsEqual(const AOther: TDBSnippet): Boolean;
end;
TDBSnippetsTable = class(TObject)
strict private
var
fTable: TObjectDictionary<TSnippetID, TDBSnippet>;
public
constructor Create;
destructor Destroy; override;
function GetEnumerator: IEnumerator<TDBSnippet>;
function Contains(const ASnippetID: TSnippetID): Boolean;
function Get(const ASnippetID: TSnippetID): TDBSnippet;
procedure Add(const ASnippet: TDBSnippet);
procedure Update(const ASnippet: TDBSnippet);
procedure Delete(const ASnippetID: TSnippetID);
procedure Clear;
function Size: Integer;
function IsEmpty: Boolean;
end;
EDBSnippetsTable = class(EBug);
implementation
{ TODO - cPerformance: If cost of cloning creating read-only snippets on each
lookup causes a performance bottleneck, considering caching read only
snippets. If this is done make sure that snippets are removed from cache each
time snippet with same ID is snippet table is modified. }
uses
// Delphi
SysUtils,
Generics.Defaults,
// Project
UComparers;
{ TDBSnippet }
function TDBSnippet.CloneAsEditable: IEditableSnippet;
begin
Result := TEditableSnippet.Create(Self);
end;
function TDBSnippet.CloneAsReadOnly:
ISnippet;
begin
Result := TReadOnlySnippet.Create(Self);
end;
class function TDBSnippet.CreateFrom(ASnippet: IEditableSnippet): TDBSnippet;
begin
Result := TDBSnippet.Create(ASnippet as TEditableSnippet);
end;
function TDBSnippet.IsEqual(const AOther: TDBSnippet): Boolean;
begin
Result := Self.GetID = AOther.GetID;
end;
{ TDBSnippetsTable }
procedure TDBSnippetsTable.Add(const ASnippet: TDBSnippet);
begin
fTable.Add(ASnippet.GetID, ASnippet);
end;
procedure TDBSnippetsTable.Clear;
begin
fTable.Clear;
end;
function TDBSnippetsTable.Contains(const ASnippetID: TSnippetID): Boolean;
begin
Result := fTable.ContainsKey(ASnippetID);
end;
constructor TDBSnippetsTable.Create;
begin
inherited Create;
fTable := TObjectDictionary<TSnippetID,TDBSnippet>.Create(
TRulesFactory<TSnippetID>.CreateFromComparator(
TSnippetID.TComparator.Create
),
TRulesFactory<TDBSnippet>.Construct(
function (const Left, Right: TDBSnippet): Integer
begin
Result := TSnippetID.Compare(Left.GetID, Right.GetID);
end,
function (const Snippet: TDBSnippet): Integer
begin
Result := Snippet.GetID.Hash;
end
)
);
fTable.OwnsKeys := False;
fTable.OwnsValues := True;
end;
procedure TDBSnippetsTable.Delete(const ASnippetID: TSnippetID);
begin
fTable.Remove(ASnippetID);
end;
destructor TDBSnippetsTable.Destroy;
begin
fTable.Free;
inherited;
end;
function TDBSnippetsTable.Get(const ASnippetID: TSnippetID): TDBSnippet;
begin
Result := fTable[ASnippetID];
end;
function TDBSnippetsTable.GetEnumerator: IEnumerator<TDBSnippet>;
begin
Result := fTable.Values.GetEnumerator;
end;
function TDBSnippetsTable.IsEmpty: Boolean;
begin
Result := fTable.Count = 0;
end;
function TDBSnippetsTable.Size: Integer;
begin
Result := fTable.Count;
end;
procedure TDBSnippetsTable.Update(const ASnippet: TDBSnippet);
begin
fTable[ASnippet.GetID].UpdateFrom(ASnippet);
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.