Cockatrice/libcockatrice_card/libcockatrice/card/printing/printing_info.h
BruebachL 749223c2dc
[Card Database] Improve loading times through binary cache (#7051)
* [Card Database] Improve loading times through binary cache

Took 10 minutes


Took 9 minutes

Took 16 seconds

* [Card Database] Remove lib qt include

Took 18 minutes


Took 14 seconds

* Downgrade to 6.3 datastream

Took 5 minutes

* go up to 6.4 datastream

Took 1 minute

* Address comments

* Small bug fixes

Took 20 minutes


Took 10 seconds

* More fixes.

Took 4 minutes


Took 4 seconds

* Even more fixes.

Took 11 minutes


Took 4 seconds

* More fixes.

Took 6 minutes

Took 26 seconds

Took 8 minutes

* Namespace instead of class

Took 6 minutes

---------

Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
2026-07-27 23:12:53 +02:00

161 lines
4.8 KiB
C++

#ifndef COCKATRICE_PRINTING_INFO_H
#define COCKATRICE_PRINTING_INFO_H
#include "../set/card_set.h"
#include <QList>
#include <QMap>
#include <QMutex>
#include <QSharedPointer>
#include <QVariant>
class PrintingInfo;
using SetToPrintingsMap = QMap<QString, QList<PrintingInfo>>;
/**
* @class PrintingInfo
* @ingroup CardPrintings
*
* @brief Represents metadata for a specific variation of a card within a set.
*
* A card can have multiple variations across sets. PrintingInfo associates
* a card with one such variation, and provides per-printing attributes
* such as identifiers or additional properties.
*
* Equality is defined as both the set and the property values being equal.
*/
class PrintingInfo
{
public:
/**
* @brief Constructs a PrintingInfo associated with a specific set.
*
* @param _set The set this printing belongs to (defaults to null).
*/
explicit PrintingInfo(const CardSetPtr &_set = nullptr);
/**
* @brief Destroys the PrintingInfo.
*
* Defaulted since no special cleanup is required.
*/
~PrintingInfo() = default;
/**
* @brief Equality operator.
*
* Two PrintingInfo objects are equal if they refer to the same set
* and contain the exact same property key/value pairs.
*
* @param other Another PrintingInfo to compare against.
* @return True if both set and properties are equal, otherwise false.
*/
bool operator==(const PrintingInfo &other) const
{
return this->set == other.set && this->getPropertiesHash() == other.getPropertiesHash();
}
/**
* @brief check if the info is empty, as if default constructed.
*
* @return True if both set and properties are empty, otherwise false.
*/
bool isEmpty() const
{
return set == nullptr && getPropertiesHash().isEmpty();
}
private:
CardSetPtr set; ///< The set this variation belongs to.
// Properties are stored as a pre-serialized blob (cheap to load) and the
// QVariantHash is materialized on first query. This avoids constructing
// thousands of QVariants per card at database-load time.
mutable QByteArray propertiesBlob; ///< Serialized properties (load form).
mutable QVariantHash propertiesCache; ///< Materialized properties (query form).
mutable bool propertiesLoaded = false; ///< Whether propertiesCache is valid.
mutable QSharedPointer<QBasicMutex> propertiesMutex =
QSharedPointer<QBasicMutex>::create(); ///< Guards lazy materialization.
void ensurePropertiesLoaded() const;
public:
/**
* @brief Returns the set this printing belongs to.
*
* @return Pointer to the associated CardSet.
*/
[[nodiscard]] CardSetPtr getSet() const
{
return set;
}
/**
* @brief Returns the list of property names defined for this printing.
*
* @return List of keys stored in the properties map.
*/
[[nodiscard]] QStringList getProperties() const
{
return getPropertiesHash().keys();
}
[[nodiscard]] const QVariantHash &getPropertiesHash() const
{
ensurePropertiesLoaded();
return propertiesCache;
}
/**
* @brief Retrieves the value of a specific property.
*
* @param propertyName The key name of the property to query.
* @return The property value as a string, or an empty string if not set.
*/
[[nodiscard]] QString getProperty(const QString &propertyName) const
{
return getPropertiesHash().value(propertyName).toString();
}
/**
* @brief Sets or updates the value of a specific property.
*
* If the property already exists, its value is replaced.
*
* @param _name The name of the property.
* @param _value The string value to assign.
*/
void setProperty(const QString &_name, const QString &_value);
void setProperties(const QVariantHash &_props);
/**
* @brief Stores the pre-serialized properties blob and marks the materialized
* cache as invalid. Used by the binary cache reader to avoid building a
* QVariantHash at load time.
* @param _blob The serialized properties (as written by the cache writer).
*/
void setPropertiesBlob(QByteArray _blob) const
{
QMutexLocker lock(propertiesMutex.data());
propertiesBlob = std::move(_blob);
propertiesLoaded = false;
propertiesCache.clear();
}
/**
* @brief Returns the providerID for this printing.
*
* @return A string representing the providerID.
*/
[[nodiscard]] QString getUuid() const;
/**
* @brief Returns the flavorName for this printing.
*
* @return The flavorName, or empty if it isn't present.
*/
[[nodiscard]] QString getFlavorName() const;
};
#endif // COCKATRICE_PRINTING_INFO_H