Skip to content
Permalink
Browse files

Use a thread safe DB access method for threaded input is spent check

  • Loading branch information
zpalmtree committed Feb 8, 2020
1 parent 2ee9995 commit c75d4b5a9eb291f075ca14a25681d6d05a2aed6a
@@ -21,5 +21,7 @@ namespace CryptoNote
virtual std::error_code write(IWriteBatch &batch) = 0;

virtual std::error_code read(IReadBatch &batch) = 0;

virtual std::error_code readThreadSafe(IReadBatch &batch) = 0;
};
} // namespace CryptoNote
@@ -1188,7 +1188,7 @@ namespace CryptoNote
bool DatabaseBlockchainCache::checkIfSpent(const Crypto::KeyImage &keyImage, uint32_t blockIndex) const
{
auto batch = BlockchainReadBatch().requestBlockIndexBySpentKeyImage(keyImage);
auto res = database.read(batch);
auto res = database.readThreadSafe(batch);
if (res)
{
logger(Logging::ERROR) << "checkIfSpent failed, request to database failed: " << res.message();
@@ -188,6 +188,48 @@ std::error_code RocksDBWrapper::read(IReadBatch &batch)
return std::error_code();
}

std::error_code RocksDBWrapper::readThreadSafe(IReadBatch &batch)
{
if (state.load() != INITIALIZED)
{
throw std::runtime_error("Not initialized.");
}

rocksdb::ReadOptions readOptions;

std::vector<std::string> rawKeys(batch.getRawKeys());

std::vector<std::string> values(rawKeys.size());

std::vector<bool> resultStates;

int i = 0;

for (const std::string &key : rawKeys)
{
const rocksdb::Status status = db->Get(readOptions, rocksdb::Slice(key), &values[i]);

if (status.ok())
{
resultStates.push_back(true);
}
else
{
if (!status.IsNotFound())
{
return make_error_code(CryptoNote::error::DataBaseErrorCodes::INTERNAL_ERROR);
}

resultStates.push_back(false);
}

i++;
}

batch.submitRawResult(values, resultStates);
return std::error_code();
}

rocksdb::Options RocksDBWrapper::getDBOptions(const DataBaseConfig &config)
{
rocksdb::DBOptions dbOptions;
@@ -41,6 +41,8 @@ namespace CryptoNote

std::error_code read(IReadBatch &batch) override;

std::error_code readThreadSafe(IReadBatch &batch) override;

private:
std::error_code write(IWriteBatch &batch, bool sync);

0 comments on commit c75d4b5

Please sign in to comment.
You can’t perform that action at this time.