forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPropertyDatabase.cs
More file actions
1381 lines (1181 loc) · 50.5 KB
/
Copy pathPropertyDatabase.cs
File metadata and controls
1381 lines (1181 loc) · 50.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
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
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
// #define USE_PERFORMANCE_TRACKER
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using UnityEditorInternal;
using UnityEngine;
namespace UnityEditor.Search
{
public interface IPropertyDatabaseView : IDisposable
{
public bool Store(string documentId, string propertyPath, object value);
public bool Store(ulong documentKey, Hash128 propertyHash, object value);
public bool Store(Hash128 propertyHash, object value);
public bool Store(in PropertyDatabaseRecordKey recordKey, object value);
bool TryLoad(in PropertyDatabaseRecordKey recordKey, out object data);
bool TryLoad(in PropertyDatabaseRecordKey recordKey, out IPropertyDatabaseRecordValue data);
bool TryLoad(ulong documentKey, out IEnumerable<object> data);
bool TryLoad(ulong documentKey, out IEnumerable<IPropertyDatabaseRecord> records);
void Invalidate(string documentId);
void Invalidate(ulong documentKey);
void Invalidate(in PropertyDatabaseRecordKey recordKey);
void InvalidateMask(ulong documentKeyMask);
IEnumerable<IPropertyDatabaseRecord> EnumerateAll();
void Sync();
void Clear();
public PropertyDatabaseRecordKey CreateRecordKey(string documentId, string propertyPath);
public PropertyDatabaseRecordKey CreateRecordKey(ulong documentKey, Hash128 propertyPathHash);
public PropertyDatabaseRecordKey CreateRecordKey(string propertyPath);
public PropertyDatabaseRecordKey CreateRecordKey(string documentId, Hash128 propertyHash);
public PropertyDatabaseRecordKey CreateRecordKey(Hash128 propertyHash);
public ulong CreateDocumentKey(string documentId);
public Hash128 CreatePropertyHash(string propertyPath);
TValue GetValueFromRecord<TValue>(IPropertyDatabaseRecordValue recordValue);
public bool IsPersistableType(Type type);
}
class PropertyDatabaseLock : IDisposable
{
static Dictionary<string, SharedCounter> s_DatabaseFiles = new Dictionary<string, SharedCounter>();
class SharedCounter
{
public int value;
public SharedCounter(int value)
{
this.value = value;
}
}
string m_Path;
bool m_Disposed;
PropertyDatabaseLock(string propertyDatabasePath, SharedCounter counter)
{
m_Path = propertyDatabasePath;
}
PropertyDatabaseLock(PropertyDatabaseLock other)
{
this.m_Path = other.m_Path;
this.m_Disposed = other.m_Disposed;
}
public static bool TryOpen(string propertyDatabasePath, out PropertyDatabaseLock pdbl)
{
pdbl = null;
try
{
lock (s_DatabaseFiles)
{
if (s_DatabaseFiles.TryGetValue(propertyDatabasePath, out var _))
{
return false;
}
var counter = new SharedCounter(1);
s_DatabaseFiles.Add(propertyDatabasePath, counter);
pdbl = new PropertyDatabaseLock(propertyDatabasePath, counter);
return true;
}
}
catch (Exception)
{
pdbl = null;
return false;
}
}
public PropertyDatabaseLock Share()
{
lock(s_DatabaseFiles)
{
if (m_Disposed)
throw new ObjectDisposedException(m_Path, "Trying to share a lock that has already been disposed.");
if (!s_DatabaseFiles.TryGetValue(m_Path, out var counter))
throw new System.Exception($"Data base {m_Path} is not opened.");
++counter.value;
return new PropertyDatabaseLock(this);
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~PropertyDatabaseLock()
{
Dispose(false);
}
void Dispose(bool disposed)
{
lock (s_DatabaseFiles)
{
if (m_Disposed)
return;
m_Disposed = true;
if (s_DatabaseFiles.TryGetValue(m_Path, out var counter))
{
--counter.value;
if (counter.value == 0)
{
s_DatabaseFiles.Remove(m_Path);
}
}
}
}
}
public class PropertyDatabase : IDisposable
{
PropertyDatabaseVolatileMemoryStore m_LocalVolatileStore;
PropertyDatabaseMemoryStore m_LocalStore;
PropertyDatabaseFileStore m_FileStore;
PropertyStringTable m_StringTable;
PropertyDatabaseLock m_PropertyDatabaseLock;
internal static readonly int version = ((0x50 << 24) | (0x44 << 16) | 0x0007) ^ PropertyStringTable.version ^ ((InternalEditorUtility.GetUnityVersion().Major & 0xffff) << 16 | InternalEditorUtility.GetUnityVersion().Minor);
public string filePath { get; }
internal string stringTableFilePath { get; }
public bool autoFlush { get; }
public bool valid => m_Valid && !disposed;
const double k_DefaultBackgroundUpdateDebounceInSeconds = 5.0;
Delayer m_Debounce;
bool m_Valid;
internal bool disposed { get; private set; }
internal PropertyDatabaseErrorView disposedView { get; private set; }
public PropertyDatabase(string filePath)
: this(filePath, false)
{}
public PropertyDatabase(string filePath, bool autoFlush, double backgroundUpdateDebounceInSeconds = k_DefaultBackgroundUpdateDebounceInSeconds)
{
if (string.IsNullOrEmpty(filePath))
throw new System.ArgumentNullException(nameof(filePath));
this.filePath = filePath;
stringTableFilePath = GetStringTablePath(filePath);
disposedView = new PropertyDatabaseErrorView($"The property database has already been disposed.");
if (PropertyDatabaseLock.TryOpen(filePath, out m_PropertyDatabaseLock))
{
m_LocalVolatileStore = new PropertyDatabaseVolatileMemoryStore();
m_LocalStore = new PropertyDatabaseMemoryStore();
m_FileStore = new PropertyDatabaseFileStore(filePath);
m_StringTable = new PropertyStringTable(stringTableFilePath, 30);
// Do not allow automatic background updates while running tests. The writing of the file
// causes an assembly leak during the test Unity.IntegrationTests.Scripting.AssemblyReloadTest.AssemblyReloadDoesntLeakAssemblies
// on MacOs. I haven't found out why exactly does the writing of a file causes an assembly to be held, so instead I deactivate
// the automatic update during tests.
this.autoFlush = autoFlush && !Utils.IsRunningTests();
m_Debounce = Delayer.Debounce(_ => TriggerBackgroundUpdate(), TimeSpan.FromSeconds(backgroundUpdateDebounceInSeconds));
m_Valid = true;
}
else
{
this.autoFlush = false;
m_Valid = false;
}
}
public bool Store(string documentId, string propertyPath, object value)
{
using (var view = GetView())
return view.Store(documentId, propertyPath, value);
}
public bool Store(ulong documentKey, Hash128 propertyHash, object value)
{
using (var view = GetView())
return view.Store(documentKey, propertyHash, value);
}
public bool Store(Hash128 propertyHash, object value)
{
using (var view = GetView())
return view.Store(propertyHash, value);
}
public bool Store(in PropertyDatabaseRecordKey recordKey, object value)
{
using (var view = GetView())
return view.Store(recordKey, value);
}
internal bool Store(in PropertyDatabaseRecordKey recordKey, in PropertyDatabaseRecordValue value)
{
using (var view = (PropertyDatabaseView)GetView())
return view.Store(recordKey, value);
}
internal bool Store(in PropertyDatabaseRecord record)
{
using (var view = (PropertyDatabaseView)GetView())
return view.Store(record);
}
public bool TryLoad(in PropertyDatabaseRecordKey recordKey, out object value)
{
using (var view = GetView())
return view.TryLoad(recordKey, out value);
}
public bool TryLoad(in PropertyDatabaseRecordKey recordKey, out IPropertyDatabaseRecordValue value)
{
using (var view = GetView())
return view.TryLoad(recordKey, out value);
}
internal bool TryLoad(in PropertyDatabaseRecordKey recordKey, out PropertyDatabaseRecordValue value)
{
using (var view = (PropertyDatabaseView)GetView())
return view.TryLoad(recordKey, out value);
}
public bool TryLoad(ulong documentKey, out IEnumerable<object> data)
{
using (var view = GetView())
return view.TryLoad(documentKey, out data);
}
public bool TryLoad(ulong documentKey, out IEnumerable<IPropertyDatabaseRecord> records)
{
using (var view = GetView())
return view.TryLoad(documentKey, out records);
}
public void Invalidate(string documentId)
{
using (var view = GetView())
view.Invalidate(documentId);
}
public void Invalidate(ulong documentKey)
{
using (var view = GetView())
view.Invalidate(documentKey);
}
public void Invalidate(in PropertyDatabaseRecordKey recordKey)
{
using (var view = GetView())
view.Invalidate(recordKey);
}
internal void Invalidate(uint documentKeyHiWord)
{
using (var view = GetView())
view.Invalidate(documentKeyHiWord);
}
public void InvalidateMask(ulong documentKeyMask)
{
using (var view = GetView())
view.InvalidateMask(documentKeyMask);
}
internal void InvalidateMask(uint documentKeyHiWordMask)
{
using (var view = GetView())
view.InvalidateMask(documentKeyHiWordMask);
}
public void Clear()
{
using (var view = GetView())
view.Clear();
}
internal PropertyDatabaseRecord CreateRecord(string documentId, string propertyPath, object value)
{
return CreateRecord(CreateDocumentKey(documentId), CreatePropertyHash(propertyPath), value);
}
internal PropertyDatabaseRecord CreateRecord(string documentId, string propertyPath, object value, PropertyStringTableView view)
{
return CreateRecord(CreateDocumentKey(documentId), CreatePropertyHash(propertyPath), value, view);
}
internal PropertyDatabaseRecord CreateRecord(ulong documentKey, Hash128 propertyPathHash, object value)
{
var key = CreateRecordKey(documentKey, propertyPathHash);
return CreateRecord(key, value);
}
internal PropertyDatabaseRecord CreateRecord(ulong documentKey, Hash128 propertyPathHash, object value, PropertyStringTableView view)
{
var key = CreateRecordKey(documentKey, propertyPathHash);
return CreateRecord(key, value, view);
}
internal PropertyDatabaseRecord CreateRecord(string propertyPath, object value)
{
return CreateRecord(CreatePropertyHash(propertyPath), value);
}
internal PropertyDatabaseRecord CreateRecord(string propertyPath, object value, PropertyStringTableView view)
{
return CreateRecord(CreatePropertyHash(propertyPath), value, view);
}
internal PropertyDatabaseRecord CreateRecord(Hash128 propertyPathHash, object value)
{
return CreateRecord(0, propertyPathHash, value);
}
internal PropertyDatabaseRecord CreateRecord(Hash128 propertyPathHash, object value, PropertyStringTableView view)
{
return CreateRecord(0, propertyPathHash, value, view);
}
internal PropertyDatabaseRecord CreateRecord(in PropertyDatabaseRecordKey recordKey, object value)
{
var recordValue = CreateRecordValue(value);
return CreateRecord(recordKey, recordValue);
}
internal PropertyDatabaseRecord CreateRecord(in PropertyDatabaseRecordKey recordKey, object value, PropertyStringTableView view)
{
var recordValue = CreateRecordValue(value, view);
return CreateRecord(recordKey, recordValue);
}
internal static PropertyDatabaseRecord CreateRecord(in PropertyDatabaseRecordKey recordKey, PropertyDatabaseRecordValue recordValue)
{
if (!IsSupportedPropertyType(recordValue.propertyType))
throw new ArgumentException($"Property type of {nameof(recordValue)} is not supported.");
return new PropertyDatabaseRecord(recordKey, recordValue);
}
public static PropertyDatabaseRecordKey CreateRecordKey(string documentId, string propertyPath)
{
return CreateRecordKey(documentId, CreatePropertyHash(propertyPath));
}
public static Hash128 CreatePropertyHash(string propertyPath)
{
return Hash128.Compute(propertyPath);
}
public static PropertyDatabaseRecordKey CreateRecordKey(ulong documentKey, Hash128 propertyPathHash)
{
return new PropertyDatabaseRecordKey(documentKey, propertyPathHash);
}
public static PropertyDatabaseRecordKey CreateRecordKey(string propertyPath)
{
return CreateRecordKey(CreatePropertyHash(propertyPath));
}
public static PropertyDatabaseRecordKey CreateRecordKey(string documentId, Hash128 propertyHash)
{
return CreateRecordKey(CreateDocumentKey(documentId), propertyHash);
}
public static PropertyDatabaseRecordKey CreateRecordKey(Hash128 propertyHash)
{
return CreateRecordKey(0, propertyHash);
}
public static ulong CreateDocumentKey(string documentId)
{
return string.IsNullOrEmpty(documentId) ? 0UL : documentId.GetHashCode64();
}
internal PropertyDatabaseRecordValue CreateRecordValue(object value)
{
if (!valid)
return PropertyDatabaseRecordValue.invalid;
using (var view = m_StringTable.GetView())
return CreateRecordValue(value, view);
}
internal PropertyDatabaseRecordValue CreateRecordValue(object value, PropertyStringTableView stringTableView)
{
if (!IsPersistableType(value.GetType()))
throw new ArgumentException($"Type \"{value.GetType()}\" is not supported.");
return PropertyDatabaseSerializerManager.Serialize(value, stringTableView);
}
internal object GetObjectFromRecordValue(in PropertyDatabaseRecordValue recordValue)
{
if (!valid)
return null;
using (var view = m_StringTable.GetView())
return GetObjectFromRecordValue(recordValue, view);
}
public TValue GetValueFromRecord<TValue>(IPropertyDatabaseRecordValue recordValue)
{
if (!valid)
return default;
using (var view = m_StringTable.GetView())
return GetValueFromRecord<TValue>(recordValue, view);
}
internal object GetObjectFromRecordValue(in PropertyDatabaseRecordValue recordValue, PropertyStringTableView stringTableView)
{
if (!IsSupportedPropertyType(recordValue.propertyType))
throw new ArgumentException($"Property type \"{recordValue.propertyType}\" of {nameof(recordValue)} is not supported.");
return PropertyDatabaseSerializerManager.Deserialize(recordValue, stringTableView);
}
internal object GetObjectFromRecordValue(IPropertyDatabaseRecordValue recordValue)
{
if (!valid)
return null;
using (var view = m_StringTable.GetView())
return GetObjectFromRecordValue(recordValue, view);
}
internal object GetObjectFromRecordValue(IPropertyDatabaseRecordValue recordValue, PropertyStringTableView stringTableView)
{
if (recordValue == null)
return null;
if (recordValue.type != PropertyDatabaseType.Volatile)
return GetObjectFromRecordValue((PropertyDatabaseRecordValue)recordValue, stringTableView);
var volatileRecordValue = (PropertyDatabaseVolatileRecordValue)recordValue;
return volatileRecordValue.value;
}
internal TValue GetValueFromRecord<TValue>(IPropertyDatabaseRecordValue recordValue, PropertyStringTableView stringTableView)
{
if (recordValue == null)
return default;
if (recordValue.type != PropertyDatabaseType.Volatile)
return (TValue)GetObjectFromRecordValue((PropertyDatabaseRecordValue)recordValue, stringTableView);
var volatileRecordValue = (PropertyDatabaseVolatileRecordValue)recordValue;
return (TValue)volatileRecordValue.value;
}
public static bool IsPersistableType(Type type)
{
return PropertyDatabaseSerializerManager.SerializerExists(type);
}
internal static bool IsSupportedPropertyType(byte propertyType)
{
return PropertyDatabaseSerializerManager.DeserializerExists((PropertyDatabaseType)propertyType);
}
public IPropertyDatabaseView GetView(bool delayedSync = false)
{
{
if (valid)
return new PropertyDatabaseView(this, m_LocalVolatileStore, m_LocalStore, m_FileStore, m_StringTable, m_PropertyDatabaseLock.Share(), delayedSync);
if (!disposed)
return new PropertyDatabaseErrorView($"The property database \"{filePath}\" is already opened.");
return disposedView;
}
}
public void Flush()
{
if (!valid)
return;
var task = TriggerBackgroundUpdate();
task.Wait();
}
internal Task TriggerBackgroundUpdate()
{
var task = Task.Run(() =>
{
try
{
MergeStoresToFile();
}
catch (ThreadAbortException)
{
// Rethrow but do not log.
throw;
}
catch (Exception ex)
{
Debug.LogException(ex);
throw;
}
});
return task;
}
internal void StoresChanged()
{
if (!autoFlush)
return;
lock (this)
{
m_Debounce?.Execute();
}
}
public string GetInfo()
{
if (!valid)
return string.Empty;
var sb = new StringBuilder();
sb.AppendLine($"Property Database: \"{filePath}\"");
GetStoreInfo(m_LocalVolatileStore, "Volatile Store", sb);
GetStoreInfo(m_LocalStore, "Non-Serialized Store", sb);
GetStoreInfo(m_FileStore, "Serialized Store (on disk)", sb);
GetStringTableInfo(m_StringTable, sb);
return sb.ToString();
}
static void GetStoreInfo(IPropertyDatabaseStore store, string storeTitle, StringBuilder sb)
{
using (var view = store.GetView())
{
sb.AppendLine($"\t{storeTitle}:");
sb.AppendLine($"\t\tCount: {view.length}");
sb.AppendLine($"\t\tSize: {Utils.FormatBytes(view.byteSize)}");
}
}
static void GetStringTableInfo(PropertyStringTable st, StringBuilder sb)
{
var longestStringsCount = 5;
using (var view = st.GetView())
{
sb.AppendLine($"\tString Table: {st.filePath}");
sb.AppendLine($"\t\tVersion: {view.version}");
sb.AppendLine($"\t\tCount: {view.count}");
sb.AppendLine($"\t\tSymbols slots: {view.symbolSlots}");
sb.AppendLine($"\t\tAllocated bytes for strings: {Utils.FormatBytes(view.allocatedStringBytes)}");
sb.AppendLine($"\t\tUsed bytes for strings: {Utils.FormatBytes(view.usedStringBytes)}");
sb.AppendLine($"\t\tAverage byte size for strings: {Utils.FormatBytes(view.GetAverageBytesPerString())}");
sb.AppendLine($"\t\tFile size: {Utils.FormatBytes(view.fileSize)}");
sb.AppendLine($"\t\tTop {longestStringsCount} longest strings:");
var strings = view.GetAllStrings();
foreach (var str in strings.OrderByDescending(s => s.Length).Take(longestStringsCount))
{
sb.AppendLine($"\t\t\t{str}");
}
}
}
void MergeStoresToFile()
{
if (!valid)
return;
// TODO: When trackers are removed, put back GetView()
using (var view = new PropertyDatabaseView(this, m_LocalVolatileStore, m_LocalStore, m_FileStore, m_StringTable, m_PropertyDatabaseLock.Share(), false))
view.MergeStores();
}
static string GetStringTablePath(string propertyDatabasePath)
{
return propertyDatabasePath + ".st";
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
void Dispose(bool _)
{
lock (this)
{
if (disposed)
return;
m_Debounce?.Dispose();
m_Debounce = null;
m_PropertyDatabaseLock?.Dispose();
m_PropertyDatabaseLock = null;
disposed = true;
}
}
~PropertyDatabase()
{
Dispose(false);
}
}
struct PropertyDatabaseView : IPropertyDatabaseView
{
bool m_Disposed;
bool m_DelayedSync;
PropertyDatabase m_PropertyDatabase;
PropertyDatabaseMemoryStore m_MemoryStore;
PropertyDatabaseFileStore m_FileStore;
PropertyDatabaseLock m_Lock;
PropertyDatabaseVolatileMemoryStoreView m_VolatileMemoryStoreView;
PropertyDatabaseMemoryStoreView m_MemoryStoreView;
PropertyDatabaseFileStoreView m_FileStoreView;
PropertyStringTableView m_StringTableView;
// Internal for debugging
internal PropertyDatabaseVolatileMemoryStoreView volatileMemoryStoreView => m_VolatileMemoryStoreView;
internal PropertyDatabaseMemoryStoreView memoryStoreView => m_MemoryStoreView;
internal PropertyDatabaseFileStoreView fileStoreView => m_FileStoreView;
internal PropertyStringTableView stringTableView => m_StringTableView;
internal PropertyDatabase database => m_PropertyDatabase;
internal PropertyDatabaseView(PropertyDatabase propertyDatabase, PropertyDatabaseVolatileMemoryStore volatileMemoryStore, PropertyDatabaseMemoryStore memoryStore, PropertyDatabaseFileStore fileStore, PropertyStringTable stringTable, PropertyDatabaseLock propertyDatabaseLock, bool delayedSync)
{
m_PropertyDatabase = propertyDatabase;
m_MemoryStore = memoryStore;
m_FileStore = fileStore;
m_VolatileMemoryStoreView = (PropertyDatabaseVolatileMemoryStoreView)volatileMemoryStore.GetView();
m_MemoryStoreView = (PropertyDatabaseMemoryStoreView)memoryStore.GetView();
m_FileStoreView = (PropertyDatabaseFileStoreView)fileStore.GetView();
m_StringTableView = stringTable.GetView(delayedSync);
m_Disposed = false;
m_DelayedSync = delayedSync;
m_Lock = propertyDatabaseLock;
}
public void Dispose()
{
if (m_Disposed)
return;
if (m_DelayedSync)
Sync();
m_PropertyDatabase = null;
m_MemoryStore = null;
m_FileStore = null;
m_VolatileMemoryStoreView.Dispose();
m_MemoryStoreView.Dispose();
m_FileStoreView.Dispose();
m_StringTableView.Dispose();
m_Lock.Dispose();
m_Lock = null;
m_DelayedSync = false;
m_Disposed = true;
}
public bool Store(string documentId, string propertyPath, object value)
{
if (m_PropertyDatabase.disposed)
return m_PropertyDatabase.disposedView.Store(documentId, propertyPath, value);
var recordKey = CreateRecordKey(documentId, propertyPath);
return Store(recordKey, value);
}
public bool Store(ulong documentKey, Hash128 propertyHash, object value)
{
if (m_PropertyDatabase.disposed)
return m_PropertyDatabase.disposedView.Store(documentKey, propertyHash, value);
var recordKey = CreateRecordKey(documentKey, propertyHash);
return Store(recordKey, value);
}
public bool Store(Hash128 propertyHash, object value)
{
if (m_PropertyDatabase.disposed)
return m_PropertyDatabase.disposedView.Store(propertyHash, value);
var recordKey = CreateRecordKey(propertyHash);
return Store(recordKey, value);
}
public bool Store(in PropertyDatabaseRecordKey recordKey, object value)
{
{
if (m_PropertyDatabase.disposed)
return m_PropertyDatabase.disposedView.Store(recordKey, value);
if (!IsPersistableType(value.GetType()))
return m_VolatileMemoryStoreView.Store(recordKey, value, !m_DelayedSync);
var record = m_PropertyDatabase.CreateRecord(recordKey, value, m_StringTableView);
return Store(record);
}
}
public bool Store(in PropertyDatabaseRecordKey recordKey, in PropertyDatabaseRecordValue value)
{
if (m_PropertyDatabase.disposed)
return m_PropertyDatabase.disposedView.Store(recordKey, value);
var record = CreateRecord(recordKey, value);
return Store(record);
}
public bool Store(in PropertyDatabaseRecord record)
{
{
if (m_PropertyDatabase.disposed)
return m_PropertyDatabase.disposedView.Store(record);
if (!record.recordValue.valid)
return false;
var success = m_MemoryStoreView.Store(record, !m_DelayedSync);
if (success)
{
m_PropertyDatabase.StoresChanged();
}
return success;
}
}
public bool TryLoad(in PropertyDatabaseRecordKey recordKey, out object data)
{
{
data = null;
if (m_PropertyDatabase.disposed)
return m_PropertyDatabase.disposedView.TryLoad(recordKey, out data);
if (!TryLoad(recordKey, out PropertyDatabaseRecordValue recordValue))
return m_VolatileMemoryStoreView.TryLoad(recordKey, out data);
data = GetObjectFromRecordValue(recordValue);
return true;
}
}
public bool TryLoad(in PropertyDatabaseRecordKey recordKey, out PropertyDatabaseRecordValue data)
{
{
if (m_PropertyDatabase.disposed)
return m_PropertyDatabase.disposedView.TryLoad(recordKey, out data);
data = PropertyDatabaseRecordValue.invalid;
if (m_MemoryStoreView.TryLoad(recordKey, out PropertyDatabaseRecord memoryRecord, true))
{
// If data is invalid, it was deliberately made invalid, so it should be invalid in the
// filestore too.
data = memoryRecord.recordValue;
return memoryRecord.IsValid();
}
if (!m_FileStoreView.TryLoad(recordKey, out PropertyDatabaseRecord fileRecord))
return false;
// Cache loaded value into memory store.
data = fileRecord.recordValue;
m_MemoryStoreView.Store(fileRecord, !m_DelayedSync);
return true;
}
}
public bool TryLoad(in PropertyDatabaseRecordKey recordKey, out IPropertyDatabaseRecordValue data)
{
{
if (m_PropertyDatabase.disposed)
return m_PropertyDatabase.disposedView.TryLoad(recordKey, out data);
if (m_VolatileMemoryStoreView.TryLoad(recordKey, out IPropertyDatabaseRecord volatileRecord))
{
data = volatileRecord.value;
return true;
}
var success = TryLoad(recordKey, out PropertyDatabaseRecordValue recordValue);
data = recordValue;
return success;
}
}
public bool TryLoad(ulong documentKey, out IEnumerable<object> data)
{
{
data = null;
if (m_PropertyDatabase.disposed)
return m_PropertyDatabase.disposedView.TryLoad(documentKey, out data);
if (!TryLoad(documentKey, out IEnumerable<IPropertyDatabaseRecord> records))
return false;
var results = new List<object>();
foreach (var propertyDatabaseRecord in records)
{
results.Add(GetObjectFromRecordValue(propertyDatabaseRecord.value));
}
data = results;
return true;
}
}
public bool TryLoad(ulong documentKey, out IEnumerable<IPropertyDatabaseRecord> records)
{
{
records = null;
if (m_PropertyDatabase.disposed)
return m_PropertyDatabase.disposedView.TryLoad(documentKey, out records);
SortedSet<IPropertyDatabaseRecord> allRecords = new SortedSet<IPropertyDatabaseRecord>(new IPropertyDatabaseRecordComparer());
if (m_VolatileMemoryStoreView.TryLoad(documentKey, out var volatileRecords))
allRecords.UnionWith(volatileRecords);
if (m_MemoryStoreView.TryLoad(documentKey, out var memoryRecords))
allRecords.UnionWith(memoryRecords);
if (m_FileStoreView.TryLoad(documentKey, out var fileRecords))
allRecords.UnionWith(fileRecords);
if (allRecords.Count == 0)
return false;
records = allRecords;
return true;
}
}
public void Invalidate(string documentId)
{
if (m_PropertyDatabase.disposed)
{
m_PropertyDatabase.disposedView.Invalidate(documentId);
return;
}
var documentKey = PropertyDatabase.CreateDocumentKey(documentId);
Invalidate(documentKey);
}
public void Invalidate(ulong documentKey)
{
{
if (m_PropertyDatabase.disposed)
{
m_PropertyDatabase.disposedView.Invalidate(documentKey);
return;
}
m_VolatileMemoryStoreView.Invalidate(documentKey, !m_DelayedSync);
m_MemoryStoreView.Invalidate(documentKey, !m_DelayedSync);
m_FileStoreView.InvalidateInMemory(documentKey, !m_DelayedSync);
m_PropertyDatabase.StoresChanged();
}
}
public void Invalidate(in PropertyDatabaseRecordKey recordKey)
{
{
if (m_PropertyDatabase.disposed)
{
m_PropertyDatabase.disposedView.Invalidate(recordKey);
return;
}
m_VolatileMemoryStoreView.Invalidate(recordKey, !m_DelayedSync);
m_MemoryStoreView.Invalidate(recordKey, !m_DelayedSync);
m_FileStoreView.InvalidateInMemory(m_MemoryStoreView, recordKey, !m_DelayedSync);
m_PropertyDatabase.StoresChanged();
}
}
internal void Invalidate(uint documentKeyHiWord)
{
{
if (m_PropertyDatabase.disposed)
{
m_PropertyDatabase.disposedView.Invalidate(documentKeyHiWord);
return;
}
m_VolatileMemoryStoreView.Invalidate(documentKeyHiWord, !m_DelayedSync);
m_MemoryStoreView.Invalidate(documentKeyHiWord, !m_DelayedSync);
m_FileStoreView.InvalidateInMemory(documentKeyHiWord, !m_DelayedSync);
m_PropertyDatabase.StoresChanged();
}
}
public void InvalidateMask(ulong documentKeyMask)
{
{
if (m_PropertyDatabase.disposed)
{
m_PropertyDatabase.disposedView.InvalidateMask(documentKeyMask);
return;
}
m_VolatileMemoryStoreView.InvalidateMask(documentKeyMask, !m_DelayedSync);
m_MemoryStoreView.InvalidateMask(documentKeyMask, !m_DelayedSync);
m_FileStoreView.InvalidateMaskInMemory(documentKeyMask, !m_DelayedSync);
m_PropertyDatabase.StoresChanged();
}
}
internal void InvalidateMask(uint documentKeyHiWordMask)
{
{
if (m_PropertyDatabase.disposed)
{
m_PropertyDatabase.disposedView.InvalidateMask(documentKeyHiWordMask);
return;
}
m_VolatileMemoryStoreView.InvalidateMask(documentKeyHiWordMask, !m_DelayedSync);
m_MemoryStoreView.InvalidateMask(documentKeyHiWordMask, !m_DelayedSync);
m_FileStoreView.InvalidateMaskInMemory(documentKeyHiWordMask, !m_DelayedSync);
m_PropertyDatabase.StoresChanged();
}
}
public IEnumerable<IPropertyDatabaseRecord> EnumerateAll()
{
{
if (m_PropertyDatabase.disposed)
return m_PropertyDatabase.disposedView.EnumerateAll();
SortedSet<IPropertyDatabaseRecord> allRecords = new SortedSet<IPropertyDatabaseRecord>(new IPropertyDatabaseRecordComparer());
allRecords.UnionWith(m_VolatileMemoryStoreView.EnumerateAll());
allRecords.UnionWith(m_MemoryStoreView.EnumerateAll());
allRecords.UnionWith(m_FileStoreView.EnumerateAll());
return allRecords;
}
}
public void Sync()
{
if (m_PropertyDatabase.disposed)
{
m_PropertyDatabase.disposedView.Sync();
return;
}
m_VolatileMemoryStoreView.Sync();
m_MemoryStoreView.Sync();
m_FileStoreView.Sync();
m_StringTableView.Sync();
}
public void Clear()
{
if (m_PropertyDatabase.disposed)
{
m_PropertyDatabase.disposedView.Clear();
return;
}
m_VolatileMemoryStoreView.Clear();
m_MemoryStoreView.Clear();
m_FileStoreView.Clear();
m_StringTableView.Clear();
}
public PropertyDatabaseRecord CreateRecord(string documentId, string propertyPath, object value)
{
if (m_PropertyDatabase.disposed)
return m_PropertyDatabase.disposedView.CreateRecord(documentId, propertyPath, value);
return m_PropertyDatabase.CreateRecord(documentId, propertyPath, value, m_StringTableView);
}
public PropertyDatabaseRecord CreateRecord(ulong documentKey, Hash128 propertyPathHash, object value)
{
if (m_PropertyDatabase.disposed)
return m_PropertyDatabase.disposedView.CreateRecord(documentKey, propertyPathHash, value);
return m_PropertyDatabase.CreateRecord(documentKey, propertyPathHash, value, m_StringTableView);
}
public PropertyDatabaseRecord CreateRecord(string propertyPath, object value)
{
if (m_PropertyDatabase.disposed)
return m_PropertyDatabase.disposedView.CreateRecord(propertyPath, value);
return m_PropertyDatabase.CreateRecord(propertyPath, value, m_StringTableView);
}
public PropertyDatabaseRecord CreateRecord(Hash128 propertyPathHash, object value)
{
if (m_PropertyDatabase.disposed)
return m_PropertyDatabase.disposedView.CreateRecord(propertyPathHash, value);
return m_PropertyDatabase.CreateRecord(propertyPathHash, value, m_StringTableView);
}
public PropertyDatabaseRecord CreateRecord(in PropertyDatabaseRecordKey recordKey, object value)