mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-07 11:46:30 +02:00
Reduce memory usage.
This commit is contained in:
@@ -1,59 +1,45 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using MyEverything;
|
||||
|
||||
namespace Wox.Infrastructure.MFTSearch
|
||||
namespace Wox.Plugin.FindFile.MFTSearch
|
||||
{
|
||||
internal class MFTSearcherCache
|
||||
{
|
||||
private Dictionary<string, Dictionary<ulong, USNRecord>> _volumes_files = new Dictionary<string, Dictionary<ulong, USNRecord>>();
|
||||
private Dictionary<string, Dictionary<ulong, USNRecord>> _volumes_folders = new Dictionary<string, Dictionary<ulong, USNRecord>>();
|
||||
public Dictionary<string, Dictionary<ulong, USNRecord>> VolumeRecords = new Dictionary<string, Dictionary<ulong, USNRecord>>();
|
||||
|
||||
public MFTSearcherCache() { }
|
||||
|
||||
public bool ContainsVolume(string volume)
|
||||
{
|
||||
return _volumes_files.ContainsKey(volume) && _volumes_folders.ContainsKey(volume);
|
||||
return VolumeRecords.ContainsKey(volume);
|
||||
}
|
||||
public void AddRecord(string volume, List<USNRecord> r, USNRecordType type)
|
||||
|
||||
public void AddRecord(string volume, List<USNRecord> r)
|
||||
{
|
||||
if (type == USNRecordType.File)
|
||||
{
|
||||
CheckHashTableKey(_volumes_files, volume);
|
||||
r.ForEach(x => _volumes_files[volume].Add(x.FRN, x));
|
||||
}
|
||||
else
|
||||
{
|
||||
CheckHashTableKey(_volumes_folders, volume);
|
||||
r.ForEach(x => _volumes_folders[volume].Add(x.FRN, x));
|
||||
}
|
||||
CheckHashTableKey(volume);
|
||||
r.ForEach(x => VolumeRecords[volume].Add(x.FRN, x));
|
||||
}
|
||||
public void AddRecord(string volume, USNRecord record, USNRecordType type)
|
||||
|
||||
public void AddRecord(string volume, USNRecord record)
|
||||
{
|
||||
if (type == USNRecordType.File)
|
||||
{
|
||||
CheckHashTableKey(_volumes_files, volume);
|
||||
_volumes_files[volume].Add(record.FRN, record);
|
||||
}
|
||||
else
|
||||
{
|
||||
CheckHashTableKey(_volumes_folders, volume);
|
||||
_volumes_folders[volume].Add(record.FRN, record);
|
||||
}
|
||||
CheckHashTableKey(volume);
|
||||
VolumeRecords[volume].Add(record.FRN, record);
|
||||
}
|
||||
private void CheckHashTableKey(Dictionary<string, Dictionary<ulong, USNRecord>> hashtable, string key)
|
||||
|
||||
public void CheckHashTableKey(string volume)
|
||||
{
|
||||
if (!hashtable.ContainsKey(key))
|
||||
hashtable.Add(key, new Dictionary<ulong, USNRecord>());
|
||||
if (!VolumeRecords.ContainsKey(volume))
|
||||
VolumeRecords.Add(volume, new Dictionary<ulong, USNRecord>());
|
||||
}
|
||||
|
||||
public bool DeleteRecord(string volume, ulong frn)
|
||||
{
|
||||
bool result = false;
|
||||
result = DeleteRecordHashTableItem(_volumes_files, volume, frn);
|
||||
if (!result) result = DeleteRecordHashTableItem(_volumes_folders, volume, frn);
|
||||
result = DeleteRecordHashTableItem(VolumeRecords, volume, frn);
|
||||
return result;
|
||||
}
|
||||
|
||||
private bool DeleteRecordHashTableItem(Dictionary<string, Dictionary<ulong, USNRecord>> hashtable, string volume, ulong frn)
|
||||
{
|
||||
if (hashtable.ContainsKey(volume) && hashtable[volume].ContainsKey(frn))
|
||||
@@ -66,13 +52,12 @@ namespace Wox.Infrastructure.MFTSearch
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public void UpdateRecord(string volume, USNRecord record, USNRecordType type)
|
||||
|
||||
public void UpdateRecord(string volume, USNRecord record)
|
||||
{
|
||||
if (type == USNRecordType.File)
|
||||
RealUpdateRecord(volume, _volumes_files, record);
|
||||
else
|
||||
RealUpdateRecord(volume, _volumes_folders, record);
|
||||
RealUpdateRecord(volume, VolumeRecords, record);
|
||||
}
|
||||
|
||||
private bool RealUpdateRecord(string volume, Dictionary<string, Dictionary<ulong, USNRecord>> source, USNRecord record)
|
||||
{
|
||||
if (source.ContainsKey(volume) && source[volume].ContainsKey(record.FRN))
|
||||
@@ -85,48 +70,40 @@ namespace Wox.Infrastructure.MFTSearch
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public List<USNRecord> FindByName(string filename)
|
||||
{
|
||||
filename = filename.ToLower();
|
||||
var fileQuery = from filesInVolumeDic in _volumes_files.Values
|
||||
var fileQuery = from filesInVolumeDic in VolumeRecords.Values
|
||||
from eachFilePair in filesInVolumeDic
|
||||
where eachFilePair.Value.Name.ToLower().Contains(filename)
|
||||
select eachFilePair.Value;
|
||||
|
||||
var folderQuery = from fldsInVolumeDic in _volumes_folders.Values
|
||||
from eachFldPair in fldsInVolumeDic
|
||||
where eachFldPair.Value.Name.ToLower().Contains(filename)
|
||||
select eachFldPair.Value;
|
||||
|
||||
List<USNRecord> result = new List<USNRecord>();
|
||||
|
||||
result.AddRange(fileQuery);
|
||||
result.AddRange(folderQuery);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public USNRecord FindByFrn(string volume, ulong frn)
|
||||
{
|
||||
if ((!_volumes_files.ContainsKey(volume)) || (!_volumes_folders.ContainsKey(volume)))
|
||||
if ((!VolumeRecords.ContainsKey(volume)))
|
||||
throw new Exception(string.Format("DB not contain the volume: {0}", volume));
|
||||
USNRecord result = null;
|
||||
_volumes_files[volume].TryGetValue(frn, out result);
|
||||
if (result != null) return result;
|
||||
_volumes_folders[volume].TryGetValue(frn, out result);
|
||||
VolumeRecords[volume].TryGetValue(frn, out result);
|
||||
return result;
|
||||
}
|
||||
public long FileCount
|
||||
|
||||
public long RecordsCount
|
||||
{
|
||||
get { return _volumes_files.Sum(x => x.Value.Count); }
|
||||
get { return VolumeRecords.Sum(x => x.Value.Count); }
|
||||
}
|
||||
public long FolderCount
|
||||
{
|
||||
get { return _volumes_folders.Sum(x => x.Value.Count); }
|
||||
}
|
||||
public Dictionary<ulong, USNRecord> GetFolderSource(string volume)
|
||||
|
||||
public Dictionary<ulong, USNRecord> GetVolumeRecords(string volume)
|
||||
{
|
||||
Dictionary<ulong, USNRecord> result = null;
|
||||
_volumes_folders.TryGetValue(volume, out result);
|
||||
VolumeRecords.TryGetValue(volume, out result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user