2014-10-22 18:36:49 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
2014-10-27 18:22:25 +08:00
|
|
|
|
namespace Wox.Plugin.FindFile.MFTSearch
|
2014-10-22 18:36:49 +08:00
|
|
|
|
{
|
|
|
|
|
internal class MFTSearcherCache
|
|
|
|
|
{
|
2014-10-27 18:22:25 +08:00
|
|
|
|
public Dictionary<string, Dictionary<ulong, USNRecord>> VolumeRecords = new Dictionary<string, Dictionary<ulong, USNRecord>>();
|
2014-10-22 18:36:49 +08:00
|
|
|
|
|
|
|
|
|
public MFTSearcherCache() { }
|
|
|
|
|
|
2014-10-23 23:23:40 +08:00
|
|
|
|
public bool ContainsVolume(string volume)
|
2014-10-22 18:36:49 +08:00
|
|
|
|
{
|
2014-10-27 18:22:25 +08:00
|
|
|
|
return VolumeRecords.ContainsKey(volume);
|
2014-10-22 18:36:49 +08:00
|
|
|
|
}
|
2014-10-27 18:22:25 +08:00
|
|
|
|
|
|
|
|
|
public void AddRecord(string volume, List<USNRecord> r)
|
2014-10-22 18:36:49 +08:00
|
|
|
|
{
|
2014-11-03 18:49:18 +08:00
|
|
|
|
EnsureVolumeExistInHashTable(volume);
|
2014-10-27 18:22:25 +08:00
|
|
|
|
r.ForEach(x => VolumeRecords[volume].Add(x.FRN, x));
|
2014-10-22 18:36:49 +08:00
|
|
|
|
}
|
2014-10-27 18:22:25 +08:00
|
|
|
|
|
|
|
|
|
public void AddRecord(string volume, USNRecord record)
|
2014-10-23 23:23:40 +08:00
|
|
|
|
{
|
2014-11-03 18:49:18 +08:00
|
|
|
|
EnsureVolumeExistInHashTable(volume);
|
|
|
|
|
if (!VolumeRecords[volume].ContainsKey(record.FRN))
|
|
|
|
|
{
|
|
|
|
|
VolumeRecords[volume].Add(record.FRN, record);
|
|
|
|
|
}
|
2014-10-23 23:23:40 +08:00
|
|
|
|
}
|
2014-10-27 18:22:25 +08:00
|
|
|
|
|
2014-11-03 18:49:18 +08:00
|
|
|
|
public void EnsureVolumeExistInHashTable(string volume)
|
2014-10-22 18:36:49 +08:00
|
|
|
|
{
|
2014-10-27 18:22:25 +08:00
|
|
|
|
if (!VolumeRecords.ContainsKey(volume))
|
|
|
|
|
VolumeRecords.Add(volume, new Dictionary<ulong, USNRecord>());
|
2014-10-22 18:36:49 +08:00
|
|
|
|
}
|
2014-10-27 18:22:25 +08:00
|
|
|
|
|
2014-10-23 23:23:40 +08:00
|
|
|
|
public bool DeleteRecord(string volume, ulong frn)
|
2014-10-22 18:36:49 +08:00
|
|
|
|
{
|
2014-10-23 23:23:40 +08:00
|
|
|
|
bool result = false;
|
2014-10-27 18:22:25 +08:00
|
|
|
|
result = DeleteRecordHashTableItem(VolumeRecords, volume, frn);
|
2014-10-23 23:23:40 +08:00
|
|
|
|
return result;
|
|
|
|
|
}
|
2014-10-27 18:22:25 +08:00
|
|
|
|
|
2014-10-23 23:23:40 +08:00
|
|
|
|
private bool DeleteRecordHashTableItem(Dictionary<string, Dictionary<ulong, USNRecord>> hashtable, string volume, ulong frn)
|
|
|
|
|
{
|
|
|
|
|
if (hashtable.ContainsKey(volume) && hashtable[volume].ContainsKey(frn))
|
|
|
|
|
{
|
|
|
|
|
hashtable[volume].Remove(frn);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
2014-10-22 18:36:49 +08:00
|
|
|
|
{
|
2014-10-23 23:23:40 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-10-27 18:22:25 +08:00
|
|
|
|
|
|
|
|
|
public void UpdateRecord(string volume, USNRecord record)
|
2014-10-23 23:23:40 +08:00
|
|
|
|
{
|
2014-10-27 18:22:25 +08:00
|
|
|
|
RealUpdateRecord(volume, VolumeRecords, record);
|
2014-10-23 23:23:40 +08:00
|
|
|
|
}
|
2014-10-27 18:22:25 +08:00
|
|
|
|
|
2014-10-23 23:23:40 +08:00
|
|
|
|
private bool RealUpdateRecord(string volume, Dictionary<string, Dictionary<ulong, USNRecord>> source, USNRecord record)
|
|
|
|
|
{
|
|
|
|
|
if (source.ContainsKey(volume) && source[volume].ContainsKey(record.FRN))
|
|
|
|
|
{
|
|
|
|
|
source[volume][record.FRN] = record;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return false;
|
2014-10-22 18:36:49 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-10-27 18:22:25 +08:00
|
|
|
|
|
2014-11-03 17:30:48 +08:00
|
|
|
|
public List<USNRecord> FindByName(string filename, long maxResult = -1)
|
2014-10-22 18:36:49 +08:00
|
|
|
|
{
|
2014-10-23 23:23:40 +08:00
|
|
|
|
List<USNRecord> result = new List<USNRecord>();
|
2014-10-23 18:39:11 +08:00
|
|
|
|
|
2014-11-03 17:30:48 +08:00
|
|
|
|
foreach (Dictionary<ulong, USNRecord> 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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-10-23 23:23:40 +08:00
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2014-10-27 18:22:25 +08:00
|
|
|
|
|
2014-10-23 23:23:40 +08:00
|
|
|
|
public USNRecord FindByFrn(string volume, ulong frn)
|
|
|
|
|
{
|
2014-10-27 18:22:25 +08:00
|
|
|
|
if ((!VolumeRecords.ContainsKey(volume)))
|
2014-10-23 23:23:40 +08:00
|
|
|
|
throw new Exception(string.Format("DB not contain the volume: {0}", volume));
|
|
|
|
|
USNRecord result = null;
|
2014-10-27 18:22:25 +08:00
|
|
|
|
VolumeRecords[volume].TryGetValue(frn, out result);
|
2014-10-23 23:23:40 +08:00
|
|
|
|
return result;
|
|
|
|
|
}
|
2014-10-27 18:22:25 +08:00
|
|
|
|
|
|
|
|
|
public long RecordsCount
|
2014-10-22 18:36:49 +08:00
|
|
|
|
{
|
2014-10-27 18:22:25 +08:00
|
|
|
|
get { return VolumeRecords.Sum(x => x.Value.Count); }
|
2014-10-22 18:36:49 +08:00
|
|
|
|
}
|
2014-10-27 18:22:25 +08:00
|
|
|
|
|
|
|
|
|
public Dictionary<ulong, USNRecord> GetVolumeRecords(string volume)
|
2014-10-22 18:36:49 +08:00
|
|
|
|
{
|
2014-10-23 23:23:40 +08:00
|
|
|
|
Dictionary<ulong, USNRecord> result = null;
|
2014-10-27 18:22:25 +08:00
|
|
|
|
VolumeRecords.TryGetValue(volume, out result);
|
2014-10-23 23:23:40 +08:00
|
|
|
|
return result;
|
2014-10-22 18:36:49 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|