summaryrefslogtreecommitdiffstats
path: root/common/storage_kyoto.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'common/storage_kyoto.cpp')
-rw-r--r--common/storage_kyoto.cpp25
1 files changed, 18 insertions, 7 deletions
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 @@
15class Storage::Private 15class Storage::Private
16{ 16{
17public: 17public:
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
26Storage::Private::Private(const QString &storageRoot, const QString &name) 27Storage::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
46Storage::Storage(const QString &storageRoot, const QString &name) 53Storage::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
61bool Storage::startTransaction(TransactionType type) 68bool 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 }