Commit 7721ea8e authored by Shashwat Jolly's avatar Shashwat Jolly
Browse files

Implement EtebaseCollection caching using custom Akonadi attribute

- Made new CollectionCacheAttribute
- Modified JournalsFetchJob to save cache buffer to collection attribute
- Cache will be used to get EtebaseCollection object to fetch items in retrieveItems()
parent cf68f33c
......@@ -25,6 +25,7 @@ set(etesyncresource_SRCS
calendartaskbasehandler.cpp
calendarhandler.cpp
taskhandler.cpp
collectioncacheattribute.cpp
setupwizard.cpp
${etesyncconfig_SRCS}
......
/*
* SPDX-FileCopyrightText: 2020 Shashwat Jolly <shashwat.jolly@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "etebaseadapter.h"
#include "collectioncacheattribute.h"
#include <AkonadiCore/Attribute>
CollectionCacheAttribute::CollectionCacheAttribute(QByteArray collectionCache)
: mCollectionCache(collectionCache)
{
}
void CollectionCacheAttribute::setCollectionCache(const QByteArray &collectionCache)
{
mCollectionCache = collectionCache;
}
QByteArray CollectionCacheAttribute::collectionCache() const
{
return mCollectionCache;
}
QByteArray CollectionCacheAttribute::type() const
{
static const QByteArray sType("etebasecollectioncache");
return sType;
}
Akonadi::Attribute *CollectionCacheAttribute::clone() const
{
return new CollectionCacheAttribute(mCollectionCache);
}
QByteArray CollectionCacheAttribute::serialized() const
{
return mCollectionCache;
}
void CollectionCacheAttribute::deserialize(const QByteArray &data)
{
mCollectionCache = data;
}
/*
* SPDX-FileCopyrightText: 2020 Shashwat Jolly <shashwat.jolly@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef COLLECTIONCACHEATTRIBUTE_H
#define COLLECTIONCACHEATTRIBUTE_H
#include "etebaseadapter.h"
#include <AkonadiCore/Attribute>
class CollectionCacheAttribute : public Akonadi::Attribute
{
public:
explicit CollectionCacheAttribute(QByteArray collectionCache = nullptr);
void setCollectionCache(const QByteArray &collectionCache);
QByteArray collectionCache() const;
QByteArray type() const override;
Attribute *clone() const override;
QByteArray serialized() const override;
void deserialize(const QByteArray &data) override;
private:
QByteArray mCollectionCache;
};
#endif
......@@ -57,14 +57,21 @@ struct EtebaseDeleter
{
std::free(ptr);
}
void operator()(void *ptr)
{
std::free(ptr);
}
};
using EtebaseClientPtr = std::unique_ptr<EtebaseClient, EtebaseDeleter>;
using EtebaseAccountPtr = std::unique_ptr<EtebaseAccount, EtebaseDeleter>;
using EtebaseFetchOptionsPtr = std::unique_ptr<EtebaseFetchOptions, EtebaseDeleter>;
using EtebaseCollectionListResponsePtr = std::unique_ptr<EtebaseCollectionListResponse, EtebaseDeleter>;
using EtebaseCollectionManagerPtr = std::unique_ptr<EtebaseCollectionManager, EtebaseDeleter>;
using EtebaseCollectionPtr = std::unique_ptr<EtebaseCollection, EtebaseDeleter>;
using EtebaseCollectionMetadataPtr = std::unique_ptr<EtebaseCollectionMetadata, EtebaseDeleter>;
using EtebaseCachePtr = std::unique_ptr<void, EtebaseDeleter>;
using CharPtr = std::unique_ptr<char, EtebaseDeleter>;
QString QStringFromCharPtr(const CharPtr &str);
......
......@@ -14,6 +14,7 @@
#include <KCalendarCore/Event>
#include <KCalendarCore/Todo>
#include "collectioncacheattribute.h"
#include "etesync_debug.h"
using namespace EteSyncAPI;
......@@ -42,14 +43,14 @@ void JournalsFetchJob::fetchJournals()
{
QString stoken;
bool done = 0;
EtebaseCollectionManager *collectionManager = etebase_account_get_collection_manager(mAccount);
EtebaseCollectionManagerPtr collectionManager(etebase_account_get_collection_manager(mAccount));
while (!done) {
EtebaseFetchOptions *fetchOptions = etebase_fetch_options_new();
etebase_fetch_options_set_stoken(fetchOptions, stoken);
etebase_fetch_options_set_limit(fetchOptions, 30);
EtebaseFetchOptionsPtr fetchOptions(etebase_fetch_options_new());
etebase_fetch_options_set_stoken(fetchOptions.get(), stoken);
etebase_fetch_options_set_limit(fetchOptions.get(), 30);
EtebaseCollectionListResponse *collectionList = etebase_collection_manager_list(collectionManager, fetchOptions);
EtebaseCollectionListResponsePtr collectionList(etebase_collection_manager_list(collectionManager.get(), fetchOptions.get()));
if (!collectionList) {
setError(int(etebase_error_get_code()));
const char *err = etebase_error_get_message();
......@@ -57,28 +58,31 @@ void JournalsFetchJob::fetchJournals()
return;
}
stoken = QString::fromUtf8(etebase_collection_list_response_get_stoken(collectionList));
done = etebase_collection_list_response_is_done(collectionList);
stoken = QString::fromUtf8(etebase_collection_list_response_get_stoken(collectionList.get()));
done = etebase_collection_list_response_is_done(collectionList.get());
uintptr_t dataLength = etebase_collection_list_response_get_data_length(collectionList);
uintptr_t dataLength = etebase_collection_list_response_get_data_length(collectionList.get());
qCDebug(ETESYNC_LOG) << "Retrieved collection list length" << dataLength;
const EtebaseCollection *etesyncCollections[dataLength];
if (etebase_collection_list_response_get_data(collectionList, etesyncCollections)) {
if (etebase_collection_list_response_get_data(collectionList.get(), etesyncCollections)) {
setError(int(etesync_get_error_code()));
const char *err = etebase_error_get_message();
setErrorText(QString::fromUtf8(err));
}
Collection collection;
for (int i = 0; i < dataLength; i++) {
setupCollection(etesyncCollections[i]);
setupCollection(collection, etesyncCollections[i]);
saveCollectionCache(collectionManager.get(), etesyncCollections[i], collection);
}
int removedCollectionsLength = etebase_collection_list_response_get_removed_memberships_length(collectionList);
int removedCollectionsLength = etebase_collection_list_response_get_removed_memberships_length(collectionList.get());
if (removedCollectionsLength) {
const EtebaseRemovedCollection *removedEtesyncCollections[removedCollectionsLength];
etebase_collection_list_response_get_removed_memberships(collectionList, removedEtesyncCollections);
etebase_collection_list_response_get_removed_memberships(collectionList.get(), removedEtesyncCollections);
for (int i = 0; i < removedCollectionsLength; i++) {
Collection collection;
const QString journalUid = QString::fromUtf8(etebase_removed_collection_get_uid(removedEtesyncCollections[i]));
......@@ -89,7 +93,7 @@ void JournalsFetchJob::fetchJournals()
}
}
void JournalsFetchJob::setupCollection(const EtebaseCollection *etesyncCollection)
void JournalsFetchJob::setupCollection(Akonadi::Collection &collection, const EtebaseCollection *etesyncCollection)
{
qCDebug(ETESYNC_LOG) << "Setting up collection" << etebase_collection_get_uid(etesyncCollection);
if (!etesyncCollection) {
......@@ -105,8 +109,6 @@ void JournalsFetchJob::setupCollection(const EtebaseCollection *etesyncCollectio
QStringList mimeTypes;
Collection collection;
auto attr = collection.attribute<EntityDisplayAttribute>(Collection::AddIfMissing);
const QString displayName = QString::fromUtf8(etebase_collection_metadata_get_name(metaData.get()));
......@@ -154,3 +156,12 @@ void JournalsFetchJob::setupCollection(const EtebaseCollection *etesyncCollectio
mCollections.push_back(collection);
}
void JournalsFetchJob::saveCollectionCache(const EtebaseCollectionManager *collectionManager, const EtebaseCollection *etebaseCollection, Collection &collection)
{
uintptr_t ret_size;
EtebaseCachePtr cache(etebase_collection_manager_cache_save(collectionManager, etebaseCollection, &ret_size));
QByteArray cacheData((char *)cache.get(), ret_size);
CollectionCacheAttribute *collectionCacheAttribute = collection.attribute<CollectionCacheAttribute>(Collection::AddIfMissing);
collectionCacheAttribute->setCollectionCache(cacheData);
}
......@@ -41,8 +41,8 @@ public:
protected:
void fetchJournals();
void setupCollection(const EtebaseCollection *etesyncCollection);
void setupCollection(Akonadi::Collection &collection, const EtebaseCollection *etesyncCollection);
void saveCollectionCache(const EtebaseCollectionManager *collectionManager, const EtebaseCollection *etebaseCollection, Akonadi::Collection &collection);
private:
const EtebaseAccount *mAccount = nullptr;
Akonadi::Collection::List mCollections;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment