Path: redb/redb.Export/Services/ImportService.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/Services/ImportService.cs ---- using System.Data; using System.IO.Compression; using System.Text; using System.Text.Json; using redb.Export.Models; using redb.Export.Providers; namespace redb.Export.Services; /// /// Imports data from a .redb file into a REDB database. /// /// Uses a streaming approach: records are read line-by-line, accumulated into /// batches, and bulk-inserted via . /// The file may be plain JSONL or ZIP-compressed (auto-detected). /// /// public sealed class ImportService { private readonly IDataProvider _provider; private readonly bool _verbose; private readonly int _batchSize; private long _typesCount; private long _rolesCount; private long _usersCount; private long _userRolesCount; private long _listsCount; private long _listItemsCount; private long _schemesCount; private long _structuresCount; private long _migrationsCount; private bool _fileCarriesMigrationHistory; private long _objectsCount; private long _permissionsCount; private long _valuesCount; private static readonly JsonSerializerOptions JsonOptions = new() { PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower }; private readonly int _parallelism; private ParallelImportWriter? _writer; private CountingStream? _progress; private long _totalBytes; private DateTime _loadStarted; /// /// Initializes a new instance of . /// /// Connection the import runs on. /// Print progress. /// Rows per bulk insert. /// /// 1 (the default) keeps the whole import in one transaction on one connection. More than 1 writes /// over that many connections and rebuilds the secondary indexes of the large tables afterwards; /// see for what is given up in exchange. /// public ImportService(IDataProvider provider, bool verbose, int batchSize, int parallelism = 1) { if (parallelism < 1) throw new ArgumentOutOfRangeException(nameof(parallelism), "Parallelism must be at least 1."); if (parallelism > 1 && provider is not IParallelImportProvider) throw new NotSupportedException($"Parallel import is not available for the '{provider.Name}' provider."); _provider = provider; _verbose = verbose; _batchSize = batchSize; _parallelism = parallelism; } /// /// Imports data from the specified .redb file. /// /// Path to the .redb file (JSONL or ZIP). /// /// When true, all existing data is truncated before import /// via . /// /// When true, only prints statistics without writing to the database. /// Cancellation token. public async Task ImportAsync( string inputPath, bool clean, bool dryRun, CancellationToken ct = default) { var startTime = DateTime.UtcNow; // Before anything is cleaned or written: a target of another schema than this build's stops here with the // exceptions of a service start. Without it a parallel import committed its cleaning, then failed on the // first batch naming a column the target does not have, and left an emptied database behind. await SchemaGate.CheckTargetAsync(_provider, ct); Log($"Importing from: {inputPath}"); var isZip = await IsZipFileAsync(inputPath); Log($"Detected: {(isZip ? "ZIP compressed" : "Plain JSONL")}"); Stream dataStream; ZipArchive? archive = null; if (isZip) { archive = ZipFile.OpenRead(inputPath); var entry = archive.Entries.FirstOrDefault(e => e.Name.EndsWith(".jsonl")) ?? throw new InvalidOperationException("No .jsonl file found in archive"); dataStream = entry.Open(); _totalBytes = entry.Length; } else { dataStream = File.OpenRead(inputPath); _totalBytes = new FileInfo(inputPath).Length; } // Progress and the time estimate are told from how far into the file the reader is. _progress = new CountingStream(dataStream); dataStream = _progress; try { using var reader = new StreamReader(dataStream, Encoding.UTF8); var headerLine = await reader.ReadLineAsync(ct) ?? throw new InvalidOperationException("Empty file"); var headerDoc = JsonDocument.Parse(headerLine); var provider = headerDoc.RootElement.TryGetProperty("provider", out var p) ? p.GetString() : "unknown"; var exportedAt = headerDoc.RootElement.TryGetProperty("exported_at", out var e) ? e.GetDateTime() : DateTime.MinValue; Log($"Source: {provider}"); Log($"Exported: {exportedAt:yyyy-MM-dd HH:mm:ss} UTC"); Console.WriteLine(); if (dryRun) { await PrintDryRunInfoAsync(reader, ct); return; } // One transaction for the whole import, cleaning included: a run that stops half-way - a broken or // truncated file, a cancellation, a server error - leaves the database exactly as it found it, with // its constraints on. Before, rows went in batch by batch as read, the constraints came back only on // success, and an import with cleaning that failed left an emptied, half-filled database with its // integrity switched off (review 2026-09-24). // The target's own _types rows stay, the cleaning keeps them: they are the built-in types of this build, and // a file of an older version carries its own (TimeSpan and TimeOnly as text before they were ticks). Only the // file's types the target lacks are added. var targetTypeIds = await ImportFinishing.TargetTypeIdsAsync(_provider, ct); await _provider.BeginImportAsync(ct); var committed = false; var parallelFinished = false; try { if (clean) { Log("Cleaning database..."); await _provider.CleanDatabaseAsync(ct); Log("Database cleaned."); } IReadOnlyList droppedIndexes = []; if (_parallelism > 1) { // From here on several connections write and commit on their own, so the cleaning is // committed first: there is no single transaction to roll back any more. var parallel = (IParallelImportProvider)_provider; await _provider.CommitImportAsync(ct); committed = true; droppedIndexes = await parallel.DropLoadIndexesAsync(ct); Log($"Took {droppedIndexes.Count} secondary indexes out for the load; they are rebuilt at the end."); await parallel.RelaxConstraintsForLoadAsync(ct); _writer = await ParallelImportWriter.StartAsync(parallel, _parallelism, ct); Log($"Writing over {_parallelism} connections."); } else { await _provider.DisableConstraintsAsync(ct); } _loadStarted = DateTime.UtcNow; var tables = CreateDataTables(); long sequenceValue = 0; ExportFooter? footer = null; Log("Importing data (streaming)..."); string? line; while ((line = await reader.ReadLineAsync(ct)) != null) { var doc = JsonDocument.Parse(line); if (!doc.RootElement.TryGetProperty("type", out var typeProp)) { if (doc.RootElement.TryGetProperty("sequence_value", out var seqProp)) { sequenceValue = seqProp.GetInt64(); } continue; } var type = typeProp.GetString(); if (_currentRecordType != null && _currentRecordType != type) { await FlushCurrentTableAsync(tables, ct); } _currentRecordType = type; switch (type) { case "type": var typeRecord = JsonSerializer.Deserialize(line, JsonOptions)!; if (!targetTypeIds.Contains(typeRecord.Id)) AddTypeRow(tables.Types, typeRecord); tables.TypesTotal++; if (tables.Types.Rows.Count >= _batchSize) await FlushTableAsync("_types", tables.Types, ct); break; case "list": AddListRow(tables.Lists, JsonSerializer.Deserialize(line, JsonOptions)!); tables.ListsTotal++; if (tables.Lists.Rows.Count >= _batchSize) await FlushTableAsync("_lists", tables.Lists, ct); break; case "scheme": AddSchemeRow(tables.Schemes, JsonSerializer.Deserialize(line, JsonOptions)!); tables.SchemesTotal++; if (tables.Schemes.Rows.Count >= _batchSize) await FlushTableAsync("_schemes", tables.Schemes, ct); break; case "structure": AddStructureRow(tables.Structures, JsonSerializer.Deserialize(line, JsonOptions)!); tables.StructuresTotal++; if (tables.Structures.Rows.Count >= _batchSize) await FlushTableAsync("_structures", tables.Structures, ct); break; case "migration": AddMigrationRow(tables.Migrations, JsonSerializer.Deserialize(line, JsonOptions)!); tables.MigrationsTotal++; if (tables.Migrations.Rows.Count >= _batchSize) await FlushTableAsync("_migrations", tables.Migrations, ct); break; case "role": AddRoleRow(tables.Roles, JsonSerializer.Deserialize(line, JsonOptions)!); tables.RolesTotal++; if (tables.Roles.Rows.Count >= _batchSize) await FlushTableAsync("_roles", tables.Roles, ct); break; case "user": AddUserRow(tables.Users, JsonSerializer.Deserialize(line, JsonOptions)!); tables.UsersTotal++; if (tables.Users.Rows.Count >= _batchSize) await FlushTableAsync("_users", tables.Users, ct); break; case "user_role": AddUserRoleRow(tables.UserRoles, JsonSerializer.Deserialize(line, JsonOptions)!); tables.UserRolesTotal++; if (tables.UserRoles.Rows.Count >= _batchSize) await FlushTableAsync("_users_roles", tables.UserRoles, ct); break; case "object": AddObjectRow(tables.Objects, JsonSerializer.Deserialize(line, JsonOptions)!); _objectsCount++; if (tables.Objects.Rows.Count >= _batchSize) await FlushTableAsync("_objects", tables.Objects, ct); break; case "list_item": AddListItemRow(tables.ListItems, JsonSerializer.Deserialize(line, JsonOptions)!); tables.ListItemsTotal++; if (tables.ListItems.Rows.Count >= _batchSize) await FlushTableAsync("_list_items", tables.ListItems, ct); break; case "permission": AddPermissionRow(tables.Permissions, JsonSerializer.Deserialize(line, JsonOptions)!); tables.PermissionsTotal++; if (tables.Permissions.Rows.Count >= _batchSize) await FlushTableAsync("_permissions", tables.Permissions, ct); break; case "value": AddValueRow(tables.Values, JsonSerializer.Deserialize(line, JsonOptions)!); _valuesCount++; if (tables.Values.Rows.Count >= _batchSize) await FlushTableAsync("_values", tables.Values, ct); break; case "footer": footer = JsonSerializer.Deserialize(line, JsonOptions); _fileCarriesMigrationHistory = footer?.TotalMigrations is not null; if (footer?.SequenceValue > 0) { sequenceValue = footer.SequenceValue; } break; } } await FlushCurrentTableAsync(tables, ct); if (_verbose && _lastTable != null) { _tableRowCounts.TryGetValue(_lastTable, out var lastCount); WriteProgressLine($" {_lastTable}: {lastCount:N0} rows", final: true); } if (_writer != null) Console.WriteLine($"Data read in {(DateTime.UtcNow - _loadStarted):hh\\:mm\\:ss}; waiting for the writers, then the indexes are rebuilt."); if (_writer != null) { await _writer.CompleteAsync(); // Every row is written; what is left puts the database back in shape. Each step is attempted // whatever the one before it did, and the failures are reported together: one failing step (the // id sequence of a database that had lost it) used to leave every index disabled and every // constraint and trigger off. var failures = await FinishParallelLoadAsync(footer, tables, sequenceValue, droppedIndexes, ct); parallelFinished = true; if (failures.Count > 0) { throw new InvalidOperationException( $"The parallel import has written its rows, and {failures.Count} of its finishing steps " + "failed; every step was attempted:" + string.Concat(failures.Select(f => $"{Environment.NewLine} - {f.Step}: {f.Error.Message}")) + $"{Environment.NewLine}Run the import again with --clean and --parallel: it reloads the data " + "and repeats every step.", new AggregateException(failures.Select(f => f.Error))); } } else { // The footer is the last line of every export: without it the file was cut short, and with it every // table must have arrived in full. Checked before the commit, so a short file changes nothing. VerifyAgainstFooter(footer, tables); await _provider.EnableConstraintsAsync(ct); if (sequenceValue > 0) { Log($"Setting sequence to: {sequenceValue}"); await _provider.SetSequenceValueAsync(sequenceValue, ct); } await _provider.CommitImportAsync(ct); committed = true; } _typesCount = tables.TypesTotal; _listsCount = tables.ListsTotal; _schemesCount = tables.SchemesTotal; _structuresCount = tables.StructuresTotal; _migrationsCount = tables.MigrationsTotal; _rolesCount = tables.RolesTotal; _usersCount = tables.UsersTotal; _userRolesCount = tables.UserRolesTotal; _listItemsCount = tables.ListItemsTotal; _permissionsCount = tables.PermissionsTotal; } finally { if (!committed) await _provider.AbortImportAsync(); if (_writer != null) { await _writer.DisposeAsync(); _writer = null; if (!parallelFinished) { Console.WriteLine(); Console.WriteLine("The parallel import did not finish. The database holds part of the data, the secondary indexes"); Console.WriteLine("of the large tables are out (dropped with their definitions kept in _import_dropped_indexes, or"); Console.WriteLine("disabled, depending on the database), and foreign key checks may still be off."); Console.WriteLine("Run the import again with --clean and --parallel: it reloads the data and puts all of that back."); } } } // The rows are committed: the scheme metadata cache is built now, not by the first query of an application. var cached = await ImportFinishing.SyncMetadataCacheAsync(_provider, ct); Log($"Scheme metadata cache built for {cached} scheme(s)."); if (await ImportFinishing.AnalyzeAsync(_provider, ct)) Log("Planner statistics refreshed (ANALYZE)."); var timeAsText = await ImportFinishing.TimeValuesAsTextAsync(_provider, ct); // A file of an earlier version does not carry the history of the data migrations its database had; the // values arrive with every applied migration in them, and the target cannot tell which those were. var historyMissing = !_fileCarriesMigrationHistory && await HasSchemesAsync(ct); var duration = DateTime.UtcNow - startTime; Console.WriteLine(); Console.WriteLine("Import completed successfully!"); if (timeAsText > 0) { Console.WriteLine( $" Note: {timeAsText:N0} imported TimeSpan or TimeOnly value(s) are stored as text by an older version. " + "Convert them with the statement of the redb upgrade notes (\"TimeSpan and TimeOnly are stored as ticks\") " + "before an application reads them."); } if (historyMissing) { Console.WriteLine( " Warning: the file was written by an earlier version and does not carry the history of the data " + "migrations (redb Pro) applied to its database. Do not run data migrations on this database before " + "checking which of them the data already has: a migration that is not idempotent would be applied " + "a second time. Exporting the source again with this version carries the history."); } Console.WriteLine($" Duration: {duration:hh\\:mm\\:ss}"); Console.WriteLine(); Console.WriteLine("Statistics:"); Console.WriteLine($" Types: {_typesCount:N0}"); Console.WriteLine($" Roles: {_rolesCount:N0}"); Console.WriteLine($" Users: {_usersCount:N0}"); Console.WriteLine($" UserRoles: {_userRolesCount:N0}"); Console.WriteLine($" Lists: {_listsCount:N0}"); Console.WriteLine($" ListItems: {_listItemsCount:N0}"); Console.WriteLine($" Schemes: {_schemesCount:N0}"); Console.WriteLine($" Structures: {_structuresCount:N0}"); Console.WriteLine($" Migrations: {_migrationsCount:N0}"); Console.WriteLine($" Objects: {_objectsCount:N0}"); Console.WriteLine($" Permissions: {_permissionsCount:N0}"); Console.WriteLine($" Values: {_valuesCount:N0}"); } finally { await dataStream.DisposeAsync(); archive?.Dispose(); } } /// /// Refuses an import whose file ended before its footer, or whose tables did not all arrive in full. The /// counts in the footer are written by the exporter as it goes; a mismatch means a cut or damaged file. /// private void VerifyAgainstFooter(ExportFooter? footer, ImportTables tables) { if (footer is null) throw new InvalidOperationException( "The file ends before its footer: it was cut short, or it is not a complete redb export. " + "Nothing was imported; the database is unchanged."); var mismatches = new List(); void Check(string table, long expected, long actual) { if (expected != actual) mismatches.Add($"{table}: expected {expected:N0}, read {actual:N0}"); } Check("types", footer.TotalTypes, tables.TypesTotal); Check("roles", footer.TotalRoles, tables.RolesTotal); Check("users", footer.TotalUsers, tables.UsersTotal); Check("user roles", footer.TotalUserRoles, tables.UserRolesTotal); Check("lists", footer.TotalLists, tables.ListsTotal); Check("list items", footer.TotalListItems, tables.ListItemsTotal); Check("schemes", footer.TotalSchemes, tables.SchemesTotal); Check("structures", footer.TotalStructures, tables.StructuresTotal); // A file of an earlier version has no such total and no migration records. if (footer.TotalMigrations is { } migrations) Check("migrations", migrations, tables.MigrationsTotal); Check("objects", footer.TotalObjects, _objectsCount); Check("permissions", footer.TotalPermissions, tables.PermissionsTotal); Check("values", footer.TotalValues, _valuesCount); if (mismatches.Count > 0) throw new InvalidOperationException( "The file does not hold what its footer lists (" + string.Join("; ", mismatches) + "). " + "Nothing was imported; the database is unchanged."); } /// Whether the database holds any scheme after the import. private async Task HasSchemesAsync(CancellationToken ct) { await using var cmd = _provider.Connection.CreateCommand(); cmd.CommandText = "SELECT COUNT(*) FROM _schemes"; return Convert.ToInt64(await cmd.ExecuteScalarAsync(ct), System.Globalization.CultureInfo.InvariantCulture) > 0; } private readonly Dictionary _tableRowCounts = new(); private string? _lastTable; private string? _currentRecordType; private async Task FlushTableAsync(string tableName, DataTable table, CancellationToken ct) { if (table.Rows.Count == 0) return; _tableRowCounts.TryGetValue(tableName, out var currentCount); currentCount += table.Rows.Count; _tableRowCounts[tableName] = currentCount; if (_verbose) { if (_lastTable != null && _lastTable != tableName) { _tableRowCounts.TryGetValue(_lastTable, out var prevCount); WriteProgressLine($" {_lastTable}: {prevCount:N0} rows", final: true); } _lastTable = tableName; WriteProgressLine($" {tableName}: {currentCount:N0} rows... {ProgressText()}", final: false); } if (_writer != null) { // The batch is handed over as a copy: the reader keeps filling this table while a worker sends that one. await _writer.EnqueueAsync(tableName, table.Copy()); } else { await _provider.BulkInsertAsync(tableName, table, ct); } table.Clear(); } private async Task FlushCurrentTableAsync(ImportTables tables, CancellationToken ct) { switch (_currentRecordType) { case "type": await FlushTableAsync("_types", tables.Types, ct); break; case "list": await FlushTableAsync("_lists", tables.Lists, ct); break; case "scheme": await FlushTableAsync("_schemes", tables.Schemes, ct); break; case "structure": await FlushTableAsync("_structures", tables.Structures, ct); break; case "migration": await FlushTableAsync("_migrations", tables.Migrations, ct); break; case "role": await FlushTableAsync("_roles", tables.Roles, ct); break; case "user": await FlushTableAsync("_users", tables.Users, ct); break; case "user_role": await FlushTableAsync("_users_roles", tables.UserRoles, ct); break; case "object": await FlushTableAsync("_objects", tables.Objects, ct); break; case "list_item": await FlushTableAsync("_list_items", tables.ListItems, ct); break; case "permission": await FlushTableAsync("_permissions", tables.Permissions, ct); break; case "value": await FlushTableAsync("_values", tables.Values, ct); break; } } private async Task PrintDryRunInfoAsync(StreamReader reader, CancellationToken ct) { Console.WriteLine("DRY RUN - No changes will be made"); Console.WriteLine(); long types = 0, lists = 0, schemes = 0, structures = 0, migrations = 0; long roles = 0, users = 0, userRoles = 0; long objects = 0, listItems = 0, permissions = 0, values = 0; string? line; while ((line = await reader.ReadLineAsync(ct)) != null) { var doc = JsonDocument.Parse(line); if (!doc.RootElement.TryGetProperty("type", out var typeProp)) continue; switch (typeProp.GetString()) { case "type": types++; break; case "list": lists++; break; case "scheme": schemes++; break; case "structure": structures++; break; case "migration": migrations++; break; case "role": roles++; break; case "user": users++; break; case "user_role": userRoles++; break; case "object": objects++; break; case "list_item": listItems++; break; case "permission": permissions++; break; case "value": values++; break; } } Console.WriteLine("Would import:"); Console.WriteLine($" Types: {types:N0}"); Console.WriteLine($" Roles: {roles:N0}"); Console.WriteLine($" Users: {users:N0}"); Console.WriteLine($" UserRoles: {userRoles:N0}"); Console.WriteLine($" Lists: {lists:N0}"); Console.WriteLine($" ListItems: {listItems:N0}"); Console.WriteLine($" Schemes: {schemes:N0}"); Console.WriteLine($" Structures: {structures:N0}"); Console.WriteLine($" Migrations: {migrations:N0}"); Console.WriteLine($" Objects: {objects:N0}"); Console.WriteLine($" Permissions: {permissions:N0}"); Console.WriteLine($" Values: {values:N0}"); } /// /// Index builds running at the same time, whatever the number of writer connections. Each build /// sorts the whole table; more of them at once compete for the same disk and memory and finish /// no sooner. /// private const int MaxConcurrentIndexBuilds = 5; private int _progressLineLength; /// /// Writes the progress line over the previous one and blanks whatever the previous one left /// beyond it. A final line ends with a newline; a running one is overwritten by the next. /// private void WriteProgressLine(string text, bool final) { var padding = Math.Max(0, _progressLineLength - text.Length); Console.Write("\r" + text + new string(' ', padding)); if (final) { Console.WriteLine(); _progressLineLength = 0; } else { _progressLineLength = text.Length; } } /// /// How far into the file the reader is and how long the rest should take, from the bytes read so /// far. It measures reading; in a parallel import the index rebuild comes on top and is timed /// separately. /// private string ProgressText() { if (_progress == null || _totalBytes <= 0) return ""; var fraction = Math.Clamp((double)_progress.BytesRead / _totalBytes, 0d, 1d); var elapsed = DateTime.UtcNow - _loadStarted; if (fraction < 0.005 || elapsed.TotalSeconds < 2) return $"data {fraction:P0}"; var remaining = TimeSpan.FromSeconds(elapsed.TotalSeconds * (1 - fraction) / fraction); var indexes = _parallelism > 1 ? "; indexes follow" : ""; return $"data {fraction:P0}, {elapsed:hh\\:mm\\:ss} elapsed, about {remaining:hh\\:mm\\:ss} left{indexes}"; } /// /// Builds the indexes again, several at a time: one statement per connection. Where the /// database lets index builds on one table run side by side, any free connection takes the next /// index; where it does not, each table gets one connection and its indexes go in turn. /// /// /// The finishing steps of a parallel load, each attempted whatever the others did: the file against its footer, /// the id sequence, the index rebuild, then the constraints (after the indexes: checking foreign keys over the /// loaded rows wants them in place). Returns the steps that failed. /// private async Task> FinishParallelLoadAsync( ExportFooter? footer, ImportTables tables, long sequenceValue, IReadOnlyList droppedIndexes, CancellationToken ct) { var failures = new List<(string Step, Exception Error)>(); async Task StepAsync(string step, Func run) { try { await run(); } catch (Exception ex) { failures.Add((step, ex)); } } // The footer is the last line of every export: without it the file was cut short, and with it every table // must have arrived in full. The serial import says "nothing was imported" there; a parallel one has written. await StepAsync("file check", () => { try { VerifyAgainstFooter(footer, tables); } catch (InvalidOperationException ex) { throw new InvalidOperationException(ex.Message.Replace(" Nothing was imported; the database is unchanged.", ""), ex); } return Task.CompletedTask; }); if (sequenceValue > 0) { await StepAsync("id sequence", () => { Log($"Setting sequence to: {sequenceValue}"); return _provider.SetSequenceValueAsync(sequenceValue, ct); }); } await StepAsync("index rebuild", () => RebuildIndexesAsync(droppedIndexes, ct)); await StepAsync("constraints", async () => { var constraintsStarted = DateTime.UtcNow; await ((IParallelImportProvider)_provider).RestoreConstraintsAfterLoadAsync(ct); Log($"Constraints restored in {(DateTime.UtcNow - constraintsStarted):hh\\:mm\\:ss}."); }); return failures; } private async Task RebuildIndexesAsync(IReadOnlyList indexes, CancellationToken ct) { var parallel = (IParallelImportProvider)_provider; if (indexes.Count == 0) { await parallel.ForgetDroppedIndexesAsync(ct); return; } Console.WriteLine(); // One queue for everything, or one queue per table. List> queues = parallel.BuildsIndexesOfOneTableConcurrently ? [new System.Collections.Concurrent.ConcurrentQueue(indexes)] : indexes.GroupBy(i => i.Table) .Select(g => new System.Collections.Concurrent.ConcurrentQueue(g)) .ToList(); var connections = _writer!.Connections.Take(MaxConcurrentIndexBuilds).ToList(); var builders = parallel.BuildsIndexesOfOneTableConcurrently ? connections.Select(c => (Connection: c, Queue: queues[0])).ToList() : connections.Zip(queues, (c, q) => (Connection: c, Queue: q)).ToList(); if (builders.Count < queues.Count) throw new InvalidOperationException( $"{queues.Count} tables have indexes to rebuild and only {builders.Count} connections are open; raise --parallel."); Console.WriteLine($"Rebuilding {indexes.Count} indexes, {builders.Count} at a time..."); var started = DateTime.UtcNow; var done = 0; async Task BuildAsync((IDataProvider Connection, System.Collections.Concurrent.ConcurrentQueue Queue) work) { var builder = (IParallelImportProvider)work.Connection; var pending = work.Queue; while (pending.TryDequeue(out var index)) { var indexStarted = DateTime.UtcNow; await builder.CreateIndexAsync(index, ct); var finished = Interlocked.Increment(ref done); Console.WriteLine( $" [{finished}/{indexes.Count}] {index.Table}.{index.Name} {(DateTime.UtcNow - indexStarted):hh\\:mm\\:ss}"); } } await Task.WhenAll(builders.Select(BuildAsync)); await parallel.ForgetDroppedIndexesAsync(ct); Console.WriteLine($"Indexes rebuilt in {(DateTime.UtcNow - started):hh\\:mm\\:ss}."); } private void Log(string message) { if (_verbose) Console.WriteLine(message); } #region DataTable Creation and Row Mapping private sealed class ImportTables { public DataTable Types { get; } = CreateTypesTable(); public DataTable Lists { get; } = CreateListsTable(); public DataTable Schemes { get; } = CreateSchemesTable(); public DataTable Structures { get; } = CreateStructuresTable(); public DataTable Migrations { get; } = CreateMigrationsTable(); public DataTable Roles { get; } = CreateRolesTable(); public DataTable Users { get; } = CreateUsersTable(); public DataTable UserRoles { get; } = CreateUserRolesTable(); public DataTable Objects { get; } = CreateObjectsTable(); public DataTable ListItems { get; } = CreateListItemsTable(); public DataTable Permissions { get; } = CreatePermissionsTable(); public DataTable Values { get; } = CreateValuesTable(); public long TypesTotal { get; set; } public long ListsTotal { get; set; } public long SchemesTotal { get; set; } public long StructuresTotal { get; set; } public long MigrationsTotal { get; set; } public long RolesTotal { get; set; } public long UsersTotal { get; set; } public long UserRolesTotal { get; set; } public long ListItemsTotal { get; set; } public long PermissionsTotal { get; set; } } private static ImportTables CreateDataTables() => new(); private static DataTable CreateTypesTable() { var dt = new DataTable(); dt.Columns.Add("_id", typeof(long)); dt.Columns.Add("_name", typeof(string)); dt.Columns.Add("_db_type", typeof(string)); dt.Columns.Add("_type", typeof(string)); return dt; } private static void AddTypeRow(DataTable dt, TypeRecord r) { dt.Rows.Add(r.Id, r.Name, r.DbType ?? (object)DBNull.Value, r.DotnetType ?? (object)DBNull.Value); } private static DataTable CreateListsTable() { var dt = new DataTable(); dt.Columns.Add("_id", typeof(long)); dt.Columns.Add("_name", typeof(string)); dt.Columns.Add("_alias", typeof(string)); return dt; } private static void AddListRow(DataTable dt, ListRecord r) { dt.Rows.Add(r.Id, r.Name, r.Alias ?? (object)DBNull.Value); } private static DataTable CreateSchemesTable() { var dt = new DataTable(); dt.Columns.Add("_id", typeof(long)); dt.Columns.Add("_id_parent", typeof(long)); dt.Columns.Add("_name", typeof(string)); dt.Columns.Add("_alias", typeof(string)); dt.Columns.Add("_name_space", typeof(string)); // uuid-semantic: object, not Guid - SQLite stores it as an RFC-ordered BLOB (GuidToDb). dt.Columns.Add("_structure_hash", typeof(object)); dt.Columns.Add("_type", typeof(long)); dt.Columns.Add("_tags", typeof(string)); dt.Columns.Add("_renamed_from", typeof(string)); return dt; } private void AddSchemeRow(DataTable dt, SchemeRecord r) { dt.Rows.Add( r.Id, r.IdParent ?? (object)DBNull.Value, r.Name, r.Alias ?? (object)DBNull.Value, r.NameSpace ?? (object)DBNull.Value, GuidDb(r.StructureHash), r.SchemeType, r.Tags ?? (object)DBNull.Value, r.RenamedFrom ?? (object)DBNull.Value ); } private static DataTable CreateStructuresTable() { var dt = new DataTable(); dt.Columns.Add("_id", typeof(long)); dt.Columns.Add("_id_parent", typeof(long)); dt.Columns.Add("_id_scheme", typeof(long)); dt.Columns.Add("_id_override", typeof(long)); dt.Columns.Add("_id_type", typeof(long)); dt.Columns.Add("_id_list", typeof(long)); dt.Columns.Add("_name", typeof(string)); dt.Columns.Add("_alias", typeof(string)); dt.Columns.Add("_order", typeof(int)); dt.Columns.Add("_readonly", typeof(bool)); dt.Columns.Add("_allow_not_null", typeof(bool)); dt.Columns.Add("_collection_type", typeof(long)); dt.Columns.Add("_key_type", typeof(long)); dt.Columns.Add("_is_compress", typeof(bool)); dt.Columns.Add("_store_null", typeof(bool)); dt.Columns.Add("_default_value", typeof(byte[])); dt.Columns.Add("_default_editor", typeof(string)); dt.Columns.Add("_unique", typeof(bool)); dt.Columns.Add("_unique_version", typeof(long)); dt.Columns.Add("_lazy", typeof(bool)); dt.Columns.Add("_unique_scope", typeof(long)); dt.Columns.Add("_tags", typeof(string)); dt.Columns.Add("_renamed_from", typeof(string)); return dt; } private static void AddStructureRow(DataTable dt, StructureRecord r) { dt.Rows.Add( r.Id, r.IdParent ?? (object)DBNull.Value, r.IdScheme, r.IdOverride ?? (object)DBNull.Value, r.IdType, r.IdList ?? (object)DBNull.Value, r.Name, r.Alias ?? (object)DBNull.Value, r.Order ?? (object)DBNull.Value, r.Readonly ?? (object)DBNull.Value, r.AllowNotNull ?? (object)DBNull.Value, r.CollectionType ?? (object)DBNull.Value, r.KeyType ?? (object)DBNull.Value, r.IsCompress ?? (object)DBNull.Value, r.StoreNull ?? (object)DBNull.Value, r.DefaultValue ?? (object)DBNull.Value, r.DefaultEditor ?? (object)DBNull.Value, r.Unique ?? (object)DBNull.Value, r.UniqueVersion ?? (object)DBNull.Value, r.Lazy ?? (object)DBNull.Value, r.UniqueScope ?? (object)DBNull.Value, r.Tags ?? (object)DBNull.Value, r.RenamedFrom ?? (object)DBNull.Value ); } private static DataTable CreateRolesTable() { var dt = new DataTable(); dt.Columns.Add("_id", typeof(long)); dt.Columns.Add("_name", typeof(string)); dt.Columns.Add("_id_configuration", typeof(long)); return dt; } private static void AddRoleRow(DataTable dt, RoleRecord r) { dt.Rows.Add(r.Id, r.Name, r.IdConfiguration ?? (object)DBNull.Value); } private static DataTable CreateUsersTable() { var dt = new DataTable(); dt.Columns.Add("_id", typeof(long)); dt.Columns.Add("_login", typeof(string)); dt.Columns.Add("_password", typeof(string)); dt.Columns.Add("_name", typeof(string)); dt.Columns.Add("_phone", typeof(string)); dt.Columns.Add("_email", typeof(string)); dt.Columns.Add("_date_register", typeof(DateTimeOffset)); dt.Columns.Add("_date_dismiss", typeof(DateTimeOffset)); dt.Columns.Add("_enabled", typeof(bool)); dt.Columns.Add("_key", typeof(long)); dt.Columns.Add("_code_int", typeof(long)); dt.Columns.Add("_code_string", typeof(string)); dt.Columns.Add("_code_guid", typeof(Guid)); dt.Columns.Add("_note", typeof(string)); // uuid-semantic: object, not Guid - SQLite stores it as an RFC-ordered BLOB (GuidToDb). dt.Columns.Add("_hash", typeof(object)); dt.Columns.Add("_id_configuration", typeof(long)); return dt; } private void AddUserRow(DataTable dt, UserRecord r) { dt.Rows.Add( r.Id, r.Login, r.Password, r.Name, r.Phone ?? (object)DBNull.Value, r.Email ?? (object)DBNull.Value, (object)r.DateRegister, r.DateDismiss.HasValue ? (object)r.DateDismiss.Value : DBNull.Value, (object)r.Enabled, r.Key ?? (object)DBNull.Value, r.CodeInt ?? (object)DBNull.Value, r.CodeString ?? (object)DBNull.Value, r.CodeGuid ?? (object)DBNull.Value, r.Note ?? (object)DBNull.Value, GuidDb(r.Hash), r.IdConfiguration ?? (object)DBNull.Value ); } private static DataTable CreateUserRolesTable() { var dt = new DataTable(); dt.Columns.Add("_id", typeof(long)); dt.Columns.Add("_id_role", typeof(long)); dt.Columns.Add("_id_user", typeof(long)); return dt; } private static void AddUserRoleRow(DataTable dt, UserRoleRecord r) { dt.Rows.Add(r.Id, r.IdRole, r.IdUser); } private static DataTable CreateObjectsTable() { var dt = new DataTable(); dt.Columns.Add("_id", typeof(long)); dt.Columns.Add("_id_parent", typeof(long)); dt.Columns.Add("_id_scheme", typeof(long)); dt.Columns.Add("_id_owner", typeof(long)); dt.Columns.Add("_id_who_change", typeof(long)); dt.Columns.Add("_date_create", typeof(DateTimeOffset)); dt.Columns.Add("_date_modify", typeof(DateTimeOffset)); dt.Columns.Add("_date_begin", typeof(DateTimeOffset)); dt.Columns.Add("_date_complete", typeof(DateTimeOffset)); dt.Columns.Add("_key", typeof(long)); dt.Columns.Add("_name", typeof(string)); dt.Columns.Add("_note", typeof(string)); // uuid-semantic: object, not Guid - SQLite stores it as an RFC-ordered BLOB (GuidToDb). dt.Columns.Add("_hash", typeof(object)); dt.Columns.Add("_value_long", typeof(long)); dt.Columns.Add("_value_string", typeof(string)); dt.Columns.Add("_value_guid", typeof(Guid)); dt.Columns.Add("_value_bool", typeof(bool)); dt.Columns.Add("_value_double", typeof(double)); dt.Columns.Add("_value_numeric", typeof(decimal)); dt.Columns.Add("_value_datetime", typeof(DateTimeOffset)); dt.Columns.Add("_value_bytes", typeof(byte[])); dt.Columns.Add("_value_unique", typeof(string)); return dt; } private void AddObjectRow(DataTable dt, ObjectRecord r) { dt.Rows.Add( r.Id, r.IdParent ?? (object)DBNull.Value, r.IdScheme, (object)r.IdOwner, (object)r.IdWhoChange, (object)r.DateCreate, (object)r.DateModify, r.DateBegin.HasValue ? (object)r.DateBegin.Value : DBNull.Value, r.DateComplete.HasValue ? (object)r.DateComplete.Value : DBNull.Value, r.Key ?? (object)DBNull.Value, r.Name ?? (object)DBNull.Value, r.Note ?? (object)DBNull.Value, GuidDb(r.Hash), r.ValueLong ?? (object)DBNull.Value, r.ValueString ?? (object)DBNull.Value, r.ValueGuid ?? (object)DBNull.Value, r.ValueBool ?? (object)DBNull.Value, r.ValueDouble ?? (object)DBNull.Value, r.ValueNumeric ?? (object)DBNull.Value, r.ValueDatetime.HasValue ? (object)r.ValueDatetime.Value : DBNull.Value, r.ValueBytes ?? (object)DBNull.Value, r.ValueUnique ?? (object)DBNull.Value ); } private static DataTable CreateListItemsTable() { var dt = new DataTable(); dt.Columns.Add("_id", typeof(long)); dt.Columns.Add("_id_list", typeof(long)); dt.Columns.Add("_value", typeof(string)); dt.Columns.Add("_alias", typeof(string)); dt.Columns.Add("_id_object", typeof(long)); return dt; } private static void AddListItemRow(DataTable dt, ListItemRecord r) { dt.Rows.Add( r.Id, r.IdList, r.Value ?? (object)DBNull.Value, r.Alias ?? (object)DBNull.Value, r.IdObject ?? (object)DBNull.Value ); } private static DataTable CreatePermissionsTable() { var dt = new DataTable(); dt.Columns.Add("_id", typeof(long)); dt.Columns.Add("_id_role", typeof(long)); dt.Columns.Add("_id_user", typeof(long)); dt.Columns.Add("_id_ref", typeof(long)); dt.Columns.Add("_select", typeof(bool)); dt.Columns.Add("_insert", typeof(bool)); dt.Columns.Add("_update", typeof(bool)); dt.Columns.Add("_delete", typeof(bool)); return dt; } private static void AddPermissionRow(DataTable dt, PermissionRecord r) { dt.Rows.Add( r.Id, r.IdRole ?? (object)DBNull.Value, r.IdUser ?? (object)DBNull.Value, (object)r.IdRef, r.Select ?? (object)DBNull.Value, r.Insert ?? (object)DBNull.Value, r.Update ?? (object)DBNull.Value, r.Delete ?? (object)DBNull.Value ); } private static DataTable CreateMigrationsTable() { var dt = new DataTable(); dt.Columns.Add("_id", typeof(long)); dt.Columns.Add("_migration_id", typeof(string)); dt.Columns.Add("_scheme_id", typeof(long)); dt.Columns.Add("_structure_id", typeof(long)); dt.Columns.Add("_property_name", typeof(string)); dt.Columns.Add("_expression_hash", typeof(string)); dt.Columns.Add("_migration_type", typeof(string)); dt.Columns.Add("_applied_at", typeof(DateTimeOffset)); dt.Columns.Add("_applied_by", typeof(string)); dt.Columns.Add("_sql_executed", typeof(string)); // INT on PostgreSQL and SQL Server. dt.Columns.Add("_affected_rows", typeof(int)); dt.Columns.Add("_duration_ms", typeof(int)); dt.Columns.Add("_dry_run", typeof(bool)); return dt; } private static void AddMigrationRow(DataTable dt, MigrationRecord r) { dt.Rows.Add( r.Id, r.MigrationId, r.SchemeId, r.StructureId ?? (object)DBNull.Value, r.PropertyName ?? (object)DBNull.Value, r.ExpressionHash ?? (object)DBNull.Value, r.MigrationType, (object)r.AppliedAt, r.AppliedBy ?? (object)DBNull.Value, r.SqlExecuted ?? (object)DBNull.Value, r.AffectedRows.HasValue ? checked((int)r.AffectedRows.Value) : DBNull.Value, r.DurationMs.HasValue ? checked((int)r.DurationMs.Value) : DBNull.Value, (object)r.DryRun ); } private static DataTable CreateValuesTable() { var dt = new DataTable(); dt.Columns.Add("_id", typeof(long)); dt.Columns.Add("_id_structure", typeof(long)); dt.Columns.Add("_id_object", typeof(long)); dt.Columns.Add("_String", typeof(string)); dt.Columns.Add("_Long", typeof(long)); dt.Columns.Add("_Guid", typeof(Guid)); dt.Columns.Add("_Double", typeof(double)); dt.Columns.Add("_DateTimeOffset", typeof(DateTimeOffset)); dt.Columns.Add("_Boolean", typeof(bool)); dt.Columns.Add("_ByteArray", typeof(byte[])); dt.Columns.Add("_Numeric", typeof(decimal)); dt.Columns.Add("_ListItem", typeof(long)); dt.Columns.Add("_Object", typeof(long)); dt.Columns.Add("_array_parent_id", typeof(long)); dt.Columns.Add("_array_index", typeof(string)); // uuid-semantic: object, not Guid - SQLite stores it as an RFC-ordered BLOB (GuidToDb). dt.Columns.Add("_unique", typeof(object)); return dt; } private void AddValueRow(DataTable dt, ValueRecord r) { dt.Rows.Add( r.Id, r.IdStructure, r.IdObject, r.String ?? (object)DBNull.Value, r.Long ?? (object)DBNull.Value, r.Guid ?? (object)DBNull.Value, r.Double ?? (object)DBNull.Value, r.DateTimeOffset ?? (object)DBNull.Value, r.Boolean ?? (object)DBNull.Value, r.ByteArray ?? (object)DBNull.Value, r.Numeric ?? (object)DBNull.Value, r.ListItem ?? (object)DBNull.Value, r.Object ?? (object)DBNull.Value, r.ArrayParentId ?? (object)DBNull.Value, r.ArrayIndex ?? (object)DBNull.Value, GuidDb(r.Unique) ); } /// /// Provider form of a uuid-semantic column value (_hash, _structure_hash, /// _values._unique): the Guid itself for PostgreSQL/MSSQL, an RFC 4122-ordered /// BLOB for SQLite. Genuine binary columns never go through this. /// private object GuidDb(Guid? value) => value is { } v ? _provider.GuidToDb(v) : DBNull.Value; #endregion private static async Task IsZipFileAsync(string filePath) { await using var stream = File.OpenRead(filePath); var buffer = new byte[4]; var read = await stream.ReadAsync(buffer); // ZIP magic number: PK\x03\x04 return read >= 4 && buffer[0] == 0x50 && buffer[1] == 0x4B && buffer[2] == 0x03 && buffer[3] == 0x04; } }