summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDenis Dzyubenko <denis.dzyubenko@nokia.com>2012-03-22 11:21:33 +0100
committerQt by Nokia <qt-info@nokia.com>2012-03-23 18:09:03 +0100
commit6db3585569eadd9aaaaa7871ca7a41b03e4d947d (patch)
treeb1ab03f4b402948de3c2867705f8547765772a35
parent544e3d4d98c769ec953435c9cf12e843ca35a9f5 (diff)
Fixed previous transactionless cursor patch
With Hbtree it is no longer possible to open a read transaction while write transaction is active, or worse to begin a read transaction, and then begin a write transaction that can rewrite pages that were supposed to be held by the read transaction. Change-Id: Ie008883b959772ae9f2f31173c3176198c556f2b Reviewed-by: Ali Akhtarzada <ali.akhtarzada@nokia.com>
-rw-r--r--src/daemon/jsondbindex.cpp22
-rw-r--r--src/daemon/jsondbindex.h1
-rw-r--r--src/daemon/jsondbindexquery.cpp20
-rw-r--r--src/daemon/jsondbindexquery.h1
-rw-r--r--src/daemon/jsondbobjecttable.cpp38
5 files changed, 41 insertions, 41 deletions
diff --git a/src/daemon/jsondbindex.cpp b/src/daemon/jsondbindex.cpp
index 891d7a6..90ce8fa 100644
--- a/src/daemon/jsondbindex.cpp
+++ b/src/daemon/jsondbindex.cpp
@@ -432,7 +432,8 @@ void JsonDbIndex::checkIndex()
qDebug() << "checkIndex" << mPropertyName;
int countf = 0;
- JsonDbBtree::Transaction *txnf = mBdb.data()->btree()->beginRead();
+ bool isInTransaction = mBdb.data()->btree()->writeTransaction();
+ JsonDbBtree::Transaction *txnf = mBdb.data()->btree()->writeTransaction() ? mBdb.data()->btree()->writeTransaction() : mBdb.data()->btree()->beginWrite();
JsonDbBtree::Cursor cursorf(txnf);
bool ok = cursorf.first();
if (ok) {
@@ -452,12 +453,14 @@ void JsonDbIndex::checkIndex()
outkey1 = outkey2;
}
}
- txnf->abort();
+ if (!isInTransaction)
+ txnf->abort();
qDebug() << "checkIndex" << mPropertyName << "reversed";
// now check other direction
int countr = 0;
- JsonDbBtree::Transaction *txnr = mBdb.data()->btree()->beginRead();
+ isInTransaction = mBdb.data()->btree()->writeTransaction();
+ JsonDbBtree::Transaction *txnr = mBdb.data()->btree()->writeTransaction() ? mBdb.data()->btree()->writeTransaction() : mBdb.data()->btree()->beginWrite();
JsonDbBtree::Cursor cursorr(txnr);
ok = cursorr.last();
if (ok) {
@@ -477,7 +480,8 @@ void JsonDbIndex::checkIndex()
outkey1 = outkey2;
}
}
- txnr->abort();
+ if (!isInTransaction)
+ txnr->abort();
qDebug() << "checkIndex" << mPropertyName << "done" << countf << countr << "entries checked";
}
@@ -489,15 +493,17 @@ void JsonDbIndex::setCacheSize(quint32 cacheSize)
mBdb->setCacheSize(cacheSize);
}
-JsonDbIndexCursor::JsonDbIndexCursor(JsonDbIndex *index) :
- mTxn(index->bdb()->btree()->beginRead()),
- mCursor(mTxn)
+JsonDbIndexCursor::JsonDbIndexCursor(JsonDbIndex *index)
+ : isOwnTransaction(!index->bdb()->btree()->writeTransaction()),
+ mTxn(isOwnTransaction ? index->bdb()->btree()->beginWrite() : index->bdb()->btree()->writeTransaction()),
+ mCursor(mTxn)
{
}
JsonDbIndexCursor::~JsonDbIndexCursor()
{
- mTxn->abort();
+ if (isOwnTransaction)
+ mTxn->abort();
}
bool JsonDbIndexCursor::seek(const QJsonValue &value)
diff --git a/src/daemon/jsondbindex.h b/src/daemon/jsondbindex.h
index 534880c..97520fa 100644
--- a/src/daemon/jsondbindex.h
+++ b/src/daemon/jsondbindex.h
@@ -147,6 +147,7 @@ public:
bool prev();
private:
+ bool isOwnTransaction;
JsonDbBtree::Transaction *mTxn;
JsonDbBtree::Cursor mCursor;
JsonDbIndex *mIndex;
diff --git a/src/daemon/jsondbindexquery.cpp b/src/daemon/jsondbindexquery.cpp
index 9f77d97..a8140cc 100644
--- a/src/daemon/jsondbindexquery.cpp
+++ b/src/daemon/jsondbindexquery.cpp
@@ -84,27 +84,23 @@ JsonDbIndexQuery::JsonDbIndexQuery(JsonDbPartition *partition, JsonDbObjectTable
{
if (propertyName != JsonDbString::kUuidStr) {
mBdbIndex = table->indexSpec(propertyName)->index->bdb();
- mTxn = mBdbIndex->btree()->beginRead();
+ isOwnTransaction = !mBdbIndex->btree()->writeTransaction();
+ mTxn = isOwnTransaction ? mBdbIndex->btree()->beginWrite() : mBdbIndex->btree()->writeTransaction();
mCursor = new JsonDbBtree::Cursor(mTxn);
} else {
- mTxn = table->bdb()->btree()->beginRead();
+ isOwnTransaction = !table->bdb()->btree()->writeTransaction();
+ mTxn = isOwnTransaction ? table->bdb()->btree()->beginWrite() : table->bdb()->btree()->writeTransaction();
mCursor = new JsonDbBtree::Cursor(mTxn);
}
}
JsonDbIndexQuery::~JsonDbIndexQuery()
{
- if (mResidualQuery) {
- delete mResidualQuery;
- mResidualQuery = 0;
- }
- if (mTxn && mCursor) {
+ delete mResidualQuery;
+ if (isOwnTransaction)
mTxn->abort();
- delete mCursor;
- mCursor = 0;
- }
- for (int i = 0; i < mQueryConstraints.size(); i++) {
+ delete mCursor;
+ for (int i = 0; i < mQueryConstraints.size(); i++)
delete mQueryConstraints[i];
- }
}
QString JsonDbIndexQuery::partition() const
diff --git a/src/daemon/jsondbindexquery.h b/src/daemon/jsondbindexquery.h
index a5614c1..cc21517 100644
--- a/src/daemon/jsondbindexquery.h
+++ b/src/daemon/jsondbindexquery.h
@@ -109,6 +109,7 @@ protected:
JsonDbPartition *mPartition;
JsonDbObjectTable *mObjectTable;
JsonDbBtree *mBdbIndex;
+ bool isOwnTransaction;
JsonDbBtree::Transaction *mTxn;
JsonDbBtree::Cursor *mCursor;
const JsonDbOwner *mOwner;
diff --git a/src/daemon/jsondbobjecttable.cpp b/src/daemon/jsondbobjecttable.cpp
index 560bfdc..807afff 100644
--- a/src/daemon/jsondbobjecttable.cpp
+++ b/src/daemon/jsondbobjecttable.cpp
@@ -349,15 +349,11 @@ void JsonDbObjectTable::reindexObjects(const QString &indexName, const QStringLi
IndexSpec &indexSpec = mIndexes[indexName];
JsonDbIndex *index = indexSpec.index;
- bool inTransaction = index->bdb()->writeTransaction();
- bool inObjectTableTransaction = mBdb->writeTransaction();
- JsonDbBtree::Transaction *bdbTxn = 0;
- if (!inObjectTableTransaction)
- bdbTxn = mBdb->btree()->beginRead();
- else
- bdbTxn = mBdb->writeTransaction();
+ bool isInIndexTransaction = index->bdb()->writeTransaction();
+ bool isInObjectTableTransaction = mBdb->writeTransaction();
+ JsonDbBtree::Transaction *bdbTxn = mBdb->writeTransaction() ? mBdb->writeTransaction() : mBdb->btree()->beginWrite();
JsonDbBtree::Cursor cursor(bdbTxn);
- if (!inTransaction)
+ if (!isInIndexTransaction)
index->begin();
for (bool ok = cursor.first(); ok; ok = cursor.next()) {
QByteArray baKey, baObject;
@@ -373,9 +369,9 @@ void JsonDbObjectTable::reindexObjects(const QString &indexName, const QStringLi
if (!fieldValue.isNull())
index->indexObject(objectKey, object, stateNumber);
}
- if (!inTransaction)
+ if (!isInIndexTransaction)
index->commit(stateNumber);
- if (!inObjectTableTransaction)
+ if (!isInObjectTableTransaction)
bdbTxn->abort();
if (jsondbSettings->verbose())
qDebug() << "} reindexObjects";
@@ -504,7 +500,8 @@ GetObjectsResult JsonDbObjectTable::getObjects(const QString &keyName, const QJs
}
if (!mIndexes.contains(keyName) && (keyName == JsonDbString::kTypeStr)) {
- JsonDbBtree::Transaction *txn = mBdb->btree()->beginRead();
+ bool isInTransaction = mBdb->btree()->writeTransaction();
+ JsonDbBtree::Transaction *txn = mBdb->btree()->writeTransaction() ? mBdb->btree()->writeTransaction() : mBdb->btree()->beginWrite();
JsonDbBtree::Cursor cursor(txn);
for (bool ok = cursor.first(); ok; ok = cursor.next()) {
QByteArray baKey, baObject;
@@ -518,7 +515,8 @@ GetObjectsResult JsonDbObjectTable::getObjects(const QString &keyName, const QJs
continue;
objectList.append(object);
}
- txn->abort();
+ if (!isInTransaction)
+ txn->abort();
result.data = objectList;
return result;
}
@@ -528,12 +526,8 @@ GetObjectsResult JsonDbObjectTable::getObjects(const QString &keyName, const QJs
//fprintf(stderr, "getObject bdb=%p\n", indexSpec->index->bdb());
if (indexSpec->lazy)
updateIndex(indexSpec->index);
- JsonDbBtree::Transaction *txn;
- bool activeWriteTransaction = indexSpec->index->bdb()->writeTransaction();
- if (activeWriteTransaction)
- txn = indexSpec->index->bdb()->writeTransaction();
- else
- txn = indexSpec->index->bdb()->btree()->beginRead();
+ bool isInTransaction = indexSpec->index->bdb()->writeTransaction();
+ JsonDbBtree::Transaction *txn = indexSpec->index->bdb()->writeTransaction() ? indexSpec->index->bdb()->writeTransaction() : indexSpec->index->bdb()->btree()->beginWrite();
JsonDbBtree::Cursor cursor(txn);
if (cursor.seekRange(forwardKey)) {
do {
@@ -565,7 +559,7 @@ GetObjectsResult JsonDbObjectTable::getObjects(const QString &keyName, const QJs
}
} while (cursor.next());
}
- if (!activeWriteTransaction)
+ if (!isInTransaction)
txn->abort();
result.data = objectList;
@@ -612,7 +606,8 @@ void JsonDbObjectTable::changesSince(quint32 stateNumber, QMap<ObjectKey,ObjectC
QElapsedTimer timer;
if (jsondbSettings->performanceLog())
timer.start();
- JsonDbBtree::Transaction *txn = mBdb->btree()->beginRead();
+ bool inTransaction = mBdb->btree()->writeTransaction();
+ JsonDbBtree::Transaction *txn = mBdb->btree()->writeTransaction() ? mBdb->btree()->writeTransaction() : mBdb->btree()->beginWrite();
JsonDbBtree::Cursor cursor(txn);
QByteArray baStateKey(5, 0);
makeStateKey(baStateKey, stateNumber);
@@ -670,7 +665,8 @@ void JsonDbObjectTable::changesSince(quint32 stateNumber, QMap<ObjectKey,ObjectC
} while (cursor.next());
}
*changes = changeMap;
- txn->abort();
+ if (!inTransaction)
+ txn->abort();
if (jsondbSettings->performanceLog())
qDebug() << "changesSince" << mFilename << timer.elapsed() << "ms";