forked from PNNL-Comp-Mass-Spec/MASIC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clsSpectraCache.cs
1081 lines (924 loc) · 39.1 KB
/
clsSpectraCache.cs
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
using System;
using System.Collections.Generic;
using System.IO;
namespace MASIC
{
/// <summary>
/// Utilizes a spectrum pool to store mass spectra
/// </summary>
public class clsSpectraCache : clsMasicEventNotifier, IDisposable
{
/// <summary>
/// Constructor
/// </summary>
/// <param name="cacheOptions"></param>
public clsSpectraCache(clsSpectrumCacheOptions cacheOptions)
{
mCacheOptions = cacheOptions;
InitializeVariables();
}
public void Dispose()
{
ClosePageFile();
DeleteSpectrumCacheFiles();
}
#region "Constants and Enums"
private const string SPECTRUM_CACHE_FILE_PREFIX = "$SpecCache";
private const string SPECTRUM_CACHE_FILE_BASENAME_TERMINATOR = "_Temp";
private const int SPECTRUM_CACHE_MAX_FILE_AGE_HOURS = 12;
private enum eCacheStateConstants
{
/// <summary>
/// No data present
/// </summary>
UnusedSlot = 0,
/// <summary>
/// In memory, but never cached
/// </summary>
NeverCached = 1,
/// <summary>
/// Loaded from cache, and in memory; or, loaded using XRaw; safe to purge without caching
/// </summary>
LoadedFromCache = 2
}
#endregion
#region "Classwide Variables"
/// <summary>
/// Pool (collection) of currently loaded spectra
/// </summary>
private IScanMemoryCache spectraPool;
private readonly clsSpectrumCacheOptions mCacheOptions;
private BinaryReader mPageFileReader;
private BinaryWriter mPageFileWriter;
private bool mDirectoryPathValidated;
private int mMaximumPoolLength;
/// <summary>
/// Records the byte offset of the data in the page file for a given scan number
/// </summary>
private Dictionary<int, long> mSpectrumByteOffset;
#endregion
/// <summary>
/// Number of cache events
/// </summary>
public int CacheEventCount { get; private set; }
/// <summary>
/// Number of times the spectrum was found in the in-memory spectrum pool
/// </summary>
public int SpectraPoolHitEventCount { get; private set; }
/// <summary>
/// Spectrum cache directory path
/// </summary>
public string CacheDirectoryPath
{
get => mCacheOptions.DirectoryPath;
set => mCacheOptions.DirectoryPath = value;
}
/// <summary>
/// Maximum memory that the cache is allowed to use
/// </summary>
[Obsolete("Legacy parameter; no longer used")]
public float CacheMaximumMemoryUsageMB
{
get => mCacheOptions.MaximumMemoryUsageMB;
set => mCacheOptions.MaximumMemoryUsageMB = value;
}
/// <summary>
/// Minimum memory that the cache will use
/// </summary>
[Obsolete("Legacy parameter; no longer used")]
public float CacheMinimumFreeMemoryMB
{
get => mCacheOptions.MinimumFreeMemoryMB;
set
{
if (mCacheOptions.MinimumFreeMemoryMB < 10)
{
mCacheOptions.MinimumFreeMemoryMB = 10;
}
mCacheOptions.MinimumFreeMemoryMB = value;
}
}
/// <summary>
/// Number of spectra to keep in the in-memory cache
/// </summary>
public int CacheSpectraToRetainInMemory
{
get => mCacheOptions.SpectraToRetainInMemory;
set
{
if (value < 100)
value = 100;
mCacheOptions.SpectraToRetainInMemory = value;
}
}
/// <summary>
/// When True, disk caching is disabled
/// </summary>
public bool DiskCachingAlwaysDisabled
{
get => mCacheOptions.DiskCachingAlwaysDisabled;
set => mCacheOptions.DiskCachingAlwaysDisabled = value;
}
/// <summary>
/// Number of times a spectrum was loaded from disk and cached in the SpectraPool
/// </summary>
public int UnCacheEventCount { get; private set; }
/// <summary>
/// The number of spectra we expect to read, updated to the number cached (to disk)
/// </summary>
public int SpectrumCount { get; set; }
/// <summary>
/// Adds spectrum to the spectrum pool
/// </summary>
/// <param name="spectrum"></param>
/// <param name="scanNumber"></param>
/// <returns>Index of the spectrum in the pool in targetPoolIndex</returns>
public bool AddSpectrumToPool(
clsMSSpectrum spectrum,
int scanNumber)
{
try
{
if (SpectrumCount > CacheSpectraToRetainInMemory + 5 &&
!DiskCachingAlwaysDisabled &&
ValidatePageFileIO(true))
{
// Store all of the spectra in one large file
CacheSpectrumWork(spectrum);
CacheEventCount += 1;
return true;
}
if (spectraPool.GetItem(scanNumber, out var cacheItem))
{
// Replace the spectrum data with spectrum
cacheItem.Scan.ReplaceData(spectrum, scanNumber);
cacheItem.CacheState = eCacheStateConstants.NeverCached;
}
else
{
// Need to add the spectrum
AddItemToSpectraPool(new ScanMemoryCacheItem(spectrum, eCacheStateConstants.NeverCached));
}
return true;
}
catch (Exception ex)
{
ReportError(ex.Message, ex);
return false;
}
}
private void CacheSpectrumWork(clsMSSpectrum spectrumToCache)
{
const int MAX_RETRIES = 3;
// See if the given spectrum is already present in the page file
var scanNumber = spectrumToCache.ScanNumber;
if (mSpectrumByteOffset.ContainsKey(scanNumber))
{
// Page file already contains the given scan;
// re-cache the item. for some reason we have updated peaks.
mSpectrumByteOffset.Remove(scanNumber);
// Data not changed; do not re-write
//return;
}
var initialOffset = mPageFileWriter.BaseStream.Position;
// Write the spectrum to the page file
// Record the current offset in the hashtable
mSpectrumByteOffset.Add(scanNumber, mPageFileWriter.BaseStream.Position);
if (mSpectrumByteOffset.Count > SpectrumCount)
SpectrumCount = mSpectrumByteOffset.Count;
var retryCount = MAX_RETRIES;
while (true)
{
try
{
// Write the scan number
mPageFileWriter.Write(scanNumber);
// Write the ion count
mPageFileWriter.Write(spectrumToCache.IonCount);
// Write the m/z values
for (var index = 0; index < spectrumToCache.IonCount; index++)
mPageFileWriter.Write(spectrumToCache.IonsMZ[index]);
// Write the intensity values
for (var index = 0; index < spectrumToCache.IonCount; index++)
mPageFileWriter.Write(spectrumToCache.IonsIntensity[index]);
// Write four blank bytes (not really necessary, but adds a little padding between spectra)
mPageFileWriter.Write(0);
break;
}
catch (Exception ex)
{
retryCount -= 1;
var message = string.Format("Error caching scan {0}: {1}", scanNumber, ex.Message);
if (retryCount >= 0)
{
OnWarningEvent(message);
// Wait 2, 4, or 8 seconds, then try again
var sleepSeconds = Math.Pow(2, MAX_RETRIES - retryCount);
System.Threading.Thread.Sleep((int)(sleepSeconds * 1000));
mPageFileWriter.BaseStream.Seek(initialOffset, SeekOrigin.Begin);
}
else
{
throw new Exception(message, ex);
}
}
}
}
/// <summary>
/// Close the page file
/// </summary>
public void ClosePageFile()
{
try
{
if (mPageFileReader != null)
{
mPageFileReader.Close();
mPageFileReader = null;
}
if (mPageFileWriter != null)
{
mPageFileWriter.Close();
mPageFileWriter = null;
}
}
catch (Exception)
{
// Ignore errors here
}
mSpectrumByteOffset = new Dictionary<int, long>();
GC.Collect();
GC.WaitForPendingFinalizers();
System.Threading.Thread.Sleep(500);
}
/// <summary>
/// Constructs the full path for the given spectrum file
/// </summary>
/// <returns>The file path, or an empty string if unable to validate the spectrum cache directory</returns>
private string ConstructCachedSpectrumPath()
{
if (!ValidateCachedSpectrumDirectory())
{
return string.Empty;
}
var randomGenerator = new Random();
// Create the cache file name, using both a timestamp and a random number between 1 and 9999
var baseName = SPECTRUM_CACHE_FILE_PREFIX +
DateTime.UtcNow.Hour + DateTime.UtcNow.Minute + DateTime.UtcNow.Second + DateTime.UtcNow.Millisecond +
randomGenerator.Next(1, 9999);
var fileName = baseName + SPECTRUM_CACHE_FILE_BASENAME_TERMINATOR + ".bin";
return Path.Combine(mCacheOptions.DirectoryPath, fileName);
}
/// <summary>
/// Looks for and deletes the spectrum cache files created by this instance of MASIC
/// Additionally, looks for and deletes spectrum cache files with modification dates more than SPECTRUM_CACHE_MAX_FILE_AGE_HOURS from the present
/// </summary>
public void DeleteSpectrumCacheFiles()
{
var fileDateTolerance = DateTime.UtcNow.Subtract(new TimeSpan(SPECTRUM_CACHE_MAX_FILE_AGE_HOURS, 0, 0));
try
{
// Delete the cached files for this instance of clsMasic
var filePathMatch = ConstructCachedSpectrumPath();
var charIndex = filePathMatch.IndexOf(SPECTRUM_CACHE_FILE_BASENAME_TERMINATOR, StringComparison.Ordinal);
if (charIndex < 0)
{
ReportError("charIndex was less than 0; this is unexpected in DeleteSpectrumCacheFiles");
return;
}
var basePath = filePathMatch.Substring(0, charIndex);
var cacheFiles = Directory.GetFiles(mCacheOptions.DirectoryPath, Path.GetFileName(basePath) + "*");
foreach (var cacheFile in cacheFiles)
{
File.Delete(cacheFile);
}
}
catch (Exception ex)
{
// Ignore errors here
ReportError("Error deleting cached spectrum files for this task", ex);
}
// Now look for old spectrum cache files
try
{
var filePathMatch = SPECTRUM_CACHE_FILE_PREFIX + "*" + SPECTRUM_CACHE_FILE_BASENAME_TERMINATOR + "*";
var spectrumFile = new FileInfo(Path.GetFullPath(ConstructCachedSpectrumPath()));
if (spectrumFile.Directory == null)
{
ReportWarning("Unable to determine the spectrum cache directory path in DeleteSpectrumCacheFiles; this is unexpected");
return;
}
foreach (var candidateFile in spectrumFile.Directory.GetFiles(filePathMatch))
{
if (candidateFile.LastWriteTimeUtc < fileDateTolerance)
{
candidateFile.Delete();
}
}
}
catch (Exception ex)
{
ReportError("Error deleting old cached spectrum files", ex);
}
}
/// <summary>
/// Checks the spectraPool for available capacity, caching the oldest item if full
/// </summary>
private void AddItemToSpectraPool(ScanMemoryCacheItem itemToAdd)
{
// Disk caching disabled: expand the size of the in-memory cache
if (spectraPool.Count >= mMaximumPoolLength &&
mCacheOptions.DiskCachingAlwaysDisabled)
{
// The pool is full, but disk caching is disabled, so we have to expand the pool
var newPoolLength = mMaximumPoolLength + 500;
var currentPoolLength = Math.Min(mMaximumPoolLength, spectraPool.Capacity);
mMaximumPoolLength = newPoolLength;
if (newPoolLength > currentPoolLength)
{
spectraPool.Capacity = mMaximumPoolLength;
}
}
// Need to cache the spectrum stored at mNextAvailablePoolIndex
// Removes the oldest spectrum from spectraPool
if (spectraPool.AddNew(itemToAdd, out var cacheItem))
{
// An item was removed from the spectraPool. Write it to the disk cache if needed.
if (cacheItem.CacheState == eCacheStateConstants.LoadedFromCache)
{
// Already cached previously, simply reset the slot
}
else if (cacheItem.CacheState == eCacheStateConstants.NeverCached &&
ValidatePageFileIO(true))
{
// Store all of the spectra in one large file
CacheSpectrumWork(cacheItem.Scan);
}
if (cacheItem.CacheState != eCacheStateConstants.UnusedSlot)
{
// Reset .ScanNumber, .IonCount, and .CacheState
cacheItem.Scan.Clear(0);
cacheItem.CacheState = eCacheStateConstants.UnusedSlot;
CacheEventCount += 1;
}
}
}
/// <summary>
/// Configures the spectra cache after all options have been set.
/// </summary>
public void InitializeSpectraPool()
{
mMaximumPoolLength = mCacheOptions.SpectraToRetainInMemory;
if (mMaximumPoolLength < 1)
mMaximumPoolLength = 1;
CacheEventCount = 0;
UnCacheEventCount = 0;
SpectraPoolHitEventCount = 0;
mDirectoryPathValidated = false;
ClosePageFile();
if (spectraPool == null || spectraPool.Capacity < mMaximumPoolLength)
{
if (mCacheOptions.DiskCachingAlwaysDisabled)
{
spectraPool = new MemoryCacheArray(mMaximumPoolLength);
}
else
{
spectraPool = new MemoryCacheLRU(mMaximumPoolLength);
}
}
else
{
spectraPool.Clear();
}
}
private void InitializeVariables()
{
mCacheOptions.Reset();
InitializeSpectraPool();
}
/// <summary>
/// Get default cache options
/// </summary>
/// <returns></returns>
public static clsSpectrumCacheOptions GetDefaultCacheOptions()
{
var udtCacheOptions = new clsSpectrumCacheOptions
{
DiskCachingAlwaysDisabled = false,
DirectoryPath = Path.GetTempPath(),
SpectraToRetainInMemory = 1000
};
return udtCacheOptions;
}
/// <summary>
/// Load the spectrum from disk and cache in SpectraPool
/// </summary>
/// <param name="scanNumber">Scan number to load</param>
/// <param name="msSpectrum">Output: spectrum for scan number</param>
/// <returns>True if successfully uncached, false if an error</returns>
private bool UnCacheSpectrum(int scanNumber, out clsMSSpectrum msSpectrum)
{
// Make sure we have a valid object
var cacheItem = new ScanMemoryCacheItem(new clsMSSpectrum(scanNumber), eCacheStateConstants.LoadedFromCache);
msSpectrum = cacheItem.Scan;
// Uncache the spectrum from disk
if (!UnCacheSpectrumWork(scanNumber, msSpectrum))
{
// Scan not found; use a blank mass spectrum
// Its cache state will be set to LoadedFromCache, which is ok, since we don't need to cache it to disk
msSpectrum.Clear(scanNumber);
}
cacheItem.CacheState = eCacheStateConstants.LoadedFromCache;
AddItemToSpectraPool(cacheItem);
return true;
}
/// <summary>
/// Load the spectrum from disk and cache in SpectraPool
/// </summary>
/// <param name="scanNumber">Scan number to load</param>
/// <param name="msSpectrum"><see cref="clsMSSpectrum"/> object to store data into; supplying 'null' is an exception.</param>
/// <returns>True if successfully uncached, false if an error</returns>
private bool UnCacheSpectrumWork(int scanNumber, clsMSSpectrum msSpectrum)
{
var success = false;
msSpectrum.Clear();
// All of the spectra are stored in one large file
// Lookup the byte offset for the given spectrum
if (ValidatePageFileIO(false) &&
mSpectrumByteOffset.ContainsKey(scanNumber))
{
var byteOffset = mSpectrumByteOffset[scanNumber];
// Make sure all previous spectra are flushed to disk
mPageFileWriter.Flush();
// Read the spectrum from the page file
mPageFileReader.BaseStream.Seek(byteOffset, SeekOrigin.Begin);
var scanNumberInCacheFile = mPageFileReader.ReadInt32();
var ionCount = mPageFileReader.ReadInt32();
if (scanNumberInCacheFile != scanNumber)
{
ReportWarning("Scan number In cache file doesn't agree with expected scan number in UnCacheSpectrum");
}
msSpectrum.Clear(scanNumber, ionCount);
// Optimization: byte read, Buffer.BlockCopy, and AddRange can be very efficient, and therefore faster than ReadDouble() and Add.
// It may require more memory, but it is all very short term, and should be removed by a level 1 garbage collection
var byteCount = ionCount * 8;
var byteBuffer = new byte[byteCount];
var dblBuffer = new double[ionCount];
mPageFileReader.Read(byteBuffer, 0, byteCount);
Buffer.BlockCopy(byteBuffer, 0, dblBuffer, 0, byteCount);
msSpectrum.IonsMZ.AddRange(dblBuffer);
mPageFileReader.Read(byteBuffer, 0, byteCount);
Buffer.BlockCopy(byteBuffer, 0, dblBuffer, 0, byteCount);
msSpectrum.IonsIntensity.AddRange(dblBuffer);
//for (var index = 0; index < ionCount; index++)
// msSpectrum.IonsMZ.Add(mPageFileReader.ReadDouble());
//
//for (var index = 0; index < ionCount; index++)
// msSpectrum.IonsIntensity.Add(mPageFileReader.ReadDouble());
UnCacheEventCount += 1;
success = true;
}
return success;
}
private bool ValidateCachedSpectrumDirectory()
{
if (string.IsNullOrWhiteSpace(mCacheOptions.DirectoryPath))
{
// Need to define the spectrum caching directory path
mCacheOptions.DirectoryPath = Path.GetTempPath();
mDirectoryPathValidated = false;
}
if (!mDirectoryPathValidated)
{
try
{
if (!Path.IsPathRooted(mCacheOptions.DirectoryPath))
{
mCacheOptions.DirectoryPath = Path.Combine(Path.GetTempPath(), mCacheOptions.DirectoryPath);
}
if (!Directory.Exists(mCacheOptions.DirectoryPath))
{
Directory.CreateDirectory(mCacheOptions.DirectoryPath);
if (!Directory.Exists(mCacheOptions.DirectoryPath))
{
ReportError("Error creating spectrum cache directory: " + mCacheOptions.DirectoryPath);
return false;
}
}
mDirectoryPathValidated = true;
return true;
}
catch (Exception ex)
{
// Error defining .DirectoryPath
ReportError("Error creating spectrum cache directory");
return false;
}
}
return true;
}
/// <summary>
/// Validates that we can read and write from a Page file
/// Opens the page file reader and writer if not yet opened
/// </summary>
/// <param name="createIfUninitialized"></param>
/// <returns></returns>
private bool ValidatePageFileIO(bool createIfUninitialized)
{
if (mPageFileReader != null)
{
return true;
}
if (!createIfUninitialized)
{
return false;
}
try
{
// Construct the page file path
var cacheFilePath = ConstructCachedSpectrumPath();
// Initialize the binary writer and create the file
mPageFileWriter = new BinaryWriter(new FileStream(cacheFilePath, FileMode.Create, FileAccess.Write, FileShare.Read));
// Write a header line
mPageFileWriter.Write(
"MASIC Spectrum Cache Page File. Created " + DateTime.Now.ToLongDateString() + " " +
DateTime.Now.ToLongTimeString());
// Add 64 bytes of white space
for (var index = 0; index <= 63; index++)
mPageFileWriter.Write(byte.MinValue);
mPageFileWriter.Flush();
// Initialize the binary reader
mPageFileReader = new BinaryReader(new FileStream(cacheFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 8192));
return true;
}
catch (Exception ex)
{
ReportError(ex.Message, ex);
return false;
}
}
/// <summary>
/// Get a spectrum from the pool or cache, potentially without updating the pool
/// </summary>
/// <param name="scanNumber">Scan number to load</param>
/// <param name="spectrum">The requested spectrum</param>
/// <param name="canSkipPool">if true and the spectrum is not in the pool, it will be read from the disk cache without updating the pool.
/// This should be true for any spectrum requests that are not likely to be repeated within the next <see cref="clsSpectrumCacheOptions.SpectraToRetainInMemory"/> requests.</param>
/// <returns>True if the scan was found in the spectrum pool (or was successfully added to the pool)</returns>
public bool GetSpectrum(int scanNumber, out clsMSSpectrum spectrum, bool canSkipPool = true)
{
try
{
if (spectraPool.GetItem(scanNumber, out var cacheItem))
{
SpectraPoolHitEventCount++;
spectrum = cacheItem.Scan;
return true;
}
if (!canSkipPool)
{
// Need to load the spectrum
var success = UnCacheSpectrum(scanNumber, out var cacheSpectrum);
spectrum = cacheSpectrum;
return success;
}
spectrum = new clsMSSpectrum(scanNumber);
UnCacheSpectrumWork(scanNumber, spectrum);
// Maintain functionality: return true, even if the spectrum was not in the cache file.
return true;
}
catch (Exception ex)
{
ReportError(ex.Message, ex);
spectrum = null;
return false;
}
}
/// <summary>
/// Container to group needed information in the in-memory cache
/// </summary>
private class ScanMemoryCacheItem
{
/// <summary>
/// Cache state
/// </summary>
public eCacheStateConstants CacheState { get; set; }
/// <summary>
/// Mass Spectrum
/// </summary>
public clsMSSpectrum Scan { get; }
/// <summary>
/// Constructor
/// </summary>
/// <param name="scan"></param>
/// <param name="cacheState"></param>
public ScanMemoryCacheItem(clsMSSpectrum scan, eCacheStateConstants cacheState = eCacheStateConstants.NeverCached)
{
Scan = scan;
CacheState = cacheState;
}
}
/// <summary>
/// Interface to allow choosing between two different in-memory cache implementations
/// </summary>
private interface IScanMemoryCache
{
/// <summary>
/// Number of items in the cache
/// </summary>
int Count { get; }
/// <summary>
/// Limit of items in the cache. Set will throw an exception if new value is smaller than <see cref="Count"/>
/// </summary>
int Capacity { get; set; }
/// <summary>
/// Retrieve the item for the scan number
/// </summary>
/// <param name="scanNumber"></param>
/// <param name="item"></param>
/// <returns>true if item available in cache</returns>
bool GetItem(int scanNumber, out ScanMemoryCacheItem item);
/// <summary>
/// Adds an item to the cache. Will not add duplicates. Will remove (and return) the oldest item if necessary.
/// </summary>
/// <param name="newItem">Item to add to the cache</param>
/// <param name="removedItem">Item removed from the cache, or default if remove not needed</param>
/// <returns>true if <paramref name="removedItem"/> is an item removed from the cache</returns>
bool AddNew(ScanMemoryCacheItem newItem, out ScanMemoryCacheItem removedItem);
/// <summary>
/// Clear all contents
/// </summary>
void Clear();
}
/// <summary>
/// Basic in-memory cache option. Less memory than an LRU implementation.
/// </summary>
private class MemoryCacheArray : IScanMemoryCache
{
private readonly List<ScanMemoryCacheItem> mMemoryCache;
private readonly Dictionary<int, int> mScanNumberToIndexMap;
private int mCapacity;
private int mLastIndex;
/// <summary>
/// Constructor
/// </summary>
/// <param name="initialCapacity"></param>
public MemoryCacheArray(int initialCapacity)
{
mCapacity = initialCapacity;
mMemoryCache = new List<ScanMemoryCacheItem>(initialCapacity);
mScanNumberToIndexMap = new Dictionary<int, int>(initialCapacity);
mLastIndex = -1;
Count = 0;
}
/// <summary>
/// Number of spectra in the cache
/// </summary>
public int Count { get; private set; }
/// <summary>
/// Spectrum cache capacity
/// </summary>
public int Capacity
{
get => mCapacity;
set
{
if (value < mMemoryCache.Count)
{
throw new ArgumentOutOfRangeException(nameof(value), "capacity was less than the current size.");
}
mCapacity = value;
mMemoryCache.Capacity = value;
}
}
/// <summary>
/// Get the spectrum for the given scan
/// </summary>
/// <param name="scanNumber"></param>
/// <param name="item"></param>
/// <returns></returns>
public bool GetItem(int scanNumber, out ScanMemoryCacheItem item)
{
if (!mScanNumberToIndexMap.TryGetValue(scanNumber, out var index))
{
item = null;
return false;
}
item = mMemoryCache[index];
return true;
}
/// <summary>
/// Add a new spectrum (and remove the oldest one if the cache is at capacity)
/// </summary>
/// <param name="newItem"></param>
/// <param name="removedItem"></param>
/// <returns></returns>
public bool AddNew(ScanMemoryCacheItem newItem, out ScanMemoryCacheItem removedItem)
{
var itemRemoved = RemoveOldestItem(out removedItem);
Add(newItem);
return itemRemoved;
}
/// <summary>
/// Add an item to the cache.
/// </summary>
/// <param name="newItem"></param>
/// <returns>true if the item could be added, false otherwise (like if the cache is already full)</returns>
private bool Add(ScanMemoryCacheItem newItem)
{
if (Count == mCapacity)
{
return false;
}
if (mScanNumberToIndexMap.ContainsKey(newItem.Scan.ScanNumber))
{
return true;
}
if (Count >= mCapacity)
{
mLastIndex++;
if (mLastIndex >= Count)
mLastIndex = 0;
//cache.Insert(mLastIndex, newItem);
mMemoryCache[mLastIndex] = newItem;
}
else
{
mLastIndex = mMemoryCache.Count;
mMemoryCache.Add(newItem);
}
mScanNumberToIndexMap.Add(newItem.Scan.ScanNumber, mLastIndex);
Count++;
return true;
}
/// <summary>
/// Remove the oldest item from the cache, but only if it is full
/// </summary>
/// <param name="oldItem">oldest item in the cache</param>
/// <returns>true if item was removed, false otherwise</returns>
private bool RemoveOldestItem(out ScanMemoryCacheItem oldItem)
{
if (Count < mCapacity)
{
oldItem = null;
return false;
}
if (mLastIndex == Count - 1)
{
oldItem = mMemoryCache[0];
}
else
{
oldItem = mMemoryCache[mLastIndex + 1];
}
mScanNumberToIndexMap.Remove(oldItem.Scan.ScanNumber);
Count--;
return true;
}
/// <summary>
/// Clear the cache
/// </summary>
public void Clear()
{
mMemoryCache.Clear();
mScanNumberToIndexMap.Clear();
mLastIndex = -1;
}
/// <summary>
/// Report the number of cached spectra, along with the cache capacity
/// </summary>
/// <returns></returns>
public override string ToString()
{
return string.Format(
"In-memory spectrum cache with {0} spectra cached (capacity {1})",
mMemoryCache.Count, mCapacity);
}
}
/// <summary>
/// LRU (Least-Recently-Used) cache implementation
/// </summary>
private class MemoryCacheLRU : IScanMemoryCache
{
private readonly LinkedList<ScanMemoryCacheItem> mMemoryCache;
private readonly Dictionary<int, LinkedListNode<ScanMemoryCacheItem>> mScanNumberToNodeMap;
private int mCapacity;
/// <summary>
/// Constructor
/// </summary>
/// <param name="initialCapacity"></param>
public MemoryCacheLRU(int initialCapacity)
{
mCapacity = initialCapacity;
mMemoryCache = new LinkedList<ScanMemoryCacheItem>();
mScanNumberToNodeMap = new Dictionary<int, LinkedListNode<ScanMemoryCacheItem>>(initialCapacity);
}
/// <summary>
/// Number of spectra in the cache
/// </summary>
public int Count => mMemoryCache.Count;
/// <summary>
/// Spectrum cache capacity
/// </summary>
public int Capacity
{
get => mCapacity;
set
{
if (value < mMemoryCache.Count)
{
throw new ArgumentOutOfRangeException(nameof(value), "capacity was less than the current size.");
}
mCapacity = value;
}
}
/// <summary>
/// Get the spectrum for the given scan
/// </summary>
/// <param name="scanNumber"></param>
/// <param name="item"></param>
/// <returns></returns>
public bool GetItem(int scanNumber, out ScanMemoryCacheItem item)
{
if (!mScanNumberToNodeMap.TryGetValue(scanNumber, out var node))
{
item = null;
return false;
}
item = node.Value;
// LRU management
mMemoryCache.Remove(node); // O(1)
mMemoryCache.AddLast(node); // O(1)
return true;