Skip to content

Commit f0abf4e

Browse files
authored
Merge pull request #942 from georg-jung/fix/cache-tryopen-objecttype
Make GitPackCache include ObjectType
2 parents a909655 + 820e843 commit f0abf4e

File tree

5 files changed

+31
-23
lines changed

5 files changed

+31
-23
lines changed

src/NerdBank.GitVersioning/ManagedGit/GitPack.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,14 @@ 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, out (Stream ContentStream, string ObjectType)? hit))
182182
{
183-
return stream!;
183+
if (hit.Value.ObjectType != objectType)
184+
{
185+
throw new GitException($"An object of type {objectType} could not be located at offset {offset}.") { ErrorCode = GitException.ErrorCodes.ObjectNotFound };
186+
}
187+
188+
return hit.Value.ContentStream;
184189
}
185190

186191
GitPackObjectType packObjectType;
@@ -216,7 +221,7 @@ public Stream GetObject(long offset, string objectType)
216221
throw;
217222
}
218223

219-
return this.cache.Add(offset, objectStream);
224+
return this.cache.Add(offset, objectStream, objectType);
220225
}
221226

222227
/// <summary>

src/NerdBank.GitVersioning/ManagedGit/GitPackCache.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ 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="stream">
26-
/// A <see cref="Stream"/> which will be set to the cached Git object.
25+
/// <param name="hit">
26+
/// A (<see cref="Stream"/>, ContentType) tuple which will be set to the cached data if found.
2727
/// </param>
2828
/// <returns>
2929
/// <see langword="true"/> if the object was found in cache; otherwise,
3030
/// <see langword="false"/>.
3131
/// </returns>
32-
public abstract bool TryOpen(long offset, [NotNullWhen(true)] out Stream? stream);
32+
public abstract bool TryOpen(long offset, [NotNullWhen(true)] out (Stream ContentStream, string ObjectType)? hit);
3333

3434
/// <summary>
3535
/// Gets statistics about the cache usage.
@@ -49,10 +49,11 @@ public abstract class GitPackCache : IDisposable
4949
/// A <see cref="Stream"/> which represents the object to add. This stream
5050
/// will be copied to the cache.
5151
/// </param>
52+
/// <param name="objectType">The object type of the object to add to the cache.</param>
5253
/// <returns>
5354
/// A <see cref="Stream"/> which represents the cached entry.
5455
/// </returns>
55-
public abstract Stream Add(long offset, Stream stream);
56+
public abstract Stream Add(long offset, Stream stream, string objectType);
5657

5758
/// <inheritdoc/>
5859
public void Dispose()

src/NerdBank.GitVersioning/ManagedGit/GitPackMemoryCache.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,33 @@ 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, Stream, string)"/> and <see cref="TryOpen(long, out ValueTuple{Stream, string}?)"/> 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, (GitPackMemoryCacheStream, string)> cache = new();
3232

3333
/// <inheritdoc/>
34-
public override Stream Add(long offset, Stream stream)
34+
public override Stream Add(long offset, Stream stream, string objectType)
3535
{
3636
var cacheStream = new GitPackMemoryCacheStream(stream);
37-
this.cache.Add(offset, cacheStream);
37+
this.cache.Add(offset, (cacheStream, objectType));
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, [NotNullWhen(true)] out (Stream ContentStream, string ObjectType)? hit)
4343
{
44-
if (this.cache.TryGetValue(offset, out GitPackMemoryCacheStream? cacheStream))
44+
if (this.cache.TryGetValue(offset, out (GitPackMemoryCacheStream Stream, string ObjectType) tuple))
4545
{
46-
stream = new GitPackMemoryCacheViewStream(cacheStream!);
46+
hit = (new GitPackMemoryCacheViewStream(tuple.Stream), tuple.ObjectType);
4747
return true;
4848
}
4949

50-
stream = null;
50+
hit = null;
5151
return false;
5252
}
5353

@@ -64,8 +64,8 @@ protected override void Dispose(bool disposing)
6464
{
6565
while (this.cache.Count > 0)
6666
{
67-
long key = this.cache.Keys.First();
68-
GitPackMemoryCacheStream? stream = this.cache[key];
67+
var key = this.cache.Keys.First();
68+
(GitPackMemoryCacheStream? stream, _) = this.cache[key];
6969
stream.Dispose();
7070
this.cache.Remove(key);
7171
}

src/NerdBank.GitVersioning/ManagedGit/GitPackNullCache.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ 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, Stream stream, string objectType)
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, [NotNullWhen(true)] out (Stream ContentStream, string ObjectType)? hit)
2929
{
30-
stream = null;
30+
hit = null;
3131
return false;
3232
}
3333

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ 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, stream, "anObjectType");
24+
Assert.True(cache.TryOpen(0, out (Stream ContentStream, string ObjectType)? hit));
25+
Assert.True(hit.HasValue);
26+
Assert.Equal("anObjectType", hit.Value.ObjectType);
2527

2628
using (stream1)
27-
using (stream2)
29+
using (Stream stream2 = hit.Value.ContentStream)
2830
{
2931
stream1.Seek(5, SeekOrigin.Begin);
3032
Assert.Equal(5, stream1.Position);

0 commit comments

Comments
 (0)