Commit 193d3a67 authored by Daniel Vrátil's avatar Daniel Vrátil 🤖

Remove Akonadi::Entity and Akonadi::AttributeEntity

Collection and Item now have each their own implementation of attribute handling
(which we can factor out into some shared code later), and by not having to deal
with the polymorphism of Entity, we can get rid of vtable in ItemPrivate and
CollectionPrivate and better re-order member variables. This saves us 16 bytes
per Item and 32 (!) bytes per Collection. Tag now requires 8 bytes more
of memory due to TagPrivate being QSharedData (but we save something on Tag now).

We could also remove the copy-d_ptr-via-temporary-assignment from from Item,
because the usecase only really affects Collections. This should make copying
Items a bit faster (the assignEntityPrivate() workaround was showing up in
callgrind quite high).
parent 89751162
......@@ -369,7 +369,7 @@ private Q_SLOTS:
QFETCH(bool, keepLocalChanges);
const QString resource(QStringLiteral("akonadi_knut_resource_0"));
Collection col = fetchCollections(resource).first();
col.attribute<EntityDisplayAttribute>(Akonadi::Entity::AddIfMissing)->setDisplayName(QStringLiteral("foo"));
col.attribute<EntityDisplayAttribute>(Akonadi::Collection::AddIfMissing)->setDisplayName(QStringLiteral("foo"));
col.setContentMimeTypes(QStringList() << Akonadi::Collection::mimeType() << QStringLiteral("foo"));
{
CollectionModifyJob *job = new CollectionModifyJob(col);
......
......@@ -156,13 +156,13 @@ private Q_SLOTS:
QSignalSpy spy(&cache, SIGNAL(dataAvailable()));
QVERIFY(spy.isValid());
cache.request(QList<Entity::Id>() << 1 << 2 << 3, scope);
cache.request(QList<Item::Id>() << 1 << 2 << 3, scope);
QTRY_COMPARE(spy.count(), 1);
QVERIFY(cache.isCached(QList<Entity::Id>() << 1 << 2 << 3));
QVERIFY(cache.isCached(QList<Item::Id>() << 1 << 2 << 3));
cache.ensureCached(QList<Entity::Id>() << 1 << 2 << 3 << 4, scope);
cache.ensureCached(QList<Item::Id>() << 1 << 2 << 3 << 4, scope);
QTRY_COMPARE(spy.count(), 2);
QVERIFY(cache.isCached(QList<Entity::Id>() << 1 << 2 << 3 << 4));
QVERIFY(cache.isCached(QList<Item::Id>() << 1 << 2 << 3 << 4));
}
};
......
......@@ -97,7 +97,7 @@ protected:
Akonadi::Tag m_parentTag;
QHash<Akonadi::Collection::Id, Akonadi::Collection> m_collections;
QHash<Akonadi::Item::Id, Akonadi::Item> m_items;
QHash<Akonadi::Item::Id, QList<Akonadi::Entity::Id> > m_childElements;
QHash<Akonadi::Collection::Id, QList<Akonadi::Collection::Id> > m_childElements;
QHash<Akonadi::Tag::Id, Akonadi::Tag> m_tags;
private:
......
......@@ -37,7 +37,7 @@ FakeServerData::FakeServerData(EntityTreeModel *model, FakeSession *session, Fak
// the slot gets called
connect(session, &FakeSession::jobAdded,
[this](Akonadi::Job * job) {
Entity::Id fetchColId = job->property("FetchCollectionId").toULongLong();
Collection::Id fetchColId = job->property("FetchCollectionId").toULongLong();
QTimer::singleShot(0, [this, fetchColId]() {
jobAdded(fetchColId);
});
......@@ -65,7 +65,7 @@ FakeServerData::FakeServerData(TagModel *model, FakeSession *session, FakeMonito
void FakeServerData::setCommands(QList< FakeAkonadiServerCommand * > list)
{
m_communicationQueue.clear();
foreach (FakeAkonadiServerCommand *command, list) {
Q_FOREACH (FakeAkonadiServerCommand *command, list) {
m_communicationQueue << command;
}
}
......@@ -97,7 +97,7 @@ void FakeServerData::jobAdded()
processNotifications();
}
void FakeServerData::returnEntities(Entity::Id fetchColId)
void FakeServerData::returnEntities(Collection::Id fetchColId)
{
if (!returnCollections(fetchColId)) {
while (!m_communicationQueue.isEmpty() && m_communicationQueue.head()->respondTo() == FakeAkonadiServerCommand::RespondToItemFetch) {
......@@ -108,7 +108,7 @@ void FakeServerData::returnEntities(Entity::Id fetchColId)
processNotifications();
}
bool FakeServerData::returnCollections(Entity::Id fetchColId)
bool FakeServerData::returnCollections(Collection::Id fetchColId)
{
if (m_communicationQueue.isEmpty()) {
return true;
......@@ -129,7 +129,7 @@ bool FakeServerData::returnCollections(Entity::Id fetchColId)
return false;
}
void FakeServerData::returnItems(Entity::Id fetchColId)
void FakeServerData::returnItems(Item::Id fetchColId)
{
FakeAkonadiServerCommand::Type commType = m_communicationQueue.head()->respondTo();
......@@ -150,4 +150,4 @@ void FakeServerData::returnTags()
FakeAkonadiServerCommand *command = m_communicationQueue.dequeue();
command->doCommand();
}
}
\ No newline at end of file
}
......@@ -41,11 +41,11 @@ public:
void setCommands(QList<FakeAkonadiServerCommand *> list);
Entity::Id nextCollectionId() const
Collection::Id nextCollectionId() const
{
return m_nextCollectionId++;
}
Entity::Id nextItemId() const
Item::Id nextItemId() const
{
return m_nextItemId++;
}
......@@ -66,9 +66,9 @@ private Q_SLOTS:
void jobAdded();
private:
bool returnCollections(Entity::Id fetchColId);
void returnItems(Entity::Id fetchColId);
void returnEntities(Entity::Id fetchColId);
bool returnCollections(Collection::Id fetchColId);
void returnItems(Item::Id fetchColId);
void returnEntities(Collection::Id fetchColId);
void returnTags();
private:
......@@ -79,8 +79,8 @@ private:
QList<FakeAkonadiServerCommand *> m_commandList;
QQueue<FakeAkonadiServerCommand *> m_communicationQueue;
mutable Entity::Id m_nextCollectionId;
mutable Entity::Id m_nextItemId;
mutable Collection::Id m_nextCollectionId;
mutable Item::Id m_nextItemId;
mutable Tag::Id m_nextTagId;
};
......
......@@ -159,6 +159,7 @@ private Q_SLOTS:
const Collection p2(collection.parentCollection());
parsedCollection = p1;
collection = p2;
qDebug() << p1.isValid() << p2.isValid();
}
}
......
......@@ -349,7 +349,7 @@ void TagTest::testModify()
//We can add an attribute
{
Akonadi::TagAttribute *attr = tag.attribute<Akonadi::TagAttribute>(AttributeEntity::AddIfMissing);
Akonadi::TagAttribute *attr = tag.attribute<Akonadi::TagAttribute>(Tag::AddIfMissing);
attr->setDisplayName(QStringLiteral("display name"));
tag.addAttribute(attr);
tag.setParent(Tag(0));
......@@ -365,7 +365,7 @@ void TagTest::testModify()
}
//We can update an attribute
{
Akonadi::TagAttribute *attr = tag.attribute<Akonadi::TagAttribute>(AttributeEntity::AddIfMissing);
Akonadi::TagAttribute *attr = tag.attribute<Akonadi::TagAttribute>(Tag::AddIfMissing);
attr->setDisplayName(QStringLiteral("display name2"));
TagModifyJob *modJob = new TagModifyJob(tag, this);
AKVERIFYEXEC(modJob);
......@@ -451,7 +451,7 @@ void TagTest::testAttributes()
Tag tag;
{
tag.setGid("gid2");
TagAttribute *attr = tag.attribute<TagAttribute>(AttributeEntity::AddIfMissing);
TagAttribute *attr = tag.attribute<TagAttribute>(Tag::AddIfMissing);
attr->setDisplayName(QStringLiteral("name"));
attr->setInToolbar(true);
tag.addAttribute(attr);
......@@ -478,7 +478,7 @@ void TagTest::testAttributes()
Tag tag2;
{
tag2.setGid("gid22");
TagAttribute *attr = tag.attribute<TagAttribute>(AttributeEntity::AddIfMissing);
TagAttribute *attr = tag.attribute<TagAttribute>(Tag::AddIfMissing);
attr->setDisplayName(QStringLiteral("name2"));
attr->setInToolbar(true);
tag2.addAttribute(attr);
......
......@@ -5,7 +5,6 @@ set(akonadicore_base_SRCS
agenttype.cpp
asyncselectionhandler.cpp
attribute.cpp
attributeentity.cpp
attributefactory.cpp
cachepolicy.cpp
changemediator_p.cpp
......@@ -24,7 +23,6 @@ set(akonadicore_base_SRCS
conflicthandler.cpp
collectionidentificationattribute.cpp
control.cpp
entity.cpp
entityannotationsattribute.cpp
entitycache.cpp
entitydeletedattribute.cpp
......@@ -76,7 +74,6 @@ ecm_generate_headers(AkonadiCore_base_HEADERS
AgentManager
AgentType
Attribute
AttributeEntity
AttributeFactory
CachePolicy
ChangeRecorder
......@@ -88,7 +85,6 @@ ecm_generate_headers(AkonadiCore_base_HEADERS
CollectionIdentificationAttribute
Control
DifferencesAlgorithmInterface
Entity
EntityAnnotationsAttribute
EntityDeletedAttribute
EntityDisplayAttribute
......
/*
Copyright (c) 2014 Christian Mollekopf <mollekopf@kolabsys.com>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library 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 Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#ifndef AKONADI_ATTRIBUTEENTITY_H
#define AKONADI_ATTRIBUTEENTITY_H
#include "akonadicore_export.h"
#include "job.h"
#include "attribute.h"
#include <QSharedPointer>
#include <QDebug>
namespace Akonadi
{
/**
* Parent class for entities that can have attributes.
* This is supposed to eventually share the code between Akonadi::Tag and Akonadi::Entity.
*
* In the current form using this in Akonadi::Entity would break the implicit sharing of it's private class,
* so AttributeEntity::Private would need to become a parent class of EntityPrivate and use the same clone()
* calls etc.
* An even better solution is probably ot make AttributeEntity a private member of Entity, with all Attribute related member functions forwarding to this class.
*/
class AKONADICORE_EXPORT AttributeEntity
{
public:
AttributeEntity();
AttributeEntity(const AttributeEntity &other);
virtual ~AttributeEntity();
//Each subclass must Q_DECL_OVERRIDE this to avoid slicing
virtual AttributeEntity &operator=(const AttributeEntity &other);
/**
* Adds an attribute to the entity.
*
* If an attribute of the same type name already exists, it is deleted and
* replaced with the new one.
*
* @param attribute The new attribute.
*
* @note The entity takes the ownership of the attribute.
*/
void addAttribute(Attribute *attribute);
/**
* Removes and deletes the attribute of the given type @p name.
*/
void removeAttribute(const QByteArray &name);
/**
* Returns @c true if the entity has an attribute of the given type @p name,
* false otherwise.
*/
bool hasAttribute(const QByteArray &name) const;
/**
* Returns a list of all attributes of the entity.
*/
Attribute::List attributes() const;
/**
* Removes and deletes all attributes of the entity.
*/
void clearAttributes();
/**
* Returns the attribute of the given type @p name if available, 0 otherwise.
*/
Attribute *attribute(const QByteArray &name) const;
/**
* Describes the options that can be passed to access attributes.
*/
enum CreateOption {
AddIfMissing ///< Creates the attribute if it is missing
};
/**
* Returns the attribute of the requested type.
* If the entity has no attribute of that type yet, a new one
* is created and added to the entity.
*
* @param option The create options.
*/
template <typename T> inline T *attribute(CreateOption option)
{
Q_UNUSED(option);
const T dummy;
if (hasAttribute(dummy.type())) {
T *attr = dynamic_cast<T *>(attribute(dummy.type()));
if (attr) {
return attr;
}
//reuse 5250
qWarning() << "Found attribute of unknown type" << dummy.type()
<< ". Did you forget to call AttributeFactory::registerAttribute()?";
}
T *attr = new T();
addAttribute(attr);
return attr;
}
/**
* Returns the attribute of the requested type or 0 if it is not available.
*/
template <typename T> inline T *attribute() const
{
const T dummy;
if (hasAttribute(dummy.type())) {
T *attr = dynamic_cast<T *>(attribute(dummy.type()));
if (attr) {
return attr;
}
//Reuse 5250
qWarning() << "Found attribute of unknown type" << dummy.type()
<< ". Did you forget to call AttributeFactory::registerAttribute()?";
}
return 0;
}
/**
* Removes and deletes the attribute of the requested type.
*/
template <typename T> inline void removeAttribute()
{
const T dummy;
removeAttribute(dummy.type());
}
/**
* Returns whether the entity has an attribute of the requested type.
*/
template <typename T> inline bool hasAttribute() const
{
const T dummy;
return hasAttribute(dummy.type());
}
private:
friend class TagModifyJob;
QSet<QByteArray> &removedAttributes() const;
class Private;
QSharedPointer<Private> d_ptr;
};
}
#endif
......@@ -19,12 +19,12 @@
#include "collection.h"
#include "collection_p.h"
#include "entity_p.h"
#include "attributefactory.h"
#include "cachepolicy.h"
#include "collectionrightsattribute_p.h"
#include "collectionstatistics.h"
#include "entity_p.h"
#include "entitydisplayattribute.h"
#include <QtCore/QDebug>
......@@ -37,47 +37,199 @@
using namespace Akonadi;
uint Akonadi::qHash(const Akonadi::Collection &collection)
{
return ::qHash(collection.id());
}
/**
* Helper method for assignment operator and copy constructor.
*/
static void assignCollectionPrivate(QSharedDataPointer<CollectionPrivate> &one,
const QSharedDataPointer<CollectionPrivate> &other)
{
// We can't simply do one = other here, we have to use a temp.
// Otherwise ProtocolHelperTest::testParentCollectionAfterCollectionParsing()
// will break.
//
// The reason are assignments like
// col = col.parentCollection()
//
// Here, parentCollection() actually returns a reference to a pointer owned
// by col. So when col (or rather, it's private class) is deleted, the pointer
// to the parent collection and therefore the reference becomes invalid.
//
// With a single-line assignment here, the parent collection would be deleted
// before it is assigned, and therefore the resulting object would point to
// uninitalized memory.
QSharedDataPointer<CollectionPrivate> temp = other;
one = temp;
}
class CollectionRoot : public Collection
{
public:
CollectionRoot()
: Collection(0)
{
QStringList types;
types << Collection::mimeType();
setContentMimeTypes(types);
setContentMimeTypes({ Collection::mimeType() });
// The root collection is read-only for the users
Collection::Rights rights;
rights |= Collection::ReadOnly;
setRights(rights);
setRights(Collection::ReadOnly);
}
};
Q_GLOBAL_STATIC(CollectionRoot, s_root)
Collection::Collection()
: Entity(new CollectionPrivate)
: d_ptr(new CollectionPrivate)
{
Q_D(Collection);
static int lastId = -1;
d->mId = lastId--;
d_ptr->mId = lastId--;
}
Collection::Collection(Id id)
: Entity(new CollectionPrivate(id))
: d_ptr(new CollectionPrivate(id))
{
}
Collection::Collection(const Collection &other)
: Entity(other)
{
assignCollectionPrivate(d_ptr, other.d_ptr);
}
Collection::~Collection()
{
}
void Collection::setId(Collection::Id identifier)
{
d_ptr->mId = identifier;
}
Collection::Id Collection::id() const
{
return d_func()->mId;
}
void Collection::setRemoteId(const QString &id)
{
d_ptr->mRemoteId = id;
}
QString Collection::remoteId() const
{
return d_ptr->mRemoteId;
}
void Collection::setRemoteRevision(const QString &revision)
{
d_ptr->mRemoteRevision = revision;
}
QString Collection::remoteRevision() const
{
return d_ptr->mRemoteRevision;
}
bool Collection::isValid() const
{
return (d_ptr->mId >= 0);
}
bool Collection::operator==(const Collection &other) const
{
// Invalid collections are the same, no matter what their internal ID is
return (!isValid() && !other.isValid()) || (d_ptr->mId == other.d_ptr->mId);
}
bool Akonadi::Collection::operator!=(const Collection &other) const
{
return (isValid() || other.isValid()) && (d_ptr->mId != other.d_ptr->mId);
}
Collection &Collection ::operator=(const Collection &other)
{
if (this != &other) {
assignCollectionPrivate(d_ptr, other.d_ptr);
}
return *this;
}
bool Akonadi::Collection::operator<(const Collection &other) const
{
return d_ptr->mId < other.d_ptr->mId;
}
void Collection::addAttribute(Attribute *attr)
{
Q_ASSERT(attr);
Attribute *existing = d_ptr->mAttributes.value(attr->type());
if (existing) {
if (attr == existing) {
return;
}
d_ptr->mAttributes.remove(attr->type());
delete existing;
}
d_ptr->mAttributes.insert(attr->type(), attr);
d_ptr->mDeletedAttributes.remove(attr->type());
}
void Collection::removeAttribute(const QByteArray &type)
{
d_ptr->mDeletedAttributes.insert(type);
delete d_ptr->mAttributes.take(type);
}
bool Collection::hasAttribute(const QByteArray &type) const
{
return d_ptr->mAttributes.contains(type);
}
Attribute::List Collection::attributes() const
{
return d_ptr->mAttributes.values();
}
void Akonadi::Collection::clearAttributes()
{
Q_FOREACH (Attribute *attr, d_ptr->mAttributes) {
d_ptr->mDeletedAttributes.insert(attr->type());
delete attr;
}
d_ptr->mAttributes.clear();
}
Attribute *Collection::attribute(const QByteArray &type) const
{
return d_ptr->mAttributes.value(type);
}
Collection &Collection::parentCollection()
{
if (!d_ptr->mParent) {
d_ptr->mParent = new Collection();
}
return *(d_ptr->mParent);
}
Collection Collection::parentCollection() const
{
if (!d_ptr->mParent) {
return *(s_defaultParentCollection);
} else {
return *(d_ptr->mParent);
}
}
void Collection::setParentCollection(const Collection &parent)
{
delete d_ptr->mParent;
d_ptr->mParent = new Collection(parent);
}
QString Collection::name() const
{
return d_func()->name;
......@@ -186,11 +338,6 @@ void Collection::setResource(const QString &resource)
d->resource = resource;
}
uint qHash(const Akonadi::Collection &collection)
{
return qHash(collection.id());
}
QDebug operator <<(QDebug d, const Akonadi::Collection &collection)
{