Menu

[r4761]: / branches / parsnip / Src / DB.UMain.pas  Maximize  Restore  History

Download this file

565 lines (502 with data), 16.5 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
{
* 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) 2005-2014, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Defines a singleton object and subsidiary classes that encapsulate the
* snippets and tags in the snippets database.
}
unit DB.UMain;
interface
uses
// Project
CS.Database.SnippetsTable,
CS.Database.Types,
CS.Utils.Dates,
UMultiCastEvents,
USingleton;
type
{
TDatabaseChangeEventKind:
Enumeration that specifies the different kind of change events triggered by
the database.
}
TDatabaseChangeEventKind = (
evChangeBegin, // a change to the database is about to take place
evChangeEnd, // a change to the database has completed
evSnippetAdded, // a snippet has been added
evBeforeSnippetDelete, // a snippet is about to be deleted
evSnippetDeleted, // a snippet has been deleted
evBeforeSnippetChange, // a snippet is about to be changed
evSnippetChanged // a snippet's properties / references have changed
// TODO: add suitable event kinds for changes to tags as required
);
{
IDatabaseChangeEventInfo:
Interface supported by objects passed to Database object's change event
handler that provides information about a change event. Some properites
are not defined for certain event types. Kind property always defined.
}
IDatabaseChangeEventInfo = interface(IInterface)
['{80DEE62F-DC23-4EE7-A0B1-5DE46F483CE1}']
function GetKind: TDatabaseChangeEventKind;
{Gets kind (type) of event.
@return Event kind.
}
function GetInfo: TObject;
{Gets additional information about event.
@return Object that provides required information.
}
property Kind: TDatabaseChangeEventKind read GetKind;
{Identifies kind (type) of an event. Always defined}
property Info: TObject read GetInfo;
{Provides additional information about the event. Actual type of object
depends on Kind. May be nil}
end;
TDBFilterFn = reference to function (ASnippet: ISnippet): Boolean;
TDatabase = class(TSingleton)
strict private
type
{
TEventInfo:
Class that provides information about a change event.
}
TEventInfo = class(TInterfacedObject, IDatabaseChangeEventInfo)
strict private
fKind: TDatabaseChangeEventKind; // Kind of event
fInfo: TObject; // Extra info about event
public
constructor Create(const Kind: TDatabaseChangeEventKind;
const Info: TObject = nil);
{Constructor. Creates an event information object.
@param Kind [in] Kind of event.
@param Info [in] Reference to further information about the event.
May be nil if event doesn't have additional information.
}
{ IDatabaseChangeEventInfo methods }
function GetKind: TDatabaseChangeEventKind;
{Gets kind (type) of event.
@return Event kind.
}
function GetInfo: TObject;
{Gets additional information about event.
@return Object that provides required information.
}
end;
strict private
class var
fInstance: TDatabase;
class function GetInstance: TDatabase; static;
strict private
var
fSnippetsTable: TDBSnippetsTable;
fAllTags: ITagSet;
fAllSnippetKinds: ISnippetKindList;
fLastModified: TUTCDateTime;
fDirty: Boolean;
fChangeEvents: TMulticastEvents; // List of change event handlers
procedure FlagUpdate;
procedure FixUpSnippetRefs;
procedure TriggerSnippetChangeEvent(const Kind: TDatabaseChangeEventKind;
const SnippetID: TSnippetID);
procedure TriggerNullDataEvent(const Kind: TDatabaseChangeEventKind);
procedure TriggerChangeEvent(const Kind: TDatabaseChangeEventKind;
const Info: TObject = nil);
{Triggers a change event. Notifies all registered listeners.
@param Kind [in] Kind of event.
@param Info [in] Reference to any further information for event. May be
nil.
}
strict protected
procedure Initialize; override;
procedure Finalize; override;
public
class property Instance: TDatabase read GetInstance;
public
procedure Load;
procedure Save;
function IsDirty: Boolean;
function LookupEditableSnippet(const ASnippetID: TSnippetID):
IEditableSnippet;
function TryLookupEditableSnippet(const ASnippetID: TSnippetID;
out ASnippet: IEditableSnippet): Boolean;
function LookupSnippet(const ASnippetID: TSnippetID): ISnippet;
function TryLookupSnippet(const ASnippetID: TSnippetID;
out ASnippet: ISnippet): Boolean;
function SelectSnippets(FilterFn: TDBFilterFn): ISnippetIDList;
// Selects 1st snippet for which FilterFn returns True. Returns True and
// passes snippet out in FoundSnippet if found. Returns False and
// FoundSnippet is undefined if no snippet found
function TrySelectSnippet(FilterFn: TDBFilterFn;
out FoundSnippet: ISnippet): Boolean;
function GetAllSnippets: ISnippetIDList;
function GetAllTags: ITagSet;
function GetAllSnippetKinds: ISnippetKindList;
function SnippetExists(const ASnippetID: TSnippetID): Boolean;
function SnippetCount: Integer;
function IsEmpty: Boolean;
// Returns a list of IDs of all snippets that depend on the snippet with
// the given ID.
function GetDependentsOf(const ASnippetID: TSnippetID): ISnippetIDList;
// Returns a list of IDs of all snippet that refer to (i.e. cross-reference)
// the snippet with the given ID.
function GetReferrersTo(const ASnippetID: TSnippetID): ISnippetIDList;
function NewSnippet: IEditableSnippet;
procedure AddSnippet(ASnippet: IEditableSnippet);
procedure UpdateSnippet(ASnippet: IEditableSnippet);
procedure DeleteSnippet(ASnippetID: TSnippetID);
procedure AddChangeEventHandler(const Handler: TNotifyEventInfo);
{Adds a change event handler to list of listeners.
@param Handler [in] Event handler to be added.
}
procedure RemoveChangeEventHandler(const Handler: TNotifyEventInfo);
{Removes a change event handler from list of listeners.
@param Handler [in] Handler to remove from list.
}
end;
function Database: TDatabase;
implementation
uses
// Project
CS.Database.IO.Factory,
CS.Database.IO.Types,
CS.Database.SnippetKinds,
CS.Database.Snippets,
CS.Database.Tags,
UBox,
UExceptions,
UQuery,
UStrUtils,
UUniqueID;
function Database: TDatabase;
begin
Result := TDatabase.Instance;
end;
{ TDatabase }
procedure TDatabase.AddChangeEventHandler(const Handler: TNotifyEventInfo);
begin
fChangeEvents.AddHandler(Handler);
end;
procedure TDatabase.AddSnippet(ASnippet: IEditableSnippet);
var
DBSnippet: TDBSnippet;
resourcestring
// Error message
sNameExists = 'Snippet "%s" already exists in database';
begin
if fSnippetsTable.Contains(ASnippet.ID) then
raise ECodeSnip.CreateFmt(sNameExists, [ASnippet.ID.ToString]);
if not fAllTags.ContainsSubSet(ASnippet.Tags) then
begin
{ TODO: Add TriggerNullDataEvent(evChangeBegin); }
fAllTags.Include(ASnippet.Tags);
{ TODO: Add change event for all tags added. Something like:
for Tag in ASnippet.Tags do
TriggerTagChangeEvent(evTagAdded, Tag); }
{ TODO: Add TriggerNullDataEvent(evChangeEnd); }
end;
TriggerNullDataEvent(evChangeBegin);
try
// TODO: Do we need to check snippet references and xrefs here?
DBSnippet := TDBSnippet.CreateFrom(ASnippet);
FlagUpdate; // sets fLastModifed
DBSnippet.SetCreated(fLastModified);
DBSnippet.SetModified(fLastModified);
fSnippetsTable.Add(DBSnippet);
Query.Update;
TriggerSnippetChangeEvent(evSnippetAdded, ASnippet.ID);
finally
TriggerNullDataEvent(evChangeEnd);
end;
end;
procedure TDatabase.DeleteSnippet(ASnippetID: TSnippetID);
var
Dependent: TSnippetID; // each snippet that depends on Snippet
Dependents: ISnippetIDList; // list of dependent snippets
Referrer: TSnippetID; // each snippet that cross references Snippet
Referrers: ISnippetIDList; // list of referencing snippets
begin
Assert(Database.SnippetExists(ASnippetID),
ClassName + '.DeleteSnippet: Snippet is not in the database');
TriggerNullDataEvent(evChangeBegin);
TriggerSnippetChangeEvent(evBeforeSnippetDelete, ASnippetID);
// Get list of referencing and dependent snippets
Dependents := nil;
Referrers := nil;
try
Dependents := Database.GetDependentsOf(ASnippetID);
Referrers := Database.GetReferrersTo(ASnippetID);
// TODO: scan all snippets and remove references that match snippet ID
// Delete snippet for XRef or Depends list of referencing snippets
for Referrer in Referrers do
fSnippetsTable.Get(Referrer).GetXRefs.Remove(ASnippetID);
for Dependent in Dependents do
fSnippetsTable.Get(Dependent).GetRequiredSnippets.Remove(
ASnippetID
);
// Delete snippet itself
fSnippetsTable.Delete(ASnippetID);
Query.Update;
finally
FlagUpdate;
TriggerSnippetChangeEvent(evSnippetDeleted, ASnippetID);
TriggerNullDataEvent(evChangeEnd);
end;
end;
procedure TDatabase.Finalize;
begin
fChangeEvents.Free;
fSnippetsTable.Free;
inherited;
end;
procedure TDatabase.FixUpSnippetRefs;
function StripBadRefs(SnippetList: ISnippetIDList): ISnippetIDList;
var
SnippetID: TSnippetID;
begin
Result := TSnippetIDList.Create(SnippetList.Count);
for SnippetID in SnippetList do
if fSnippetsTable.Contains(SnippetID) then
Result.Add(SnippetID);
end;
var
Snippet: TDBSnippet;
begin
for Snippet in fSnippetsTable do
begin
Snippet.SetRequiredSnippets(StripBadRefs(Snippet.GetRequiredSnippets));
Snippet.SetXRefs(StripBadRefs(Snippet.GetXRefs));
end;
end;
procedure TDatabase.FlagUpdate;
begin
fDirty := True;
fLastModified := TUTCDateTime.Now;
end;
function TDatabase.GetAllSnippetKinds: ISnippetKindList;
begin
Result := fAllSnippetKinds;
end;
function TDatabase.GetAllSnippets: ISnippetIDList;
var
Snippet: TDBSnippet;
begin
Result := TSnippetIDList.Create(fSnippetsTable.Size);
for Snippet in fSnippetsTable do
Result.Add(Snippet.GetID);
end;
function TDatabase.GetAllTags: ITagSet;
begin
Result := TTagSet.Create(fAllTags);
end;
function TDatabase.GetDependentsOf(const ASnippetID: TSnippetID):
ISnippetIDList;
var
Snippet: TDBSnippet;
begin
Result := TSnippetIDList.Create;
for Snippet in fSnippetsTable do
if (Snippet.GetID <> ASnippetID)
and Snippet.GetRequiredSnippets.Contains(ASnippetID) then
Result.Add(Snippet.GetID);
end;
class function TDatabase.GetInstance: TDatabase;
begin
if not Assigned(fInstance) then
fInstance := TDatabase.Create;
Result := fInstance;
end;
function TDatabase.GetReferrersTo(const ASnippetID: TSnippetID): ISnippetIDList;
var
Snippet: TDBSnippet; // references each snippet in database
begin
Result := TSnippetIDList.Create;
for Snippet in fSnippetsTable do
if (Snippet.GetID <> ASnippetID)
and Snippet.GetXRefs.Contains(ASnippetID) then
Result.Add(Snippet.GetID);
end;
procedure TDatabase.Initialize;
begin
inherited;
fSnippetsTable := TDBSnippetsTable.Create;
fAllTags := TTagSet.Create;
fAllSnippetKinds := TSnippetKindList.Create;
fChangeEvents := TMultiCastEvents.Create(Self);
end;
function TDatabase.IsDirty: Boolean;
begin
Result := fDirty;
end;
function TDatabase.IsEmpty: Boolean;
begin
Result := fSnippetsTable.Size = 0;
end;
procedure TDatabase.Load;
var
Loader: IDatabaseLoader;
begin
fSnippetsTable.Clear; // TODO: decide if loaders should clear this table
Loader := TDatabaseIOFactory.CreateLoader;
Loader.Load(fSnippetsTable, fAllTags, fLastModified);
FixUpSnippetRefs;
fDirty := False;
end;
function TDatabase.LookupEditableSnippet(const ASnippetID: TSnippetID):
IEditableSnippet;
var
Row: TDBSnippet;
begin
Row := fSnippetsTable.Get(ASnippetID);
Result := Row.CloneAsEditable;
end;
function TDatabase.LookupSnippet(const ASnippetID: TSnippetID): ISnippet;
var
Row: TDBSnippet;
begin
Row := fSnippetsTable.Get(ASnippetID);
Result := Row.CloneAsReadOnly;
end;
function TDatabase.NewSnippet: IEditableSnippet;
begin
// Snippet IDs created in TEditableSnippet.CreateNew *should* be unique, but
// we make sure the ID is unique, just in case!
repeat
Result := TEditableSnippet.CreateNew;
until not fSnippetsTable.Contains(Result.ID);
end;
procedure TDatabase.RemoveChangeEventHandler(const Handler: TNotifyEventInfo);
begin
fChangeEvents.RemoveHandler(Handler);
end;
procedure TDatabase.Save;
var
Writer: IDatabaseWriter;
begin
Writer := TDatabaseIOFactory.CreateWriter;
Writer.Save(fSnippetsTable, fAllTags, fLastModified);
fDirty := False;
end;
function TDatabase.SelectSnippets(FilterFn: TDBFilterFn): ISnippetIDList;
var
Snippet: TDBSnippet;
begin
Result := TSnippetIDList.Create;
for Snippet in fSnippetsTable do
if FilterFn(Snippet.CloneAsReadOnly) then
Result.Add(Snippet.GetID)
end;
function TDatabase.SnippetCount: Integer;
begin
Result := fSnippetsTable.Size;
end;
function TDatabase.SnippetExists(const ASnippetID: TSnippetID): Boolean;
begin
Result := fSnippetsTable.Contains(ASnippetID);
end;
procedure TDatabase.TriggerChangeEvent(const Kind: TDatabaseChangeEventKind;
const Info: TObject);
var
EvtInfo: IDatabaseChangeEventInfo; // event information object
begin
EvtInfo := TEventInfo.Create(Kind, Info);
fChangeEvents.TriggerEvents(EvtInfo);
end;
procedure TDatabase.TriggerNullDataEvent(const Kind: TDatabaseChangeEventKind);
begin
TriggerChangeEvent(Kind, nil);
end;
procedure TDatabase.TriggerSnippetChangeEvent(
const Kind: TDatabaseChangeEventKind; const SnippetID: TSnippetID);
var
Obj: TBox<TSnippetID>;
begin
Obj := TBox<TSnippetID>.Create(SnippetID);
try
TriggerChangeEvent(Kind, Obj);
finally
Obj.Free;
end;
end;
function TDatabase.TryLookupEditableSnippet(const ASnippetID: TSnippetID;
out ASnippet: IEditableSnippet): Boolean;
begin
Result := fSnippetsTable.Contains(ASnippetID);
if Result then
ASnippet := fSnippetsTable.Get(ASnippetID).CloneAsEditable
else
ASnippet := nil;
end;
function TDatabase.TryLookupSnippet(const ASnippetID: TSnippetID;
out ASnippet: ISnippet): Boolean;
begin
Result := fSnippetsTable.Contains(ASnippetID);
if Result then
ASnippet := fSnippetsTable.Get(ASnippetID).CloneAsReadOnly
else
ASnippet := nil;
end;
function TDatabase.TrySelectSnippet(FilterFn: TDBFilterFn;
out FoundSnippet: ISnippet): Boolean;
var
Snippet: TDBSnippet;
begin
for Snippet in fSnippetsTable do
if FilterFn(Snippet.CloneAsReadOnly) then
begin
FoundSnippet := Snippet.CloneAsReadOnly;
Exit(True);
end;
Result := False;
end;
procedure TDatabase.UpdateSnippet(ASnippet: IEditableSnippet);
var
DBSnippet: TDBSnippet;
begin
// TODO: refactor out this common in common with AddSnippet
if not fAllTags.ContainsSubSet(ASnippet.Tags) then
begin
{ TODO: Add TriggerNullDataEvent(evChangeBegin); }
fAllTags.Include(ASnippet.Tags);
{ TODO: Add change event for all tags added. Something like:
for Tag in ASnippet.Tags do
TriggerTagChangeEvent(evTagAdded, Tag); }
{ TODO: Add TriggerNullDataEvent(evChangeEnd); }
end;
TriggerNullDataEvent(evChangeBegin);
TriggerSnippetChangeEvent(evBeforeSnippetChange, ASnippet.ID);
DBSnippet := TDBSnippet.CreateFrom(ASnippet);
try
// TODO: Do we need to check snippet references and xrefs here?
FlagUpdate;
DBSnippet.SetModified(fLastModified);
fSnippetsTable.Update(DBSnippet);
Query.Update;
finally
TriggerSnippetChangeEvent(evSnippetChanged, ASnippet.ID);
TriggerNullDataEvent(evChangeEnd);
DBSnippet.Free;
end;
end;
{ TDatabase.TEventInfo }
constructor TDatabase.TEventInfo.Create(const Kind: TDatabaseChangeEventKind;
const Info: TObject);
begin
inherited Create;
fKind := Kind;
fInfo := Info;
end;
function TDatabase.TEventInfo.GetInfo: TObject;
begin
Result := fInfo;
end;
function TDatabase.TEventInfo.GetKind: TDatabaseChangeEventKind;
begin
Result := fKind;
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.