void InputCommandTemplateUnitTests::run() { assert( test1(L"", 1, L"") ); assert( test1(L"aaaa;#bbbb dd ff gg;cccc", 3, L"aaaa", L"#bbbb dd ff gg", L"cccc") ); assert( test1(L" {aaaa \"fff;vvv\" {'':; }gg}", 1, L" aaaa \"fff;vvv\" {'':; }gg" ) ); assert( test1(L"#abc {asdasd}; def xxx; #eee 'aaa' 'bbb' ", 3, L"#abc {asdasd}", L" def xxx", L"#eee 'aaa' 'bbb' ") ); assert( test1(L" #ddd {fff} 'f;f{f;f}f;f' {{fd;fd}f;f}", 1, L"#ddd {fff} 'f;f{f;f}f;f' {{fd;fd}f;f}") ); assert( test1(L"#ddd {fff} ; {ffff 'f;f{f;f}f;f'}", 2, L"#ddd {fff} ", L" ffff 'f;f{f;f}f;f'") ); assert( test1(L"#bbb '{};fff;\"aaa\"'", 1, L"#bbb '{};fff;\"aaa\"'") ); InputCommands g1; g1.push_back(makecmd(false, L" ", L"", L" ", 0)); assert(test2(L" ", 0, &g1)); InputCommands g2; g2.push_back(makecmd(false, L"тест", L" 123 456 789", L"тест", 3, L" 123", L" 456", L" 789" )); assert(test2(L"тест 123 456 789", 3, &g2)); InputCommands g3; g3.push_back(makecmd(false, L"тест2", L" aaa bbb ccc ", L"тест2", 4, L" aaa", L" bbb", L" ccc", L" ")); assert(test2(L"тест2 aaa bbb ccc ", 4, &g3)); InputCommands t1; t1.push_back( makecmd(true, L"# wout", L" 1 test test2", L" wout", 3, L"1", L"test", L"test2") ); assert( test2(L"# wout 1 test test2", 0, &t1) ); InputCommands t2; t2.push_back(makecmd(true, L"#wout", L" 1 {red} blue", L"wout", 3, L"1", L"red", L"blue")); assert(test2(L"#wout 1 {red} blue", 0, &t2)); InputCommands t3; t3.push_back(makecmd(true, L"#wout", L" 'a;b;c\"\"'", L"wout", 1, L"a;b;c\"\"")); t3.push_back(makecmd(true, L"#ex", L" {v;d's}", L"ex", 1, L"v;d's")); assert(test2(L"#wout 'a;b;c\"\"';#ex {v;d's}", 0, &t3)); InputCommands t4; t4.push_back(makecmd(true, L"#wout", L" %0", L"wout", 1, param0)); assert(test2(L"#wout %0", 0, &t4)); InputCommands t5; tstring p1p2(param1); p1p2.append(param2); t5.push_back(makecmd(true, L"#test", L" '%1%2' %3 %4 %0", L"test", 4, p1p2.c_str(), param3, param4, param0)); assert(test2(L"#test '%1%2' %3 %4 %0", 4, &t5)); InputCommands t6; t6.push_back(makecmd(true, L"#wait", L" 1.2 {#out {light red} {Время идти!};встать}", L"wait", 2, L"1.2", L"#out {light red} {Время идти!};встать")); assert(test2(L"#wait 1.2 {#out {light red} {Время идти!};встать}", 2, &t6)); }
void client::lset(const client::string_type & key, client::int_type index, const client::string_type & value) { send_(makecmd("LSET") << key << ' ' << index << ' ' << value.length() << CRLF << value); recv_ok_reply_(); }
void client::smove(const client::string_type & srckey, const client::string_type & dstkey, const client::string_type & value) { send_(makecmd("SMOVE") << srckey << ' ' << dstkey << ' ' << value.length() << CRLF << value); recv_int_ok_reply_(); }
void client::ltrim(const client::string_type & key, client::int_type start, client::int_type end) { send_(makecmd("LTRIM") << key << ' ' << start << ' ' << end); recv_ok_reply_(); }
client::int_type client::keys(const client::string_type & pattern, client::string_vector & out) { send_(makecmd("KEYS") << pattern); string resp = recv_bulk_reply_(); return split(resp, ' ', out); }
client::string_type client::get(const client::string_type & key) { string cmd = makecmd("GET") << key; cout<<"get cmd:"<<cmd<<endl; send_(cmd); return recv_bulk_reply_(); }
client::int_type client::lrem(const client::string_type & key, client::int_type count, const client::string_type & value) { send_(makecmd("LREM") << key << ' ' << count << ' ' << value.length() << CRLF << value); return recv_int_reply_(); }
client::int_type client::lrange(const client::string_type & key, client::int_type start, client::int_type end, client::string_vector & out) { send_(makecmd("LRANGE") << key << ' ' << start << ' ' << end); return recv_multi_bulk_reply_(out); }
void client::set(const client::string_type & key, const client::string_type & value) { string cmd = makecmd("SET") << key << ' ' << value; //send_(makecmd("SET") << key << ' ' << value.size() << CRLF << value); cout<<"client::set cmd:"<<cmd<<endl; send_(cmd); recv_ok_reply_(); }
client::int_type client::sort(const client::string_type & key, client::string_vector & out, client::sort_order order, bool lexicographically) { send_(makecmd("SORT") << key << (order == sort_order_ascending ? " ASC" : " DESC") << (lexicographically ? " ALPHA" : "")); return recv_multi_bulk_reply_(out); }
client::datatype client::type(const client::string_type & key) { send_(makecmd("TYPE") << key); string response = recv_single_line_reply_(); if (response == "none") return datatype_none; if (response == "string") return datatype_string; if (response == "list") return datatype_list; if (response == "set") return datatype_set; return datatype_none; }
void client::info(server_info & out) { send_(makecmd("INFO", true)); string response = recv_bulk_reply_(); if (response.empty()) throw protocol_error("empty"); string_vector lines; split_lines(response, lines); if (lines.empty()) throw protocol_error("empty line for info"); for (string_vector::const_iterator it = lines.begin(); it != lines.end(); ++it) { const string & line = *it; string_vector line_parts; split(line, ':', line_parts); if (line_parts.size() != 2) throw protocol_error("unexpected line format for info"); const string & key = line_parts[0]; const string & val = line_parts[1]; if (key == server_info_key_version) out.version = val; else if (key == server_info_key_bgsave_in_progress) out.bgsave_in_progress = value_from_string<unsigned long>(val) == 1; else if (key == server_info_key_connected_clients) out.connected_clients = value_from_string<unsigned long>(val); else if (key == server_info_key_connected_slaves) out.connected_slaves = value_from_string<unsigned long>(val); else if (key == server_info_key_used_memory) out.used_memory = value_from_string<unsigned long>(val); else if (key == server_info_key_changes_since_last_save) out.changes_since_last_save = value_from_string<unsigned long>(val); else if (key == server_info_key_last_save_time) out.last_save_time = value_from_string<unsigned long>(val); else if (key == server_info_key_total_connections_received) out.total_connections_received = value_from_string<unsigned long>(val); else if (key == server_info_key_total_commands_processed) out.total_commands_processed = value_from_string<unsigned long>(val); else if (key == server_info_key_uptime_in_seconds) out.uptime_in_seconds = value_from_string<unsigned long>(val); else if (key == server_info_key_uptime_in_days) out.uptime_in_days = value_from_string<unsigned long>(val); else if (key == server_info_key_role) out.role = val == server_info_value_role_master ? role_master : role_slave; else throw protocol_error(string("unexpected info key '") + key + "'"); } }
void client::shutdown() { send_(makecmd("SHUTDOWN", true)); // we expected to get a connection_error as redis closes the connection on shutdown command. try { recv_ok_reply_(); } catch (connection_error & e) { } }
time_t client::lastsave() { send_(makecmd("LASTSAVE", true)); return recv_int_reply_(); }
void client::bgsave() { send_(makecmd("BGSAVE", true)); recv_ok_reply_(); }
void client::lpush(const client::string_type & key, const client::string_type & value) { send_(makecmd("LPUSH") << key << ' ' << value.length() << CRLF << value); recv_ok_reply_(); }
void client::flushall() { send_(makecmd("FLUSHALL", true)); recv_ok_reply_(); }
void client::flushdb() { send_(makecmd("FLUSHDB", true)); recv_ok_reply_(); }
void client::move(const client::string_type & key, client::int_type dbindex) { send_(makecmd("MOVE") << key << ' ' << dbindex); recv_int_ok_reply_(); }
void client::srem(const client::string_type & key, const client::string_type & value) { send_(makecmd("SREM") << key << ' ' << value.length() << CRLF << value); recv_int_ok_reply_(); }
client::int_type client::smembers(const client::string_type & key, client::string_set & out) { send_(makecmd("SMEMBERS") << key); return recv_multi_bulk_reply_(out); }
client::int_type client::sunionstore(const client::string_type & dstkey, const client::string_vector & keys) { send_(makecmd("SUNIONSTORE") << dstkey << ' ' << keys); return recv_int_reply_(); }
client::int_type client::sunion(const client::string_vector & keys, client::string_set & out) { send_(makecmd("SUNION") << keys); return recv_multi_bulk_reply_(out); }
client::int_type client::sinter(const client::string_vector & keys, client::string_set & out) { send_(makecmd("SINTER") << keys); return recv_multi_bulk_reply_(out); }
bool client::sismember(const client::string_type & key, const client::string_type & value) { send_(makecmd("SISMEMBER") << key << ' ' << value.length() << CRLF << value); return recv_int_reply_() == 1; }
client::int_type client::llen(const client::string_type & key) { send_(makecmd("LLEN") << key); return recv_int_reply_(); }
client::string_type client::lindex(const client::string_type & key, client::int_type index) { send_(makecmd("LINDEX") << key << ' ' << index); return recv_bulk_reply_(); }
void client::select(client::int_type dbindex) { send_(makecmd("SELECT") << dbindex); recv_ok_reply_(); }
client::int_type client::scard(const client::string_type & key) { send_(makecmd("SCARD") << key); return recv_int_reply_(); }
client::string_type client::rpop(const client::string_type & key) { send_(makecmd("RPOP") << key); return recv_bulk_reply_(); }