Skip to content

Commit d0aa014

Browse files
committed
Add objectType to GitPackCache's key
1 parent 0b53771 commit d0aa014

File tree

5 files changed

+17
-15
lines changed

5 files changed

+17
-15
lines changed

src/NerdBank.GitVersioning/ManagedGit/GitPack.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public Stream GetObject(long offset, string objectType)
178178
}
179179
#endif
180180

181-
if (this.cache.TryOpen(offset, out Stream? stream))
181+
if (this.cache.TryOpen(offset, objectType, out Stream? stream))
182182
{
183183
return stream!;
184184
}
@@ -216,7 +216,7 @@ public Stream GetObject(long offset, string objectType)
216216
throw;
217217
}
218218

219-
return this.cache.Add(offset, objectStream);
219+
return this.cache.Add(offset, objectType, objectStream);
220220
}
221221

222222
/// <summary>

src/NerdBank.GitVersioning/ManagedGit/GitPackCache.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ public abstract class GitPackCache : IDisposable
2222
/// <param name="offset">
2323
/// The offset of the Git object in the Git pack.
2424
/// </param>
25+
/// <param name="objectType">The object type of the object to retrieve.</param>
2526
/// <param name="stream">
2627
/// A <see cref="Stream"/> which will be set to the cached Git object.
2728
/// </param>
2829
/// <returns>
2930
/// <see langword="true"/> if the object was found in cache; otherwise,
3031
/// <see langword="false"/>.
3132
/// </returns>
32-
public abstract bool TryOpen(long offset, [NotNullWhen(true)] out Stream? stream);
33+
public abstract bool TryOpen(long offset, string objectType, [NotNullWhen(true)] out Stream? stream);
3334

3435
/// <summary>
3536
/// Gets statistics about the cache usage.
@@ -45,14 +46,15 @@ public abstract class GitPackCache : IDisposable
4546
/// <param name="offset">
4647
/// The offset of the Git object in the Git pack.
4748
/// </param>
49+
/// <param name="objectType">The object type of the object to add to the cache.</param>
4850
/// <param name="stream">
4951
/// A <see cref="Stream"/> which represents the object to add. This stream
5052
/// will be copied to the cache.
5153
/// </param>
5254
/// <returns>
5355
/// A <see cref="Stream"/> which represents the cached entry.
5456
/// </returns>
55-
public abstract Stream Add(long offset, Stream stream);
57+
public abstract Stream Add(long offset, string objectType, Stream stream);
5658

5759
/// <inheritdoc/>
5860
public void Dispose()

src/NerdBank.GitVersioning/ManagedGit/GitPackMemoryCache.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,27 @@ namespace Nerdbank.GitVersioning.ManagedGit;
2121
/// twice, it is read from the <see cref="MemoryStream"/>, rather than the underlying <see cref="Stream"/>.
2222
/// </para>
2323
/// <para>
24-
/// <see cref="Add(long, Stream)"/> and <see cref="TryOpen(long, out Stream?)"/> return <see cref="Stream"/>
24+
/// <see cref="Add(long, string, Stream)"/> and <see cref="TryOpen(long, string, out Stream?)"/> return <see cref="Stream"/>
2525
/// objects which may operate on the same underlying <see cref="Stream"/>, but independently maintain
2626
/// their state.
2727
/// </para>
2828
/// </summary>
2929
public class GitPackMemoryCache : GitPackCache
3030
{
31-
private readonly Dictionary<long, GitPackMemoryCacheStream> cache = new Dictionary<long, GitPackMemoryCacheStream>();
31+
private readonly Dictionary<(long, string), GitPackMemoryCacheStream> cache = new Dictionary<(long, string), GitPackMemoryCacheStream>();
3232

3333
/// <inheritdoc/>
34-
public override Stream Add(long offset, Stream stream)
34+
public override Stream Add(long offset, string objectType, Stream stream)
3535
{
3636
var cacheStream = new GitPackMemoryCacheStream(stream);
37-
this.cache.Add(offset, cacheStream);
37+
this.cache.Add((offset, objectType), cacheStream);
3838
return new GitPackMemoryCacheViewStream(cacheStream);
3939
}
4040

4141
/// <inheritdoc/>
42-
public override bool TryOpen(long offset, [NotNullWhen(true)] out Stream? stream)
42+
public override bool TryOpen(long offset, string objectType, [NotNullWhen(true)] out Stream? stream)
4343
{
44-
if (this.cache.TryGetValue(offset, out GitPackMemoryCacheStream? cacheStream))
44+
if (this.cache.TryGetValue((offset, objectType), out GitPackMemoryCacheStream? cacheStream))
4545
{
4646
stream = new GitPackMemoryCacheViewStream(cacheStream!);
4747
return true;
@@ -64,7 +64,7 @@ protected override void Dispose(bool disposing)
6464
{
6565
while (this.cache.Count > 0)
6666
{
67-
long key = this.cache.Keys.First();
67+
var key = this.cache.Keys.First();
6868
GitPackMemoryCacheStream? stream = this.cache[key];
6969
stream.Dispose();
7070
this.cache.Remove(key);

src/NerdBank.GitVersioning/ManagedGit/GitPackNullCache.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ public class GitPackNullCache : GitPackCache
1919
public static GitPackNullCache Instance { get; } = new GitPackNullCache();
2020

2121
/// <inheritdoc/>
22-
public override Stream Add(long offset, Stream stream)
22+
public override Stream Add(long offset, string objectType, Stream stream)
2323
{
2424
return stream;
2525
}
2626

2727
/// <inheritdoc/>
28-
public override bool TryOpen(long offset, [NotNullWhen(true)] out Stream? stream)
28+
public override bool TryOpen(long offset, string objectType, [NotNullWhen(true)] out Stream? stream)
2929
{
3030
stream = null;
3131
return false;

test/Nerdbank.GitVersioning.Tests/ManagedGit/GitPackMemoryCacheTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public void StreamsAreIndependent()
2020
{
2121
var cache = new GitPackMemoryCache();
2222

23-
Stream stream1 = cache.Add(0, stream);
24-
Assert.True(cache.TryOpen(0, out Stream stream2));
23+
Stream stream1 = cache.Add(0, "anObjectType", stream);
24+
Assert.True(cache.TryOpen(0, "anObjectType", out Stream stream2));
2525

2626
using (stream1)
2727
using (stream2)

0 commit comments

Comments
 (0)