Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
Changes per PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
pranavkm committed Jan 29, 2015
1 parent e2c1c1f commit 5fbf802
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private CompilerCacheEntry GetOrAdd(RelativeFileInfo relativeFileInfo,

// Timestamp doesn't match but it might be because of deployment, compare the hash.
if (cacheEntry.IsPreCompiled &&
cacheEntry.Hash == RazorFileHash.GetHash(fileInfo))
cacheEntry.Hash == RazorFileHash.GetHash(fileInfo, cacheEntry.HashAlgorithmVersion))
{
// Cache hit, but we need to update the entry.
// Assigning to LastModified is an atomic operation and will result in a safe race if it is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public CompilerCacheEntry([NotNull] RazorFileInfo info, [NotNull] Type compiledT
Length = info.Length;
LastModified = info.LastModified;
Hash = info.Hash;
HashAlgorithmVersion = info.HashAlgorithmVersion;
IsPreCompiled = true;
}

Expand Down Expand Up @@ -63,6 +64,14 @@ public CompilerCacheEntry([NotNull] RelativeFileInfo info, [NotNull] Type compil
/// </summary>
public long Hash { get; }

/// <summary>
/// Gets the version of the hash algorithm used to generate <see cref="Hash"/>.
/// </summary>
/// <remarks>
/// This value is only initialized for precompiled views.
/// </remarks>
public int HashAlgorithmVersion { get; }

/// <summary>
/// Gets a flag that indicates if the file is precompiled.
/// </summary>
Expand Down
16 changes: 16 additions & 0 deletions src/Microsoft.AspNet.Mvc.Razor/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,10 @@ public class RazorFileInfo
/// A hash of the file content.
/// </summary>
public long Hash { get; set; }

/// <summary>
/// The version of hash algorithm used to generate <see cref="Hash"/>.
/// </summary>
public int HashAlgorithmVersion { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ protected virtual string GenerateFile([NotNull] RazorFileInfo fileInfo)
fileInfo.Length,
fileInfo.RelativePath,
fileInfo.FullTypeName,
fileInfo.Hash);
fileInfo.Hash,
fileInfo.HashAlgorithmVersion);
}

protected virtual string Top
Expand Down Expand Up @@ -104,6 +105,7 @@ protected virtual string FileFormat
" + nameof(RazorFileInfo.RelativePath) + @" = @""{2}"",
" + nameof(RazorFileInfo.FullTypeName) + @" = @""{3}"",
" + nameof(RazorFileInfo.Hash) + @" = {4},
" + nameof(RazorFileInfo.HashAlgorithmVersion) + @" = {5},
}};
fileInfos.Add(info);
";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ protected virtual PrecompilationCacheEntry GetCacheEntry([NotNull] RelativeFileI

if (fullTypeName != null)
{
var hash = RazorFileHash.GetHash(fileInfo.FileInfo);
var hash = RazorFileHash.GetHash(fileInfo.FileInfo, RazorFileHash.HashAlgorthmVersion1);
var razorFileInfo = new RazorFileInfo
{
RelativePath = fileInfo.RelativePath,
Expand Down
21 changes: 13 additions & 8 deletions src/Microsoft.AspNet.Mvc.Razor/Razor/RazorFileHash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,30 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.IO;
using Microsoft.AspNet.FileProviders;

namespace Microsoft.AspNet.Mvc.Razor
{
public static class RazorFileHash
{
public static long GetHash([NotNull] IFileInfo file)
/// <summary>
/// Version 1 of the hash algorithm used for generating hashes of Razor files.
/// </summary>
public static readonly int HashAlgorthmVersion1 = 1;

public static long GetHash([NotNull] IFileInfo file, int hashAlgorithmVersion)
{
if (hashAlgorithmVersion != HashAlgorthmVersion1)
{
throw new ArgumentException(Resources.RazorHash_UnsupportedHashAlgorithm,
nameof(hashAlgorithmVersion));
}

try
{
using (var stream = file.CreateReadStream())
{
return GetHash(stream);
return Crc32.Calculate(stream);
}
}
catch (Exception)
Expand All @@ -24,10 +34,5 @@ public static long GetHash([NotNull] IFileInfo file)
return 0;
}
}

internal static long GetHash(Stream stream)
{
return Crc32.Calculate(stream);
}
}
}
3 changes: 3 additions & 0 deletions src/Microsoft.AspNet.Mvc.Razor/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,7 @@
<data name="ViewMustBeContextualized" xml:space="preserve">
<value>The '{0}' method must be called before '{1}' can be invoked.</value>
</data>
<data name="RazorHash_UnsupportedHashAlgorithm" xml:space="preserve">
<value>Unsupported hash algorithm.</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ public ViewCollection()
Add(new RazorFileInfo()
{
FullTypeName = typeof(PreCompile).FullName,
Hash = RazorFileHash.GetHash(GetMemoryStream(content)),
Hash = Crc32.Calculate(GetMemoryStream(content)),
HashAlgorithmVersion = 1,
LastModified = DateTime.FromFileTimeUtc(10000),
Length = length,
RelativePath = "ab",
Expand Down Expand Up @@ -127,7 +128,8 @@ public void GetOrAdd_UsesFilesFromCache_IfTimestampDiffers_ButContentAndLengthAr
var razorFileInfo = new RazorFileInfo
{
FullTypeName = typeof(PreCompile).FullName,
Hash = RazorFileHash.GetHash(GetMemoryStream(precompiledContent)),
Hash = Crc32.Calculate(GetMemoryStream(precompiledContent)),
HashAlgorithmVersion = 1,
LastModified = DateTime.FromFileTimeUtc(10000),
Length = Encoding.UTF8.GetByteCount(precompiledContent),
RelativePath = "ab",
Expand Down Expand Up @@ -169,7 +171,8 @@ public void GetOrAdd_RecompilesFile_IfContentAndLengthAreChanged(
var razorFileInfo = new RazorFileInfo
{
FullTypeName = typeof(PreCompile).FullName,
Hash = RazorFileHash.GetHash(GetMemoryStream(precompiledContent)),
Hash = Crc32.Calculate(GetMemoryStream(precompiledContent)),
HashAlgorithmVersion = 1,
LastModified = DateTime.FromFileTimeUtc(10000),
Length = Encoding.UTF8.GetByteCount(precompiledContent),
RelativePath = "ab",
Expand Down Expand Up @@ -210,7 +213,8 @@ public void GetOrAdd_UsesValueFromCache_IfViewStartHasNotChanged()
fileProvider.AddFile("_ViewStart.cshtml", viewStartFileInfo);
var viewStartRazorFileInfo = new RazorFileInfo
{
Hash = RazorFileHash.GetHash(GetMemoryStream(viewStartContent)),
Hash = Crc32.Calculate(GetMemoryStream(viewStartContent)),
HashAlgorithmVersion = 1,
LastModified = viewStartFileInfo.LastModified,
Length = viewStartFileInfo.Length,
RelativePath = "_ViewStart.cshtml",
Expand Down Expand Up @@ -295,7 +299,8 @@ public void GetOrAdd_IgnoresCachedValueIfFileIsIdentical_ButViewStartWasDeletedS
FullTypeName = typeof(RuntimeCompileIdentical).FullName,
RelativePath = viewStartFileInfo.PhysicalPath,
LastModified = viewStartFileInfo.LastModified,
Hash = RazorFileHash.GetHash(viewStartFileInfo),
Hash = RazorFileHash.GetHash(viewStartFileInfo, hashAlgorithmVersion: 1),
HashAlgorithmVersion = 1,
Length = viewStartFileInfo.Length
};
fileProvider.AddFile(viewStartFileInfo.PhysicalPath, viewStartFileInfo);
Expand Down Expand Up @@ -332,7 +337,8 @@ public static IEnumerable<object[]> GetOrAdd_IgnoresCachedValue_IfViewStartWasCh

var razorFileInfo = new RazorFileInfo
{
Hash = RazorFileHash.GetHash(contentStream),
Hash = Crc32.Calculate(contentStream),
HashAlgorithmVersion = 1,
LastModified = lastModified,
Length = length,
RelativePath = path
Expand Down Expand Up @@ -381,7 +387,8 @@ public void GetOrAdd_IgnoresCachedValue_IfViewStartWasChangedSinceCacheWasCreate
var razorFileInfo = new RazorFileInfo
{
FullTypeName = typeof(PreCompile).FullName,
Hash = RazorFileHash.GetHash(fileInfo),
Hash = RazorFileHash.GetHash(fileInfo, hashAlgorithmVersion: 1),
HashAlgorithmVersion = 1,
LastModified = lastModified,
Length = Encoding.UTF8.GetByteCount(content),
RelativePath = fileInfo.PhysicalPath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.AspNet.FileProviders;
using Microsoft.AspNet.Testing;
using Moq;
using Xunit;

namespace Microsoft.AspNet.Mvc.Razor
{
public class RazorHashTest
public class RazorFileHashTest
{
public static IEnumerable<object[]> GetHashCodeData
{
Expand Down Expand Up @@ -52,9 +54,25 @@ from readBytes in bytesToRead
}
}

[Theory]
[InlineData(-1)]
[InlineData(0)]
[InlineData(2)]
[InlineData(14)]
public void GetHash_ThrowsIfHashAlgorithmVersionIsUnknown(int hashAlgorithmVersion)
{
// Arrange
var file = new TestFileInfo();

// Act and Assert
ExceptionAssert.ThrowsArgument(() => RazorFileHash.GetHash(file, hashAlgorithmVersion),
"hashAlgorithmVersion",
"Unsupported hash algorithm.");
}

[Theory]
[MemberData(nameof(GetHashCodeData))]
public void GetHashCode_CalculatesHashCodeForFile(int bytesToRead, string content, long expected)
public void GetHash_CalculatesHashCodeForFile(int bytesToRead, string content, long expected)
{
// Arrange
var bytes = Encoding.UTF8.GetBytes(content);
Expand All @@ -63,13 +81,13 @@ public void GetHashCode_CalculatesHashCodeForFile(int bytesToRead, string conten
.Returns(new SlowStream(bytes, bytesToRead));

// Act
var result = RazorFileHash.GetHash(file.Object);
var result = RazorFileHash.GetHash(file.Object, hashAlgorithmVersion: 1);

// Assert
Assert.Equal(expected, result);
}

private class SlowStream : System.IO.MemoryStream
private class SlowStream : MemoryStream
{
private readonly int _bytesToRead;

Expand Down

0 comments on commit 5fbf802

Please sign in to comment.