MojErr MojDbLevelDatabase::get(MojDbLevelItem& key, MojDbStorageTxn* txn, bool forUpdate, MojDbLevelItem& valOut, bool& foundOut) { LOG_TRACE("Entering function %s", __FUNCTION__); MojAssert(m_db); MojAssert( !txn || dynamic_cast<MojDbLevelAbstractTxn *> (txn) ); foundOut = false; std::string str; MojDbLevelAbstractTxn * leveldb_txn = static_cast<MojDbLevelAbstractTxn *> (txn); leveldb::Status s; if (leveldb_txn) s = leveldb_txn->tableTxn(impl()).Get(*key.impl(), str); else s = m_db->Get(MojDbLevelEngine::getReadOptions(), *key.impl(), &str); //MojLdbErrCheck(s, _T("db->get")); if(s.IsNotFound() == false) { foundOut = true; valOut.fromBytes(reinterpret_cast<const MojByte*>(str.data()), str.size()); } return MojErrNone; }
MojErr MojDbLevelDatabase::put(MojDbLevelItem& key, MojDbLevelItem& val, MojDbStorageTxn* txn, bool updateIdQuota) { LOG_TRACE("Entering function %s", __FUNCTION__); MojAssert(m_db ); MojAssert( !txn || dynamic_cast<MojDbLevelAbstractTxn *> (txn) ); MojErr err; if (txn) { MojInt64 quotaOffset = val.size(); if (updateIdQuota) quotaOffset += key.size(); err = txn->offsetQuota(quotaOffset); MojErrCheck(err); } MojDbLevelAbstractTxn * leveldb_txn = static_cast<MojDbLevelAbstractTxn *> (txn); leveldb::Status s; if(leveldb_txn) { leveldb_txn->tableTxn(impl()).Put(*key.impl(), *val.impl()); } else s = m_db->Put(MojDbLevelEngine::getWriteOptions(), *key.impl(), *val.impl()); #if defined(MOJ_DEBUG) char str_buf[1024]; size_t size1 = key.size(); size_t size2 = val.size(); MojErr err2 = MojByteArrayToHex(key.data(), size1, str_buf); MojErrCheck(err2); if (size1 > 16) // if the object-id is in key strncat(str_buf, (char *)(key.data()) + (size1 - 17), 16); LOG_DEBUG("[db_ldb] ldb put: %s; keylen: %zu, key: %s ; vallen = %zu; err = %d\n", this->m_name.data(), size1, str_buf, size2, err); #endif if(leveldb_txn) ;//MojLdbErrCheck(batch->status(), _T("db->put")); else MojLdbErrCheck(s, _T("db->put")); postUpdate(txn, key.size() + val.size()); return MojErrNone; }
MojErr MojDbLevelDatabase::del(MojDbLevelItem& key, bool& foundOut, MojDbStorageTxn* txn) { LOG_TRACE("Entering function %s", __FUNCTION__); MojAssert(m_db); MojAssert( !txn || dynamic_cast<MojDbLevelAbstractTxn *> (txn) ); foundOut = false; MojErr err = txn->offsetQuota(-(MojInt64) key.size()); MojErrCheck(err); MojDbLevelAbstractTxn * leveldb_txn = static_cast<MojDbLevelAbstractTxn *> (txn); leveldb::Status st; if(leveldb_txn) { leveldb_txn->tableTxn(impl()).Delete(*key.impl()); } else st = m_db->Delete(MojDbLevelEngine::getWriteOptions(), *key.impl()); #if defined(MOJ_DEBUG) char str_buf[1024]; // big enough for any key size_t size = key.size(); MojErr err2 = MojByteArrayToHex(key.data(), size, str_buf); MojErrCheck(err2); if (size > 16) // if the object-id is in key strncat(str_buf, (char *)(key.data()) + (size - 17), 16); LOG_DEBUG("[db_ldb] ldbdel: %s; keylen: %zu, key= %s; err = %d \n", this->m_name.data(), size, str_buf, !st.ok()); #endif if (st.IsNotFound() == false) { MojLdbErrCheck(st, _T("db->del")); foundOut = true; } postUpdate(txn, key.size()); return MojErrNone; }