//
// ▄▄▄▄ ▄▄▄▄ ▄▄▄▄
// ▄▄▄▄▄▄█ █▄▄▄▄▄█ █▄▄▄▄▄█ █
// █__ --█ █__ --█ ◄█ - █
// █▄▄▄▄▄█▄▄█▄▄▄▄▄█▄▄█▄▄█▄▄▄▄▄█
// ┍━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━ ━ ━┉ ┉ ┉
// │ Copyright (c) JP Dillingham.
// │
// │ This program is free software: you can redistribute it and/or modify
// │ it under the terms of the GNU Affero General Public License as published
// │ by the Free Software Foundation, version 3.
// │
// │ This program is distributed in the hope that it will be useful,
// │ but WITHOUT ANY WARRANTY; without even the implied warranty of
// │ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// │ GNU Affero General Public License for more details.
// │
// │ You should have received a copy of the GNU Affero General Public License
// │ along with this program. If not, see https://www.gnu.org/licenses/.
// │
// │ This program is distributed with Additional Terms pursuant to Section 7
// │ of the AGPLv3. See the LICENSE file in the root directory of this
// │ project for the complete terms and conditions.
// │
// │ https://slskd.org
// │
// ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ╌ ╌╌╌╌ ╌
// │ SPDX-FileCopyrightText: JP Dillingham
// │ SPDX-License-Identifier: AGPL-3.0-only
// ╰───────────────────────────────────────────╶──── ─ ─── ─ ── ──┈ ┈
//
namespace slskd.Shares
{
using System;
using System.Collections.Generic;
using Soulseek;
///
/// Persistent storage of shared files and metadata.
///
public interface IShareRepository : IDisposable
{
///
/// Gets the connection string for this repository.
///
string ConnectionString { get; }
///
/// Backs the current database up to the database at the specified .
///
/// The destination repository.
void BackupTo(IShareRepository repository);
///
/// Checkpoints the current database, flushing the contents of the WAL (write ahead log) into the table(s).
///
void Checkpoint();
///
/// Counts the number of directories in the database.
///
/// The optional directory prefix used for counting subdirectories.
/// The number of directories.
int CountDirectories(string parentDirectory = null);
///
/// Counts the number of files in the database.
///
/// The optional directory prefix used for counting files in a subdirectory.
/// The number of files.
int CountFiles(string parentDirectory = null);
///
/// Creates a new database.
///
///
/// Creates tables using 'IF NOT EXISTS', so this is idempotent unless 'discardExisting` is specified, in which case
/// tables are explicitly dropped prior to creation.
///
/// An optional value that determines whether the existing database should be discarded.
void Create(bool discardExisting = false);
///
/// Dumps the contents of the database to a file.
///
/// The destination file.
void DumpTo(string filename);
///
/// Enable connection keepalive.
///
/// A value indicating whether the keepalive logic should be executed.
void EnableKeepalive(bool enable);
///
/// Finds the filename and length of the file matching the specified .
///
/// The fully qualified remote path of the file.
/// The filename, if found.
(string Filename, long Size) FindFileInfo(string maskedFilename);
///
/// Finds and returns the most recent scan record.
///
/// The most recent scan record, or default if no scan was found.
Scan FindLatestScan();
///
/// Flags the latest scan as suspect, indicating that the cached contents may have diverged from physical storage.
///
void FlagLatestScanAsSuspect();
///
/// Inserts a directory.
///
/// The fully qualified local name of the directory.
/// The timestamp to assign to the record.
void InsertDirectory(string name, long timestamp);
///
/// Inserts a file.
///
/// The fully qualified remote path of the file.
/// The fully qualified local path of the file.
/// The timestamp at which the file was last modified, according to the host OS.
/// The Soulseek.File instance representing the file.
/// The timestamp to assign to the record.
void InsertFile(string maskedFilename, string originalFilename, DateTime touchedAt, File file, long timestamp);
///
/// Inserts a scan record at the specified .
///
/// The timestamp associated with the scan.
/// The options snapshot at the start of the scan.
void InsertScan(long timestamp, Options.SharesOptions options);
///
/// Lists all directories.
///
/// The optional directory prefix used for listing subdirectories.
/// The list of directories.
IEnumerable ListDirectories(string parentDirectory = null);
///
/// Lists all files.
///
/// The optional parent directory.
/// A value indicating whether the fully qualified path should be returned.
/// The list of files.
IEnumerable ListFiles(string parentDirectory = null, bool includeFullPath = false);
///
/// Returns the list of all started at or after the specified
/// unix timestamp.
///
/// A unix timestamp that serves as the lower bound of the time-based listing.
/// The operation context, including the list of found scans.
IEnumerable ListScans(long startedAtOrAfter = 0);
///
/// Deletes directory records with a timestamp prior to the specified .
///
/// The timestamp before which to delete directories.
/// The number of records deleted.
long PruneDirectories(long olderThanTimestamp);
///
/// Deletes file records with a timestamp prior to the specified .
///
/// The timestamp before which to delete files.
/// The number of records deleted.
long PruneFiles(long olderThanTimestamp);
///
/// Rebuilds the filename index table using the data in the files table.
///
void RebuildFilenameIndex();
///
/// Restores the current database from the database at the specified .
///
/// The destination repository.
void RestoreFrom(IShareRepository repository);
///
/// Searches the database for files matching the specified .
///
/// The search query.
/// An optional row limit.
/// The list of matching files.
IEnumerable Search(SearchQuery query, int? limit = null);
///
/// Attempts to validate the backing database.
///
/// A value indicating whether the database is valid.
bool TryValidate();
///
/// Attempts to validate the backing database.
///
/// The list of problems, if the database is invalid.
/// A value indicating whether the database is valid.
bool TryValidate(out IEnumerable problems);
///
/// Updates the scan started at the specified to set the .
///
/// The timestamp associated with the scan.
/// The timestamp at the conclusion of the scan.
void UpdateScan(long timestamp, long end);
///
/// Reclaims unused space.
///
void Vacuum();
}
}