forked from fantaisie-software/purebasic
-
Notifications
You must be signed in to change notification settings - Fork 1
/
EditHistory.pb
1679 lines (1327 loc) · 53.1 KB
/
EditHistory.pb
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;--------------------------------------------------------------------------------------------
; Copyright (c) Fantaisie Software. All rights reserved.
; Dual licensed under the GPL and Fantaisie Software licenses.
; See LICENSE and LICENSE-FANTAISIE in the project root for license information.
;--------------------------------------------------------------------------------------------
UseSQLiteDatabase()
; whether or not to compress new history entries
; can be disabled for better debugging. should be enabled in releases
#ENABLE_HISTORY_COMPRESSION = #True
#HISTORY_COMPRESSION_LEVEL = 1 ; best speed
; enable to process events asynchronously (better for performance)
; disable this only for testing
#HISTORY_WRITE_ASYNC = #True
; enable to purge sessions which don't contain any edit events
#HISTORY_PURGE_EMPTY_SESSIONS = #True
; keep previous versions of displayed sources cached
; to speed up loading (as reconstructing multiple diffs takes time)
; especially when browsing within the history of one file, this helps a lot
#HISTORY_EVENT_SOURCE_CACHE = 100
; maximum number of diffs to create before writing again the full source in the event log.
; this must be smaller than the cache size, otherwise loading a single source with too many
; diff events will require more steps than fit in the cache and lead to bad performance
#HISTORY_EVENT_MAX_DIFFS = 75
;- Import extra sqlite stuff
;
ImportC ""
sqlite3_last_insert_rowid.q(*sqlite3)
sqlite3_bind_blob.l(*sqlite3_stmt, index.l, *buffer, size.l, *destructor)
sqlite3_bind_int.l(*sqlite3_stmt, index.l, value.l)
sqlite3_bind_null.l(*sqlite3_stmt, index.l)
CompilerIf #PB_Compiler_Unicode
sqlite3_bind_text16.l(*sqlite3_stmt, index.l, text, size.l, *destructor)
sqlite3_prepare16_v2.l(*sqlite3, zSql, nByte.l, *ppStmt, *pzTail)
CompilerElse
sqlite3_bind_text.l(*sqlite3_stmt, index.l, text, size.l, *destructor)
sqlite3_prepare_v2.l(*sqlite3, zSql, nByte.l, *ppStmt, *pzTail)
CompilerEndIf
sqlite3_step.l(*sqlite3_stmt)
sqlite3_finalize.l(*sqlite3_stmt)
sqlite3_threadsafe.l()
EndImport
#SQLITE_STATIC = 0
#SQLITE_TRANSIENT = -1
#SQLITE_OK = 0
#SQLITE_ROW = 100
#SQLITE_DONE = 101
;- Libmba stuff
;
CompilerIf #CompileWindows
#Diff_Library = "libmba/libmba.lib"
CompilerElse
#Diff_Library = "libmba/libmba.a"
CompilerEndIf
ImportC #BUILD_DIRECTORY + #Diff_Library
diff.l(*a, aoff.l, n.l, *b, boff.l, m.l, *idx_fn, *cmp_fn, *context, dmax.l, *ses, *sn.LONG, *buf)
varray_new(membsize, *al)
varray_del.l(*va)
varray_get(*va, idx)
EndImport
Enumeration
#DIFF_MATCH = 1
#DIFF_DELETE
#DIFF_INSERT
EndEnumeration
Structure diff_edit Align #PB_Structure_AlignC
op.u
__padding.b[2] ; on all os
off.l ; /* off into s1 if MATCH or DELETE but s2 if INSERT */
len.l
EndStructure
;- Data types in history
;
Enumeration 1
#DATA_Empty ; data is null. the file is empty
#DATA_Same ; data is null. the file is the same as on the previous event
#DATA_Diff ; data is a diff since the previous event
#DATA_Full ; data is the full file content (this is the case for new files in a session that are not empty)
#DATA_DiffZ ; data is a diff (zlib compressed)
#DATA_FullZ ; data is full file (zlib compressed)
EndEnumeration
; asynchronous handling of events for speed
Structure HistoryEvent
*QueueNext.HistoryEvent ; next event in queue
*ProcessedNext.HistoryEvent ; next in list of processed events
SourceID.i ; id of the originating source
HistoryName.s ; filename or unique name for new files
Encoding.l
Event.l
Time.l
*Content ; 0 if size=0
Size.l
Checksum.l
FreshFile.l ; has the file been saved?
DiffCount.l ; number of diffs written since the last full source (updated when the event is written)
EventID.l ; this is known after the event is written and used by the followup event
EndStructure
Structure EventSource
EventID.l
Encoding.l
*Buffer ; never #null (0-size event sources are not cached)
Size.l
EndStructure
Global NewList EventSourceCache.EventSource()
Global OSSessionID$, SessionID
Global CurrentHistoryFile$, CurrentHistorySource
Global CompilerVersionWritten = #False
Global StartOfDay
Global CurrentUser$ = UserName()
Global NewMap HistoryFirstLines.i() ; map of first displayed lines for each file
Global NewList *HistoryEvents.HistoryEvent()
Global *HistoryMutex = CreateMutex()
Global *HistorySemaphore = CreateSemaphore()
; Note: cannot use a LinkedList as it is not threadsafe
Global *HistoryQueueHead.HistoryEvent = 0
Global *HistoryQueueTail.HistoryEvent = 0
; List of already processed events (one per file) managed by the thread only
Global *HistoryFirstProcessed.HistoryEvent = 0
Declare History_EventThread(*Dummy)
Procedure.s SqlEscape(String$)
ProcedureReturn ReplaceString(String$, "'", "''")
EndProcedure
CompilerIf #PB_Compiler_Debugger
Procedure DatabaseUpdate_DEBUG(db, sql$)
Debug "[DB UPDATE] " + sql$
r = DatabaseUpdate(db, sql$)
If r = 0
Debug "[DB FAILURE] " + DatabaseError()
CallDebugger
EndIf
ProcedureReturn r
EndProcedure
Macro DatabaseUpdate(db, sql)
DatabaseUpdate_DEBUG(db, sql)
EndMacro
Procedure DatabaseQuery_DEBUG(db, sql$)
Debug "[DB QUERY] " + sql$
r = DatabaseQuery(db, sql$)
If r = 0
Debug "[DB FAILURE] " + DatabaseError()
CallDebugger
EndIf
ProcedureReturn r
EndProcedure
Macro DatabaseQuery(db, sql)
DatabaseQuery_DEBUG(db, sql)
EndMacro
CompilerEndIf
Procedure.s History_VersionString(CompilerVersion$)
Title$ = CompilerVersion$
Title$ = RemoveString(Title$, "Windows - ") ; remove the OS part as it is redundant information
Title$ = RemoveString(Title$, "Linux - ")
Title$ = RemoveString(Title$, "MacOS X - ")
Title$ = RemoveString(Title$, #ProductName$ + " ") ; also remove the prefix
ProcedureReturn Title$
EndProcedure
Procedure.s History_GetOption(Key$)
Value$ = ""
If DatabaseQuery(#DB_History, "SELECT value FROM options WHERE key = '" + SqlEscape(Key$) + "'")
If NextDatabaseRow(#DB_History)
Value$ = GetDatabaseString(#DB_History, 0)
EndIf
FinishDatabaseQuery(#DB_History)
EndIf
ProcedureReturn Value$
EndProcedure
Procedure History_SetOption(Key$, Value$)
; the ON CONFLICT clause of the table causes any old value for the same key to be deleted
; automatically
DatabaseUpdate(#DB_History, "INSERT INTO options (key, value) VALUES ('" + SqlEscape(Key$) + "', '" + SqlEscape(Value$) + "')")
EndProcedure
Procedure StartHistorySession()
; no setup if history is disabled
If EnableHistory = #False
ProcedureReturn
EndIf
; check threadsafety of sqlite (should be always on)
If sqlite3_threadsafe() = 0
MessageRequester(#ProductName$, "Critical: sqlite is not compiled threadsafe")
End
EndIf
; make sure the db file exists
If OpenFile(#FILE_Database, HistoryDatabaseFile$)
CloseFile(#FILE_Database)
EndIf
; open database
HistoryActive = OpenDatabase(#DB_History, HistoryDatabaseFile$, "", "", #PB_Database_SQLite)
If HistoryActive
; Options table: stores unique key value pairs
; This table is used for DB version checks, so do not modify it in the future!
DatabaseUpdate(#DB_History, "CREATE TABLE IF NOT EXISTS options(key TEXT UNIQUE ON CONFLICT REPLACE, value NOT NULL)")
; The minor version is increased when compatible changes are done.
; I.e. a "1.1" IDE can still access a "1.5" database
; The major version is increased when incompatible changes are made to
; prevent corruption of a newer db or confusing old IDE versions
;
; Right now the version is "1.1" and this IDE will access any DB of version "1.X"
;
Major$ = History_GetOption("version.major")
Minor$ = History_GetOption("version.minor")
; Check for compatible major version
If Major$ <> "" And Major$ <> "1"
MessageRequester(#ProductName$, LanguagePattern("History", "VersionError", "%filename%", HistoryDatabaseFile$))
CloseDatabase(#DB_History)
HistoryActive = 0
ProcedureReturn
EndIf
; write version options if not present
If Major$ = ""
History_SetOption("version.major", "1")
EndIf
If Minor$ = ""
History_SetOption("version.minor", "1")
EndIf
; Session table:
; session_id : primary key of table
; os_id : ID created by Session_Start() to detect dead instances. set to null for properly ended sessions
; version : Compiler version (default compiler of the IDE)
; user : OS user name
; start_time : session start time
; end_time : session end time (0 for still running)
; warned : 1 if a warning was displayed for a detected dead session
;
DatabaseUpdate(#DB_History, "CREATE TABLE IF NOT EXISTS session(session_id INTEGER PRIMARY KEY, os_id STRING, version TEXT NOT NULL, user TEXT NOT NULL, start_time INTEGER NOT NULL, end_time INTEGER NOT NULL, warned INTEGER NOT NULL)")
; Event table:
; event_id : primary key
; session_id : session id
; filename : file name (for new files: "::unsaved::" + unique id + creation time stamp)
; event : one of the #HISTORY_XXX values
; time : event time stamp
; type : one of the #DATA_XXX values
; previous_event : id of previous event for the file (if any)
; encoding : encoding of source file
; data : file data or null, depending on type
;
DatabaseUpdate(#DB_History, "CREATE TABLE IF NOT EXISTS event(event_id INTEGER PRIMARY KEY, session_id INTEGER NOT NULL, filename TEXT NOT NULL, event INTEGER NOT NULL, time INTEGER NOT NULL, type INTEGER NOT NULL, previous_event INTEGER, encoding INTEGER NOT NULL, data BLOB)")
; create indices for fast searching
DatabaseUpdate(#DB_History, "CREATE INDEX IF NOT EXISTS idx_session1 ON session (start_time)")
DatabaseUpdate(#DB_History, "CREATE INDEX IF NOT EXISTS idx_session2 ON session (end_time)")
DatabaseUpdate(#DB_History, "CREATE INDEX IF NOT EXISTS idx_event1 ON event (session_id, filename)")
; get version string
; note: this is probably empty still if the compiler is too slow to load so it will be updated later
Title$ = History_VersionString(DefaultCompiler\VersionString$)
; Setup detection of crashed sessions
; The OSSessionID is only unique for currently running sessions
; It is used to detect which unended session is still running and which is crashed
OSSessionID$ = Session_Start()
DatabaseUpdate(#DB_History, "INSERT INTO session(os_id, version, user, start_time, end_time, warned) values ('" + OSSessionID$ + "', '" + SqlEscape(Title$) + "', '" + SqlEscape(UserName()) + "', " + Str(Date()) + ", 0, 0)")
SessionID = sqlite3_last_insert_rowid(DatabaseID(#DB_History))
AddWindowTimer(#WINDOW_Main, #TIMER_History, HistoryTimer * 60000) ; value is in minutes
; create thread for asynchronous event writing
; must disable the debugger here, because the thread proc is in DisableDebugger too,
; so the debugger will complain that it does not know the procedure
CompilerIf #HISTORY_WRITE_ASYNC
DisableDebugger
If CreateThread(@History_EventThread(), 0) = 0
MessageRequester(#ProductName$, "Critical error: Cannot create thread for session history recording")
CloseDatabase(#DB_History)
HistoryActive = 0
EndIf
EnableDebugger
CompilerEndIf
Else
MessageRequester(#ProductName$, LanguagePattern("History", "FileError", "%filename%", HistoryDatabaseFile$))
EndIf
EndProcedure
Procedure HistoryCompilerLoaded()
If HistoryActive And CompilerVersionWritten = #False
Title$ = History_VersionString(DefaultCompiler\VersionString$)
DatabaseUpdate(#DB_History, "UPDATE session SET version = '" + SqlEscape(Title$) + "' WHERE session_id = " + Str(SessionID))
CompilerVersionWritten = #True
EndIf
EndProcedure
Procedure DetectCrashedHistorySession()
If HistoryActive
NewList CrashedSID.i()
; get all sessions that are marked as not closed and not warned
; display most recent if multiple crashes (could happen if multiple instances are running and the pc crashes)
sql$ = "SELECT session_id, os_id FROM session "
sql$ + "WHERE end_time = 0 AND warned = 0 "
sql$ + "ORDER BY start_time DESC"
If DatabaseQuery(#DB_History, sql$)
; check any hits against the running sessions
While NextDatabaseRow(#DB_History)
sid = GetDatabaseLong(#DB_History, 0)
OSID$ = GetDatabaseString(#DB_History, 1)
If sid <> SessionID And Session_IsRunning(OSID$) = #False
AddElement(CrashedSID())
CrashedSID() = sid
EndIf
Wend
FinishDatabaseQuery(#DB_History)
EndIf
If ListSize(CrashedSID()) > 0
; set the warned flag for all these sessions
DatabaseUpdate(#DB_History, "BEGIN TRANSACTION")
ForEach CrashedSID()
DatabaseUpdate(#DB_History, "UPDATE session SET warned = 1 WHERE session_id = " + Str(CrashedSID()))
Next CrashedSID()
DatabaseUpdate(#DB_History, "COMMIT TRANSACTION")
; ask if the session history should be shown
If MessageRequester(#ProductName$, Language("History", "CrashedInfo"), #FLAG_Question|#PB_MessageRequester_YesNo) = #PB_MessageRequester_Yes
FirstElement(CrashedSID())
OpenEditHistoryWindow(CrashedSID())
EndIf
EndIf
EndIf
EndProcedure
Procedure History_FlushEvents()
CompilerIf #HISTORY_WRITE_ASYNC
; setup a window to display if the wait is too long (hidden)
Window.DialogWindow = OpenDialog(?Dialog_HistoryShutdown)
SetGadgetState(#GADGET_HistoryShutdown_Progress, #PB_ProgressBar_Unknown)
WaitStart.q = ElapsedMilliseconds()
Hidden = #True
; wait until the queue is clear
; no need for any locking as it is just a simple check
While *HistoryQueueHead
WaitWindowEvent(50)
If Hidden And (ElapsedMilliseconds() - WaitStart > 250)
HideWindow(#WINDOW_EditHistoryShutdown, #False)
Hidden = #False
CompilerIf #CompileLinuxGtk2
gtk_window_set_position_(WindowID(#WINDOW_EditHistoryShutdown), #GTK_WIN_POS_CENTER_ALWAYS)
CompilerEndIf
CompilerIf #CompileMacCocoa
PB_Gadget_CenterWindow(WindowID(#WINDOW_EditHistoryShutdown))
CompilerEndIf
EndIf
Wend
Window\Close(0)
CompilerEndIf
EndProcedure
Procedure HistoryShutdownEvents()
If HistoryActive
; write a close event for any still open files
; the ide only saves, but does not close each file on shutdown!
ForEach FileList()
HistoryEvent(@FileList(), #HISTORY_Close)
Next FileList()
EndIf
EndProcedure
Procedure EndHistorySession()
If HistoryActive
; wait until all these events are written
; the WriteShutdownEvents() procedure can write quite a lot of events, so this could take some time
History_FlushEvents()
; write that the session correctly ended
DatabaseUpdate(#DB_History, "UPDATE session SET os_id = null, end_time = " + Str(Date()) + " WHERE session_id = " + Str(SessionID))
; end detection of bad sessions
Session_End(OSSessionID$)
; Now purge old sessions if needed
;
PurgeList$ = ""
If HistoryPurgeMode = 1
; purge by session count
; exclude running and crashed (not warned) sessions
sql$ = "SELECT session_id FROM session "
sql$ + "WHERE end_time <> 0 OR warned = 1 "
sql$ + "ORDER BY start_time DESC"
If DatabaseQuery(#DB_History, sql$)
; skip the number of sessions to keep
Count = 0
While Count < MaxSessionCount And NextDatabaseRow(#DB_History)
Count + 1
Wend
; record sessions for deleting
While NextDatabaseRow(#DB_History)
PurgeList$ + ", " + Str(GetDatabaseLong(#DB_History, 0))
Wend
FinishDatabaseQuery(#DB_History)
EndIf
ElseIf HistoryPurgeMode = 2
; purge by start date
CurrentDate = Date()
StartOfDay = Date(Year(CurrentDate), Month(CurrentDate), Day(CurrentDate), 0, 0, 0)
CutOff = AddDate(StartOfDay, #PB_Date_Day, -MaxSessionDays)
; cut off by end_time
; for crashed sessions, use start time, but exclude not yet warned ones
sql$ = "SELECT session_id FROM session "
sql$ + "WHERE (end_time < " + Str(CutOff) + " AND end_time <> 0) "
sql$ + "OR (end_time = 0 AND start_time < " + Str(CutOff) + " AND warned = 1)"
If DatabaseQuery(#DB_History, sql$)
; record sessions for deleting
While NextDatabaseRow(#DB_History)
PurgeList$ + ", " + Str(GetDatabaseLong(#DB_History, 0))
Wend
FinishDatabaseQuery(#DB_History)
EndIf
EndIf
CompilerIf #HISTORY_PURGE_EMPTY_SESSIONS
NewList FoundSessionID$()
If DatabaseQuery(#DB_History, "SELECT session_id FROM session")
While NextDatabaseRow(#DB_History)
AddElement(FoundSessionID$())
FoundSessionID$() = GetDatabaseString(#DB_History, 0)
Wend
FinishDatabaseQuery(#DB_History)
ForEach FoundSessionID$()
If DatabaseQuery(#DB_History, "SELECT COUNT(*) FROM event WHERE session_id='" + FoundSessionID$() + "'")
If NextDatabaseRow(#DB_History)
If GetDatabaseLong(#DB_History, 0) = 0
PurgeList$ + ", " + FoundSessionID$()
EndIf
EndIf
FinishDatabaseQuery(#DB_History)
EndIf
Next
EndIf
FreeList(FoundSessionID$())
CompilerEndIf
If PurgeList$ <> ""
; strip beginning ", "
PurgeList$ = Mid(PurgeList$, 3)
; purge them all at once
DatabaseUpdate(#DB_History, "BEGIN TRANSACTION")
DatabaseUpdate(#DB_History, "DELETE FROM event WHERE session_id IN (" + PurgeList$ + ")")
DatabaseUpdate(#DB_History, "DELETE FROM session WHERE session_id IN (" + PurgeList$ + ")")
DatabaseUpdate(#DB_History, "COMMIT TRANSACTION")
EndIf
; close db
CloseDatabase(#DB_History)
EndIf
EndProcedure
; Make a unique ID string for file identification
;
Procedure.s History_MakeUniqueId()
Length = 16
Dim Buffer.a(Length-1)
If OpenCryptRandom()
CryptRandomData(@Buffer(0), Length)
CloseCryptRandom()
Else
RandomData(@Buffer(0), Length)
EndIf
Result$ = ""
For i = 0 To Length-1
Result$ + RSet(Hex(Buffer(i)), 2, "0")
Next i
ProcedureReturn Result$
EndProcedure
CompilerIf #HISTORY_WRITE_ASYNC
DisableDebugger
CompilerEndIf
Procedure History_FreeEvent(*Event.HistoryEvent)
If *Event\Content
FreeMemory(*Event\Content)
EndIf
ClearStructure(*Event, HistoryEvent)
FreeMemory(*Event)
EndProcedure
Procedure History_AsyncUpdateName(PreviousName$, Name$)
CompilerIf #HISTORY_WRITE_ASYNC = #False
Debug "Async: updating event names: " + PreviousName$ + " to " + Name$
CompilerEndIf
; Use bind variables as we cannot modify strings in this thread
Protected Success = #False
CompilerIf #PB_Compiler_Unicode
If sqlite3_prepare16_v2(DatabaseID(#DB_History), @"UPDATE event SET filename = ? WHERE filename = ? AND session_id = ?", -1, @*Statement, #Null) = #SQLITE_OK
Success = #True
sqlite3_bind_text16(*Statement, 1, @Name$, -1, #SQLITE_STATIC)
sqlite3_bind_text16(*Statement, 2, @PreviousName$, -1, #SQLITE_STATIC)
EndIf
CompilerElse
If sqlite3_prepare_v2(DatabaseID(#DB_History), @"UPDATE event SET filename = ? WHERE filename = ? AND session_id = ?", -1, @*Statement, #Null) = #SQLITE_OK
Success = #True
sqlite3_bind_text(*Statement, 1, @Name$, -1, #SQLITE_STATIC)
sqlite3_bind_text(*Statement, 2, @PreviousName$, -1, #SQLITE_STATIC)
EndIf
CompilerEndIf
If Success
sqlite3_bind_int(*Statement, 3, SessionID)
While sqlite3_step(*Statement) = #SQLITE_ROW: Wend
sqlite3_finalize(*Statement)
EndIf
EndProcedure
Procedure History_AsyncDeleteEvent(EventID)
CompilerIf #HISTORY_WRITE_ASYNC = #False
Debug "Async: deleting event with id: " + Str(EventID)
CompilerEndIf
Protected Success = #False
CompilerIf #PB_Compiler_Unicode
If sqlite3_prepare16_v2(DatabaseID(#DB_History), @"DELETE FROM event WHERE event_id = ?", -1, @*Statement, #Null) = #SQLITE_OK
Success = #True
EndIf
CompilerElse
If sqlite3_prepare_v2(DatabaseID(#DB_History), @"DELETE FROM event WHERE event_id = ?", -1, @*Statement, #Null) = #SQLITE_OK
Success = #True
EndIf
CompilerEndIf
If Success
sqlite3_bind_int(*Statement, 1, EventID)
While sqlite3_step(*Statement) = #SQLITE_ROW: Wend
sqlite3_finalize(*Statement)
EndIf
EndProcedure
Structure HistoryDiffLine
Checksum.l
Offset.l
Length.l
EndStructure
Structure HistoryDiffLines
Line.HistoryDiffLine[0]
EndStructure
ProcedureC History_Diff_idx(*Lines.HistoryDiffLines, idx.l, *context)
ProcedureReturn @*Lines\Line[idx]
EndProcedure
ProcedureC History_Diff_cmp(*e1.HistoryDiffLine, *e2.HistoryDiffLine, *context)
If *e1\Checksum = *e2\Checksum
ProcedureReturn 0
Else
ProcedureReturn 1
EndIf
EndProcedure
Procedure History_DiffEditSize(*edit.diff_edit, Array Lines.HistoryDiffLine(1))
Size = 0
Last = *edit\off + *edit\len - 1
For index = *edit\off To Last
Size + Lines(index)\Length
Next index
ProcedureReturn Size
EndProcedure
Procedure History_DiffPreProcess(*Event.HistoryEvent, Array Lines.HistoryDiffLine(1))
*Pointer.PTR = *Event\Content
*BufferEnd = *Pointer + *Event\Size
Lines = 0
Space = ArraySize(Lines()) ; don't do +1 to have the extra space for the last line
*LineStart = *Pointer
While *Pointer < *BufferEnd
; detect next newline
If *Pointer\b = 13
*Pointer + 1
If *Pointer < *BufferEnd And *Pointer\b = 10
*Pointer + 1
EndIf
ElseIf *Pointer\b = 10
*Pointer + 1
Else
; no newline
*Pointer + 1
Continue
EndIf
; newline found
If Space = 0
ReDim Lines(Lines * 2)
Space = Lines
EndIf
Lines(Lines)\Checksum = CRC32Fingerprint(*LineStart, *Pointer-*LineStart)
Lines(Lines)\Offset = *LineStart - *Event\Content
Lines(Lines)\Length = *Pointer - *LineStart
Lines + 1
Space - 1
*LineStart = *Pointer
Wend
; add the last line
If *Pointer > *LineStart
Lines(Lines)\Checksum = CRC32Fingerprint(*LineStart, *Pointer-*LineStart)
Lines(Lines)\Offset = *Pointer - *Event\Content
Lines(Lines)\Length = *Pointer - *LineStart
Lines + 1
EndIf
ProcedureReturn Lines
EndProcedure
; Generates an edit script:
; Beginning: <long> full size of target
;
; C<long> : copy from source
; D<long> : skip from source
; A<long><content> : add content from diff
;
; returns size of diff'ed content
; returns 0 if diff is larger than real file
Procedure History_MakeDiff(*Output, *OutSize.INTEGER, *Event.HistoryEvent, *Previous.HistoryEvent)
Protected Dim NewLines.HistoryDiffLine(1000)
Protected Dim OldLines.HistoryDiffLine(1000)
LinesNew = History_DiffPreProcess(*Event, NewLines())
LinesOld = History_DiffPreProcess(*Previous, OldLines())
*ses = varray_new(SizeOf(diff_edit), #Null)
sn.l = 0
diff(@OldLines(0), 0, LinesOld, @NewLines(0), 0, LinesNew, @History_Diff_idx(), @History_Diff_cmp(), #Null, 0, *ses, @sn, 0)
*OutputEnd = *Output + *OutSize\i
*Pointer.PTR = *Output
*Pointer\l = *Event\Size ; store original size
*Pointer + 4 ; skip original size storage
For i = 0 To sn-1
*edit.diff_edit = varray_get(*ses, i)
If *Pointer + 5 > *OutputEnd
varray_del(*ses)
ProcedureReturn #False
EndIf
Select *edit\op
Case #DIFF_MATCH
*Pointer\b = 'C': *Pointer + 1
*Pointer\l = History_DiffEditSize(*edit, OldLines()): *Pointer + 4
Case #DIFF_DELETE
*Pointer\b = 'D': *Pointer + 1
*Pointer\l = History_DiffEditSize(*edit, OldLines()): *Pointer + 4
Case #DIFF_INSERT
*Pointer\b = 'A': *Pointer + 1
EditSize = History_DiffEditSize(*edit, NewLines())
*Pointer\l = EditSize: *Pointer + 4
If *Pointer + EditSize > *OutputEnd
varray_del(*ses)
ProcedureReturn #False
EndIf
CopyMemory(*Event\Content + NewLines(*edit\off)\Offset, *Pointer, EditSize)
*Pointer + EditSize
EndSelect
Next i
varray_del(*ses)
*OutSize\i = *Pointer - *Output
ProcedureReturn #True
EndProcedure
Procedure History_ApplyDiff(*Output, *PreviousBuffer, *Diff.PTR, DiffSize)
*DiffEnd = *Diff + DiffSize
*Diff + 4 ; skip expanded size
While *Diff < *DiffEnd
Select *Diff\b
Case 'C'
*Diff + 1
Size = *Diff\l: *Diff + 4
CopyMemory(*PreviousBuffer, *Output, Size)
*PreviousBuffer + Size
*Output + Size
Case 'D'
*Diff + 1
Size = *Diff\l: *Diff + 4
; just skip on the input
*PreviousBuffer + Size
Case 'A'
*Diff + 1
Size = *Diff\l: *Diff + 4
CopyMemory(*Diff, *Output, Size)
*Diff + Size
*Output + Size
Default
; something is wrong here
ProcedureReturn #False
EndSelect
Wend
ProcedureReturn #True
EndProcedure
Procedure History_WriteEvent(*Event.HistoryEvent)
; calculate checksum if needed
If *Event\Checksum = 0 And *Event\Content And *Event\Size
*Event\Checksum = CRC32Fingerprint(*Event\Content, *Event\Size)
EndIf
; find any previous event for the same source and remove it from the list
; this list is accessed only by this thread
*Update.HistoryEvent = 0 ; the one to update on a delete
*Previous.HistoryEvent = *HistoryFirstProcessed
While *Previous And *Previous\SourceID <> *Event\SourceID
*Update = *Previous
*Previous = *Previous\ProcessedNext
Wend
; previous event found
If *Previous
; If the Event is #HISTORY_Edit, decide if there were changes
; Drop the event if there were no changes since last time
If *Event\Event = #HISTORY_Edit And *Previous\Size = *Event\Size And *Previous\Checksum = *Event\Checksum
History_FreeEvent(*Event)
ProcedureReturn
EndIf
; unlink the previous event from list
If *Update
*Update\ProcessedNext = *Previous\ProcessedNext
Else
*HistoryFirstProcessed = *Previous\ProcessedNext
EndIf
; if the event is close, and the only previous event is open or create,
; drop both from the db to avoid lots of empty open/close event combinations
If *Event\Event = #HISTORY_Close And (*Previous\Event = #HISTORY_Create Or *Previous\Event = #HISTORY_Open)
History_AsyncDeleteEvent(*Previous\EventID)
History_FreeEvent(*Previous)
History_FreeEvent(*Event)
ProcedureReturn
EndIf
; if a fresh file is saved, rename all old events to the new filename
If *Event\Event = #HISTORY_SaveAs And *Previous\FreshFile
History_AsyncUpdateName(*Previous\HistoryName, *Event\HistoryName)
EndIf
EndIf
; prepare/compress the contents for storage
StorageSize = 0
*StorageBuffer = #Null
If *Event\Content = #Null
Type = #DATA_Empty
PreviousEventID = 0
*Event\DiffCount = 0
ElseIf *Previous = #Null Or *Previous\Content = #Null Or *Previous\DiffCount >= #HISTORY_EVENT_MAX_DIFFS
; write full data if no previous data exists, or the max diff count is reached
Type = #DATA_Full
PreviousEventID = 0
StorageSize = *Event\Size
*StorageBuffer = *Event\Content
; attempt to compress
CompilerIf #ENABLE_HISTORY_COMPRESSION
CompressedSize = compressBound(*Event\Size) + 4
*CompressedBuffer = AllocateMemory(CompressedSize)
If *CompressedBuffer
CompressedSize - 4
If compress2(*CompressedBuffer + 4, @CompressedSize, *Event\Content , *Event\Size, #HISTORY_COMPRESSION_LEVEL) = #Z_OK
Type = #DATA_FullZ
PokeL(*CompressedBuffer, *Event\Size) ; store original size in first 4 bytes
StorageSize = CompressedSize + 4
*StorageBuffer = *CompressedBuffer
Else
FreeMemory(*CompressedBuffer)
EndIf
EndIf
CompilerEndIf
*Event\DiffCount = 0
ElseIf *Event\Size = *Previous\Size And *Event\Checksum = *Previous\Checksum
Type = #DATA_Same
PreviousEventID = *Previous\EventID
*Event\DiffCount = *Previous\DiffCount + 1
Else
; fallback to full if something fails below
Type = #DATA_Full
PreviousEventID = 0
StorageSize = *Event\Size
*StorageBuffer = *Event\Content
*Event\DiffCount = *Previous\DiffCount + 1
; try to diff
*DiffBuffer = AllocateMemory(StorageSize + 4) ; 4 bytes for the unpacked size
If *DiffBuffer
DiffSize = StorageSize + 4
If History_MakeDiff(*DiffBuffer, @DiffSize, *Event, *Previous)
Type = #DATA_Diff
*StorageBuffer = *DiffBuffer
StorageSize = DiffSize
PreviousEventID = *Previous\EventID
Else
FreeMemory(*DiffBuffer) ; diff is larger than original!
*DiffBuffer = 0
EndIf
EndIf
; attempt to compress
CompilerIf #ENABLE_HISTORY_COMPRESSION
CompressedSize = compressBound(StorageSize) + 4
*CompressedBuffer = AllocateMemory(CompressedSize)
If *CompressedBuffer
CompressedSize - 4
If compress2(*CompressedBuffer + 4, @CompressedSize, *StorageBuffer, StorageSize, #HISTORY_COMPRESSION_LEVEL) = #Z_OK
If Type = #DATA_Diff
Type = #DATA_DiffZ
FreeMemory(*DiffBuffer) ; diff buffer is no longer needed. storagebuffer is overwritten below
Else
Type = #DATA_FullZ
EndIf
PokeL(*CompressedBuffer, StorageSize) ; store original size in first 4 bytes
StorageSize = CompressedSize + 4
*StorageBuffer = *CompressedBuffer
Else
FreeMemory(*CompressedBuffer)
EndIf
EndIf
CompilerEndIf
EndIf
; Update the DB
; We cannot use the Database functions for thread safety
; Use the sqlite functions directly
; In PB, sqlite is compiled threadsafe, so this is ok
CompilerIf #HISTORY_WRITE_ASYNC = #False
Debug "Async: writing history event: " + Str(*Event\Event)
CompilerEndIf
; Use bind variables as we cannot modify strings in this thread
Protected Success = #False
CompilerIf #PB_Compiler_Unicode
If sqlite3_prepare16_v2(DatabaseID(#DB_History), @"INSERT INTO event(session_id, filename, event, time, type, previous_event, encoding, data) VALUES (?,?,?,?,?,?,?,?)", -1, @*Statement, #Null) = #SQLITE_OK
Success = #True
sqlite3_bind_int (*Statement, 1, SessionID)
sqlite3_bind_text16(*Statement, 2, @*Event\HistoryName, -1, #SQLITE_STATIC)
EndIf
CompilerElse
If sqlite3_prepare_v2(DatabaseID(#DB_History), @"INSERT INTO event(session_id, filename, event, time, type, previous_event, encoding, data) VALUES (?,?,?,?,?,?,?,?)", -1, @*Statement, #Null) = #SQLITE_OK
Success = #True
sqlite3_bind_int (*Statement, 1, SessionID)
sqlite3_bind_text(*Statement, 2, @*Event\HistoryName, -1, #SQLITE_STATIC)
EndIf
CompilerEndIf
If Success
sqlite3_bind_int (*Statement, 3, *Event\Event)
sqlite3_bind_int (*Statement, 4, *Event\Time)
sqlite3_bind_int (*Statement, 5, Type)
sqlite3_bind_int (*Statement, 6, PreviousEventID)
sqlite3_bind_int (*Statement, 7, *Event\Encoding)
If *StorageBuffer
sqlite3_bind_blob(*Statement, 8, *StorageBuffer, StorageSize, #SQLITE_STATIC)
Else
sqlite3_bind_null(*Statement, 8)
EndIf
While sqlite3_step(*Statement) = #SQLITE_ROW: Wend
sqlite3_finalize(*Statement)
; retrieve the event id
*Event\EventID = sqlite3_last_insert_rowid(DatabaseID(#DB_History))
EndIf
; free compressed buffer
If *StorageBuffer And *StorageBuffer <> *Event\Content
FreeMemory(*StorageBuffer)
EndIf
; free the previous event info
If *Previous
History_FreeEvent(*Previous)
EndIf
; if this is a close, free the current event