using System; using System.Collections.Generic; using System.Linq; namespace Wox.Plugin.FindFile.MFTSearch { internal class MFTSearcherCache { public Dictionary> VolumeRecords = new Dictionary>(); public MFTSearcherCache() { } public bool ContainsVolume(string volume) { return VolumeRecords.ContainsKey(volume); } public void AddRecord(string volume, List r) { CheckHashTableKey(volume); r.ForEach(x => VolumeRecords[volume].Add(x.FRN, x)); } public void AddRecord(string volume, USNRecord record) { CheckHashTableKey(volume); VolumeRecords[volume].Add(record.FRN, record); } public void CheckHashTableKey(string volume) { if (!VolumeRecords.ContainsKey(volume)) VolumeRecords.Add(volume, new Dictionary()); } public bool DeleteRecord(string volume, ulong frn) { bool result = false; result = DeleteRecordHashTableItem(VolumeRecords, volume, frn); return result; } private bool DeleteRecordHashTableItem(Dictionary> hashtable, string volume, ulong frn) { if (hashtable.ContainsKey(volume) && hashtable[volume].ContainsKey(frn)) { hashtable[volume].Remove(frn); return true; } else { return false; } } public void UpdateRecord(string volume, USNRecord record) { RealUpdateRecord(volume, VolumeRecords, record); } private bool RealUpdateRecord(string volume, Dictionary> source, USNRecord record) { if (source.ContainsKey(volume) && source[volume].ContainsKey(record.FRN)) { source[volume][record.FRN] = record; return true; } else { return false; } } public List FindByName(string filename, long maxResult = -1) { List result = new List(); foreach (Dictionary dictionary in VolumeRecords.Values) { foreach (var usnRecord in dictionary) { if (usnRecord.Value.Name.IndexOf(filename, StringComparison.OrdinalIgnoreCase) >= 0) { result.Add(usnRecord.Value); if (maxResult > 0 && result.Count() >= maxResult) break; } if (maxResult > 0 && result.Count() >= maxResult) break; } } return result; } public USNRecord FindByFrn(string volume, ulong frn) { if ((!VolumeRecords.ContainsKey(volume))) throw new Exception(string.Format("DB not contain the volume: {0}", volume)); USNRecord result = null; VolumeRecords[volume].TryGetValue(frn, out result); return result; } public long RecordsCount { get { return VolumeRecords.Sum(x => x.Value.Count); } } public Dictionary GetVolumeRecords(string volume) { Dictionary result = null; VolumeRecords.TryGetValue(volume, out result); return result; } } }