M1 Dati e libreria: modello, archivi SQLite con scrittore unico, nomi dei file, scrittura sicura, miniature, impronte, _febe.jsonl, copie, eliminazione e ricostruzione
Modello del dominio e interfacce degli archivi in Febe.Core; implementazione SQLite in Febe.Data (WAL, chiavi esterne, migrazioni numerate in transazione, coda di scrittura su un solo thread); logica della libreria in Febe.Core/Library; 164 prove MSTest, compresa la ricostruzione del database da una libreria di prova e la scala a 200.000 elementi. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
This commit is contained in:
69 files changed
+12472
No files matched your search
@@ -0,0 +1,99 @@
|
||||
using Febe.Core.Domain;
|
||||
|
||||
namespace Febe.Core.Storage;
|
||||
|
||||
/// <summary>Accounts (spec §4.2, §8.3 <c>account</c>).</summary>
|
||||
public interface IAccountStore
|
||||
{
|
||||
Task<Account?> GetAsync(long accountId, CancellationToken ct = default);
|
||||
|
||||
/// <summary>The account with this Instagram identifier: the identity of an account.</summary>
|
||||
Task<Account?> FindByInstagramIdAsync(string instagramId, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Case-insensitive lookup by current user name.</summary>
|
||||
Task<Account?> FindByUsernameAsync(string username, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Alberto's own profile («Tu»), when known.</summary>
|
||||
Task<Account?> GetMineAsync(CancellationToken ct = default);
|
||||
|
||||
/// <summary>All accounts, ordered by user name.</summary>
|
||||
Task<IReadOnlyList<Account>> ListAsync(CancellationToken ct = default);
|
||||
|
||||
/// <summary>All accounts with their counts; «new» counts are entries discovered at or after <paramref name="newSince"/>.</summary>
|
||||
Task<IReadOnlyList<AccountSummary>> ListSummariesAsync(DateTimeOffset newSince, CancellationToken ct = default);
|
||||
|
||||
Task<AccountStats> GetStatsAsync(long accountId, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Adds an account and returns it with its key.</summary>
|
||||
/// <exception cref="DuplicateRecordException">Another account has the same Instagram identifier, or is already «Tu».</exception>
|
||||
Task<Account> AddAsync(Account account, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Replaces every field of an existing account (settings screen).</summary>
|
||||
Task UpdateAsync(Account account, CancellationToken ct = default);
|
||||
|
||||
Task SetStateAsync(long accountId, AccountState state, DateTimeOffset at, UnreachableReason? reason = null, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Updates last and next check times. Null values leave the stored value unchanged.</summary>
|
||||
Task UpdateChecksAsync(long accountId, AccountCheckTimes times, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Stores the backfill resume point; <paramref name="completedAt"/> marks the backfill as finished.</summary>
|
||||
Task SetBackfillAsync(long accountId, string? cursor, DateTimeOffset? completedAt, CancellationToken ct = default);
|
||||
|
||||
/// <summary>
|
||||
/// Records a rename seen in the data (spec §4.2): updates the user name, keeps the folder (a default folder is
|
||||
/// pinned to the old name) and raises the «Renamed» notice.
|
||||
/// </summary>
|
||||
Task RenameAsync(long accountId, string newUsername, DateTimeOffset at, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Adds notices (e.g. unrecognized format) to an account.</summary>
|
||||
Task AddNoticesAsync(long accountId, AccountNotices notices, DateTimeOffset at, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Clears notices once Alberto has seen them.</summary>
|
||||
Task ClearNoticesAsync(long accountId, AccountNotices notices, CancellationToken ct = default);
|
||||
|
||||
/// <summary>
|
||||
/// Removes the account record and, by cascade, its entries, files records and highlights. Files on disk are not
|
||||
/// touched: removing an account with its files is a library operation, and the default is to keep them and make
|
||||
/// the account «Solo archivio» (spec §4.2).
|
||||
/// </summary>
|
||||
Task DeleteAsync(long accountId, CancellationToken ct = default);
|
||||
}
|
||||
|
||||
/// <summary>Check times of an account; null values are left unchanged.</summary>
|
||||
public sealed record AccountCheckTimes
|
||||
{
|
||||
public DateTimeOffset? LastProfileCheckAt { get; init; }
|
||||
|
||||
public DateTimeOffset? NextProfileCheckAt { get; init; }
|
||||
|
||||
public DateTimeOffset? LastStoriesCheckAt { get; init; }
|
||||
|
||||
public DateTimeOffset? NextStoriesCheckAt { get; init; }
|
||||
|
||||
/// <summary>Profile facts read with the check (null: unchanged).</summary>
|
||||
public int? PostCount { get; init; }
|
||||
|
||||
public bool? IsPrivate { get; init; }
|
||||
|
||||
public bool? IsFollowed { get; init; }
|
||||
|
||||
public string? DisplayName { get; init; }
|
||||
}
|
||||
|
||||
/// <summary>A unique constraint refused a record (duplicate Instagram identifier, second «Tu», ...).</summary>
|
||||
public sealed class DuplicateRecordException : Exception
|
||||
{
|
||||
public DuplicateRecordException()
|
||||
{
|
||||
}
|
||||
|
||||
public DuplicateRecordException(string message)
|
||||
: base(message)
|
||||
{
|
||||
}
|
||||
|
||||
public DuplicateRecordException(string message, Exception innerException)
|
||||
: base(message, innerException)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using Febe.Core.Domain;
|
||||
|
||||
namespace Febe.Core.Storage;
|
||||
|
||||
/// <summary>Probable duplicates waiting for Alberto (spec §8.3 <c>duplicate_pair</c>, §9.3, §9.5).</summary>
|
||||
public interface IDuplicateStore
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds a pair. Idempotent: if the same pending pair (same sides) exists, it is returned unchanged.
|
||||
/// </summary>
|
||||
Task<DuplicatePair> AddAsync(DuplicatePair pair, CancellationToken ct = default);
|
||||
|
||||
Task<DuplicatePair?> GetAsync(long pairId, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Pending pairs in creation order, after <paramref name="afterId"/>.</summary>
|
||||
Task<IReadOnlyList<DuplicatePair>> ListPendingAsync(DuplicateOrigin? origin = null, long afterId = 0, int limit = 200, CancellationToken ct = default);
|
||||
|
||||
Task<int> CountPendingAsync(DuplicateOrigin? origin = null, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Records Alberto's decision. Applying it to files is the caller's job.</summary>
|
||||
Task DecideAsync(long pairId, DuplicateDecision decision, DateTimeOffset at, CancellationToken ct = default);
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
using Febe.Core.Abstractions;
|
||||
using Febe.Core.Domain;
|
||||
|
||||
namespace Febe.Core.Storage;
|
||||
|
||||
/// <summary>The engine queue (spec §8.3 <c>task</c>). Persisted: a restart resets nothing.</summary>
|
||||
public interface ITaskStore
|
||||
{
|
||||
/// <summary>
|
||||
/// Enqueues a task. If a pending task with the same kind, account and payload exists, it is kept and moved earlier
|
||||
/// (earliest due time, highest priority, manual flag kept) instead of adding a second one; that task is returned.
|
||||
/// </summary>
|
||||
Task<EngineTask> EnqueueAsync(EngineTask task, CancellationToken ct = default);
|
||||
|
||||
Task<EngineTask?> GetAsync(long taskId, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Pending tasks due at <paramref name="now"/>: highest priority first, then earliest due, then oldest.</summary>
|
||||
Task<IReadOnlyList<EngineTask>> PeekDueAsync(DateTimeOffset now, int limit, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Pending tasks by due time (the queue shown in Attività, «verso le»).</summary>
|
||||
Task<IReadOnlyList<EngineTask>> ListPendingAsync(int limit, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Pending → Running, atomically. False if the task was not pending any more.</summary>
|
||||
Task<bool> TryStartAsync(long taskId, DateTimeOffset at, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Running → Done.</summary>
|
||||
Task CompleteAsync(long taskId, DateTimeOffset at, CancellationToken ct = default);
|
||||
|
||||
/// <summary>
|
||||
/// Records a failure: with <paramref name="retryAt"/> the task goes back to pending at that time, otherwise it
|
||||
/// becomes Failed. Attempts are incremented.
|
||||
/// </summary>
|
||||
Task FailAsync(long taskId, DateTimeOffset at, string error, DateTimeOffset? retryAt, CancellationToken ct = default);
|
||||
|
||||
Task RescheduleAsync(long taskId, DateTimeOffset dueAt, CancellationToken ct = default);
|
||||
|
||||
Task CancelAsync(long taskId, DateTimeOffset at, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Cancels every pending task of an account. Returns how many.</summary>
|
||||
Task<int> CancelForAccountAsync(long accountId, DateTimeOffset at, CancellationToken ct = default);
|
||||
|
||||
/// <summary>At start-up: tasks left running by a previous process go back to pending. Returns how many.</summary>
|
||||
Task<int> RequeueRunningAsync(CancellationToken ct = default);
|
||||
|
||||
/// <summary>Deletes finished tasks (done, failed, cancelled) completed before <paramref name="before"/>.</summary>
|
||||
Task<int> PurgeFinishedAsync(DateTimeOffset before, CancellationToken ct = default);
|
||||
}
|
||||
|
||||
/// <summary>Counted actions toward the site (spec §7.4 <c>action_log</c>): the budgets are computed from here, not in memory.</summary>
|
||||
public interface IActionLogStore
|
||||
{
|
||||
Task<ActionLogRecord> AppendAsync(ActionLogRecord record, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Sum of amounts of a category in [from, to), optionally for one account.</summary>
|
||||
Task<long> SumAsync(ActionKind category, DateTimeOffset from, DateTimeOffset to, long? accountId = null, CancellationToken ct = default);
|
||||
|
||||
/// <summary>
|
||||
/// Usage of every category: the rolling window (now − 60 min, now] and the local calendar day of
|
||||
/// <paramref name="now"/> in <paramref name="zone"/>. Categories without actions are included with zero.
|
||||
/// </summary>
|
||||
Task<IReadOnlyDictionary<ActionKind, BudgetUsage>> GetUsageAsync(DateTimeOffset now, TimeZoneInfo zone, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Time of the last action (of a category, or of any category except data downloads when null).</summary>
|
||||
Task<DateTimeOffset?> GetLastActionAtAsync(ActionKind? category = null, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Actions in [from, to), oldest first.</summary>
|
||||
Task<IReadOnlyList<ActionLogRecord>> ListAsync(DateTimeOffset from, DateTimeOffset to, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Deletes actions older than <paramref name="before"/>. Keep at least two days for the daily budgets.</summary>
|
||||
Task<int> PruneAsync(DateTimeOffset before, CancellationToken ct = default);
|
||||
}
|
||||
|
||||
/// <summary>Engine sessions (spec §8.3 <c>session</c>).</summary>
|
||||
public interface ISessionStore
|
||||
{
|
||||
Task<SessionRecord> StartAsync(DateTimeOffset at, CancellationToken ct = default);
|
||||
|
||||
Task EndAsync(long sessionId, DateTimeOffset at, SessionOutcome outcome, int actionCount, string? summary = null, CancellationToken ct = default);
|
||||
|
||||
Task<SessionRecord?> GetAsync(long sessionId, CancellationToken ct = default);
|
||||
|
||||
/// <summary>The most recently started session.</summary>
|
||||
Task<SessionRecord?> GetLastAsync(CancellationToken ct = default);
|
||||
|
||||
/// <summary>Sessions overlapping [from, to), by start time (strip of the day).</summary>
|
||||
Task<IReadOnlyList<SessionRecord>> ListAsync(DateTimeOffset from, DateTimeOffset to, CancellationToken ct = default);
|
||||
|
||||
/// <summary>At start-up: sessions left open become Interrupted, ended at <paramref name="at"/>. Returns how many.</summary>
|
||||
Task<int> CloseInterruptedAsync(DateTimeOffset at, CancellationToken ct = default);
|
||||
}
|
||||
|
||||
/// <summary>Stops of the circuit breaker (spec §7.6, §8.3 <c>incident</c>).</summary>
|
||||
public interface IIncidentStore
|
||||
{
|
||||
/// <summary>Opens an incident and returns it with its key.</summary>
|
||||
Task<Incident> OpenAsync(Incident incident, CancellationToken ct = default);
|
||||
|
||||
Task ResolveAsync(long incidentId, DateTimeOffset at, CancellationToken ct = default);
|
||||
|
||||
Task<Incident?> GetAsync(long incidentId, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Unresolved incidents, oldest first.</summary>
|
||||
Task<IReadOnlyList<Incident>> ListOpenAsync(CancellationToken ct = default);
|
||||
|
||||
/// <summary>Incidents that started in [from, to), oldest first.</summary>
|
||||
Task<IReadOnlyList<Incident>> ListAsync(DateTimeOffset from, DateTimeOffset to, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Incidents of a level since an instant («tre rallentamenti in sette giorni»; «la seconda nelle 24 ore»).</summary>
|
||||
Task<int> CountAsync(SignalLevel level, DateTimeOffset since, CancellationToken ct = default);
|
||||
}
|
||||
|
||||
/// <summary>The readable timeline (spec §8.3 <c>event</c>).</summary>
|
||||
public interface IEventStore
|
||||
{
|
||||
Task<EventRecord> AppendAsync(EventRecord record, CancellationToken ct = default);
|
||||
|
||||
/// <summary>A page of the timeline, newest first.</summary>
|
||||
Task<IReadOnlyList<EventRecord>> ListAsync(EventQuery query, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Events after a known one, oldest first: incremental updates of the interface.</summary>
|
||||
Task<IReadOnlyList<EventRecord>> ListAfterAsync(long afterId, int limit = 200, CancellationToken ct = default);
|
||||
|
||||
Task<int> PruneAsync(DateTimeOffset before, CancellationToken ct = default);
|
||||
}
|
||||
|
||||
/// <summary>A page of the timeline: newest first, before <see cref="BeforeId"/> when given.</summary>
|
||||
public sealed record EventQuery
|
||||
{
|
||||
public long? BeforeId { get; init; }
|
||||
|
||||
public int Limit { get; init; } = 100;
|
||||
|
||||
public long? AccountId { get; init; }
|
||||
|
||||
public EventSeverity? MinSeverity { get; init; }
|
||||
|
||||
public DateTimeOffset? From { get; init; }
|
||||
|
||||
public DateTimeOffset? To { get; init; }
|
||||
}
|
||||
|
||||
/// <summary>Local copy of the list of profiles Alberto follows (spec §4.2, §8.3 <c>following</c>).</summary>
|
||||
public interface IFollowingStore
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds or updates profiles as they are read (page by page, possibly over several sessions). Matches by Instagram
|
||||
/// identifier when present, otherwise by user name.
|
||||
/// </summary>
|
||||
Task UpsertAsync(IReadOnlyList<FollowingRecord> records, CancellationToken ct = default);
|
||||
|
||||
/// <summary>The list in reading order.</summary>
|
||||
Task<IReadOnlyList<FollowingRecord>> ListAsync(CancellationToken ct = default);
|
||||
|
||||
Task<int> CountAsync(CancellationToken ct = default);
|
||||
|
||||
/// <summary>When the most recent profile was read.</summary>
|
||||
Task<DateTimeOffset?> GetLastReadAtAsync(CancellationToken ct = default);
|
||||
|
||||
/// <summary>After a complete re-read started at <paramref name="readStartedAt"/>: removes profiles not seen (unfollowed).</summary>
|
||||
Task<int> RemoveNotReadSinceAsync(DateTimeOffset readStartedAt, CancellationToken ct = default);
|
||||
|
||||
Task ClearAsync(CancellationToken ct = default);
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
using Febe.Core.Domain;
|
||||
|
||||
namespace Febe.Core.Storage;
|
||||
|
||||
/// <summary>Entries (spec §4.3, §8.3 <c>entry</c>) and the library queries of the interface (spec §4.6).</summary>
|
||||
public interface IEntryStore
|
||||
{
|
||||
Task<Entry?> GetAsync(long entryId, CancellationToken ct = default);
|
||||
|
||||
Task<EntryWithMedia?> GetWithMediaAsync(long entryId, CancellationToken ct = default);
|
||||
|
||||
/// <summary>The entry with this identity, in any state (deleted included).</summary>
|
||||
Task<Entry?> FindAsync(long accountId, EntryKind kind, string instagramId, CancellationToken ct = default);
|
||||
|
||||
/// <summary>
|
||||
/// «Is this Instagram identifier known?» for (account, kind), in any state — deleted entries count as known so
|
||||
/// they are never downloaded again (spec §4.6).
|
||||
/// </summary>
|
||||
Task<bool> IsKnownAsync(long accountId, EntryKind kind, string instagramId, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Which of these identifiers are already known for (account, kind), in any state. For readers: one query per page.</summary>
|
||||
Task<IReadOnlySet<string>> GetKnownInstagramIdsAsync(long accountId, EntryKind kind, IReadOnlyCollection<string> instagramIds, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Entries of an account with this Instagram identifier, any kind and state (e.g. a story that entered a highlight).</summary>
|
||||
Task<IReadOnlyList<Entry>> FindByInstagramIdAsync(long accountId, string instagramId, CancellationToken ct = default);
|
||||
|
||||
/// <summary>
|
||||
/// An entry of (account, kind) published within <paramref name="tolerance"/> of <paramref name="publishedAt"/>:
|
||||
/// recognizes imported entries that have no Instagram identifier (spec §9.5, two seconds).
|
||||
/// </summary>
|
||||
Task<Entry?> FindByPublicationTimeAsync(long accountId, EntryKind kind, DateTimeOffset publishedAt, TimeSpan tolerance, CancellationToken ct = default);
|
||||
|
||||
/// <summary>
|
||||
/// Adds an entry with its files in one transaction. Idempotent on (account, kind, Instagram identifier): when the
|
||||
/// entry is already known, nothing changes and the stored entry is returned with <c>Created = false</c>.
|
||||
/// Positions of the files must be distinct.
|
||||
/// </summary>
|
||||
Task<AddEntryResult> AddAsync(Entry entry, IReadOnlyList<Media> media, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Adds many entries in one transaction (import, rebuild). Same semantics as <see cref="AddAsync"/>.</summary>
|
||||
Task<IReadOnlyList<AddEntryResult>> AddManyAsync(IReadOnlyList<NewEntry> entries, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Replaces the mutable fields of an entry (Instagram identifier, code, caption, author, state, dates).</summary>
|
||||
Task UpdateAsync(Entry entry, CancellationToken ct = default);
|
||||
|
||||
Task SetStateAsync(long entryId, EntryState state, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Recomputes the state of an entry from its files (<see cref="EntryStateRules.FromMedia"/>).</summary>
|
||||
Task<EntryState> RecomputeStateAsync(long entryId, CancellationToken ct = default);
|
||||
|
||||
/// <summary>
|
||||
/// «Eliminato»: the entry stays without files so it is not downloaded again (spec §4.6). Its files become
|
||||
/// <see cref="MediaState.Deleted"/> and lose their path and perceptual hash; SHA-256 and size are kept. Deleting files on disk is the caller's job.
|
||||
/// </summary>
|
||||
Task MarkDeletedAsync(long entryId, DateTimeOffset at, CancellationToken ct = default);
|
||||
|
||||
/// <summary>«Eliminalo e lascia che torni»: removes the record (files records and highlight links by cascade).</summary>
|
||||
Task RemoveAsync(long entryId, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Entries of an account ordered by publication time, newest first (account tabs, collection pipeline).</summary>
|
||||
Task<IReadOnlyList<Entry>> ListByAccountAsync(long accountId, EntryListOptions? options = null, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Entries of (account, kind) discovered in [from, to): per-account daily backfill limit.</summary>
|
||||
Task<int> CountDiscoveredAsync(long accountId, EntryKind kind, DateTimeOffset from, DateTimeOffset to, CancellationToken ct = default);
|
||||
|
||||
/// <summary>A window of the library mosaic with keyset paging: stays fast over 200,000 entries.</summary>
|
||||
Task<LibraryPage> QueryLibraryAsync(LibraryQuery query, CancellationToken ct = default);
|
||||
|
||||
Task<LibraryTotals> GetLibraryTotalsAsync(LibraryFilter filter, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Entries per local calendar day in Alberto's zone, newest day first.</summary>
|
||||
Task<IReadOnlyList<DayCount>> CountLibraryByDayAsync(LibraryFilter filter, TimeZoneInfo zone, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Tiles for given entries, in the given order (e.g. the items of a highlight collection).</summary>
|
||||
Task<IReadOnlyList<LibraryTile>> GetTilesAsync(IReadOnlyList<long> entryIds, CancellationToken ct = default);
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
namespace Febe.Core.Storage;
|
||||
|
||||
/// <summary>
|
||||
/// Everything Febe keeps in its database, as one object (implemented by <c>Febe.Data.FebeDatabase</c>).
|
||||
/// Writes are serialized by a single writer; reads run concurrently.
|
||||
/// </summary>
|
||||
public interface IFebeStorage : IAsyncDisposable
|
||||
{
|
||||
IAccountStore Accounts { get; }
|
||||
|
||||
IEntryStore Entries { get; }
|
||||
|
||||
IMediaStore Media { get; }
|
||||
|
||||
IHighlightStore Highlights { get; }
|
||||
|
||||
IDuplicateStore Duplicates { get; }
|
||||
|
||||
ITaskStore Tasks { get; }
|
||||
|
||||
IActionLogStore ActionLog { get; }
|
||||
|
||||
ISessionStore Sessions { get; }
|
||||
|
||||
IIncidentStore Incidents { get; }
|
||||
|
||||
IEventStore Events { get; }
|
||||
|
||||
IFollowingStore Following { get; }
|
||||
|
||||
IImportStore Imports { get; }
|
||||
|
||||
ISettingsStore Settings { get; }
|
||||
|
||||
IDatabaseMaintenance Maintenance { get; }
|
||||
}
|
||||
|
||||
/// <summary>Maintenance of the database file.</summary>
|
||||
public interface IDatabaseMaintenance
|
||||
{
|
||||
/// <summary>Full path of the database file.</summary>
|
||||
string DatabasePath { get; }
|
||||
|
||||
/// <summary>Schema version (PRAGMA user_version).</summary>
|
||||
int SchemaVersion { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Writes a consistent copy of the database to a new file with <c>VACUUM INTO</c> (spec §8.5).
|
||||
/// The destination must not exist.
|
||||
/// </summary>
|
||||
Task BackupToAsync(string destinationFile, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Runs <c>PRAGMA quick_check</c>; true when the database is sound.</summary>
|
||||
Task<bool> CheckIntegrityAsync(CancellationToken ct = default);
|
||||
|
||||
/// <summary>Lets SQLite refresh its statistics (<c>PRAGMA optimize</c>).</summary>
|
||||
Task OptimizeAsync(CancellationToken ct = default);
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
using Febe.Core.Domain;
|
||||
|
||||
namespace Febe.Core.Storage;
|
||||
|
||||
/// <summary>Highlight collections and their ordered contents (spec §8.3 <c>highlight</c>, <c>highlight_entry</c>).</summary>
|
||||
public interface IHighlightStore
|
||||
{
|
||||
Task<Highlight?> GetAsync(long highlightId, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Collections of an account in profile order.</summary>
|
||||
Task<IReadOnlyList<Highlight>> ListAsync(long accountId, bool includeRemoved = false, CancellationToken ct = default);
|
||||
|
||||
Task<Highlight?> FindAsync(long accountId, string instagramId, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Lookup by title, for collections known only from an import or a rebuild.</summary>
|
||||
Task<Highlight?> FindByTitleAsync(long accountId, string title, CancellationToken ct = default);
|
||||
|
||||
/// <summary>
|
||||
/// Adds or updates a collection. Matches an existing one by key, then by (account, Instagram identifier).
|
||||
/// Returns the stored collection.
|
||||
/// </summary>
|
||||
Task<Highlight> UpsertAsync(Highlight highlight, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Sets the profile order of the given collections (positions 0, 1, ...).</summary>
|
||||
Task SetOrderAsync(long accountId, IReadOnlyList<long> highlightIdsInOrder, CancellationToken ct = default);
|
||||
|
||||
/// <summary>The collection disappeared from the profile; its contents and files stay.</summary>
|
||||
Task MarkRemovedAsync(long highlightId, DateTimeOffset at, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Contents of a collection in order.</summary>
|
||||
Task<IReadOnlyList<HighlightItem>> ListItemsAsync(long highlightId, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Replaces the contents of a collection with these entries, in this order.</summary>
|
||||
Task SetItemsAsync(long highlightId, IReadOnlyList<long> entryIdsInOrder, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Appends an entry at the end of a collection if it is not there yet.</summary>
|
||||
Task AddItemAsync(long highlightId, long entryId, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Collections that contain an entry (an entry may be in several).</summary>
|
||||
Task<IReadOnlyList<Highlight>> ListForEntryAsync(long entryId, CancellationToken ct = default);
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
using Febe.Core.Domain;
|
||||
|
||||
namespace Febe.Core.Storage;
|
||||
|
||||
/// <summary>Imports and their files (spec §8.3 <c>import_run</c>, <c>import_file</c>; §9).</summary>
|
||||
public interface IImportStore
|
||||
{
|
||||
Task<ImportRun> CreateRunAsync(ImportRun run, CancellationToken ct = default);
|
||||
|
||||
Task UpdateRunAsync(ImportRun run, CancellationToken ct = default);
|
||||
|
||||
Task<ImportRun?> GetRunAsync(long runId, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Imports, newest first.</summary>
|
||||
Task<IReadOnlyList<ImportRun>> ListRunsAsync(CancellationToken ct = default);
|
||||
|
||||
/// <summary>
|
||||
/// Adds files to an import in one transaction. Files already recorded for the same relative path are left
|
||||
/// unchanged (so an interrupted analysis resumes).
|
||||
/// </summary>
|
||||
Task AddFilesAsync(IReadOnlyList<ImportFile> files, CancellationToken ct = default);
|
||||
|
||||
Task UpdateFileAsync(ImportFile file, CancellationToken ct = default);
|
||||
|
||||
Task UpdateFilesAsync(IReadOnlyList<ImportFile> files, CancellationToken ct = default);
|
||||
|
||||
Task<ImportFile?> GetFileAsync(long fileId, CancellationToken ct = default);
|
||||
|
||||
Task<ImportFile?> FindFileAsync(long runId, string relativePath, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Files of an import, by key, with optional filters.</summary>
|
||||
Task<IReadOnlyList<ImportFile>> ListFilesAsync(ImportFileQuery query, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Files of the same import with this SHA-256 (duplicates inside the source).</summary>
|
||||
Task<IReadOnlyList<ImportFile>> FindFilesBySha256Async(long runId, string sha256, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Counts by outcome and by state.</summary>
|
||||
Task<ImportSummary> GetSummaryAsync(long runId, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Source folders with their counts and association (review, mockup 09).</summary>
|
||||
Task<IReadOnlyList<ImportFolder>> ListFoldersAsync(long runId, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Associates every file of a source folder with an account (null: not associated).</summary>
|
||||
Task SetFolderTargetAsync(long runId, string folder, long? accountId, CancellationToken ct = default);
|
||||
}
|
||||
|
||||
/// <summary>Filters for the files of an import.</summary>
|
||||
public sealed record ImportFileQuery
|
||||
{
|
||||
public required long RunId { get; init; }
|
||||
|
||||
public ImportOutcome? Outcome { get; init; }
|
||||
|
||||
public ImportFileState? State { get; init; }
|
||||
|
||||
public string? Folder { get; init; }
|
||||
|
||||
public long AfterId { get; init; }
|
||||
|
||||
public int Limit { get; init; } = 500;
|
||||
}
|
||||
|
||||
/// <summary>Counts of an import.</summary>
|
||||
public sealed record ImportSummary(
|
||||
int Total,
|
||||
IReadOnlyDictionary<ImportOutcome, int> ByOutcome,
|
||||
IReadOnlyDictionary<ImportFileState, int> ByState,
|
||||
long Bytes);
|
||||
|
||||
/// <summary>A source folder of an import.</summary>
|
||||
public sealed record ImportFolder(string Folder, int Files, string? ProbableAccount, long? TargetAccountId);
|
||||
@@ -0,0 +1,52 @@
|
||||
using Febe.Core.Domain;
|
||||
|
||||
namespace Febe.Core.Storage;
|
||||
|
||||
/// <summary>Files of entries (spec §8.3 <c>media</c>), hashes and the download queue.</summary>
|
||||
public interface IMediaStore
|
||||
{
|
||||
Task<Media?> GetAsync(long mediaId, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Files of an entry in position order.</summary>
|
||||
Task<IReadOnlyList<Media>> ListByEntryAsync(long entryId, CancellationToken ct = default);
|
||||
|
||||
/// <summary>
|
||||
/// Every file with this SHA-256 across the whole library, any state, with its entry and account
|
||||
/// («Già in libreria», spec §9.3; exact duplicates after a download, §9.5).
|
||||
/// </summary>
|
||||
Task<IReadOnlyList<MediaLocation>> FindBySha256Async(string sha256, CancellationToken ct = default);
|
||||
|
||||
/// <summary>
|
||||
/// Relative paths of the files of an account that have one, with their entry: recognizes entries without an
|
||||
/// Instagram identifier (imported) by their files (rebuild, import resume).
|
||||
/// </summary>
|
||||
Task<IReadOnlyDictionary<string, long>> MapPathsAsync(long accountId, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Perceptual hashes of the photos present in the library, to load the in-memory index.</summary>
|
||||
Task<IReadOnlyList<PerceptualHashRecord>> LoadPerceptualHashesAsync(CancellationToken ct = default);
|
||||
|
||||
/// <summary>
|
||||
/// Files waiting to be downloaded, in queue order: stories first (they expire), then by expiry and discovery.
|
||||
/// </summary>
|
||||
Task<IReadOnlyList<PendingDownload>> ListPendingDownloadsAsync(int limit, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Files waiting to be downloaded, for one account or overall («lo storico non corre più veloce dei download»).</summary>
|
||||
Task<int> CountPendingDownloadsAsync(long? accountId = null, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Records a file now on disk and recomputes the state of its entry, in one transaction.</summary>
|
||||
Task MarkStoredAsync(long mediaId, StoredFile file, MediaState state, DateTimeOffset at, CancellationToken ct = default);
|
||||
|
||||
/// <summary>
|
||||
/// Records a failed attempt: <paramref name="state"/> is Failed, Expired (address expired) or Skipped (a limit).
|
||||
/// Increments attempts and recomputes the state of the entry.
|
||||
/// </summary>
|
||||
Task MarkFailedAsync(long mediaId, MediaState state, string? error, DateTimeOffset at, CancellationToken ct = default);
|
||||
|
||||
/// <summary>A fresh address read from the page: the file goes back to «da scaricare».</summary>
|
||||
Task UpdateSourceAsync(long mediaId, string? sourceUrl, string? coverUrl, DateTimeOffset seenAt, CancellationToken ct = default);
|
||||
|
||||
Task SetThumbnailAsync(long mediaId, bool hasThumbnail, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Replaces every field of a file record.</summary>
|
||||
Task UpdateAsync(Media media, CancellationToken ct = default);
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
using System.Globalization;
|
||||
|
||||
namespace Febe.Core.Storage;
|
||||
|
||||
/// <summary>Key-value settings (spec §8.3 <c>setting</c>). Keys are listed in <see cref="SettingKeys"/>.</summary>
|
||||
public interface ISettingsStore
|
||||
{
|
||||
Task<string?> GetAsync(string key, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Stores a value; null removes the key.</summary>
|
||||
Task SetAsync(string key, string? value, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Stores several values in one transaction; null values remove their key.</summary>
|
||||
Task SetManyAsync(IReadOnlyDictionary<string, string?> values, CancellationToken ct = default);
|
||||
|
||||
Task<IReadOnlyDictionary<string, string>> GetAllAsync(CancellationToken ct = default);
|
||||
}
|
||||
|
||||
/// <summary>Known setting keys. Values are invariant-culture text.</summary>
|
||||
public static class SettingKeys
|
||||
{
|
||||
/// <summary>Library folder (spec §4.7 «Libreria»).</summary>
|
||||
public const string LibraryRoot = "libreria.cartella";
|
||||
|
||||
/// <summary>General naming scheme.</summary>
|
||||
public const string NamingScheme = "libreria.schema";
|
||||
|
||||
/// <summary>Naming scheme for highlight collections.</summary>
|
||||
public const string HighlightNamingScheme = "libreria.schema-evidenza";
|
||||
|
||||
/// <summary>«Imposta la data del file a quella di pubblicazione» (true/false).</summary>
|
||||
public const string SetFileTimeToPublication = "libreria.data-file-pubblicazione";
|
||||
}
|
||||
|
||||
/// <summary>Typed helpers over <see cref="ISettingsStore"/>.</summary>
|
||||
public static class SettingsStoreExtensions
|
||||
{
|
||||
public static async Task<bool> GetBoolAsync(this ISettingsStore store, string key, bool fallback, CancellationToken ct = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(store);
|
||||
var text = await store.GetAsync(key, ct).ConfigureAwait(false);
|
||||
return bool.TryParse(text, out var value) ? value : fallback;
|
||||
}
|
||||
|
||||
public static Task SetBoolAsync(this ISettingsStore store, string key, bool value, CancellationToken ct = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(store);
|
||||
return store.SetAsync(key, value ? "true" : "false", ct);
|
||||
}
|
||||
|
||||
public static async Task<long?> GetInt64Async(this ISettingsStore store, string key, CancellationToken ct = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(store);
|
||||
var text = await store.GetAsync(key, ct).ConfigureAwait(false);
|
||||
return long.TryParse(text, NumberStyles.Integer, CultureInfo.InvariantCulture, out var value) ? value : null;
|
||||
}
|
||||
|
||||
public static Task SetInt64Async(this ISettingsStore store, string key, long? value, CancellationToken ct = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(store);
|
||||
return store.SetAsync(key, value?.ToString(CultureInfo.InvariantCulture), ct);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
using Febe.Core.Domain;
|
||||
|
||||
namespace Febe.Core.Storage;
|
||||
|
||||
/// <summary>Kind filter of the library (mockup 01): «Foto» and «Video» concern posts; the others have their own entry.</summary>
|
||||
public enum LibraryKindFilter
|
||||
{
|
||||
/// <summary>«Tutto»: posts, stories, highlights and tagged posts. Profile pictures are not in the library (spec §4.3).</summary>
|
||||
All,
|
||||
|
||||
/// <summary>«Foto»: posts with at least one photo.</summary>
|
||||
Photos,
|
||||
|
||||
/// <summary>«Video»: posts with at least one video (reels included).</summary>
|
||||
Videos,
|
||||
|
||||
/// <summary>«Storie».</summary>
|
||||
Stories,
|
||||
|
||||
/// <summary>«In evidenza»: highlight items, including stories referenced by a collection.</summary>
|
||||
Highlights,
|
||||
|
||||
/// <summary>«Taggati».</summary>
|
||||
Tagged,
|
||||
|
||||
/// <summary>Profile pictures: the «Profilo» tab of an account.</summary>
|
||||
ProfilePictures,
|
||||
|
||||
/// <summary>Posts of any media (the «Post» tab of an account).</summary>
|
||||
Posts,
|
||||
}
|
||||
|
||||
/// <summary>Filters of the library mosaic and of the account tabs (spec §4.6).</summary>
|
||||
public sealed record LibraryFilter
|
||||
{
|
||||
public LibraryKindFilter Kind { get; init; } = LibraryKindFilter.All;
|
||||
|
||||
/// <summary>Only these accounts (null: all).</summary>
|
||||
public IReadOnlyList<long>? AccountIds { get; init; }
|
||||
|
||||
/// <summary>Published at or after (UTC).</summary>
|
||||
public DateTimeOffset? From { get; init; }
|
||||
|
||||
/// <summary>Published before (UTC).</summary>
|
||||
public DateTimeOffset? To { get; init; }
|
||||
|
||||
/// <summary>«Provenienza»: downloaded or imported (null: both).</summary>
|
||||
public Provenance? Provenance { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Search by account name (user name or display name) and caption text. Case and accents are ignored.
|
||||
/// </summary>
|
||||
public string? Search { get; init; }
|
||||
|
||||
/// <summary>Entry states shown. Null means <see cref="EntryStateRules.Visible"/> (entries with files).</summary>
|
||||
public IReadOnlyList<EntryState>? States { get; init; }
|
||||
}
|
||||
|
||||
/// <summary>Keyset position in the library order (publication time descending, then id descending).</summary>
|
||||
public readonly record struct LibraryCursor(DateTimeOffset PublishedAt, long EntryId)
|
||||
{
|
||||
/// <summary>A cursor that starts the older direction at a given instant (to jump to a date).</summary>
|
||||
public static LibraryCursor At(DateTimeOffset instant) => new(instant, long.MaxValue);
|
||||
}
|
||||
|
||||
public enum LibraryDirection
|
||||
{
|
||||
/// <summary>Entries published before the cursor (scrolling down).</summary>
|
||||
Older,
|
||||
|
||||
/// <summary>Entries published after the cursor (scrolling up, or new entries arriving on top).</summary>
|
||||
Newer,
|
||||
}
|
||||
|
||||
/// <summary>A window of the library. Without a cursor the window starts from the newest entry.</summary>
|
||||
public sealed record LibraryQuery
|
||||
{
|
||||
public LibraryFilter Filter { get; init; } = new();
|
||||
|
||||
public LibraryCursor? Cursor { get; init; }
|
||||
|
||||
public LibraryDirection Direction { get; init; } = LibraryDirection.Older;
|
||||
|
||||
/// <summary>Maximum tiles (1–1000).</summary>
|
||||
public int Limit { get; init; } = 200;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// One tile of the mosaic: an entry with its cover file (the file at the lowest position).
|
||||
/// <see cref="CoverSha256"/> is the thumbnail key.
|
||||
/// </summary>
|
||||
public sealed record LibraryTile(
|
||||
long EntryId,
|
||||
long AccountId,
|
||||
string Username,
|
||||
EntryKind Kind,
|
||||
DateTimeOffset PublishedAt,
|
||||
EntryState State,
|
||||
Provenance Provenance,
|
||||
int MediaCount,
|
||||
long? CoverMediaId,
|
||||
MediaKind? CoverKind,
|
||||
int? CoverWidth,
|
||||
int? CoverHeight,
|
||||
TimeSpan? CoverDuration,
|
||||
string? CoverSha256,
|
||||
bool CoverHasThumbnail);
|
||||
|
||||
/// <summary>
|
||||
/// A window of tiles, always in display order (newest first). <see cref="Older"/> and <see cref="Newer"/> continue
|
||||
/// the window in each direction; null means there is nothing more that way.
|
||||
/// </summary>
|
||||
public sealed record LibraryPage(IReadOnlyList<LibraryTile> Tiles, LibraryCursor? Older, LibraryCursor? Newer);
|
||||
|
||||
/// <summary>Number of entries of a local calendar day (headers of the mosaic, «14 nuovi elementi»).</summary>
|
||||
public readonly record struct DayCount(DateOnly Day, int Count);
|
||||
|
||||
/// <summary>Totals for the title of the library («48.212 elementi di 38 account»).</summary>
|
||||
public readonly record struct LibraryTotals(int Entries, int Accounts);
|
||||
|
||||
/// <summary>Options of a per-account list of entries.</summary>
|
||||
public sealed record EntryListOptions
|
||||
{
|
||||
/// <summary>Only this kind (null: all).</summary>
|
||||
public EntryKind? Kind { get; init; }
|
||||
|
||||
/// <summary>Only these states (null: all, deleted included).</summary>
|
||||
public IReadOnlyList<EntryState>? States { get; init; }
|
||||
|
||||
/// <summary>Continue after this cursor (publication time descending).</summary>
|
||||
public LibraryCursor? Before { get; init; }
|
||||
|
||||
public int Limit { get; init; } = 200;
|
||||
}
|
||||
|
||||
/// <summary>Counts of an account (header of the account card, mockup 05; list of mockup 03).</summary>
|
||||
public sealed record AccountStats
|
||||
{
|
||||
public long AccountId { get; init; }
|
||||
|
||||
/// <summary>Entries in the library (with files), all kinds except profile pictures.</summary>
|
||||
public int EntriesInLibrary { get; init; }
|
||||
|
||||
public int Posts { get; init; }
|
||||
|
||||
public int Stories { get; init; }
|
||||
|
||||
public int HighlightItems { get; init; }
|
||||
|
||||
public int Tagged { get; init; }
|
||||
|
||||
public int ProfilePictures { get; init; }
|
||||
|
||||
/// <summary>Highlight collections on the profile (not removed).</summary>
|
||||
public int HighlightCollections { get; init; }
|
||||
|
||||
/// <summary>Bytes of the files present.</summary>
|
||||
public long Bytes { get; init; }
|
||||
|
||||
/// <summary>Files still to download.</summary>
|
||||
public int PendingFiles { get; init; }
|
||||
|
||||
public DateTimeOffset? LastPublishedAt { get; init; }
|
||||
}
|
||||
|
||||
/// <summary>An account with its counts, for the accounts list.</summary>
|
||||
/// <param name="Account">The account.</param>
|
||||
/// <param name="EntriesInLibrary">«In libreria».</param>
|
||||
/// <param name="Posts">Posts in the library (backfill progress: «340 di 1.280 post»).</param>
|
||||
/// <param name="NewPostsSince">Posts discovered since the given instant («1 nuovo post oggi»).</param>
|
||||
/// <param name="NewStoriesSince">Stories discovered since the given instant («2 nuove storie oggi»).</param>
|
||||
public sealed record AccountSummary(Account Account, int EntriesInLibrary, int Posts, int NewPostsSince, int NewStoriesSince);
|
||||
|
||||
/// <summary>A perceptual hash of a library file, to load the in-memory index.</summary>
|
||||
public readonly record struct PerceptualHashRecord(long MediaId, long AccountId, ulong Hash);
|
||||
|
||||
/// <summary>Result of adding an entry: the stored entry and whether it was new.</summary>
|
||||
public sealed record AddEntryResult(EntryWithMedia Stored, bool Created);
|
||||
|
||||
/// <summary>An entry to add with its files.</summary>
|
||||
public sealed record NewEntry(Entry Entry, IReadOnlyList<Media> Media);
|
||||
Reference in new issue
Block a user