summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2015-10-29 17:52:45 +0100
committerChristian Mollekopf <chrigi_1@fastmail.fm>2015-10-29 17:52:45 +0100
commit0a2d20c474206553d5c981fd2a772188083101e9 (patch)
tree37a46837b4fa93834a9380ee02ef4188786e9acd
parent70faf80be4146b17a59f9616404b21625d7400f6 (diff)
downloadsink-0a2d20c474206553d5c981fd2a772188083101e9.tar.gz
sink-0a2d20c474206553d5c981fd2a772188083101e9.zip
Updated the index on modifications and removals.
Misses tests.
-rw-r--r--common/index.cpp5
-rw-r--r--common/index.h2
-rw-r--r--common/storage.h8
-rw-r--r--common/storage_lmdb.cpp17
-rw-r--r--examples/dummyresource/resourcefactory.cpp29
5 files changed, 38 insertions, 23 deletions
diff --git a/common/index.cpp b/common/index.cpp
index f752f5f..2fc0fe3 100644
--- a/common/index.cpp
+++ b/common/index.cpp
@@ -19,6 +19,11 @@ void Index::add(const QByteArray &key, const QByteArray &value)
19 mDb.write(key, value); 19 mDb.write(key, value);
20} 20}
21 21
22void Index::remove(const QByteArray &key, const QByteArray &value)
23{
24 mDb.remove(key, value);
25}
26
22void Index::lookup(const QByteArray &key, const std::function<void(const QByteArray &value)> &resultHandler, 27void Index::lookup(const QByteArray &key, const std::function<void(const QByteArray &value)> &resultHandler,
23 const std::function<void(const Error &error)> &errorHandler) 28 const std::function<void(const Error &error)> &errorHandler)
24{ 29{
diff --git a/common/index.h b/common/index.h
index e1b7e3a..0ca32af 100644
--- a/common/index.h
+++ b/common/index.h
@@ -29,7 +29,7 @@ public:
29 Index(const QByteArray &name, Akonadi2::Storage::Transaction &); 29 Index(const QByteArray &name, Akonadi2::Storage::Transaction &);
30 30
31 void add(const QByteArray &key, const QByteArray &value); 31 void add(const QByteArray &key, const QByteArray &value);
32 // void remove(const QByteArray &key, const QByteArray &value); 32 void remove(const QByteArray &key, const QByteArray &value);
33 33
34 void lookup(const QByteArray &key, const std::function<void(const QByteArray &value)> &resultHandler, 34 void lookup(const QByteArray &key, const std::function<void(const QByteArray &value)> &resultHandler,
35 const std::function<void(const Error &error)> &errorHandler); 35 const std::function<void(const Error &error)> &errorHandler);
diff --git a/common/storage.h b/common/storage.h
index aa0be4c..a4137ce 100644
--- a/common/storage.h
+++ b/common/storage.h
@@ -63,11 +63,17 @@ public:
63 bool write(const QByteArray &key, const QByteArray &value, const std::function<void(const Storage::Error &error)> &errorHandler = std::function<void(const Storage::Error &error)>()); 63 bool write(const QByteArray &key, const QByteArray &value, const std::function<void(const Storage::Error &error)> &errorHandler = std::function<void(const Storage::Error &error)>());
64 64
65 /** 65 /**
66 * Remove a value 66 * Remove a key
67 */ 67 */
68 void remove(const QByteArray &key, 68 void remove(const QByteArray &key,
69 const std::function<void(const Storage::Error &error)> &errorHandler = std::function<void(const Storage::Error &error)>()); 69 const std::function<void(const Storage::Error &error)> &errorHandler = std::function<void(const Storage::Error &error)>());
70 /** 70 /**
71 * Remove a key-value pair
72 */
73 void remove(const QByteArray &key, const QByteArray &value,
74 const std::function<void(const Storage::Error &error)> &errorHandler = std::function<void(const Storage::Error &error)>());
75
76 /**
71 * Read values with a given key. 77 * Read values with a given key.
72 * 78 *
73 * * An empty @param key results in a full scan 79 * * An empty @param key results in a full scan
diff --git a/common/storage_lmdb.cpp b/common/storage_lmdb.cpp
index be5a9da..1516e69 100644
--- a/common/storage_lmdb.cpp
+++ b/common/storage_lmdb.cpp
@@ -149,6 +149,12 @@ bool Storage::NamedDatabase::write(const QByteArray &sKey, const QByteArray &sVa
149void Storage::NamedDatabase::remove(const QByteArray &k, 149void Storage::NamedDatabase::remove(const QByteArray &k,
150 const std::function<void(const Storage::Error &error)> &errorHandler) 150 const std::function<void(const Storage::Error &error)> &errorHandler)
151{ 151{
152 remove(k, QByteArray(), errorHandler);
153}
154
155void Storage::NamedDatabase::remove(const QByteArray &k, const QByteArray &value,
156 const std::function<void(const Storage::Error &error)> &errorHandler)
157{
152 if (!d || !d->transaction) { 158 if (!d || !d->transaction) {
153 Error error(d->name.toLatin1(), ErrorCodes::GenericError, "Not open"); 159 Error error(d->name.toLatin1(), ErrorCodes::GenericError, "Not open");
154 if (d) { 160 if (d) {
@@ -161,14 +167,19 @@ void Storage::NamedDatabase::remove(const QByteArray &k,
161 MDB_val key; 167 MDB_val key;
162 key.mv_size = k.size(); 168 key.mv_size = k.size();
163 key.mv_data = const_cast<void*>(static_cast<const void*>(k.data())); 169 key.mv_data = const_cast<void*>(static_cast<const void*>(k.data()));
164 rc = mdb_del(d->transaction, d->dbi, &key, 0); 170 if (value.isEmpty()) {
171 rc = mdb_del(d->transaction, d->dbi, &key, 0);
172 } else {
173 MDB_val data;
174 data.mv_size = value.size();
175 data.mv_data = const_cast<void*>(static_cast<const void*>(value.data()));
176 rc = mdb_del(d->transaction, d->dbi, &key, &data);
177 }
165 178
166 if (rc) { 179 if (rc) {
167 Error error(d->name.toLatin1(), ErrorCodes::GenericError, QString("Error on mdb_del: %1 %2").arg(rc).arg(mdb_strerror(rc)).toLatin1()); 180 Error error(d->name.toLatin1(), ErrorCodes::GenericError, QString("Error on mdb_del: %1 %2").arg(rc).arg(mdb_strerror(rc)).toLatin1());
168 errorHandler ? errorHandler(error) : d->defaultErrorHandler(error); 181 errorHandler ? errorHandler(error) : d->defaultErrorHandler(error);
169 } 182 }
170
171 return;
172} 183}
173 184
174int Storage::NamedDatabase::scan(const QByteArray &k, 185int Storage::NamedDatabase::scan(const QByteArray &k,
diff --git a/examples/dummyresource/resourcefactory.cpp b/examples/dummyresource/resourcefactory.cpp
index 0a2e90b..b742611 100644
--- a/examples/dummyresource/resourcefactory.cpp
+++ b/examples/dummyresource/resourcefactory.cpp
@@ -41,13 +41,6 @@
41#define ENTITY_TYPE_EVENT "event" 41#define ENTITY_TYPE_EVENT "event"
42#define ENTITY_TYPE_MAIL "mail" 42#define ENTITY_TYPE_MAIL "mail"
43 43
44static void index(const QByteArray &index, const QVariant &value, const QByteArray &uid, Akonadi2::Storage::Transaction &transaction)
45{
46 if (value.isValid()) {
47 Index(index, transaction).add(value.toByteArray(), uid);
48 }
49}
50
51/** 44/**
52 * Index types: 45 * Index types:
53 * * uid - property 46 * * uid - property
@@ -74,13 +67,20 @@ public:
74 add(newEntity.getProperty("remoteId"), uid, transaction); 67 add(newEntity.getProperty("remoteId"), uid, transaction);
75 } 68 }
76 69
77 void modifiedEntity(const QByteArray &key, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &oldEntity, const Akonadi2::ApplicationDomain::BufferAdaptor &newEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE 70 void modifiedEntity(const QByteArray &uid, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &oldEntity, const Akonadi2::ApplicationDomain::BufferAdaptor &newEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE
78 { 71 {
72 Akonadi2::ApplicationDomain::TypeImplementation<DomainType>::removeIndex(uid, oldEntity, transaction);
73 Akonadi2::ApplicationDomain::TypeImplementation<DomainType>::index(uid, newEntity, transaction);
74 remove(oldEntity.getProperty("remoteId"), uid, transaction);
75 add(newEntity.getProperty("remoteId"), uid, transaction);
79 } 76 }
80 77
81 void deletedEntity(const QByteArray &key, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &oldEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE 78 void deletedEntity(const QByteArray &uid, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &oldEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE
82 { 79 {
80 Akonadi2::ApplicationDomain::TypeImplementation<DomainType>::removeIndex(uid, oldEntity, transaction);
81 remove(oldEntity.getProperty("remoteId"), uid, transaction);
83 } 82 }
83
84private: 84private:
85 void add(const QVariant &value, const QByteArray &uid, Akonadi2::Storage::Transaction &transaction) 85 void add(const QVariant &value, const QByteArray &uid, Akonadi2::Storage::Transaction &transaction)
86 { 86 {
@@ -89,16 +89,9 @@ private:
89 } 89 }
90 } 90 }
91 91
92 void remove(const QByteArray &uid, Akonadi2::Storage::Transaction &transaction) 92 void remove(const QVariant &value, const QByteArray &uid, Akonadi2::Storage::Transaction &transaction)
93 {
94 //Knowning the indexed value would probably help removing the uid efficiently. Otherwise we have to execute a full scan.
95 // Index(mIndexIdentifier, transaction).remove(uid);
96 }
97
98 void modify(Akonadi2::Storage::Transaction &transaction)
99 { 93 {
100 //Knowning the indexed value would probably help removing the uid efficiently. Otherwise we have to execute a full scan. 94 Index(mIndexIdentifier, transaction).remove(value.toByteArray(), uid);
101 // Index(mIndexIdentifier, transaction).remove(uid);
102 } 95 }
103 96
104 QByteArray mIndexIdentifier; 97 QByteArray mIndexIdentifier;