Skip to content

Commit

Permalink
Don't use pointers for each attribute
Browse files Browse the repository at this point in the history
We don't actually need the symbols, just the strings they point to
  • Loading branch information
shermp committed Jul 12, 2023
1 parent eb04f94 commit 9b8ce21
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/ndb/NDBDbus.cc
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ QString NDBDbus::mdGetMetadata(QString const& cID, bool compact) {
QVariantMap md = metadata->getMetadata(cID);
if (md.empty()) {
return empty;
} else if (md[*metadata->CONTENT_ID].isNull() || !md[*metadata->CONTENT_ID].isValid()) {
} else if (md[metadata->CONTENT_ID].isNull() || !md[metadata->CONTENT_ID].isValid()) {
return empty;
}
QJsonDocument jd = QJsonDocument::fromVariant(md);
Expand Down
39 changes: 22 additions & 17 deletions src/ndb/NDBMetadata.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@
Setting 16 bytes just to be safe here. */
#define VOLUME_SIZE 16

#define NDB_RESOLVE_ATTR(attr, required) \
resolveSymbolRTLD("ATTRIBUTE_" #attr, nh_symoutptr(attr)); \
if ((required) && !(attr)) \
{ \
nh_log("could not dlsym attribute %s", #attr); \
initResult = SymbolError; \
return; \
} \
if ((attr)) \
availableAttr.insert(*attr);
#define NDB_RESOLVE_ATTR(attr, required) \
resolveSymbolRTLD("ATTRIBUTE_" #attr, nh_symoutptr(tmp_attr)); \
if ((required) && !(tmp_attr)) \
{ \
nh_log("could not dlsym attribute %s", #attr); \
initResult = SymbolError; \
return; \
} \
if ((tmp_attr)) \
{ \
attr = *tmp_attr; \
availableAttr.insert(attr); \
}

namespace NDB {

Expand Down Expand Up @@ -77,6 +80,8 @@ NDBMetadata::NDBMetadata(QObject* parent) : QObject(parent) {
initResult = SymbolError;
return;
}

QString *tmp_attr = nullptr;
// Getting/setting Volume/Content attribute/keys
NDB_RESOLVE_ATTR(ATTRIBUTION, true);
NDB_RESOLVE_ATTR(CONTENT_ID, true);
Expand Down Expand Up @@ -126,7 +131,7 @@ QStringList NDBMetadata::getBookList(std::function<bool (Volume*)> filter) {
symbols.Volume__forEach(*dbName, [&](Volume *v) {
if (volIsValid(v) && filter(v)) {
QVariantMap values = getMetadata(v);
QString cID = values[*CONTENT_ID].toString();
QString cID = values[CONTENT_ID].toString();
bookList.append(cID);
}
});
Expand All @@ -140,16 +145,16 @@ QStringList NDBMetadata::getBookListAll() {
QStringList NDBMetadata::getBookListDownloaded() {
return getBookList([&](Volume* v) {
QVariantMap values = getMetadata(v);
bool isDownloaded = values[*IS_DOWNLOADED].toBool();
int filesize = values[*FILE_SIZE].toInt();
bool isDownloaded = values[IS_DOWNLOADED].toBool();
int filesize = values[FILE_SIZE].toInt();
return isDownloaded && filesize > 0;
});
}

QStringList NDBMetadata::getBookListSideloaded() {
return getBookList([&](Volume* v) {
QVariantMap values = getMetadata(v);
QString cID = values[*CONTENT_ID].toString();
QString cID = values[CONTENT_ID].toString();
return cID.startsWith("file:///");
});
}
Expand All @@ -163,18 +168,18 @@ Result NDBMetadata::setMetadata(QString const& cID, QVariantMap md) {
const QString key = i.key();
const QVariant val = i.value();
// Skip keys that don't exist, and ContentID
if (!availableAttr.contains(key) || key == *CONTENT_ID) {
if (!availableAttr.contains(key) || key == CONTENT_ID) {
NDB_DEBUG("Skipping %s", key.toUtf8().constData());
continue;
}
// Check metadata types match expected
auto type = val.userType();
bool validType = true;
if (key == *SERIES_NUMBER_FLOAT) {
if (key == SERIES_NUMBER_FLOAT) {
if (type != QMetaType::Double && type != QMetaType::Float) {
validType = false;
}
} else if (key == *FILE_SIZE && type != QMetaType::Int) {
} else if (key == FILE_SIZE && type != QMetaType::Int) {
validType = false;
} else if (type != QMetaType::QString) {
validType = false;
Expand Down
22 changes: 11 additions & 11 deletions src/ndb/NDBMetadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ class NDBMetadata : public QObject {
QStringList getBookListDownloaded();
QStringList getBookListSideloaded();

QString* ATTRIBUTION;
QString* CONTENT_ID;
QString* CONTENT_TYPE;
QString* DESCRIPTION;
QString* FILE_SIZE;
QString* IS_DOWNLOADED;
QString* SERIES;
QString* SERIES_ID;
QString* SERIES_NUMBER;
QString* SERIES_NUMBER_FLOAT;
QString* SUBTITLE;
QString ATTRIBUTION;
QString CONTENT_ID;
QString CONTENT_TYPE;
QString DESCRIPTION;
QString FILE_SIZE;
QString IS_DOWNLOADED;
QString SERIES;
QString SERIES_ID;
QString SERIES_NUMBER;
QString SERIES_NUMBER_FLOAT;
QString SUBTITLE;

private:
QString* dbName = nullptr;
Expand Down

0 comments on commit 9b8ce21

Please sign in to comment.