Scan tracker: take into account MediaLibrary

This commit is contained in:
emeric
2024-03-18 12:01:48 +01:00
parent ed1be02aaf
commit 5bfd31ff8a
@@ -47,21 +47,23 @@ namespace lms::api::subsonic
class ScanTracker class ScanTracker
{ {
public: public:
ObjectId extractLastRetrievedObjectId(const ClientInfo& info, std::size_t offset); struct ScanInfo
void setObjectId(const ClientInfo& info, std::size_t offset, ObjectId lastRetrievedId); {
std::string clientAddress;
std::string clientName;
std::string userName;
MediaLibraryId library;
std::size_t offset{};
auto operator<=>(const ScanInfo&) const = default;
};
ObjectId extractLastRetrievedObjectId(const ScanInfo& info);
void setObjectId(const ScanInfo& info, ObjectId lastRetrievedId);
void cleanOutdatedScanEntries(); void cleanOutdatedScanEntries();
private: private:
using ClockType = std::chrono::steady_clock; using ClockType = std::chrono::steady_clock;
struct Client
{
std::string clientAddress;
std::string clientName;
std::string userName;
std::size_t offset{};
auto operator<=>(const Client&) const = default;
};
struct Entry struct Entry
{ {
ClockType::time_point timePoint; ClockType::time_point timePoint;
@@ -71,16 +73,16 @@ namespace lms::api::subsonic
static constexpr ClockType::duration maxEntryDuration{ std::chrono::seconds{30} }; static constexpr ClockType::duration maxEntryDuration{ std::chrono::seconds{30} };
std::mutex _mutex; std::mutex _mutex;
std::map<Client, Entry> _ongoingScans; std::map<ScanInfo, Entry> _ongoingScans;
}; };
template<typename ObjectId> template<typename ObjectId>
ObjectId ScanTracker<ObjectId>::extractLastRetrievedObjectId(const ClientInfo& info, std::size_t offset) ObjectId ScanTracker<ObjectId>::extractLastRetrievedObjectId(const ScanInfo& scanInfo)
{ {
ObjectId res; ObjectId res;
const std::scoped_lock lock{ _mutex }; const std::scoped_lock lock{ _mutex };
auto it{ _ongoingScans.find({ info.ipAddress, info.name, info.user, offset }) }; auto it{ _ongoingScans.find(scanInfo) };
if (it != _ongoingScans.end()) if (it != _ongoingScans.end())
{ {
res = it->second.objectId; res = it->second.objectId;
@@ -91,10 +93,10 @@ namespace lms::api::subsonic
} }
template<typename ObjectId> template<typename ObjectId>
void ScanTracker<ObjectId>::setObjectId(const ClientInfo& info, std::size_t offset, ObjectId lastRetrievedId) void ScanTracker<ObjectId>::setObjectId(const ScanInfo& scanInfo, ObjectId lastRetrievedId)
{ {
const std::scoped_lock lock{ _mutex }; const std::scoped_lock lock{ _mutex };
_ongoingScans[Client{ info.ipAddress, info.name, info.user, offset }] = { ClockType::now(), lastRetrievedId }; _ongoingScans[scanInfo] = { ClockType::now(), lastRetrievedId };
} }
template<typename ObjectId> template<typename ObjectId>
@@ -201,7 +203,16 @@ namespace lms::api::subsonic
} }
else else
{ {
if (TrackId cachedLastRetrievedId{ currentScansInProgress.extractLastRetrievedObjectId(context.clientInfo, songOffset) }; cachedLastRetrievedId.isValid()) ScanTracker<TrackId>::ScanInfo scanInfo
{
.clientAddress = context.clientInfo.ipAddress,
.clientName = context.clientInfo.name,
.userName = context.clientInfo.user,
.library = mediaLibrary,
.offset = songOffset
};
if (TrackId cachedLastRetrievedId{ currentScansInProgress.extractLastRetrievedObjectId(scanInfo) }; cachedLastRetrievedId.isValid())
{ {
Track::find(context.dbSession, cachedLastRetrievedId, songCount, [&](const Track::pointer& track) Track::find(context.dbSession, cachedLastRetrievedId, songCount, [&](const Track::pointer& track)
{ {
@@ -215,7 +226,10 @@ namespace lms::api::subsonic
} }
if (lastRetrievedId.isValid()) if (lastRetrievedId.isValid())
currentScansInProgress.setObjectId(context.clientInfo, songOffset + songCount, lastRetrievedId); {
scanInfo.offset = songOffset + songCount;
currentScansInProgress.setObjectId(scanInfo, lastRetrievedId);
}
} }
} }