Skip to content
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

Addresses issue of upgrading from v4.x databases in LiteDB versions 5.0.18-5.0.20 #2499

Merged
merged 3 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions LiteDB/Engine/Disk/StreamFactory/FileStreamFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ internal class FileStreamFactory : IStreamFactory
private readonly string _password;
private readonly bool _readonly;
private readonly bool _hidden;
private readonly bool _useAesStream;
jdtkw marked this conversation as resolved.
Show resolved Hide resolved

public FileStreamFactory(string filename, string password, bool readOnly, bool hidden)
public FileStreamFactory(string filename, string password, bool readOnly, bool hidden, bool useAesStream = true)
pictos marked this conversation as resolved.
Show resolved Hide resolved
{
_filename = filename;
_password = password;
_readonly = readOnly;
_hidden = hidden;
_useAesStream = useAesStream;
}

/// <summary>
Expand Down Expand Up @@ -57,7 +59,7 @@ public Stream GetStream(bool canWrite, bool sequencial)
File.SetAttributes(_filename, FileAttributes.Hidden);
}

return _password == null ? (Stream)stream : new AesStream(_password, stream);
return _password == null || !_useAesStream ? (Stream)stream : new AesStream(_password, stream);
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions LiteDB/Engine/EngineSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class EngineSettings
/// <summary>
/// Create new IStreamFactory for datafile
/// </summary>
internal IStreamFactory CreateDataFactory()
internal IStreamFactory CreateDataFactory(bool useAesStream = true)
{
if (this.DataStream != null)
{
Expand All @@ -85,7 +85,7 @@ internal IStreamFactory CreateDataFactory()
}
else if (!string.IsNullOrEmpty(this.Filename))
{
return new FileStreamFactory(this.Filename, this.Password, this.ReadOnly, false);
return new FileStreamFactory(this.Filename, this.Password, this.ReadOnly, false, useAesStream);
}

throw new ArgumentException("EngineSettings must have Filename or DataStream as data source");
Expand Down
4 changes: 2 additions & 2 deletions LiteDB/Engine/FileReader/FileReaderV7.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public FileReaderV7(EngineSettings settings)

public void Open()
{
var streamFactory = _settings.CreateDataFactory();
var streamFactory = _settings.CreateDataFactory(false);

// open datafile from stream factory
_stream = streamFactory.GetStream(true, true);
Expand Down Expand Up @@ -437,7 +437,7 @@ protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_stream.Dispose();
_stream?.Dispose();
_aes?.Dispose();
}

Expand Down
21 changes: 13 additions & 8 deletions LiteDB/Engine/Services/RebuildService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,19 @@ public RebuildService(EngineSettings settings)
{
_settings = settings;

// open, read first 16kb, and close data file
var buffer = this.ReadFirstBytes();
// test for prior version
jdtkw marked this conversation as resolved.
Show resolved Hide resolved
var bufferV7 = this.ReadFirstBytes(false);
if (FileReaderV7.IsVersion(bufferV7))
{
_fileVersion = 7;
return;
}

// test for valid reader to use
_fileVersion =
FileReaderV7.IsVersion(buffer) ? 7 :
FileReaderV8.IsVersion(buffer) ? 8 : throw LiteException.InvalidDatabase();
// open, read first 16kb, and close data file
var buffer = this.ReadFirstBytes();

// test for valid reader to use
_fileVersion = FileReaderV8.IsVersion(buffer) ? 8 : throw LiteException.InvalidDatabase();
}

public long Rebuild(RebuildOptions options)
Expand Down Expand Up @@ -109,10 +114,10 @@ public long Rebuild(RebuildOptions options)
/// <summary>
/// Read first 16kb (2 PAGES) in bytes
/// </summary>
private byte[] ReadFirstBytes()
private byte[] ReadFirstBytes(bool useAesStream = true)
{
var buffer = new byte[PAGE_SIZE * 2];
var factory = _settings.CreateDataFactory();
var factory = _settings.CreateDataFactory(useAesStream);

using (var stream = factory.GetStream(false, true))
{
Expand Down