Skip to content

Commit

Permalink
Memcached seems to be empty #7
Browse files Browse the repository at this point in the history
  • Loading branch information
topilski committed Jul 29, 2016
1 parent 8e4a2a4 commit 6ffe677
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 13 deletions.
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
0.7.1 /
[Alexandr Topilski]
- Statitstics module
- [Memcached] content database and keys functionality

0.7.0 / Jule 17, 2016
[Alexandr Topilski]
Expand Down
72 changes: 65 additions & 7 deletions src/core/memcached/db_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,36 @@
#include "common/utils.h"
#include "common/sprintf.h"

namespace {

struct KeysHolder {
const std::string key_start;
const std::string key_end;
const uint64_t limit;
std::vector<std::string>* r;
memcached_return_t addKey(const char *key, size_t key_length) {
std::string received_key(key, key_length);
if (r->size() <= limit && key_start < received_key && received_key < key_end) {
r->push_back(received_key);
return MEMCACHED_SUCCESS;
}

return MEMCACHED_SUCCESS;
}
};

memcached_return_t memcached_dump_callback(const memcached_st* ptr,
const char* key,
size_t key_length,
void* context) {
UNUSED(ptr);

KeysHolder* holder = static_cast<KeysHolder*>(context);
return holder->addKey(key, key_length);
}

}

namespace fastonosql {
namespace core {
template<>
Expand Down Expand Up @@ -184,10 +214,24 @@ const char* DBConnection::versionApi() {
return memcached_lib_version();
}

common::Error DBConnection::keys(const char* args) {
UNUSED(args);
common::Error DBConnection::keys(const std::string& key_start, const std::string& key_end,
uint64_t limit, std::vector<std::string>* ret) {
if (!isConnected()) {
DNOTREACHED();
return common::make_error_value("Not connected", common::Value::E_ERROR);
}

KeysHolder hld = {key_start, key_end, limit, ret};
memcached_dump_fn func[1] = {0};
func[0] = memcached_dump_callback;
memcached_return_t error = memcached_dump(connection_.handle_, func, &hld, SIZEOFMASS(func));
if (error == MEMCACHED_ERROR) {
std::string buff = common::MemSPrintf("Keys function error: %s",
memcached_strerror(connection_.handle_, error));
return common::make_error_value(buff, common::ErrorValue::E_ERROR);
}

return notSupported("KEYS");
return common::Error();
}

common::Error DBConnection::info(const char* args, ServerInfo::Common* statsout) {
Expand Down Expand Up @@ -448,18 +492,32 @@ common::Error DBConnection::help(int argc, char** argv) {

common::Error keys(CommandHandler* handler, int argc, char** argv, FastoObject* out) {
UNUSED(argc);
UNUSED(argv);
UNUSED(out);

DBConnection* mem = static_cast<DBConnection*>(handler);
return mem->keys("items");
std::vector<std::string> keysout;
common::Error er = mem->keys(argv[0], argv[1], atoll(argv[2]), &keysout);
if (!er) {
common::ArrayValue* ar = common::Value::createArrayValue();
for (size_t i = 0; i < keysout.size(); ++i) {
common::StringValue* val = common::Value::createStringValue(keysout[i]);
ar->append(val);
}
FastoObjectArray* child = new FastoObjectArray(out, ar, mem->delimiter(), mem->nsSeparator());
out->addChildren(child);
}

return er;
}

common::Error stats(CommandHandler* handler, int argc, char** argv, FastoObject* out) {
DBConnection* mem = static_cast<DBConnection*>(handler);
const char* args = argc == 1 ? argv[0] : nullptr;
if (args && strcasecmp(args, "items") == 0) {
return mem->keys(args);
char* largv[3] = {0};
largv[0] = "a";
largv[1] = "z";
largv[2] = "100";
return keys(handler, SIZEOFMASS(argv), largv, out);
}

ServerInfo::Common statsout;
Expand Down
5 changes: 4 additions & 1 deletion src/core/memcached/db_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ struct DBConnection
config_t config() const;
static const char* versionApi();

common::Error keys(const char* args) WARN_UNUSED_RESULT;
common::Error keys(const std::string& key_start, const std::string& key_end, uint64_t limit, std::vector<std::string>* ret) WARN_UNUSED_RESULT;
common::Error info(const char* args, ServerInfo::Common* statsout) WARN_UNUSED_RESULT;
common::Error dbkcount(size_t* size) WARN_UNUSED_RESULT;

Expand Down Expand Up @@ -102,6 +102,9 @@ common::Error help(CommandHandler* handler, int argc, char** argv, FastoObject*
static const std::vector<CommandHolder> memcachedCommands = {
CommandHolder("VERSION", "-",
"Return the Memcached server version.", UNDEFINED_SINCE, UNDEFINED_EXAMPLE_STR, 0, 0, &version_server),
CommandHolder("KEYS", "<key_start> <key_end> <limit>",
"Find all keys matching the given limits.",
UNDEFINED_SINCE, UNDEFINED_EXAMPLE_STR, 3, 0, &keys),
CommandHolder("STATS", "[<args>]",
"These command can return various stats that we will explain.", UNDEFINED_SINCE, UNDEFINED_EXAMPLE_STR, 0, 1, &stats),
CommandHolder("FLUSH_ALL", "[<time>]",
Expand Down
11 changes: 6 additions & 5 deletions src/core/memcached/driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include "core/memcached/db_connection.h"

#define INFO_REQUEST "STATS"
#define GET_KEYS "STATS ITEMS"
#define GET_KEYS_PATTERN_1ARGS_I "KEYS a z %d"

#define DELETE_KEY_PATTERN_1ARGS_S "DELETE %s"
#define GET_KEY_PATTERN_1ARGS_S "GET %s"
Expand Down Expand Up @@ -209,9 +209,11 @@ void Driver::handleLoadDatabaseContentEvent(events::LoadDatabaseContentRequestEv
QObject* sender = ev->sender();
notifyProgress(sender, 0);
events::LoadDatabaseContentResponceEvent::value_type res(ev->value());
FastoObjectIPtr root = FastoObject::createRoot(GET_KEYS);
std::string patternResult = common::MemSPrintf(GET_KEYS_PATTERN_1ARGS_I, res.count_keys);
FastoObjectIPtr root = FastoObject::createRoot(patternResult);
notifyProgress(sender, 50);
FastoObjectCommand* cmd = createCommand<Command>(root, GET_KEYS, common::Value::C_INNER);
FastoObjectCommand* cmd = createCommand<Command>(root, patternResult,
common::Value::C_INNER);
common::Error er = execute(cmd);
if (er && er->isError()) {
res.setErrorInfo(er);
Expand All @@ -230,8 +232,7 @@ void Driver::handleLoadDatabaseContentEvent(events::LoadDatabaseContentRequestEv

for (size_t i = 0; i < ar->size(); ++i) {
std::string key;
bool isok = ar->getString(i, &key);
if (isok) {
if (ar->getString(i, &key)) {
NKey k(key);
NDbKValue ress(k, NValue());
res.keys.push_back(ress);
Expand Down

0 comments on commit 6ffe677

Please sign in to comment.