Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 12 additions & 6 deletions src/DryDB/ReadOnlyDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,21 @@ public record DatabaseLoadOptions
{
public static DatabaseLoadOptions Default => new();

// This factory must not reference any file I/O type: it is rooted by every use
// of DatabaseLoadOptions, so a FileStream branch here would link file support
// (pread and friends) into applications that never open a file — see
// FileStorage.Factory, which OpenFileAsync swaps in, for the file path.
public static readonly StorageFactory DefaultStorageFactory = (stream, pageSize) =>
{
if (stream is FileStream fs)
{
return new PreadPageLoader(fs.SafeFileHandle);
}

if (stream is MemoryStream ms)
{
return new InMemoryPageLoader(ms.ToArray());
}

throw new NotSupportedException($"unsupported stream type: {stream.GetType().Name}");
throw new NotSupportedException(
$"unsupported stream type: {stream.GetType().Name}. " +
$"To read a file, use ReadOnlyDatabase.OpenFileAsync, or set " +
$"DatabaseLoadOptions.StorageFactory = FileStorage.Factory.");
};

public int CacheSize { get; set; } = 2000 * 1024 * 1024;
Expand All @@ -39,6 +41,10 @@ public sealed class ReadOnlyDatabase : IDisposable
public static async ValueTask<ReadOnlyDatabase> OpenFileAsync(string path, DatabaseLoadOptions? options = null, CancellationToken cancellationToken = default)
{
options ??= DatabaseLoadOptions.Default;
if (ReferenceEquals(options.StorageFactory, DatabaseLoadOptions.DefaultStorageFactory))
{
options = options with { StorageFactory = FileStorage.Factory };
}
var fs = File.OpenRead(path);
return await OpenAsync(fs, options, cancellationToken);
}
Expand Down
21 changes: 21 additions & 0 deletions src/DryDB/Storages/FileStorage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.IO;

namespace DryDB.Storages;

/// <summary>
/// The <see cref="StorageFactory"/> that serves <see cref="FileStream"/>s through
/// <see cref="PreadPageLoader"/>. Kept out of
/// <see cref="DatabaseLoadOptions.DefaultStorageFactory"/> so that applications that
/// never read a file do not link file I/O (trimmers and AOT compilers root everything
/// the default factory references); <see cref="ReadOnlyDatabase.OpenFileAsync"/>
/// swaps this factory in. To pass an already opened <see cref="FileStream"/> to
/// <see cref="ReadOnlyDatabase.OpenAsync"/>, set
/// <see cref="DatabaseLoadOptions.StorageFactory"/> to <see cref="Factory"/>.
/// </summary>
public static class FileStorage
{
public static readonly StorageFactory Factory = (stream, pageSize) =>
stream is FileStream fs
? new PreadPageLoader(fs.SafeFileHandle)
: DatabaseLoadOptions.DefaultStorageFactory(stream, pageSize);
}
Loading