Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

cleanup code and command line options

git-svn-id: http://kazuho.31tools.com/svn/incline/trunk@242 4d3e2a30-9d6d-0410-bc8c-dac56cff10b3
  • Loading branch information...
commit 2c1066349b31cd4d67f03da3c4e227c30abd2923 1 parent 646f9ec
kazuho authored
View
24 src/incline.cc
@@ -12,6 +12,8 @@ using namespace std;
static getoptpp::opt_str opt_mode('m', "mode", false, "mode", "standalone");
static getoptpp::opt_str opt_source('s', "source", true, "definition file");
+static getoptpp::opt_flag opt_print_only(0, "print-only",
+ "print the SQLs to be issued instead");
static getoptpp::opt_str opt_forwarder_log_file(0, "forwarder-log-file", false,
"", "");
@@ -23,7 +25,11 @@ static void run_all_stmt(incline_dbms* dbh, const vector<string>& stmt)
for (vector<string>::const_iterator si = stmt.begin();
si != stmt.end();
++si) {
- dbh->execute(*si);
+ if (*opt_print_only) {
+ cout << *si << endl;
+ } else {
+ dbh->execute(*si);
+ }
}
}
@@ -65,7 +71,15 @@ main(int argc, char** argv)
// parse command
getoptpp::opt_version opt_version('v', "version", VERSION);
- getoptpp::opt_help opt_help('h', "help", argv[0], "load-triggers");
+ getoptpp::opt_help
+ opt_help('h', "help", argv[0],
+ "command\n\n"
+ "Commands:\n"
+ " create-trigger installs necessary triggers\n"
+ " drop-trigger unistalls triggers\n"
+ " create-queue creates queue tables\n"
+ " drop-queue drops queue tables\n"
+ " forward runs the forwarder\n");
if (! getoptpp::getopt(argc, argv)) {
exit(1);
}
@@ -142,12 +156,6 @@ main(int argc, char** argv)
} else if (command == "drop-trigger") {
vector<string> stmt(mgr->drop_trigger_all(true));
run_all_stmt(dbh(), stmt);
- } else if (command == "print-trigger") {
- vector<string> stmt(mgr->create_trigger_all(false));
- picojson::value a(picojson::array_type, false);
- copy(stmt.begin(), stmt.end(), back_inserter(a.get<picojson::array>()));
- a.serialize(ostream_iterator<char>(cout));
- cout << endl;
} else if (command == "create-queue") {
vector<string> stmt(aq_driver()->create_table_all(false, dbh()));
run_all_stmt(dbh(), stmt);
View
3  src/incline_dbms.h
@@ -17,7 +17,8 @@ class incline_dbms {
virtual unsigned short default_port() const = 0;
virtual std::vector<std::string> create_trigger(const std::string& name, const std::string& event, const std::string& time, const std::string& table, const std::map<std::string, std::string>& funcvar, const std::string& funcbody) const = 0;
virtual std::vector<std::string> drop_trigger(const std::string& name, const std::string& table, bool if_exists) const = 0;
- virtual std::string create_queue_table(const std::string& table_name, const std::string& column_defs, bool if_not_exists) const = 0;
+ virtual std::string serial_column_type() const = 0;
+ virtual std::string create_table_suffix() const = 0;
virtual std::string delete_using(const std::string& table_name, const std::vector<std::string>& using_list) const = 0;
virtual bool has_replace_into() const { return false; }
};
View
8 src/incline_driver_async_qtable.cc
@@ -106,10 +106,10 @@ incline_driver_async_qtable::_create_table_of(const incline_def_async_qtable*
col_defs.push_back(ci->second + ' '
+ dbh->get_column_def(def->destination(), ci->second));
}
- return
- incline_dbms::factory_
- ->create_queue_table(table_name, incline_util::join(',', col_defs),
- if_not_exists);
+ return string("CREATE TABLE ") + (if_not_exists ? "IF NOT EXISTS " : "")
+ + table_name + " (_iq_id " + incline_dbms::factory_->serial_column_type()
+ + ",_iq_action CHAR(1) NOT NULL," + incline_util::join(',', col_defs)
+ + ",PRIMARY KEY(_iq_id))" + incline_dbms::factory_->create_table_suffix();
}
void
View
55 src/incline_driver_sharded.cc
@@ -287,6 +287,7 @@ incline_driver_sharded::rule::parse(const string& file, string& err)
RANGE_ALGO("str-case-sensitive", string);
#undef RANGE_ALGO
if (algo == "hash-int") rule = new hash_int_rule(file);
+ if (algo == "replicate") rule = new replicator_rule(file);
if (rule == NULL) {
err = "unknown sharding algorithm: " + algo;
return NULL;
@@ -341,23 +342,6 @@ incline_driver_sharded::replicator_rule::parse(const picojson::value& def)
return string();
}
-void
-incline_driver_sharded::run_forwarder(int poll_interval, int log_fd) const
-{
- incline_fw_sharded::manager shard_mgr(this, poll_interval, log_fd);
- incline_fw_replicator::manager repl_mgr(this, poll_interval, log_fd);
- vector<pthread_t> threads;
-
- // create forwarders and writers
- shard_mgr.start(threads);
- repl_mgr.start(threads);
- // wait until all forwarder threads exit
- while (! threads.empty()) {
- pthread_join(threads.back(), NULL);
- threads.pop_back();
- }
-}
-
incline_driver_sharded::~incline_driver_sharded()
{
for (vector<const rule*>::iterator ri = rules_.begin(); ri != rules_.end(); ++ri) {
@@ -434,6 +418,43 @@ incline_driver_sharded::create_def() const
return new incline_def_sharded();
}
+vector<string>
+incline_driver_sharded::create_table_all(bool if_not_exists, incline_dbms* dbh)
+ const
+{
+ vector<string> r = super::create_table_all(if_not_exists, dbh);
+ r.push_back(string("CREATE TABLE ") + (if_not_exists ? "IF NOT EXISTS " : "")
+ + "_iq_repl (tbl_name VARCHAR(255) NOT NULL,_iq_id BIGINT NOT NULL,PRIMARY KEY (tbl_name))" + incline_dbms::factory_->create_table_suffix());
+ return r;
+}
+
+vector<string>
+incline_driver_sharded::drop_table_all(bool if_exists)
+ const
+{
+ vector<string> r = super::drop_table_all(if_exists);
+ r.push_back(string("DROP TABLE ") + (if_exists ? "IF EXISTS " : "")
+ + "_iq_repl");
+ return r;
+}
+
+void
+incline_driver_sharded::run_forwarder(int poll_interval, int log_fd) const
+{
+ incline_fw_sharded::manager shard_mgr(this, poll_interval, log_fd);
+ incline_fw_replicator::manager repl_mgr(this, poll_interval, log_fd);
+ vector<pthread_t> threads;
+
+ // create forwarders and writers
+ shard_mgr.start(threads);
+ repl_mgr.start(threads);
+ // wait until all forwarder threads exit
+ while (! threads.empty()) {
+ pthread_join(threads.back(), NULL);
+ threads.pop_back();
+ }
+}
+
bool
incline_driver_sharded::should_exit_loop() const
{
View
2  src/incline_driver_sharded.h
@@ -66,6 +66,8 @@ class incline_driver_sharded : public incline_driver_async_qtable {
virtual ~incline_driver_sharded();
std::string init(const std::string& host, unsigned short port);
virtual incline_def* create_def() const;
+ std::vector<std::string> create_table_all(bool if_not_exists, incline_dbms* dbh) const;
+ std::vector<std::string> drop_table_all(bool if_exists) const;
virtual void run_forwarder(int poll_interval, int log_fd) const;
virtual bool should_exit_loop() const;
const rule* rule_of(const std::string& file) const;
View
11 src/incline_mysql.cc
@@ -43,17 +43,6 @@ incline_mysql::factory::drop_trigger(const string& name, const string& table,
}
string
-incline_mysql::factory::create_queue_table(const string& table_name,
- const string& column_defs,
- bool if_not_exists) const
-{
- return string("CREATE TABLE ") + (if_not_exists ? "IF NOT EXISTS " : "")
- + table_name + " (_iq_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,"
- "_iq_action CHAR(1) CHARACTER SET latin1 NOT NULL," + column_defs
- + ",PRIMARY KEY (_iq_id))";
-}
-
-string
incline_mysql::factory::delete_using(const string& table_name,
const vector<string>& using_list) const
{
View
3  src/incline_mysql.h
@@ -17,7 +17,8 @@ class incline_mysql : public incline_dbms {
virtual unsigned short default_port() const { return 3306; }
virtual std::vector<std::string> create_trigger(const std::string& name, const std::string& event, const std::string& time, const std::string& table, const std::map<std::string, std::string>& funcvar, const std::string& funcbody) const;
virtual std::vector<std::string> drop_trigger(const std::string& name, const std::string& table, bool if_exists) const;
- virtual std::string create_queue_table(const std::string& table_name, const std::string& column_defs, bool if_not_exists) const;
+ virtual std::string serial_column_type() const { return "BIGINT UNSIGNED NOT NULL AUTO_INCREMENT"; }
+ virtual std::string create_table_suffix() const { return " ENGINE=InnoDB DEFAULT CHARSET=utf8"; }
virtual std::string delete_using(const std::string& table_name, const std::vector<std::string>& using_list) const;
virtual bool has_replace_into() const { return true; }
};
View
10 src/incline_pgsql.cc
@@ -78,16 +78,6 @@ incline_pgsql::factory::drop_trigger(const string& name, const string& table,
}
string
-incline_pgsql::factory::create_queue_table(const string& table_name,
- const string& column_defs,
- bool if_not_exists) const
-{
- return string("CREATE TABLE ") + (if_not_exists ? "IF NOT EXISTS " : "")
- + table_name + " (_iq_id SERIAL,_iq_action CHAR(1) NOT NULL," + column_defs
- + ",PRIMARY KEY (_iq_id))";
-}
-
-string
incline_pgsql::factory::delete_using(const string& table_name,
const vector<string>& using_list) const
{
View
3  src/incline_pgsql.h
@@ -14,7 +14,8 @@ class incline_pgsql : public incline_dbms {
virtual unsigned short default_port() const { return 5432; }
virtual std::vector<std::string> create_trigger(const std::string& name, const std::string& event, const std::string& time, const std::string& table, const std::map<std::string, std::string>& funcvar, const std::string& funcbody) const;
virtual std::vector<std::string> drop_trigger(const std::string& name, const std::string& table, bool if_exists) const;
- virtual std::string create_queue_table(const std::string& table_name, const std::string& column_defs, bool if_not_exists) const;
+ virtual std::string serial_column_type() const { return "SERIAL"; }
+ virtual std::string create_table_suffix() const { return std::string(); }
virtual std::string delete_using(const std::string& table_name, const std::vector<std::string>& using_list) const;
};
protected:
Please sign in to comment.
Something went wrong with that request. Please try again.