Skip to content

Commit

Permalink
add const to select()
Browse files Browse the repository at this point in the history
  • Loading branch information
ancelmo committed Apr 12, 2019
1 parent 7966925 commit d2201dc
Show file tree
Hide file tree
Showing 16 changed files with 217 additions and 50 deletions.
5 changes: 3 additions & 2 deletions libblockchain/BlockChainImp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -893,8 +893,9 @@ void BlockChainImp::writeTotalTransactionCount(
auto currentCount = lexical_cast<int64_t>(entry->getField(SYS_VALUE));
currentCount += block.transactions().size();

entry->setField(SYS_VALUE, lexical_cast<std::string>(currentCount));
tb->update(SYS_KEY_TOTAL_TRANSACTION_COUNT, entry, tb->newCondition());
auto updateEntry = tb->newEntry();
updateEntry->setField(SYS_VALUE, lexical_cast<std::string>(currentCount));
tb->update(SYS_KEY_TOTAL_TRANSACTION_COUNT, updateEntry, tb->newCondition());
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion libprecompiled/CRUDPrecompiled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ bytes CRUDPrecompiled::call(ExecutiveContext::Ptr context, bytesConstRef param,
{
auto entries = table->select(key, table->newCondition());
auto entriesPrecompiled = std::make_shared<EntriesPrecompiled>();
entriesPrecompiled->setEntries(entries);
//entriesPrecompiled->setEntries(entries);
auto newAddress = context->registerPrecompiled(entriesPrecompiled);
out = abi.abiIn("", newAddress);
}
Expand Down
10 changes: 5 additions & 5 deletions libprecompiled/extension/DagTransferPrecompiled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,12 +325,12 @@ void DagTransferPrecompiled::userSaveCall(dev::blockverifier::ExecutiveContext::
break;
}

entry = table->newEntry();
entry->setField(DAG_TRANSFER_FIELD_NAME, user);
entry->setField(DAG_TRANSFER_FIELD_BALANCE, new_balance.str());
auto updateEntry = table->newEntry();
updateEntry->setField(DAG_TRANSFER_FIELD_NAME, user);
updateEntry->setField(DAG_TRANSFER_FIELD_BALANCE, new_balance.str());

auto count = table->update(
user, entry, table->newCondition(), std::make_shared<AccessOptions>(origin));
user, updateEntry, table->newCondition(), std::make_shared<AccessOptions>(origin));
if (count == CODE_NO_AUTHORIZED)
{ // permission denied
strErrorMsg = "permission denied";
Expand Down Expand Up @@ -582,4 +582,4 @@ void DagTransferPrecompiled::userTransferCall(
} while (0);

out = abi.abiIn("", ret);
}
}
1 change: 1 addition & 0 deletions libstorage/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace storage

/// \brief Sign of the DB key is valid or not
const char* const ID_FIELD = "_id_";
const char* const NUM_FIELD = "_num_";
const char* const STATUS = "_status_";
const char* const SYS_TABLES = "_sys_tables_";
const char* const SYS_CONSENSUS = "_sys_consensus_";
Expand Down
2 changes: 1 addition & 1 deletion libstorage/EntriesPrecompiled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ bytes dev::blockverifier::EntriesPrecompiled::call(
u256 num;
abi.abiOut(data, num);

auto entry = m_entries->get(num.convert_to<size_t>());
Entry::Ptr entry = m_entries->get(num.convert_to<size_t>());
EntryPrecompiled::Ptr entryPrecompiled = std::make_shared<EntryPrecompiled>();
entryPrecompiled->setEntry(entry);
Address address = context->registerPrecompiled(entryPrecompiled);
Expand Down
10 changes: 10 additions & 0 deletions libstorage/EntriesPrecompiled.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,20 @@ class EntriesPrecompiled : public Precompiled
ExecutiveContext::Ptr context, bytesConstRef param, Address const& origin = Address());

void setEntries(dev::storage::Entries::Ptr entries) { m_entries = entries; }
void setEntries(dev::storage::Entries::ConstPtr entries) { m_entriesConst = entries; }
dev::storage::Entries::Ptr getEntries() { return m_entries; }
dev::storage::Entries::ConstPtr getEntries() const {
if(m_entriesConst) {
return m_entries;
}
else {
return m_entries;
}
}

private:
dev::storage::Entries::Ptr m_entries;
dev::storage::Entries::ConstPtr m_entriesConst;
};

} // namespace blockverifier
Expand Down
7 changes: 4 additions & 3 deletions libstorage/EntryPrecompiled.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#pragma once

#include "libblockverifier/Precompiled.h"
#include "Table.h"

namespace dev
{
Expand Down Expand Up @@ -62,11 +63,11 @@ class EntryPrecompiled : public Precompiled
virtual bytes call(
std::shared_ptr<ExecutiveContext>, bytesConstRef param, Address const& origin = Address());

void setEntry(std::shared_ptr<dev::storage::Entry> entry) { m_entry = entry; }
std::shared_ptr<dev::storage::Entry> getEntry() { return m_entry; }
void setEntry(dev::storage::Entry::Ptr entry) { m_entry = entry; }
dev::storage::Entry::Ptr getEntry() const { return m_entry; };

private:
std::shared_ptr<dev::storage::Entry> m_entry;
dev::storage::Entry::Ptr m_entry;
};

} // namespace blockverifier
Expand Down
2 changes: 1 addition & 1 deletion libstorage/MemoryTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class MemoryTable : public Table

virtual ~MemoryTable(){};

virtual typename Entries::Ptr select(const std::string& key, Condition::Ptr condition) override
virtual typename Entries::ConstPtr select(const std::string& key, Condition::Ptr condition) override
{
try
{
Expand Down
14 changes: 9 additions & 5 deletions libstorage/MemoryTable2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ using namespace dev::precompiled;

MemoryTable2::MemoryTable2() : m_newEntries(std::make_shared<EntriesType>()) {}

Entries::Ptr MemoryTable2::select(const std::string& key, Condition::Ptr condition)
Entries::ConstPtr MemoryTable2::select(const std::string& key, Condition::Ptr condition) {
return selectNoLock(key, condition);
}

Entries::Ptr MemoryTable2::selectNoLock(const std::string& key, Condition::Ptr condition)
{
try
{
Expand Down Expand Up @@ -104,12 +108,12 @@ int MemoryTable2::update(

checkField(entry);

auto entries = select(key, condition);
auto entries = selectNoLock(key, condition);
std::vector<Change::Record> records;

for (size_t i = 0; i < entries->size(); ++i)
{
auto updateEntry = entries->get(i);
Entry::Ptr updateEntry = entries->get(i);

// if id not equals to zero and not in the m_dirty, must be new dirty entry
if (updateEntry->getID() != 0 && m_dirty.find(updateEntry->getID()) == m_dirty.end())
Expand Down Expand Up @@ -194,12 +198,12 @@ int MemoryTable2::remove(
return storage::CODE_NO_AUTHORIZED;
}

auto entries = select(key, condition);
auto entries = selectNoLock(key, condition);

std::vector<Change::Record> records;
for (size_t i = 0; i < entries->size(); ++i)
{
auto removeEntry = entries->get(i);
Entry::Ptr removeEntry = entries->get(i);

removeEntry->setStatus(1);

Expand Down
5 changes: 4 additions & 1 deletion libstorage/MemoryTable2.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <tbb/concurrent_unordered_map.h>
#include <boost/exception/diagnostic_information.hpp>
#include <boost/lexical_cast.hpp>
#include <libdevcore/FixedHash.h>
#include <type_traits>

namespace dev
Expand All @@ -47,7 +48,7 @@ class MemoryTable2 : public Table

virtual ~MemoryTable2(){};

virtual Entries::Ptr select(const std::string& key, Condition::Ptr condition) override;
virtual Entries::ConstPtr select(const std::string& key, Condition::Ptr condition) override;

virtual int update(const std::string& key, Entry::Ptr entry, Condition::Ptr condition,
AccessOptions::Ptr options = std::make_shared<AccessOptions>()) override;
Expand Down Expand Up @@ -108,6 +109,8 @@ class MemoryTable2 : public Table
virtual void rollback(const Change& _change) override;

private:
Entries::Ptr selectNoLock(const std::string& key, Condition::Ptr condition);

using EntriesType = ConcurrentEntries;
using EntriesPtr = EntriesType::Ptr;
EntriesPtr m_newEntries;
Expand Down
61 changes: 55 additions & 6 deletions libstorage/RangeCachedStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,48 @@ Entries::Ptr CachePage::process(Condition::Ptr condition) {
return out;
}

std::list<Entry::Ptr>* CachePage::entries() {
return &m_entries;
}

void CachePage::mergeFrom(CachePage::Ptr cachePage, bool move) {
for(auto entryIt = m_entries.begin(); entryIt != m_entries.end(); ++entryIt) {
auto entry = cachePage->getByID((*entryIt)->getID());
if(entry && entry->num() > (*entryIt)->num()) {
*entryIt = entry;

if(move) {
cachePage->removeByID(entry->getID());
}
}
}
}

TableInfo::Ptr TableCache::tableInfo() {
return m_tableInfo;
}

std::vector<CachePage::Ptr>* TableCache::cachePages() {
return &m_cachePages;
}

CachePage::Ptr TableCache::tempPage() {
return m_tempPage;
}

Entries::Ptr RangeCachedStorage::select(h256 hash, int num, const std::string& table,
const std::string& key, Condition::Ptr condition) {
auto it = m_caches.find(table);
if(it != m_caches.end()) {
for(auto cacheIt: it->second->cachePages) {
for(auto cacheIt: *(it->second->cachePages())) {
if(cacheIt->condition()->graterThan(condition)) {
return cacheIt->process(condition);
}
}

if(it->second->tempPage()->condition()->graterThan(condition)) {
return it->second->tempPage()->process(condition);
}
}

if(m_backend) {
Expand All @@ -105,12 +138,23 @@ Entries::Ptr RangeCachedStorage::select(h256 hash, int num, const std::string& t
cachePage->setCondition(condition);
cachePage->import(out);

for(auto pageIt = tableIt->second->cachePages.begin(); pageIt != tableIt->second->cachePages.end(); ++pageIt) {
for(auto pageIt = tableIt->second->cachePages()->begin(); pageIt != tableIt->second->cachePages()->end(); ++pageIt) {
if(condition->graterThan((*pageIt)->condition())) {
pageIt = tableIt->second->cachePages.erase(pageIt);
pageIt = tableIt->second->cachePages()->erase(pageIt);
continue;
}

if(condition->related((*pageIt)->condition())) {
//related to page, merge related entry
cachePage->mergeFrom(*pageIt);
}
}

if(condition->related(tableIt->second->tempPage()->condition())) {
cachePage->mergeFrom(tableIt->second->tempPage(), true);
}
tableIt->second->cachePages.push_back(cachePage);

tableIt->second->cachePages()->push_back(cachePage);
return out;
}

Expand All @@ -119,6 +163,10 @@ Entries::Ptr RangeCachedStorage::select(h256 hash, int num, const std::string& t

size_t RangeCachedStorage::commit(h256 hash, int64_t num, const std::vector<TableData::Ptr>& datas,
h256 const& blockHash) {
(void)hash;
(void)num;
(void)blockHash;

for(auto it: datas) {
auto tableIt = m_caches.find(it->tableName);
if(tableIt == m_caches.end()) {
Expand All @@ -127,9 +175,10 @@ size_t RangeCachedStorage::commit(h256 hash, int64_t num, const std::vector<Tabl

for(size_t i=0;i<it->entries->size();++i) {
auto entry = it->entries->get(i);
for(auto pageIt: tableIt->second->cachePages) {
for(auto pageIt: *(tableIt->second->cachePages())) {
if(pageIt->condition()->process(entry)) {
pageIt->entries->addEntry(entry);
pageIt->addEntry(entry);
break;
}
}
}
Expand Down
15 changes: 12 additions & 3 deletions libstorage/RangeCachedStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,13 @@ class CachePage: std::enable_shared_from_this<CachePage> {
virtual int64_t num();
virtual Entries::Ptr process(Condition::Ptr condition);

virtual std::list<Entry::Ptr>* entries();
virtual void mergeFrom(CachePage::Ptr cachePage, bool move = false);

private:
std::list<Entry::Ptr> m_entries;
std::map<int64_t, std::list<Entry::Ptr>::iterator > m_ID2Entry;
//std::map<std::string, std::list<Entry::Ptr>::iterator >

Condition::Ptr m_condition;
int64_t m_num = 0;
Expand All @@ -56,10 +60,15 @@ class CachePage: std::enable_shared_from_this<CachePage> {
class TableCache {
public:
typedef std::shared_ptr<TableCache> Ptr;
virtual ~TableCache() {};

virtual TableInfo::Ptr tableInfo();
virtual std::vector<CachePage::Ptr>* cachePages();
virtual CachePage::Ptr tempPage();

TableInfo::Ptr tableInfo;
std::vector<CachePage::Ptr> cachePages;
CachePage::Ptr m_unindexPage;
TableInfo::Ptr m_tableInfo;
std::vector<CachePage::Ptr> m_cachePages;
CachePage::Ptr m_tempPage;
};

class RangeCachedStorage : public Storage
Expand Down
Loading

0 comments on commit d2201dc

Please sign in to comment.