Path: redb/redb.Export/Flat/FlatSourceReader.cs
Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Export/Flat/FlatSourceReader.cs
----
using System.Data.Common;
using System.Runtime.CompilerServices;
using redb.Core.Models.Contracts;
using redb.Core.Models.Entities;
using redb.Export.Flat.Model;
using redb.Export.Providers;
namespace redb.Export.Flat;
/// One row of _values as the flat export reads it (hashes and unique keys left out).
internal sealed class FlatSourceValue
{
public long Id { get; init; }
public long StructureId { get; init; }
public long? ArrayParentId { get; init; }
public string? ArrayIndex { get; init; }
public string? String { get; init; }
public long? Long { get; init; }
public Guid? Guid { get; init; }
public double? Double { get; init; }
public DateTimeOffset? DateTimeOffset { get; init; }
public bool? Boolean { get; init; }
public byte[]? ByteArray { get; init; }
public decimal? Numeric { get; init; }
public long? ListItem { get; init; }
public long? Object { get; init; }
}
///
/// Reads the redb tables of the source database. Every command runs inside the one transaction the
/// reader was given, so the whole export sees a single snapshot. Readers are consumed one at a
/// time: a connection carries one open reader.
///
internal sealed class FlatSourceReader
{
private readonly IDataProvider _provider;
private readonly DbTransaction _transaction;
public FlatSourceReader(IDataProvider provider, DbTransaction transaction)
{
_provider = provider;
_transaction = transaction;
}
/// Id of the scheme stored under , or null when there is none.
public async Task FindSchemeIdAsync(string schemeName, CancellationToken ct)
{
await using var command = Command("SELECT _id FROM _schemes WHERE _name = @name");
AddParameter(command, "@name", schemeName);
var result = await command.ExecuteScalarAsync(ct);
return result is null or DBNull ? null : Convert.ToInt64(result);
}
/// The structures of a scheme, with the columns the structure tree is built from.
public async Task> ReadStructuresAsync(long schemeId, CancellationToken ct)
{
await using var command = Command(
"SELECT _id, _id_parent, _name, _id_type, _collection_type, _key_type, _order " +
"FROM _structures WHERE _id_scheme = @scheme");
AddParameter(command, "@scheme", schemeId);
var structures = new List();
await using var reader = await command.ExecuteReaderAsync(ct);
while (await reader.ReadAsync(ct))
{
structures.Add(new RedbStructure
{
Id = reader.GetInt64(0),
IdParent = reader.IsDBNull(1) ? null : reader.GetInt64(1),
IdScheme = schemeId,
Name = reader.GetString(2),
IdType = reader.GetInt64(3),
CollectionType = reader.IsDBNull(4) ? null : reader.GetInt64(4),
KeyType = reader.IsDBNull(5) ? null : reader.GetInt64(5),
Order = reader.IsDBNull(6) ? null : reader.GetInt64(6)
});
}
return structures;
}
///
/// Streams a redb table into the shape of its flat copy: one array per row, a value per column of
/// , read from the redb column named by .
///
public async IAsyncEnumerable