Skip to content

Commit

Permalink
Add the query_txid_plugin as a option on the develop branch
Browse files Browse the repository at this point in the history
  • Loading branch information
lelecommit committed Aug 26, 2019
1 parent a16942d commit 32ba6bb
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
/*
* Copyright (c) 2019 GXChain and zhaoxiangfei、bijianing97 .
*
* The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#pragma once
#include <graphene/db/object.hpp>
//#include <graphene/chain/protocol/types.hpp>
//#include <graphene/db/object.hpp>
#include <graphene/protocol/types.hpp>
#include <boost/multi_index/composite_key.hpp>

Expand All @@ -20,7 +41,6 @@ namespace graphene { namespace chain {
uint32_t trx_in_block;
};

// struct by_id;
struct by_txid;
struct by_blocknum;

Expand All @@ -39,4 +59,4 @@ namespace graphene { namespace chain {
} } // graphene::chain

FC_REFLECT_DERIVED( graphene::chain::trx_entry_object, (graphene::chain::object),
(txid)(block_num)(trx_in_block))
(txid)(block_num)(trx_in_block))
2 changes: 1 addition & 1 deletion libraries/plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ add_subdirectory( es_objects )
add_subdirectory( api_helper_indexes )
if(${LOAD_QUERY_TXID_PLUGIN} STREQUAL "ON")
add_subdirectory( query_txid )
endif()
endif()
1 change: 0 additions & 1 deletion libraries/plugins/query_txid/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,3 @@ if(LevelDB_INCLUDE_PATH AND LevelDB_LIBRARY AND Snappy_LIBRARY)
else(LevelDB_INCLUDE_PATH AND LevelDB_LIBRARY AND Snappy_LIBRARY)
message(FATAL_ERROR "You need leveldb and snappy")
endif()

Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
/*
* Copyright (c) 2019 GXChain and zhaoxiangfei、bijianing97 .
*
* The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#pragma once
#include <graphene/app/plugin.hpp>
#include <graphene/chain/transaction_entry_object.hpp>
#include <graphene/chain/database.hpp>


namespace graphene
{
namespace query_txid
{

using namespace chain;
namespace detail
{
Expand All @@ -35,5 +55,4 @@ class query_txid_plugin : public graphene::app::plugin

std::unique_ptr<detail::query_txid_plugin_impl> my;
};
} // namespace query_txid
} // namespace graphene
} }// graphene::query_txid
41 changes: 27 additions & 14 deletions libraries/plugins/query_txid/query_txid_plugin.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
/*
* Copyright (c) 2019 GXChain and zhaoxiangfei、bijianing97 .
*
* The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <graphene/query_txid/query_txid_plugin.hpp>
#include <fc/io/fstream.hpp>
#include <graphene/chain/transaction_entry_object.hpp>
Expand Down Expand Up @@ -34,7 +57,7 @@ class query_txid_plugin_impl
void init();
static optional<trx_entry_object> query_trx_by_id(std::string txid);
std::string db_path = "trx_entry.db";
uint64_t limit_batch = 1000; //limit of leveldb batch
uint64_t limit_batch = 1000;

private:
query_txid_plugin &_self;
Expand All @@ -43,20 +66,17 @@ class query_txid_plugin_impl
fc::signal<void(const uint64_t)> sig_remove;

static leveldb::DB *leveldb;
void consume_block(); //Consume block
void remove_trx_index(const uint64_t trx_entry_id); //Remove trx_index in db
void consume_block();
void remove_trx_index(const uint64_t trx_entry_id);
};
leveldb::DB *query_txid_plugin_impl::leveldb = nullptr;

void query_txid_plugin_impl::init()
{
try {
//Create leveldb
leveldb::Options options;
options.create_if_missing = true;
leveldb::Status s = leveldb::DB::Open(options, db_path, &leveldb);

// Respond to the sig_db_write signale
sig_db_write.connect([&]() { consume_block(); });
sig_remove.connect([&](const uint64_t trx_entry_id) { remove_trx_index(trx_entry_id); });
}
Expand Down Expand Up @@ -136,7 +156,6 @@ void query_txid_plugin_impl::remove_trx_index(const uint64_t trx_entry_id)
try {
graphene::chain::database &db = database();
const auto &trx_idx = db.get_index_type<trx_entry_index>().indices();
//ilog("remove,${trx_ent_id},bengin: ${begin},end: ${end}",("trx_ent_id",trx_entry_id)("begin",trx_idx.begin()->id.instance())("end",trx_idx.rbegin()->id.instance()));
for (auto itor = trx_idx.begin(); itor != trx_idx.end();) {
auto backup_itr = itor;
++itor;
Expand All @@ -151,8 +170,6 @@ void query_txid_plugin_impl::remove_trx_index(const uint64_t trx_entry_id)
}
} // namespace detail

// -----------------------------------query_txid_plugin --------------------------------------

query_txid_plugin::query_txid_plugin()
: my(new detail::query_txid_plugin_impl(*this))
{
Expand All @@ -179,9 +196,7 @@ void query_txid_plugin::plugin_initialize(const boost::program_options::variable
{
try {
ilog("query_txid plugin initialized");
// Add the index of the trx_entry_index object table to the database
database().add_index<primary_index<trx_entry_index>>();
// Respond to the apply_block signal
database().applied_block.connect([&](const signed_block &b) { my->collect_txid_index(b); });
if (options.count("query-txid-path")) {
my->db_path = options["query-txid-path"].as<std::string>();
Expand All @@ -191,7 +206,6 @@ void query_txid_plugin::plugin_initialize(const boost::program_options::variable
if (options.count("limit-batch")) {
my->limit_batch = options["limit-batch"].as<uint64_t>();
}
// Initialize the plugin instance
my->init();
}
FC_LOG_AND_RETHROW()
Expand All @@ -206,5 +220,4 @@ optional<trx_entry_object> query_txid_plugin::query_trx_by_id(std::string txid)
return detail::query_txid_plugin_impl::query_trx_by_id(txid);
}

} // namespace query_txid
} // namespace graphene
} } // graphene::query_txid

0 comments on commit 32ba6bb

Please sign in to comment.