diff options
author | Aaron Seigo <aseigo@kde.org> | 2014-12-05 12:56:33 +0100 |
---|---|---|
committer | Aaron Seigo <aseigo@kde.org> | 2014-12-05 12:56:33 +0100 |
commit | 130affe4addb1aff1618d8fb849889b2d4453ef2 (patch) | |
tree | 4771ee8e9bdcd2b2ab3a25ae7f9edf7766b521b6 | |
parent | a86b10296c5e8017821b422bb4e06c308e14ed04 (diff) | |
download | sink-130affe4addb1aff1618d8fb849889b2d4453ef2.tar.gz sink-130affe4addb1aff1618d8fb849889b2d4453ef2.zip |
require users of a Storage class to state up-front if they are read or readwrite
-rw-r--r-- | common/storage.h | 6 | ||||
-rw-r--r-- | common/storage_kyoto.cpp | 25 | ||||
-rw-r--r-- | common/storage_lmdb.cpp | 17 |
3 files changed, 33 insertions, 15 deletions
diff --git a/common/storage.h b/common/storage.h index 0b548fb..846e15b 100644 --- a/common/storage.h +++ b/common/storage.h | |||
@@ -5,12 +5,12 @@ | |||
5 | 5 | ||
6 | class Storage { | 6 | class Storage { |
7 | public: | 7 | public: |
8 | enum TransactionType { ReadOnly, ReadWrite }; | 8 | enum AccessMode { ReadOnly, ReadWrite }; |
9 | 9 | ||
10 | Storage(const QString &storageRoot, const QString &name); | 10 | Storage(const QString &storageRoot, const QString &name, AccessMode mode = ReadOnly); |
11 | ~Storage(); | 11 | ~Storage(); |
12 | bool isInTransaction() const; | 12 | bool isInTransaction() const; |
13 | bool startTransaction(TransactionType type = ReadWrite); | 13 | bool startTransaction(AccessMode mode = ReadWrite); |
14 | bool commitTransaction(); | 14 | bool commitTransaction(); |
15 | void abortTransaction(); | 15 | void abortTransaction(); |
16 | bool write(const char *key, size_t keySize, const char *value, size_t valueSize); | 16 | bool write(const char *key, size_t keySize, const char *value, size_t valueSize); |
diff --git a/common/storage_kyoto.cpp b/common/storage_kyoto.cpp index 40bd3e6..3d7ab8c 100644 --- a/common/storage_kyoto.cpp +++ b/common/storage_kyoto.cpp | |||
@@ -15,23 +15,30 @@ | |||
15 | class Storage::Private | 15 | class Storage::Private |
16 | { | 16 | { |
17 | public: | 17 | public: |
18 | Private(const QString &storageRoot, const QString &name); | 18 | Private(const QString &storageRoot, const QString &name, AccessMode m); |
19 | ~Private(); | 19 | ~Private(); |
20 | 20 | ||
21 | kyotocabinet::TreeDB db; | 21 | kyotocabinet::TreeDB db; |
22 | AccessMode mode; | ||
22 | bool dbOpen; | 23 | bool dbOpen; |
23 | bool inTransaction; | 24 | bool inTransaction; |
24 | }; | 25 | }; |
25 | 26 | ||
26 | Storage::Private::Private(const QString &storageRoot, const QString &name) | 27 | Storage::Private::Private(const QString &storageRoot, const QString &name, AccessMode m) |
27 | : inTransaction(false) | 28 | : mode(m), |
29 | dbOpen(false), | ||
30 | inTransaction(false) | ||
28 | { | 31 | { |
29 | QDir dir; | 32 | QDir dir; |
30 | dir.mkdir(storageRoot); | 33 | dir.mkdir(storageRoot); |
31 | 34 | ||
32 | //create file | 35 | //create file |
33 | dbOpen = db.open((storageRoot + "/" + name + ".kch").toStdString(), kyotocabinet::BasicDB::OWRITER | kyotocabinet::BasicDB::OCREATE); | 36 | uint32_t openMode = kyotocabinet::BasicDB::OCREATE | |
37 | (mode == ReadOnly ? kyotocabinet::BasicDB::OREADER | ||
38 | : kyotocabinet::BasicDB::OWRITER); | ||
39 | dbOpen = db.open((storageRoot + "/" + name + ".kch").toStdString(), openMode); | ||
34 | if (!dbOpen) { | 40 | if (!dbOpen) { |
41 | std::cerr << "Could not open database: " << db.error().codename(db.error().code()) << " " << db.error().message() << std::endl; | ||
35 | // TODO: handle error | 42 | // TODO: handle error |
36 | } | 43 | } |
37 | } | 44 | } |
@@ -43,8 +50,8 @@ Storage::Private::~Private() | |||
43 | } | 50 | } |
44 | } | 51 | } |
45 | 52 | ||
46 | Storage::Storage(const QString &storageRoot, const QString &name) | 53 | Storage::Storage(const QString &storageRoot, const QString &name, AccessMode mode) |
47 | : d(new Private(storageRoot, name)) | 54 | : d(new Private(storageRoot, name, mode)) |
48 | { | 55 | { |
49 | } | 56 | } |
50 | 57 | ||
@@ -58,12 +65,16 @@ bool Storage::isInTransaction() const | |||
58 | return d->inTransaction; | 65 | return d->inTransaction; |
59 | } | 66 | } |
60 | 67 | ||
61 | bool Storage::startTransaction(TransactionType type) | 68 | bool Storage::startTransaction(AccessMode type) |
62 | { | 69 | { |
63 | if (!d->dbOpen) { | 70 | if (!d->dbOpen) { |
64 | return false; | 71 | return false; |
65 | } | 72 | } |
66 | 73 | ||
74 | if (type == ReadWrite && d->mode != ReadWrite) { | ||
75 | return false; | ||
76 | } | ||
77 | |||
67 | if (d->inTransaction) { | 78 | if (d->inTransaction) { |
68 | return true; | 79 | return true; |
69 | } | 80 | } |
diff --git a/common/storage_lmdb.cpp b/common/storage_lmdb.cpp index 8e9b05c..7e23fd3 100644 --- a/common/storage_lmdb.cpp +++ b/common/storage_lmdb.cpp | |||
@@ -14,7 +14,7 @@ | |||
14 | class Storage::Private | 14 | class Storage::Private |
15 | { | 15 | { |
16 | public: | 16 | public: |
17 | Private(const QString &s, const QString &name); | 17 | Private(const QString &s, const QString &name, AccessMode m); |
18 | ~Private(); | 18 | ~Private(); |
19 | 19 | ||
20 | QString storageRoot; | 20 | QString storageRoot; |
@@ -23,14 +23,16 @@ public: | |||
23 | MDB_dbi dbi; | 23 | MDB_dbi dbi; |
24 | MDB_env *env; | 24 | MDB_env *env; |
25 | MDB_txn *transaction; | 25 | MDB_txn *transaction; |
26 | AccessMode mode; | ||
26 | bool readTransaction; | 27 | bool readTransaction; |
27 | bool firstOpen; | 28 | bool firstOpen; |
28 | }; | 29 | }; |
29 | 30 | ||
30 | Storage::Private::Private(const QString &s, const QString &n) | 31 | Storage::Private::Private(const QString &s, const QString &n, AccessMode m) |
31 | : storageRoot(s), | 32 | : storageRoot(s), |
32 | name(n), | 33 | name(n), |
33 | transaction(0), | 34 | transaction(0), |
35 | mode(m), | ||
34 | readTransaction(false), | 36 | readTransaction(false), |
35 | firstOpen(true) | 37 | firstOpen(true) |
36 | { | 38 | { |
@@ -65,8 +67,8 @@ Storage::Private::~Private() | |||
65 | mdb_env_close(env); | 67 | mdb_env_close(env); |
66 | } | 68 | } |
67 | 69 | ||
68 | Storage::Storage(const QString &storageRoot, const QString &name) | 70 | Storage::Storage(const QString &storageRoot, const QString &name, AccessMode mode) |
69 | : d(new Private(storageRoot, name)) | 71 | : d(new Private(storageRoot, name, mode)) |
70 | { | 72 | { |
71 | } | 73 | } |
72 | 74 | ||
@@ -80,13 +82,18 @@ bool Storage::isInTransaction() const | |||
80 | return d->transaction; | 82 | return d->transaction; |
81 | } | 83 | } |
82 | 84 | ||
83 | bool Storage::startTransaction(TransactionType type) | 85 | bool Storage::startTransaction(AccessMode type) |
84 | { | 86 | { |
85 | if (!d->env) { | 87 | if (!d->env) { |
86 | return false; | 88 | return false; |
87 | } | 89 | } |
88 | 90 | ||
89 | bool requestedRead = type == ReadOnly; | 91 | bool requestedRead = type == ReadOnly; |
92 | |||
93 | if (d->mode == ReadOnly && !requestedRead) { | ||
94 | return false; | ||
95 | } | ||
96 | |||
90 | if (d->transaction && (!d->readTransaction || requestedRead)) { | 97 | if (d->transaction && (!d->readTransaction || requestedRead)) { |
91 | return true; | 98 | return true; |
92 | } | 99 | } |