This repository has been archived by the owner on Sep 13, 2023. It is now read-only.
forked from chriseldredge/NuGet.Lucene
-
Notifications
You must be signed in to change notification settings - Fork 32
/
IndexDifferenceCalculator.cs
89 lines (72 loc) · 3.61 KB
/
IndexDifferenceCalculator.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using NuGet.Lucene.Util;
namespace NuGet.Lucene
{
public class IndexDifferenceCalculator
{
private readonly IFileSystem fileSystem;
private readonly string[] fileSystemPackages;
private readonly Dictionary<string, LucenePackage> indexedPackagesByPath;
private IndexDifferenceCalculator(IFileSystem fileSystem, string[] fileSystemPackages, Dictionary<string, LucenePackage> indexedPackagesByPath)
{
this.fileSystem = fileSystem;
this.fileSystemPackages = fileSystemPackages;
this.indexedPackagesByPath = indexedPackagesByPath;
}
public static IndexDifferences FindDifferences(
IFileSystem fileSystem,
IEnumerable<LucenePackage> indexedPackages,
CancellationToken cancellationToken)
{
return FindDifferences(fileSystem, indexedPackages, cancellationToken, _ => { }, SynchronizationMode.Incremental);
}
public static IndexDifferences FindDifferences(
IFileSystem fileSystem,
IEnumerable<LucenePackage> indexedPackages,
CancellationToken cancellationToken,
Action<SynchronizationState> setState,
SynchronizationMode mode)
{
setState(SynchronizationState.ScanningFiles);
var fileSystemPackages = fileSystem.GetFiles(string.Empty, "*" + Constants.PackageExtension, true)
.WithCancellation(cancellationToken)
.Where(file => !fileSystem.IsTempFile(file))
.ToArray();
setState(SynchronizationState.ScanningIndex);
var indexedPackagesByPath = indexedPackages
.WithCancellation(cancellationToken)
.ToDictionary(pkg => pkg.Path, StringComparer.InvariantCultureIgnoreCase);
setState(SynchronizationState.Comparing);
var calc = new IndexDifferenceCalculator(fileSystem, fileSystemPackages, indexedPackagesByPath);
return calc.Calculate(mode);
}
private IndexDifferences Calculate(SynchronizationMode mode)
{
var newPackages = fileSystemPackages.Except(indexedPackagesByPath.Keys, StringComparer.InvariantCultureIgnoreCase);
var missingPackages = indexedPackagesByPath.Keys.Except(fileSystemPackages, StringComparer.InvariantCultureIgnoreCase);
var modifiedPackages = fileSystemPackages.Intersect(indexedPackagesByPath.Keys, StringComparer.InvariantCultureIgnoreCase);
if (mode == SynchronizationMode.Incremental)
{
modifiedPackages = modifiedPackages.Where(ModifiedDateMismatch);
}
return new IndexDifferences(newPackages.ToList(), missingPackages.ToList(), modifiedPackages.ToList());
}
private bool ModifiedDateMismatch(string path)
{
var lucenePackage = indexedPackagesByPath[path];
if (!lucenePackage.Published.HasValue)
{
return true;
}
return TimestampsMismatch(lucenePackage, fileSystem.GetLastModified(path));
}
internal static bool TimestampsMismatch(LucenePackage lucenePackage, DateTimeOffset fileLastModified)
{
var diff = fileLastModified - lucenePackage.Published.GetValueOrDefault(DateTimeOffset.MinValue);
return Math.Abs(diff.TotalSeconds) > 1;
}
}
}