Menu

[r3207]: / trunk / Src / SWAG.UReader.pas  Maximize  Restore  History

Download this file

315 lines (282 with data), 8.3 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
{
* 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 a class that provides cached access to the SWAG database.
}
unit SWAG.UReader;
interface
uses
// Delphi
SysUtils,
Generics.Collections,
// Project
SWAG.UCommon,
SWAG.USnippetCache,
UExceptions,
Web.USWAGRESTMgr;
type
TSWAGRESTCallWrapper = reference to procedure (CallProc: TProc);
type
TSWAGReader = class(TObject)
strict private
const
MaxSnippetCacheSize = 50;
var
fCategories: TList<TSWAGCategory>;
fSnippetsByCategory: TDictionary<string,TList<TSWAGSnippet>>;
fSWAGRESTMgr: TSWAGRESTMgr;
fDefaultCallWrapper: TSWAGRESTCallWrapper;
fSnippetCache: TSWAGSnippetCache;
procedure HandleException(E: Exception);
procedure DownloadCategories(const Cats: TList<TSWAGCategory>);
procedure DownloadPartialSnippets(const CatID: string;
const SnipList: TList<TSWAGSnippet>);
function DownloadCompleteSnippet(const SnipID: Cardinal): TSWAGSnippet;
procedure FetchCategories(CallWrapper: TSWAGRESTCallWrapper);
procedure FetchPartialSnippets(const CatID: string;
CallWrapper: TSWAGRESTCallWrapper);
function FetchCompleteSnippet(const SnippetID: Cardinal;
CallWrapper: TSWAGRESTCallWrapper): TSWAGSnippet;
procedure FetchCompleteSnippets(const SnipIDs: TList<Cardinal>;
Snippets: TList<TSWAGSnippet>; CallWrapper: TSWAGRESTCallWrapper);
public
constructor Create(const DefaultRESTCallWrapper: TSWAGRESTCallWrapper);
destructor Destroy; override;
procedure GetCategories(const Cats: TList<TSWAGCategory>;
CallWrapper: TSWAGRESTCallWrapper = nil);
procedure GetPartialSnippets(const CatID: string;
const Snippets: TList<TSWAGSnippet>;
CallWrapper: TSWAGRESTCallWrapper = nil);
function GetCompleteSnippet(const ID: Cardinal;
CallWrapper: TSWAGRESTCallWrapper = nil): TSWAGSnippet;
procedure GetCompleteSnippets(SnipIDs: TList<Cardinal>;
Snippets: TList<TSWAGSnippet>; CallWrapper: TSWAGRESTCallWrapper = nil);
end;
type
ESWAGReader = class(ECodeSnip);
implementation
uses
// Project
UComparers,
UConsts,
Web.UExceptions;
{ TSWAGReader }
constructor TSWAGReader.Create(
const DefaultRESTCallWrapper: TSWAGRESTCallWrapper);
begin
inherited Create;
fDefaultCallWrapper := DefaultRESTCallWrapper;
fCategories := TList<TSWAGCategory>.Create;
fSnippetsByCategory := TObjectDictionary<string,TList<TSWAGSnippet>>.Create(
[doOwnsValues],
TStringEqualityComparer.Create
);
fSnippetCache := TSWAGSnippetCache.Create(MaxSnippetCacheSize);
fSWAGRESTMgr := TSWAGRESTMgr.Create;
end;
destructor TSWAGReader.Destroy;
begin
fSWAGRESTMgr.Free;
fSnippetCache.Free;
fSnippetsByCategory.Free;
fCategories.Free;
inherited;
end;
procedure TSWAGReader.DownloadCategories(const Cats: TList<TSWAGCategory>);
begin
try
fSWAGRESTMgr.GetCategories(Cats);
except
HandleException(ExceptObject as Exception);
end;
end;
function TSWAGReader.DownloadCompleteSnippet(const SnipID: Cardinal):
TSWAGSnippet;
begin
try
fSWAGRESTMgr.GetSnippet(
SnipID,
[
'id', 'category', 'file_name', 'datestamp', 'title', 'author',
'source_code', 'is_document'
],
Result
);
except
HandleException(ExceptObject as Exception);
end;
end;
procedure TSWAGReader.DownloadPartialSnippets(const CatID: string;
const SnipList: TList<TSWAGSnippet>);
begin
try
fSWAGRESTMgr.GetSnippets(CatID, ['id', 'title'], SnipList);
except
HandleException(ExceptObject as Exception);
end;
end;
procedure TSWAGReader.FetchCategories(CallWrapper: TSWAGRESTCallWrapper);
begin
fCategories.Clear;
if not Assigned(CallWrapper) then
CallWrapper := fDefaultCallWrapper;
CallWrapper(
procedure
var
CatList: TList<TSWAGCategory>;
begin
CatList := TList<TSWAGCategory>.Create;
try
DownloadCategories(CatList);
fCategories.AddRange(CatList);
finally
CatList.Free;
end
end
);
end;
function TSWAGReader.FetchCompleteSnippet(const SnippetID: Cardinal;
CallWrapper: TSWAGRESTCallWrapper): TSWAGSnippet;
var
Snippet: TSWAGSnippet;
begin
if not Assigned(CallWrapper) then
CallWrapper := fDefaultCallWrapper;
CallWrapper(
procedure
begin
Snippet := DownloadCompleteSnippet(SnippetID);
end
);
Result := Snippet;
end;
procedure TSWAGReader.FetchCompleteSnippets(const SnipIDs: TList<Cardinal>;
Snippets: TList<TSWAGSnippet>; CallWrapper: TSWAGRESTCallWrapper);
begin
if not Assigned(CallWrapper) then
CallWrapper := fDefaultCallWrapper;
CallWrapper(
procedure
var
SnipID: Cardinal;
Snippet: TSWAGSnippet;
begin
for SnipID in SnipIDs do
begin
Snippet := DownloadCompleteSnippet(SnipID);
Snippets.Add(Snippet);
end;
end
);
end;
procedure TSWAGReader.FetchPartialSnippets(const CatID: string;
CallWrapper: TSWAGRESTCallWrapper);
begin
if not Assigned(CallWrapper) then
CallWrapper := fDefaultCallWrapper;
CallWrapper(
procedure
var
SnipList: TList<TSWAGSnippet>;
begin
SnipList := TList<TSWAGSnippet>.Create;
DownloadPartialSnippets(CatID, SnipList);
fSnippetsByCategory.Add(CatID, SnipList);
end
);
end;
procedure TSWAGReader.GetCategories(const Cats: TList<TSWAGCategory>;
CallWrapper: TSWAGRESTCallWrapper);
begin
if fCategories.Count = 0 then
FetchCategories(CallWrapper);
Cats.AddRange(fCategories);
end;
function TSWAGReader.GetCompleteSnippet(const ID: Cardinal;
CallWrapper: TSWAGRESTCallWrapper): TSWAGSnippet;
begin
if not fSnippetCache.Retrieve(ID, Result) then
begin
Result := FetchCompleteSnippet(ID, CallWrapper);
fSnippetCache.Add(Result);
end;
end;
procedure TSWAGReader.GetCompleteSnippets(SnipIDs: TList<Cardinal>;
Snippets: TList<TSWAGSnippet>; CallWrapper: TSWAGRESTCallWrapper);
var
RemoteSnippetIDs: TList<Cardinal>;
DownloadedSnippets: TList<TSWAGSnippet>;
Snippet: TSWAGSnippet;
SnipID: Cardinal;
begin
RemoteSnippetIDs := TList<Cardinal>.Create;
try
for SnipID in SnipIDs do
begin
if fSnippetCache.Retrieve(SnipID, Snippet) then
Snippets.Add(Snippet)
else
RemoteSnippetIDs.Add(SnipID);
end;
if RemoteSnippetIDs.Count > 0 then
begin
DownloadedSnippets := TList<TSWAGSnippet>.Create;
try
FetchCompleteSnippets(
RemoteSnippetIDs, DownloadedSnippets, CallWrapper
);
for Snippet in DownloadedSnippets do
begin
fSnippetCache.Add(Snippet);
Snippets.Add(Snippet);
end;
finally
DownloadedSnippets.Free;
end;
end;
finally
RemoteSnippetIDs.Free;
end;
end;
procedure TSWAGReader.GetPartialSnippets(const CatID: string;
const Snippets: TList<TSWAGSnippet>; CallWrapper: TSWAGRESTCallWrapper);
begin
if not fSnippetsByCategory.ContainsKey(CatID) then
FetchPartialSnippets(CatID, CallWrapper);
Snippets.AddRange(fSnippetsByCategory[CatID]);
end;
procedure TSWAGReader.HandleException(E: Exception);
resourcestring
sHTTPError = 'The SWAG web server returned the following error:'
+ EOL2
+ '%s';
sWebTransmissionError = 'The data received from the SWAG web service was '
+ 'corrupt';
sWebServiceFailure = 'The following error was detected in the response '
+ 'received from the SWAG web service:'
+ EOL2
+ '%s';
sWebServiceError = 'The SWAG web service reported the following error:'
+ EOL2
+ '%s';
begin
if E is EHTTPError then
raise ESWAGReader.CreateFmt(sHTTPError, [E.Message]);
if E is EWebConnectionError then
raise ESWAGReader.Create(E);
if E is EWebTransmissionError then
raise ESWAGReader.Create(sWebTransmissionError);
if E is EWebServiceFailure then
raise ESWAGReader.CreateFmt(sWebServiceFailure, [E.Message]);
if E is EWebServiceError then
raise ESWAGReader.CreateFmt(sWebServiceError, [E.Message]);
raise E;
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.