mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-09-21 09:05:10 -07:00
[CardInfo] use QString QHash instead of QVariantHash for properties (#7063)
Some checks failed
Build Desktop / Configure (push) Has been cancelled
Build Docker Image / amd64 & arm64 (push) Has been cancelled
Build Desktop / Debian 13 (push) Has been cancelled
Build Desktop / Debian 12 (push) Has been cancelled
Build Desktop / Fedora 44 (push) Has been cancelled
Build Desktop / Fedora 43 (push) Has been cancelled
Build Desktop / Servatrice_Debian 12 (push) Has been cancelled
Build Desktop / Ubuntu 26.04 (push) Has been cancelled
Build Desktop / Ubuntu 24.04 (push) Has been cancelled
Build Desktop / Arch (push) Has been cancelled
Build Desktop / macOS 14 (push) Has been cancelled
Build Desktop / macOS 15 (push) Has been cancelled
Build Desktop / macOS 13 Intel (push) Has been cancelled
Build Desktop / macOS 15 Debug (push) Has been cancelled
Build Desktop / Windows 10 (push) Has been cancelled
Some checks failed
Build Desktop / Configure (push) Has been cancelled
Build Docker Image / amd64 & arm64 (push) Has been cancelled
Build Desktop / Debian 13 (push) Has been cancelled
Build Desktop / Debian 12 (push) Has been cancelled
Build Desktop / Fedora 44 (push) Has been cancelled
Build Desktop / Fedora 43 (push) Has been cancelled
Build Desktop / Servatrice_Debian 12 (push) Has been cancelled
Build Desktop / Ubuntu 26.04 (push) Has been cancelled
Build Desktop / Ubuntu 24.04 (push) Has been cancelled
Build Desktop / Arch (push) Has been cancelled
Build Desktop / macOS 14 (push) Has been cancelled
Build Desktop / macOS 15 (push) Has been cancelled
Build Desktop / macOS 13 Intel (push) Has been cancelled
Build Desktop / macOS 15 Debug (push) Has been cancelled
Build Desktop / Windows 10 (push) Has been cancelled
* [CardInfo] use QString QHash instead of QVariantHash for properties * bump CACHE_VERSION * cleanups
This commit is contained in:
parent
b44dcf5951
commit
1ed9823b56
11 changed files with 70 additions and 73 deletions
|
|
@ -12,7 +12,6 @@
|
|||
#include <QRegularExpression>
|
||||
#include <QSharedPointer>
|
||||
#include <QString>
|
||||
#include <QVariant>
|
||||
#include <algorithm>
|
||||
#include <utility>
|
||||
|
||||
|
|
@ -24,7 +23,7 @@ using CardInfoPtr = QSharedPointer<CardInfo>;
|
|||
|
||||
namespace
|
||||
{
|
||||
QByteArray serializeProperties(const QVariantHash &props)
|
||||
QByteArray serializeProperties(const QHash<QString, QString> &props)
|
||||
{
|
||||
QByteArray blob;
|
||||
QDataStream out(&blob, QIODevice::WriteOnly);
|
||||
|
|
@ -48,7 +47,7 @@ void CardInfo::ensurePropertiesLoaded() const
|
|||
propertiesLoaded = true;
|
||||
}
|
||||
|
||||
const QVariantHash &CardInfo::getPropertiesHash() const
|
||||
const QHash<QString, QString> &CardInfo::getPropertiesHash() const
|
||||
{
|
||||
ensurePropertiesLoaded();
|
||||
return propertiesCache;
|
||||
|
|
@ -57,7 +56,7 @@ const QVariantHash &CardInfo::getPropertiesHash() const
|
|||
void CardInfo::setProperty(const QString &_name, const QString &_value)
|
||||
{
|
||||
ensurePropertiesLoaded();
|
||||
if (propertiesCache.value(_name).toString() == _value) {
|
||||
if (propertiesCache.value(_name) == _value) {
|
||||
return;
|
||||
}
|
||||
propertiesCache.insert(_name, _value);
|
||||
|
|
@ -65,7 +64,7 @@ void CardInfo::setProperty(const QString &_name, const QString &_value)
|
|||
emit cardInfoChanged(smartThis);
|
||||
}
|
||||
|
||||
void CardInfo::setProperties(const QVariantHash &_props)
|
||||
void CardInfo::setProperties(const QHash<QString, QString> &_props)
|
||||
{
|
||||
ensurePropertiesLoaded();
|
||||
propertiesCache = _props;
|
||||
|
|
@ -76,7 +75,7 @@ void CardInfo::setProperties(const QVariantHash &_props)
|
|||
CardInfo::CardInfo(const QString &_name,
|
||||
const QString &_text,
|
||||
bool _isToken,
|
||||
QVariantHash _properties,
|
||||
QHash<QString, QString> _properties,
|
||||
const QList<CardRelation *> &_relatedCards,
|
||||
const QList<CardRelation *> &_reverseRelatedCards,
|
||||
SetToPrintingsMap _sets,
|
||||
|
|
@ -122,7 +121,7 @@ CardInfoPtr CardInfo::newInstance(const QString &_name)
|
|||
CardInfoPtr CardInfo::newInstance(const QString &_name,
|
||||
const QString &_text,
|
||||
bool _isToken,
|
||||
QVariantHash _properties,
|
||||
QHash<QString, QString> _properties,
|
||||
const QList<CardRelation *> &_relatedCards,
|
||||
const QList<CardRelation *> &_reverseRelatedCards,
|
||||
SetToPrintingsMap _sets,
|
||||
|
|
@ -210,10 +209,10 @@ void CardInfo::addToSet(const CardSetPtr &_set, const PrintingInfo &_info)
|
|||
refreshCachedSets();
|
||||
}
|
||||
|
||||
void CardInfo::combineLegalities(const QVariantHash &props)
|
||||
void CardInfo::combineLegalities(const QHash<QString, QString> &props)
|
||||
{
|
||||
ensurePropertiesLoaded();
|
||||
QHashIterator<QString, QVariant> it(props);
|
||||
QHashIterator it(props);
|
||||
while (it.hasNext()) {
|
||||
it.next();
|
||||
if (it.key().startsWith("format-")) {
|
||||
|
|
|
|||
|
|
@ -76,12 +76,12 @@ private:
|
|||
QString text; ///< Text description or rules text of the card.
|
||||
bool isToken; ///< Whether this card is a token or not.
|
||||
// Properties are stored as a pre-serialized blob (cheap to load) and the
|
||||
// QVariantHash is materialized on first query, so database load avoids
|
||||
// constructing thousands of QVariants per card.
|
||||
mutable QByteArray propertiesBlob; ///< Serialized properties (load form).
|
||||
mutable QVariantHash propertiesCache; ///< Materialized properties (query form).
|
||||
mutable bool propertiesLoaded = false; ///< Whether propertiesCache is valid.
|
||||
mutable QMutex propertiesMutex; ///< Guards lazy materialization.
|
||||
// QHash<QString, QString> is materialized on first query, so database load avoids
|
||||
// constructing thousands of QStrings per card.
|
||||
mutable QByteArray propertiesBlob; ///< Serialized properties (load form).
|
||||
mutable QHash<QString, QString> propertiesCache; ///< Materialized properties (query form).
|
||||
mutable bool propertiesLoaded = false; ///< Whether propertiesCache is valid.
|
||||
mutable QMutex propertiesMutex; ///< Guards lazy materialization.
|
||||
|
||||
/**
|
||||
* @brief Materializes propertiesCache from propertiesBlob if not already done.
|
||||
|
|
@ -114,7 +114,7 @@ public:
|
|||
explicit CardInfo(const QString &_name,
|
||||
const QString &_text,
|
||||
bool _isToken,
|
||||
QVariantHash _properties,
|
||||
QHash<QString, QString> _properties,
|
||||
const QList<CardRelation *> &_relatedCards,
|
||||
const QList<CardRelation *> &_reverseRelatedCards,
|
||||
SetToPrintingsMap _sets,
|
||||
|
|
@ -127,7 +127,7 @@ public:
|
|||
* Used by the binary cache reader to skip recomputing @p _simpleName and
|
||||
* @p _altNames (which otherwise require a Unicode normalization and a full
|
||||
* printing scan). Properties are supplied as a pre-serialized @p _propertiesBlob
|
||||
* so the QVariantHash is not built at load time (it is materialized on first
|
||||
* so the QHash<QString, QString> is not built at load time (it is materialized on first
|
||||
* query).
|
||||
*
|
||||
* @param _name The card name.
|
||||
|
|
@ -194,7 +194,7 @@ public:
|
|||
static CardInfoPtr newInstance(const QString &_name,
|
||||
const QString &_text,
|
||||
bool _isToken,
|
||||
QVariantHash _properties,
|
||||
QHash<QString, QString> _properties,
|
||||
const QList<CardRelation *> &_relatedCards,
|
||||
const QList<CardRelation *> &_reverseRelatedCards,
|
||||
SetToPrintingsMap _sets,
|
||||
|
|
@ -288,12 +288,12 @@ public:
|
|||
{
|
||||
return getPropertiesHash().keys();
|
||||
}
|
||||
[[nodiscard]] const QVariantHash &getPropertiesHash() const;
|
||||
[[nodiscard]] const QHash<QString, QString> &getPropertiesHash() const;
|
||||
|
||||
/**
|
||||
* @brief Stores the pre-serialized properties blob and invalidates the
|
||||
* materialized cache. Used by the binary cache reader so the
|
||||
* QVariantHash is not built at load time.
|
||||
* QHash<QString, QString> is not built at load time.
|
||||
* @param _blob The serialized properties (as written by the cache writer).
|
||||
*/
|
||||
void setPropertiesBlob(QByteArray _blob) const
|
||||
|
|
@ -305,10 +305,10 @@ public:
|
|||
}
|
||||
[[nodiscard]] QString getProperty(const QString &propertyName) const
|
||||
{
|
||||
return getPropertiesHash().value(propertyName).toString();
|
||||
return getPropertiesHash().value(propertyName);
|
||||
}
|
||||
void setProperty(const QString &_name, const QString &_value);
|
||||
void setProperties(const QVariantHash &_props);
|
||||
void setProperties(const QHash<QString, QString> &_props);
|
||||
[[nodiscard]] bool hasProperty(const QString &propertyName) const
|
||||
{
|
||||
return getPropertiesHash().contains(propertyName);
|
||||
|
|
@ -415,7 +415,7 @@ public:
|
|||
*
|
||||
* @param props Key-value mapping of format legalities.
|
||||
*/
|
||||
void combineLegalities(const QVariantHash &props);
|
||||
void combineLegalities(const QHash<QString, QString> &props);
|
||||
|
||||
/**
|
||||
* @brief Refreshes all cached fields that are calculated from the contained sets and printings.
|
||||
|
|
|
|||
|
|
@ -66,7 +66,7 @@ QVariant CardInfoComparator::getProperty(const CardInfoPtr &card, const QString
|
|||
return card->getIsToken();
|
||||
}
|
||||
|
||||
// Otherwise, check if it's a custom property in the QVariantHash
|
||||
// Otherwise, check if it's a custom property in the properties hash
|
||||
if (card->hasProperty(property)) {
|
||||
return card->getProperty(property);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,12 +13,11 @@
|
|||
#include <QElapsedTimer>
|
||||
#include <QFile>
|
||||
#include <QSaveFile>
|
||||
#include <QVariantHash>
|
||||
|
||||
namespace
|
||||
{
|
||||
constexpr quint32 CACHE_MAGIC = 0x43445243; // "CDRC"
|
||||
constexpr quint32 CACHE_VERSION = 1;
|
||||
constexpr quint32 CACHE_VERSION = 2;
|
||||
|
||||
// ---- Primitives -----------------------------------------------------------
|
||||
|
||||
|
|
@ -34,11 +33,11 @@ QString readString(QDataStream &in)
|
|||
return s;
|
||||
}
|
||||
|
||||
// Stores a QVariantHash as a single pre-serialized blob. The reader keeps the
|
||||
// blob as-is and materializes the QVariantHash lazily on first query, which is
|
||||
// Stores a QHash<QString, QString> as a single pre-serialized blob. The reader keeps the
|
||||
// blob as-is and materializes the QHash<QString, QString> lazily on first query, which is
|
||||
// what removes the allocation storm from database load (see card_info.cpp /
|
||||
// printing_info.cpp).
|
||||
void writeHashBlob(QDataStream &out, const QVariantHash &h)
|
||||
void writeHashBlob(QDataStream &out, const QHash<QString, QString> &h)
|
||||
{
|
||||
QByteArray blob;
|
||||
QDataStream blobOut(&blob, QIODevice::WriteOnly);
|
||||
|
|
|
|||
|
|
@ -172,7 +172,7 @@ void CockatriceXml3Parser::loadCardsFromXml(QXmlStreamReader &xml)
|
|||
if (xmlName == "card") {
|
||||
QString name = QString("");
|
||||
QString text = QString("");
|
||||
QVariantHash properties = QVariantHash();
|
||||
QHash<QString, QString> properties;
|
||||
QString colors = QString("");
|
||||
QList<CardRelation *> relatedCards, reverseRelatedCards;
|
||||
auto _sets = SetToPrintingsMap();
|
||||
|
|
@ -229,7 +229,7 @@ void CockatriceXml3Parser::loadCardsFromXml(QXmlStreamReader &xml)
|
|||
// behaviour. Without this check, disabling a set has no effect on v3 databases.
|
||||
if (set->getEnabled()) {
|
||||
PrintingInfo setInfo(set);
|
||||
QVariantHash printingProps;
|
||||
QHash<QString, QString> printingProps;
|
||||
if (attrs.hasAttribute("muId")) {
|
||||
printingProps.insert("muid", attrs.value("muId").toString());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -243,9 +243,9 @@ void CockatriceXml4Parser::loadSetsFromXml(QXmlStreamReader &xml)
|
|||
}
|
||||
}
|
||||
|
||||
QVariantHash CockatriceXml4Parser::loadCardPropertiesFromXml(QXmlStreamReader &xml)
|
||||
QHash<QString, QString> CockatriceXml4Parser::loadCardPropertiesFromXml(QXmlStreamReader &xml)
|
||||
{
|
||||
QVariantHash properties = QVariantHash();
|
||||
QHash<QString, QString> properties;
|
||||
while (!xml.atEnd()) {
|
||||
if (xml.readNext() == QXmlStreamReader::EndElement) {
|
||||
break;
|
||||
|
|
@ -272,7 +272,7 @@ void CockatriceXml4Parser::loadCardsFromXml(QXmlStreamReader &xml)
|
|||
if (xmlName == "card") {
|
||||
QString name = QString("");
|
||||
QString text = QString("");
|
||||
QVariantHash properties = QVariantHash();
|
||||
QHash<QString, QString> properties;
|
||||
QList<CardRelation *> relatedCards, reverseRelatedCards;
|
||||
auto _sets = SetToPrintingsMap();
|
||||
int tableRow = 0;
|
||||
|
|
@ -315,7 +315,7 @@ void CockatriceXml4Parser::loadCardsFromXml(QXmlStreamReader &xml)
|
|||
auto set = internalAddSet(setName);
|
||||
if (set->getEnabled()) {
|
||||
PrintingInfo printingInfo(set);
|
||||
QVariantHash printingProps;
|
||||
QHash<QString, QString> printingProps;
|
||||
for (QXmlStreamAttribute attr : attrs) {
|
||||
QString attrName = attr.name().toString();
|
||||
if (attrName == "picURL") {
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ inline Q_LOGGING_CATEGORY(CockatriceXml4Log, "cockatrice_xml.xml_4_parser");
|
|||
* making the parser more extensible and schema-compliant.
|
||||
*
|
||||
* @note Differences from v3:
|
||||
* - Card properties are stored in <prop> blocks as a QVariantHash.
|
||||
* - Card properties are stored in <prop> blocks as a QHash<QString, QString>.
|
||||
* - Sets can include a <priority> element.
|
||||
* - Supports user preferences via ICardPreferenceProvider (e.g., skipping rebalanced cards).
|
||||
* - Related cards support persistent relations and multiple attach types (e.g., transform).
|
||||
|
|
@ -70,9 +70,9 @@ private:
|
|||
/**
|
||||
* @brief Loads a generic <prop> block from a <card> element.
|
||||
* @param xml The open QXmlStreamReader positioned at a <prop> element.
|
||||
* @return A QVariantHash mapping property names to values.
|
||||
* @return A QHash<QString, QString> mapping property names to values.
|
||||
*/
|
||||
QVariantHash loadCardPropertiesFromXml(QXmlStreamReader &xml);
|
||||
QHash<QString, QString> loadCardPropertiesFromXml(QXmlStreamReader &xml);
|
||||
|
||||
/**
|
||||
* @brief Load all <card> elements from the XML stream.
|
||||
|
|
|
|||
|
|
@ -27,14 +27,14 @@ void PrintingInfo::ensurePropertiesLoaded() const
|
|||
void PrintingInfo::setProperty(const QString &_name, const QString &_value)
|
||||
{
|
||||
ensurePropertiesLoaded();
|
||||
if (propertiesCache.value(_name).toString() == _value) {
|
||||
if (propertiesCache.value(_name) == _value) {
|
||||
return;
|
||||
}
|
||||
propertiesCache.insert(_name, _value);
|
||||
setProperties(propertiesCache);
|
||||
}
|
||||
|
||||
void PrintingInfo::setProperties(const QVariantHash &_props)
|
||||
void PrintingInfo::setProperties(const QHash<QString, QString> &_props)
|
||||
{
|
||||
ensurePropertiesLoaded();
|
||||
propertiesCache = _props;
|
||||
|
|
@ -48,10 +48,10 @@ void PrintingInfo::setProperties(const QVariantHash &_props)
|
|||
*/
|
||||
QString PrintingInfo::getUuid() const
|
||||
{
|
||||
return getPropertiesHash().value("uuid").toString();
|
||||
return getPropertiesHash().value("uuid");
|
||||
}
|
||||
|
||||
QString PrintingInfo::getFlavorName() const
|
||||
{
|
||||
return getPropertiesHash().value("flavorName").toString();
|
||||
}
|
||||
return getPropertiesHash().value("flavorName");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -70,11 +70,11 @@ 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.
|
||||
// QHash<QString, QString> is materialized on first query. This avoids constructing
|
||||
// thousands of QStrings per card at database-load time.
|
||||
mutable QByteArray propertiesBlob; ///< Serialized properties (load form).
|
||||
mutable QHash<QString, QString> propertiesCache; ///< Materialized properties (query form).
|
||||
mutable bool propertiesLoaded = false; ///< Whether propertiesCache is valid.
|
||||
mutable QSharedPointer<QBasicMutex> propertiesMutex =
|
||||
QSharedPointer<QBasicMutex>::create(); ///< Guards lazy materialization.
|
||||
|
||||
|
|
@ -101,7 +101,7 @@ public:
|
|||
return getPropertiesHash().keys();
|
||||
}
|
||||
|
||||
[[nodiscard]] const QVariantHash &getPropertiesHash() const
|
||||
[[nodiscard]] const QHash<QString, QString> &getPropertiesHash() const
|
||||
{
|
||||
ensurePropertiesLoaded();
|
||||
return propertiesCache;
|
||||
|
|
@ -115,7 +115,7 @@ public:
|
|||
*/
|
||||
[[nodiscard]] QString getProperty(const QString &propertyName) const
|
||||
{
|
||||
return getPropertiesHash().value(propertyName).toString();
|
||||
return getPropertiesHash().value(propertyName);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -127,12 +127,12 @@ public:
|
|||
* @param _value The string value to assign.
|
||||
*/
|
||||
void setProperty(const QString &_name, const QString &_value);
|
||||
void setProperties(const QVariantHash &_props);
|
||||
void setProperties(const QHash<QString, QString> &_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.
|
||||
* QHash<QString, QString> at load time.
|
||||
* @param _blob The serialized properties (as written by the cache writer).
|
||||
*/
|
||||
void setPropertiesBlob(QByteArray _blob) const
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue