Skip to content

Commit

Permalink
Add objectType to GitPackCache's key
Browse files Browse the repository at this point in the history
  • Loading branch information
georg-jung committed May 12, 2023
1 parent 0b53771 commit d0aa014
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/NerdBank.GitVersioning/ManagedGit/GitPack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public Stream GetObject(long offset, string objectType)
}
#endif

if (this.cache.TryOpen(offset, out Stream? stream))
if (this.cache.TryOpen(offset, objectType, out Stream? stream))
{
return stream!;
}
Expand Down Expand Up @@ -216,7 +216,7 @@ public Stream GetObject(long offset, string objectType)
throw;
}

return this.cache.Add(offset, objectStream);
return this.cache.Add(offset, objectType, objectStream);
}

/// <summary>
Expand Down
6 changes: 4 additions & 2 deletions src/NerdBank.GitVersioning/ManagedGit/GitPackCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ public abstract class GitPackCache : IDisposable
/// <param name="offset">
/// The offset of the Git object in the Git pack.
/// </param>
/// <param name="objectType">The object type of the object to retrieve.</param>
/// <param name="stream">
/// A <see cref="Stream"/> which will be set to the cached Git object.
/// </param>
/// <returns>
/// <see langword="true"/> if the object was found in cache; otherwise,
/// <see langword="false"/>.
/// </returns>
public abstract bool TryOpen(long offset, [NotNullWhen(true)] out Stream? stream);
public abstract bool TryOpen(long offset, string objectType, [NotNullWhen(true)] out Stream? stream);

/// <summary>
/// Gets statistics about the cache usage.
Expand All @@ -45,14 +46,15 @@ public abstract class GitPackCache : IDisposable
/// <param name="offset">
/// The offset of the Git object in the Git pack.
/// </param>
/// <param name="objectType">The object type of the object to add to the cache.</param>
/// <param name="stream">
/// A <see cref="Stream"/> which represents the object to add. This stream
/// will be copied to the cache.
/// </param>
/// <returns>
/// A <see cref="Stream"/> which represents the cached entry.
/// </returns>
public abstract Stream Add(long offset, Stream stream);
public abstract Stream Add(long offset, string objectType, Stream stream);

/// <inheritdoc/>
public void Dispose()
Expand Down
14 changes: 7 additions & 7 deletions src/NerdBank.GitVersioning/ManagedGit/GitPackMemoryCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,27 @@ namespace Nerdbank.GitVersioning.ManagedGit;
/// twice, it is read from the <see cref="MemoryStream"/>, rather than the underlying <see cref="Stream"/>.
/// </para>
/// <para>
/// <see cref="Add(long, Stream)"/> and <see cref="TryOpen(long, out Stream?)"/> return <see cref="Stream"/>
/// <see cref="Add(long, string, Stream)"/> and <see cref="TryOpen(long, string, out Stream?)"/> return <see cref="Stream"/>
/// objects which may operate on the same underlying <see cref="Stream"/>, but independently maintain
/// their state.
/// </para>
/// </summary>
public class GitPackMemoryCache : GitPackCache
{
private readonly Dictionary<long, GitPackMemoryCacheStream> cache = new Dictionary<long, GitPackMemoryCacheStream>();
private readonly Dictionary<(long, string), GitPackMemoryCacheStream> cache = new Dictionary<(long, string), GitPackMemoryCacheStream>();

/// <inheritdoc/>
public override Stream Add(long offset, Stream stream)
public override Stream Add(long offset, string objectType, Stream stream)
{
var cacheStream = new GitPackMemoryCacheStream(stream);
this.cache.Add(offset, cacheStream);
this.cache.Add((offset, objectType), cacheStream);
return new GitPackMemoryCacheViewStream(cacheStream);
}

/// <inheritdoc/>
public override bool TryOpen(long offset, [NotNullWhen(true)] out Stream? stream)
public override bool TryOpen(long offset, string objectType, [NotNullWhen(true)] out Stream? stream)
{
if (this.cache.TryGetValue(offset, out GitPackMemoryCacheStream? cacheStream))
if (this.cache.TryGetValue((offset, objectType), out GitPackMemoryCacheStream? cacheStream))
{
stream = new GitPackMemoryCacheViewStream(cacheStream!);
return true;
Expand All @@ -64,7 +64,7 @@ protected override void Dispose(bool disposing)
{
while (this.cache.Count > 0)
{
long key = this.cache.Keys.First();
var key = this.cache.Keys.First();
GitPackMemoryCacheStream? stream = this.cache[key];
stream.Dispose();
this.cache.Remove(key);
Expand Down
4 changes: 2 additions & 2 deletions src/NerdBank.GitVersioning/ManagedGit/GitPackNullCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ public class GitPackNullCache : GitPackCache
public static GitPackNullCache Instance { get; } = new GitPackNullCache();

/// <inheritdoc/>
public override Stream Add(long offset, Stream stream)
public override Stream Add(long offset, string objectType, Stream stream)
{
return stream;
}

/// <inheritdoc/>
public override bool TryOpen(long offset, [NotNullWhen(true)] out Stream? stream)
public override bool TryOpen(long offset, string objectType, [NotNullWhen(true)] out Stream? stream)
{
stream = null;
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public void StreamsAreIndependent()
{
var cache = new GitPackMemoryCache();

Stream stream1 = cache.Add(0, stream);
Assert.True(cache.TryOpen(0, out Stream stream2));
Stream stream1 = cache.Add(0, "anObjectType", stream);
Assert.True(cache.TryOpen(0, "anObjectType", out Stream stream2));

using (stream1)
using (stream2)
Expand Down

0 comments on commit d0aa014

Please sign in to comment.