-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix LiteFileStream.SetReadStreamPosition #2459
Changes from 9 commits
95e629b
8827c39
20a88f3
0ded5de
d87ca9c
0b3a76a
d2b3788
9473c1a
e1d2bce
4d58c69
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System; | ||
using System.IO; | ||
using Xunit; | ||
|
||
namespace LiteDB.Tests.Issues; | ||
|
||
public class Issue2458_Tests | ||
{ | ||
[Fact] | ||
public void NegativeSeekFails() | ||
{ | ||
using var db = new LiteDatabase(":memory:"); | ||
var fs = db.FileStorage; | ||
AddTestFile("test", 1, fs); | ||
using Stream stream = fs.OpenRead("test"); | ||
Assert.Throws<ArgumentOutOfRangeException>(() => stream.Position = -1); | ||
} | ||
|
||
//https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.position?view=net-8.0 says seeking to a position | ||
//beyond the end of a stream is supported, so implementations should support it (error on read). | ||
[Fact] | ||
public void SeekPastFileSucceds() | ||
{ | ||
using var db = new LiteDatabase(":memory:"); | ||
var fs = db.FileStorage; | ||
AddTestFile("test", 1, fs); | ||
using Stream stream = fs.OpenRead("test"); | ||
stream.Position = Int32.MaxValue; | ||
} | ||
|
||
[Fact] | ||
public void SeekShortChunks() | ||
{ | ||
using var db = new LiteDatabase(":memory:"); | ||
var fs = db.FileStorage; | ||
using(Stream writeStream = fs.OpenWrite("test", "test")) | ||
{ | ||
writeStream.WriteByte(0); | ||
writeStream.Flush(); //Create single-byte chunk just containing a 0 | ||
writeStream.WriteByte(1); | ||
writeStream.Flush(); | ||
writeStream.WriteByte(2); | ||
} | ||
using Stream readStream = fs.OpenRead("test"); | ||
readStream.Position = 2; | ||
Assert.Equal(2, readStream.ReadByte()); | ||
} | ||
|
||
private void AddTestFile(string id, long length, ILiteStorage<string> fs) | ||
{ | ||
using Stream writeStream = fs.OpenWrite(id, id); | ||
writeStream.Write(new byte[length]); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using static LiteDB.Constants; | ||
|
@@ -23,6 +24,7 @@ public partial class LiteFileStream<TFileId> : Stream | |
private byte[] _currentChunkData = null; | ||
private int _positionInChunk = 0; | ||
private MemoryStream _buffer; | ||
private Dictionary<int, long> _chunkLengths = new Dictionary<int, long>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not trying to force you change this, but are you sure sure separating fields from their main use is a good idea? I mean if it would be used by many different partials, this would imo the best place but you are only using it in the What do you think? Does that make sense? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a big deal to me, whatever works best with the style for the repository. |
||
|
||
internal LiteFileStream(ILiteCollection<LiteFileInfo<TFileId>> files, ILiteCollection<BsonDocument> chunks, LiteFileInfo<TFileId> file, BsonValue fileId, FileAccess mode) | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While you are at it, add a new line here