diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-05-06 12:37:30 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-05-06 12:37:30 +0200 |
commit | f52ed4fd64994985f1061c5fcd20dccaa61fbc67 (patch) | |
tree | 037ab2cff55452dfcfb1eae772458ebab020d749 | |
parent | 8eab2b67fdf83c657f996debfc238703a78b337b (diff) | |
download | sink-f52ed4fd64994985f1061c5fcd20dccaa61fbc67.tar.gz sink-f52ed4fd64994985f1061c5fcd20dccaa61fbc67.zip |
A defined table layout
-rw-r--r-- | common/storage.h | 7 | ||||
-rw-r--r-- | common/storage/entitystore.cpp | 33 | ||||
-rw-r--r-- | common/storage_lmdb.cpp | 21 |
3 files changed, 53 insertions, 8 deletions
diff --git a/common/storage.h b/common/storage.h index d1dda3e..42cdcac 100644 --- a/common/storage.h +++ b/common/storage.h | |||
@@ -25,10 +25,16 @@ | |||
25 | #include <string> | 25 | #include <string> |
26 | #include <functional> | 26 | #include <functional> |
27 | #include <QString> | 27 | #include <QString> |
28 | #include <QMap> | ||
28 | 29 | ||
29 | namespace Sink { | 30 | namespace Sink { |
30 | namespace Storage { | 31 | namespace Storage { |
31 | 32 | ||
33 | struct DbLayout { | ||
34 | QByteArray name; | ||
35 | QMap<QByteArray, int> tables; | ||
36 | }; | ||
37 | |||
32 | class SINK_EXPORT DataStore | 38 | class SINK_EXPORT DataStore |
33 | { | 39 | { |
34 | public: | 40 | public: |
@@ -151,6 +157,7 @@ public: | |||
151 | }; | 157 | }; |
152 | 158 | ||
153 | DataStore(const QString &storageRoot, const QString &name, AccessMode mode = ReadOnly); | 159 | DataStore(const QString &storageRoot, const QString &name, AccessMode mode = ReadOnly); |
160 | DataStore(const QString &storageRoot, const DbLayout &layout, AccessMode mode = ReadOnly); | ||
154 | ~DataStore(); | 161 | ~DataStore(); |
155 | 162 | ||
156 | Transaction createTransaction(AccessMode mode = ReadWrite, const std::function<void(const DataStore::Error &error)> &errorHandler = std::function<void(const DataStore::Error &error)>()); | 163 | Transaction createTransaction(AccessMode mode = ReadWrite, const std::function<void(const DataStore::Error &error)> &errorHandler = std::function<void(const DataStore::Error &error)>()); |
diff --git a/common/storage/entitystore.cpp b/common/storage/entitystore.cpp index 04760f7..9539bec 100644 --- a/common/storage/entitystore.cpp +++ b/common/storage/entitystore.cpp | |||
@@ -36,6 +36,33 @@ | |||
36 | using namespace Sink; | 36 | using namespace Sink; |
37 | using namespace Sink::Storage; | 37 | using namespace Sink::Storage; |
38 | 38 | ||
39 | static Sink::Storage::DbLayout dbLayout(const QByteArray &instanceId) | ||
40 | { | ||
41 | return Sink::Storage::DbLayout { | ||
42 | instanceId, | ||
43 | { | ||
44 | {"folder.main", 0}, | ||
45 | {"folder.index.name", 1}, | ||
46 | {"folder.index.parent", 1}, | ||
47 | {"mail.main", 0}, | ||
48 | {"mail.index.date", 1}, | ||
49 | {"mail.index.folder", 1}, | ||
50 | {"mail.index.folder.sort.date", 0}, | ||
51 | {"mail.index.messageId", 1}, | ||
52 | {"mail.index.messageIdthreadId", 1}, | ||
53 | {"mail.index.parentMessageId", 1}, | ||
54 | {"mail.index.subjectthreadId", 1}, | ||
55 | {"mail.index.threadIdmessageId", 1}, | ||
56 | {"revisionType", 0}, | ||
57 | {"revisions", 0}, | ||
58 | {"uids", 0}, | ||
59 | {"default", 0}, | ||
60 | {"__flagtable", 0} | ||
61 | } | ||
62 | }; | ||
63 | } | ||
64 | |||
65 | |||
39 | class EntityStore::Private { | 66 | class EntityStore::Private { |
40 | public: | 67 | public: |
41 | Private(const ResourceContext &context, const Sink::Log::Context &ctx) : resourceContext(context), logCtx(ctx.subContext("entitystore")) {} | 68 | Private(const ResourceContext &context, const Sink::Log::Context &ctx) : resourceContext(context), logCtx(ctx.subContext("entitystore")) {} |
@@ -47,7 +74,7 @@ public: | |||
47 | 74 | ||
48 | bool exists() | 75 | bool exists() |
49 | { | 76 | { |
50 | return Sink::Storage::DataStore(Sink::storageLocation(), resourceContext.instanceId(), DataStore::ReadOnly).exists(); | 77 | return Sink::Storage::DataStore(Sink::storageLocation(), dbLayout(resourceContext.instanceId()), DataStore::ReadOnly).exists(); |
51 | } | 78 | } |
52 | 79 | ||
53 | DataStore::Transaction &getTransaction() | 80 | DataStore::Transaction &getTransaction() |
@@ -56,7 +83,7 @@ public: | |||
56 | return transaction; | 83 | return transaction; |
57 | } | 84 | } |
58 | 85 | ||
59 | Sink::Storage::DataStore store(Sink::storageLocation(), resourceContext.instanceId(), DataStore::ReadOnly); | 86 | Sink::Storage::DataStore store(Sink::storageLocation(), dbLayout(resourceContext.instanceId()), DataStore::ReadOnly); |
60 | transaction = store.createTransaction(DataStore::ReadOnly); | 87 | transaction = store.createTransaction(DataStore::ReadOnly); |
61 | return transaction; | 88 | return transaction; |
62 | } | 89 | } |
@@ -110,7 +137,7 @@ void EntityStore::startTransaction(Sink::Storage::DataStore::AccessMode accessMo | |||
110 | { | 137 | { |
111 | SinkTraceCtx(d->logCtx) << "Starting transaction: " << accessMode; | 138 | SinkTraceCtx(d->logCtx) << "Starting transaction: " << accessMode; |
112 | Q_ASSERT(!d->transaction); | 139 | Q_ASSERT(!d->transaction); |
113 | Sink::Storage::DataStore store(Sink::storageLocation(), d->resourceContext.instanceId(), accessMode); | 140 | Sink::Storage::DataStore store(Sink::storageLocation(), dbLayout(d->resourceContext.instanceId()), accessMode); |
114 | d->transaction = store.createTransaction(accessMode); | 141 | d->transaction = store.createTransaction(accessMode); |
115 | } | 142 | } |
116 | 143 | ||
diff --git a/common/storage_lmdb.cpp b/common/storage_lmdb.cpp index cb7bca4..ef4bd17 100644 --- a/common/storage_lmdb.cpp +++ b/common/storage_lmdb.cpp | |||
@@ -711,7 +711,7 @@ QList<QByteArray> DataStore::Transaction::getDatabaseNames() const | |||
711 | class DataStore::Private | 711 | class DataStore::Private |
712 | { | 712 | { |
713 | public: | 713 | public: |
714 | Private(const QString &s, const QString &n, AccessMode m); | 714 | Private(const QString &s, const QString &n, AccessMode m, const DbLayout &layout = {}); |
715 | ~Private(); | 715 | ~Private(); |
716 | 716 | ||
717 | QString storageRoot; | 717 | QString storageRoot; |
@@ -721,7 +721,7 @@ public: | |||
721 | AccessMode mode; | 721 | AccessMode mode; |
722 | }; | 722 | }; |
723 | 723 | ||
724 | DataStore::Private::Private(const QString &s, const QString &n, AccessMode m) : storageRoot(s), name(n), env(0), mode(m) | 724 | DataStore::Private::Private(const QString &s, const QString &n, AccessMode m, const DbLayout &layout) : storageRoot(s), name(n), env(0), mode(m) |
725 | { | 725 | { |
726 | const QString fullPath(storageRoot + '/' + name); | 726 | const QString fullPath(storageRoot + '/' + name); |
727 | QFileInfo dirInfo(fullPath); | 727 | QFileInfo dirInfo(fullPath); |
@@ -772,9 +772,16 @@ DataStore::Private::Private(const QString &s, const QString &n, AccessMode m) : | |||
772 | bool noLock = true; | 772 | bool noLock = true; |
773 | bool requestedRead = m == ReadOnly; | 773 | bool requestedRead = m == ReadOnly; |
774 | auto t = Transaction(new Transaction::Private(requestedRead, nullptr, name, env, noLock)); | 774 | auto t = Transaction(new Transaction::Private(requestedRead, nullptr, name, env, noLock)); |
775 | for (const auto &db : t.getDatabaseNames()) { | 775 | if (!layout.tables.isEmpty()) { |
776 | //Get dbi to store for future use. | 776 | for (auto it = layout.tables.constBegin(); it != layout.tables.constEnd(); it++) { |
777 | t.openDatabase(db); | 777 | bool allowDuplicates = it.value(); |
778 | t.openDatabase(it.key(), {}, allowDuplicates); | ||
779 | } | ||
780 | } else { | ||
781 | for (const auto &db : t.getDatabaseNames()) { | ||
782 | //Get dbi to store for future use. | ||
783 | t.openDatabase(db); | ||
784 | } | ||
778 | } | 785 | } |
779 | //To persist the dbis (this is also necessary for read-only transactions) | 786 | //To persist the dbis (this is also necessary for read-only transactions) |
780 | t.commit(); | 787 | t.commit(); |
@@ -794,6 +801,10 @@ DataStore::DataStore(const QString &storageRoot, const QString &name, AccessMode | |||
794 | { | 801 | { |
795 | } | 802 | } |
796 | 803 | ||
804 | DataStore::DataStore(const QString &storageRoot, const DbLayout &dbLayout, AccessMode mode) : d(new Private(storageRoot, dbLayout.name, mode, dbLayout)) | ||
805 | { | ||
806 | } | ||
807 | |||
797 | DataStore::~DataStore() | 808 | DataStore::~DataStore() |
798 | { | 809 | { |
799 | delete d; | 810 | delete d; |