memcachedplugin.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2014 HPCC Systems.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "platform.h"
  14. #include "memcachedplugin.hpp"
  15. #include "eclrtl.hpp"
  16. #include "jexcept.hpp"
  17. #include "jstring.hpp"
  18. #include "workunit.hpp"
  19. #include <libmemcached/util.h>
  20. #define MEMCACHED_VERSION "memcached plugin 1.0.0"
  21. ECL_MEMCACHED_API bool getECLPluginDefinition(ECLPluginDefinitionBlock *pb)
  22. {
  23. if (pb->size != sizeof(ECLPluginDefinitionBlock))
  24. return false;
  25. pb->magicVersion = PLUGIN_VERSION;
  26. pb->version = MEMCACHED_VERSION;
  27. pb->moduleName = "lib_memcached";
  28. pb->ECL = NULL;
  29. pb->flags = PLUGIN_IMPLICIT_MODULE;
  30. pb->description = "ECL plugin library for the C/C++ API libmemcached (http://libmemcached.org/)\n";
  31. return true;
  32. }
  33. namespace MemCachedPlugin {
  34. IPluginContext * parentCtx = NULL;
  35. static const unsigned unitExpire = 86400;//1 day (secs)
  36. enum eclDataType {
  37. ECL_BOOLEAN,
  38. ECL_DATA,
  39. ECL_INTEGER,
  40. ECL_REAL,
  41. ECL_STRING,
  42. ECL_UTF8,
  43. ECL_UNICODE,
  44. ECL_UNSIGNED,
  45. ECL_NONE
  46. };
  47. const char * enumToStr(eclDataType type)
  48. {
  49. switch(type)
  50. {
  51. case ECL_BOOLEAN:
  52. return "BOOLEAN";
  53. case ECL_INTEGER:
  54. return "INTEGER";
  55. case ECL_UNSIGNED:
  56. return "UNSIGNED";
  57. case ECL_REAL:
  58. return "REAL";
  59. case ECL_STRING:
  60. return "STRING";
  61. case ECL_UTF8:
  62. return "UTF8";
  63. case ECL_UNICODE:
  64. return "UNICODE";
  65. case ECL_DATA:
  66. return "DATA";
  67. case ECL_NONE:
  68. return "Nonexistent";
  69. default:
  70. return "UNKNOWN";
  71. }
  72. }
  73. class MCached : public CInterface
  74. {
  75. public :
  76. MCached(ICodeContext * ctx, const char * servers);
  77. ~MCached();
  78. //set
  79. template <class type> bool set(ICodeContext * ctx, const char * partitionKey, const char * key, type value, unsigned expire, eclDataType eclType);
  80. template <class type> bool set(ICodeContext * ctx, const char * partitionKey, const char * key, size32_t valueLength, const type * value, unsigned expire, eclDataType eclType);
  81. //get
  82. template <class type> void get(ICodeContext * ctx, const char * partitionKey, const char * key, type & value, eclDataType eclType);
  83. template <class type> void get(ICodeContext * ctx, const char * partitionKey, const char * key, size_t & valueLength, type * & value, eclDataType eclType);
  84. void getVoidPtrLenPair(ICodeContext * ctx, const char * partitionKey, const char * key, size_t & valueLength, void * & value, eclDataType eclType);
  85. bool clear(ICodeContext * ctx, unsigned when);
  86. bool exist(ICodeContext * ctx, const char * key, const char * partitionKey);
  87. eclDataType getKeyType(const char * key, const char * partitionKey);
  88. bool isSameConnection(const char * _servers) const;
  89. private :
  90. void checkServersUp(ICodeContext * ctx);
  91. void assertOnError(memcached_return_t error, const char * msgSuffix = "");
  92. const char * keyNotFoundMsg(memcached_return_t error, const char * key, StringBuffer & target) const;
  93. void connect(ICodeContext * ctx);
  94. bool reportErrorOnFail(ICodeContext * ctx, memcached_return_t error);
  95. void reportKeyTypeMismatch(ICodeContext * ctx, const char * key, uint32_t flag, eclDataType eclType);
  96. void * cpy(const char * src, size_t length);
  97. void logServerStats(ICodeContext * ctx);
  98. void init(ICodeContext * ctx);
  99. void invokePoolSecurity(ICodeContext * ctx);
  100. void invokeConnectionSecurity(ICodeContext * ctx);
  101. void setPoolSettings();
  102. void assertPool();//For internal purposes to insure correct order of the above processes and instantiation.
  103. private :
  104. memcached_st * connection;
  105. memcached_pool_st * pool;
  106. StringAttr servers;
  107. bool alreadyInitialized;
  108. unsigned typeMismatchCount;
  109. };
  110. #define OwnedMCached Owned<MemCachedPlugin::MCached>
  111. #define MAX_TYPEMISMATCHCOUNT 10
  112. static CriticalSection crit;
  113. static OwnedMCached cachedConnection;
  114. MCached * createConnection(ICodeContext * ctx, const char * servers)
  115. {
  116. CriticalBlock block(crit);
  117. if (!cachedConnection)
  118. {
  119. cachedConnection.setown(new MemCachedPlugin::MCached(ctx, servers));
  120. return LINK(cachedConnection);
  121. }
  122. if (cachedConnection->isSameConnection(servers))
  123. return LINK(cachedConnection);
  124. cachedConnection.setown(new MemCachedPlugin::MCached(ctx, servers));
  125. return LINK(cachedConnection);
  126. }
  127. //-------------------------------------------SET-----------------------------------------
  128. template<class type> bool MSet(ICodeContext * ctx, const char * _servers, const char * partitionKey, const char * key, type value, unsigned expire, eclDataType eclType)
  129. {
  130. OwnedMCached serverPool = createConnection(ctx, _servers);
  131. bool success = serverPool->set(ctx, partitionKey, key, value, expire, eclType);
  132. return success;
  133. }
  134. //Set pointer types
  135. template<class type> bool MSet(ICodeContext * ctx, const char * _servers, const char * partitionKey, const char * key, size32_t valueLength, const type * value, unsigned expire, eclDataType eclType)
  136. {
  137. OwnedMCached serverPool = createConnection(ctx, _servers);
  138. bool success = serverPool->set(ctx, partitionKey, key, valueLength, value, expire, eclType);
  139. return success;
  140. }
  141. //-------------------------------------------GET-----------------------------------------
  142. template<class type> void MGet(ICodeContext * ctx, const char * servers, const char * partitionKey, const char * key, type & returnValue, eclDataType eclType)
  143. {
  144. OwnedMCached serverPool = createConnection(ctx, servers);
  145. serverPool->get(ctx, partitionKey, key, returnValue, eclType);
  146. }
  147. template<class type> void MGet(ICodeContext * ctx, const char * servers, const char * partitionKey, const char * key, size_t & returnLength, type * & returnValue, eclDataType eclType)
  148. {
  149. OwnedMCached serverPool = createConnection(ctx, servers);
  150. serverPool->get(ctx, partitionKey, key, returnLength, returnValue, eclType);
  151. }
  152. void MGetVoidPtrLenPair(ICodeContext * ctx, const char * servers, const char * partitionKey, const char * key, size_t & returnLength, void * & returnValue, eclDataType eclType)
  153. {
  154. OwnedMCached serverPool = createConnection(ctx, servers);
  155. serverPool->getVoidPtrLenPair(ctx, partitionKey, key, returnLength, returnValue, eclType);
  156. }
  157. }//close namespace
  158. //----------------------------------SET----------------------------------------
  159. template<class type> bool MemCachedPlugin::MCached::set(ICodeContext * ctx, const char * partitionKey, const char * key, type value, unsigned expire, eclDataType eclType)
  160. {
  161. const char * _value = reinterpret_cast<const char *>(&value);//Do this even for char * to prevent compiler complaining
  162. size_t partitionKeyLength = strlen(partitionKey);
  163. if (partitionKeyLength)
  164. return !reportErrorOnFail(ctx, memcached_set_by_key(connection, partitionKey, partitionKeyLength, key, strlen(key), _value, sizeof(value), (time_t)(expire*unitExpire), (uint32_t)eclType));
  165. else
  166. return !reportErrorOnFail(ctx, memcached_set(connection, key, strlen(key), _value, sizeof(value), (time_t)(expire*unitExpire), (uint32_t)eclType));
  167. }
  168. template<class type> bool MemCachedPlugin::MCached::set(ICodeContext * ctx, const char * partitionKey, const char * key, size32_t valueLength, const type * value, unsigned expire, eclDataType eclType)
  169. {
  170. const char * _value = reinterpret_cast<const char *>(value);//Do this even for char * to prevent compiler complaining
  171. size_t partitionKeyLength = strlen(partitionKey);
  172. if (partitionKeyLength)
  173. return !reportErrorOnFail(ctx, memcached_set_by_key(connection, partitionKey, partitionKeyLength, key, strlen(key), _value, (size_t)(valueLength), (time_t)(expire*unitExpire), (uint32_t)eclType));
  174. else
  175. return !reportErrorOnFail(ctx, memcached_set(connection, key, strlen(key), _value, (size_t)(valueLength), (time_t)(expire*unitExpire), (uint32_t)eclType));
  176. }
  177. //----------------------------------GET----------------------------------------
  178. template<class type> void MemCachedPlugin::MCached::get(ICodeContext * ctx, const char * partitionKey, const char * key, type & returnValue, eclDataType eclType)
  179. {
  180. uint32_t flag = 0;
  181. size_t returnLength = 0;
  182. memcached_return_t error;
  183. OwnedMalloc<char> value;
  184. size_t partitionKeyLength = strlen(partitionKey);
  185. if (partitionKeyLength)
  186. value.setown(memcached_get_by_key(connection, partitionKey, partitionKeyLength, key, strlen(key), &returnLength, &flag, &error));
  187. else
  188. value.setown(memcached_get(connection, key, strlen(key), &returnLength, &flag, &error));
  189. StringBuffer keyMsg;
  190. assertOnError(error, keyNotFoundMsg(error, key, keyMsg));
  191. reportKeyTypeMismatch(ctx, key, flag, eclType);
  192. if (sizeof(type)!=returnLength)
  193. {
  194. VStringBuffer msg("MemCachedPlugin: ERROR - Requested type of different size (%uB) from that stored (%uB). Check logs for more information.", (unsigned)sizeof(type), (unsigned)returnLength);
  195. rtlFail(0, msg.str());
  196. }
  197. memcpy(&returnValue, value, returnLength);
  198. }
  199. template<class type> void MemCachedPlugin::MCached::get(ICodeContext * ctx, const char * partitionKey, const char * key, size_t & returnLength, type * & returnValue, eclDataType eclType)
  200. {
  201. uint32_t flag = 0;
  202. memcached_return_t error;
  203. OwnedMalloc<char> value;
  204. size_t partitionKeyLength = strlen(partitionKey);
  205. if (partitionKeyLength)
  206. value.setown(memcached_get_by_key(connection, partitionKey, partitionKeyLength, key, strlen(key), &returnLength, &flag, &error));
  207. else
  208. value.setown(memcached_get(connection, key, strlen(key), &returnLength, &flag, &error));
  209. StringBuffer keyMsg;
  210. assertOnError(error, keyNotFoundMsg(error, key, keyMsg));
  211. reportKeyTypeMismatch(ctx, key, flag, eclType);
  212. returnValue = reinterpret_cast<type*>(cpy(value, returnLength));
  213. }
  214. void MemCachedPlugin::MCached::getVoidPtrLenPair(ICodeContext * ctx, const char * partitionKey, const char * key, size_t & returnLength, void * & returnValue, eclDataType eclType)
  215. {
  216. uint32_t flag = 0;
  217. size_t returnValueLength = 0;
  218. memcached_return_t error;
  219. OwnedMalloc<char> value;
  220. size_t partitionKeyLength = strlen(partitionKey);
  221. if (partitionKeyLength)
  222. value.setown(memcached_get_by_key(connection, partitionKey, partitionKeyLength, key, strlen(key), &returnValueLength, &flag, &error));
  223. else
  224. value.setown(memcached_get(connection, key, strlen(key), &returnValueLength, &flag, &error));
  225. StringBuffer keyMsg;
  226. assertOnError(error, keyNotFoundMsg(error, key, keyMsg));
  227. reportKeyTypeMismatch(ctx, key, flag, eclType);
  228. returnLength = (size32_t)(returnValueLength);
  229. returnValue = reinterpret_cast<void*>(cpy(value, returnLength));
  230. }
  231. ECL_MEMCACHED_API void setPluginContext(IPluginContext * ctx) { MemCachedPlugin::parentCtx = ctx; }
  232. MemCachedPlugin::MCached::MCached(ICodeContext * ctx, const char * _servers)
  233. {
  234. alreadyInitialized = false;
  235. connection = NULL;
  236. pool = NULL;
  237. servers.set(_servers);
  238. typeMismatchCount = 0;
  239. pool = memcached_pool(_servers, strlen(_servers));
  240. assertPool();
  241. setPoolSettings();
  242. invokePoolSecurity(ctx);
  243. connect(ctx);
  244. checkServersUp(ctx);
  245. }
  246. //-----------------------------------------------------------------------------
  247. MemCachedPlugin::MCached::~MCached()
  248. {
  249. if (pool)
  250. {
  251. memcached_pool_release(pool, connection);
  252. connection = NULL;//For safety (from changing this destructor) as not implicit in either the above or below.
  253. memcached_pool_destroy(pool);
  254. }
  255. else if (connection)//This should never be needed but just in case.
  256. {
  257. memcached_free(connection);
  258. }
  259. }
  260. bool MemCachedPlugin::MCached::isSameConnection(const char * _servers) const
  261. {
  262. if (!_servers)
  263. return false;
  264. return stricmp(servers.get(), _servers) == 0;
  265. }
  266. void MemCachedPlugin::MCached::assertPool()
  267. {
  268. if (!pool)
  269. {
  270. StringBuffer msg = "Memcached Plugin: Failed to instantiate server pool with:";
  271. msg.newline().append(servers);
  272. rtlFail(0, msg.str());
  273. }
  274. }
  275. void * MemCachedPlugin::MCached::cpy(const char * src, size_t length)
  276. {
  277. void * value = rtlMalloc(length);
  278. return memcpy(value, src, length);
  279. }
  280. void MemCachedPlugin::MCached::checkServersUp(ICodeContext * ctx)
  281. {
  282. memcached_return_t error;
  283. char * args = NULL;
  284. OwnedMalloc<memcached_stat_st> stats;
  285. stats.setown(memcached_stat(connection, args, &error));
  286. assertex(stats);
  287. unsigned int numberOfServers = memcached_server_count(connection);
  288. unsigned int numberOfServersDown = 0;
  289. for (unsigned i = 0; i < numberOfServers; ++i)
  290. {
  291. if (stats[i].pid == -1)//perhaps not the best test?
  292. {
  293. numberOfServersDown++;
  294. VStringBuffer msg("Memcached Plugin: Failed connecting to entry %u\nwithin the server list: %s", i+1, servers.str());
  295. ctx->addWuException(msg.str(), WRN_FROM_PLUGIN, ExceptionSeverityWarning, "");
  296. }
  297. }
  298. if (numberOfServersDown == numberOfServers)
  299. rtlFail(0,"Memcached Plugin: Failed connecting to ALL servers. Check memcached on all servers and \"memcached -B ascii\" not used.");
  300. //check memcached version homogeneity
  301. for (unsigned i = 0; i < numberOfServers-1; ++i)
  302. {
  303. if (strcmp(stats[i].version, stats[i+1].version) != 0)
  304. ctx->addWuException("Memcached Plugin: Inhomogeneous versions of memcached across servers.", WRN_FROM_PLUGIN, ExceptionSeverityInformation, "");
  305. }
  306. }
  307. bool MemCachedPlugin::MCached::reportErrorOnFail(ICodeContext * ctx, memcached_return_t error)
  308. {
  309. if (error == MEMCACHED_SUCCESS)
  310. return false;
  311. VStringBuffer msg("Memcached Plugin: %s", memcached_strerror(connection, error));
  312. ctx->addWuException(msg.str(), ERR_FROM_PLUGIN, ExceptionSeverityInformation, "");
  313. return true;
  314. }
  315. void MemCachedPlugin::MCached::assertOnError(memcached_return_t error, const char * msgSuffix)
  316. {
  317. if (error != MEMCACHED_SUCCESS)
  318. {
  319. VStringBuffer msg("Memcached Plugin: %s%s", memcached_strerror(connection, error), msgSuffix);
  320. rtlFail(0, msg.str());
  321. }
  322. }
  323. const char * MemCachedPlugin::MCached::keyNotFoundMsg(memcached_return_t error, const char * key, StringBuffer & target) const
  324. {
  325. target.clear();
  326. if (error == MEMCACHED_NOTFOUND)
  327. {
  328. target = " (key: '";
  329. target.append(key).append("') ");
  330. }
  331. return target.str();
  332. }
  333. bool MemCachedPlugin::MCached::clear(ICodeContext * ctx, unsigned when)
  334. {
  335. //NOTE: memcached_flush is the actual cache flush/clear/delete and not an io buffer flush.
  336. return !reportErrorOnFail(ctx, memcached_flush(connection, (time_t)(when)));
  337. }
  338. bool MemCachedPlugin::MCached::exist(ICodeContext * ctx, const char * key, const char * partitionKey)
  339. {
  340. memcached_return_t error;
  341. size_t partitionKeyLength = strlen(partitionKey);
  342. if (partitionKeyLength)
  343. error = memcached_exist_by_key(connection, partitionKey, partitionKeyLength, key, strlen(key));
  344. else
  345. error = memcached_exist(connection, key, strlen(key));
  346. if (error == MEMCACHED_SUCCESS)
  347. return true;
  348. else if (error == MEMCACHED_NOTFOUND)
  349. return false;
  350. reportErrorOnFail(ctx, error);
  351. return false;
  352. }
  353. MemCachedPlugin::eclDataType MemCachedPlugin::MCached::getKeyType(const char * key, const char * partitionKey)
  354. {
  355. size_t returnValueLength;
  356. uint32_t flag;
  357. memcached_return_t error;
  358. size_t partitionKeyLength = strlen(partitionKey);
  359. if (partitionKeyLength)
  360. memcached_get_by_key(connection, partitionKey, partitionKeyLength, key, strlen(key), &returnValueLength, &flag, &error);
  361. else
  362. memcached_get(connection, key, strlen(key), &returnValueLength, &flag, &error);
  363. if (error == MEMCACHED_SUCCESS)
  364. return (MemCachedPlugin::eclDataType)(flag);
  365. else if (error == MEMCACHED_NOTFOUND)
  366. return ECL_NONE;
  367. else
  368. {
  369. StringBuffer msg = "Memcached Plugin: ";
  370. rtlFail(0, msg.append(memcached_strerror(connection, error)).str());
  371. }
  372. }
  373. void MemCachedPlugin::MCached::reportKeyTypeMismatch(ICodeContext * ctx, const char * key, uint32_t flag, eclDataType eclType)
  374. {
  375. if (flag && eclType != ECL_DATA && flag != eclType)
  376. {
  377. VStringBuffer msg("Memcached Plugin: The requested key '%s' is of type %s, not %s as requested.", key, enumToStr((eclDataType)(flag)), enumToStr(eclType));
  378. if (++typeMismatchCount <= MAX_TYPEMISMATCHCOUNT)
  379. ctx->logString(msg.str());//NOTE: logging locally, rather than calling ctx->addWuException, to prevent flooding the WU if this is called multiple times by every node
  380. }
  381. }
  382. void MemCachedPlugin::MCached::logServerStats(ICodeContext * ctx)
  383. {
  384. //NOTE: errors are ignored here so that at least some info is reported, such as non-connection related libmemcached version numbers
  385. memcached_return_t error;
  386. char * args = NULL;
  387. OwnedMalloc<memcached_stat_st> stats;
  388. stats.setown(memcached_stat(connection, args, &error));
  389. OwnedMalloc<char*> keys;
  390. keys.setown(memcached_stat_get_keys(connection, stats, &error));
  391. unsigned int numberOfServers = memcached_server_count(connection);
  392. for (unsigned int i = 0; i < numberOfServers; ++i)
  393. {
  394. StringBuffer statsStr;
  395. unsigned j = 0;
  396. do
  397. {
  398. OwnedMalloc<char> value;
  399. value.setown(memcached_stat_get_value(connection, &stats[i], keys[j], &error));
  400. statsStr.newline().append("libmemcached server stat - ").append(keys[j]).append(":").append(value);
  401. } while (keys[++j]);
  402. statsStr.newline().append("libmemcached client stat - libmemcached version:").append(memcached_lib_version());
  403. ctx->logString(statsStr.str());
  404. }
  405. }
  406. void MemCachedPlugin::MCached::init(ICodeContext * ctx)
  407. {
  408. logServerStats(ctx);
  409. }
  410. void MemCachedPlugin::MCached::setPoolSettings()
  411. {
  412. assertPool();
  413. assertOnError(memcached_pool_behavior_set(pool, MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY, 1));//key set in invokeConnectionSecurity. Only hashed with keys and not partitionKeys
  414. assertOnError(memcached_pool_behavior_set(pool, MEMCACHED_BEHAVIOR_KETAMA, 1));//NOTE: alias of MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA amongst others.
  415. assertOnError(memcached_pool_behavior_set(pool, MEMCACHED_BEHAVIOR_USE_UDP, 0));
  416. assertOnError(memcached_pool_behavior_set(pool, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT, 1));
  417. assertOnError(memcached_pool_behavior_set(pool, MEMCACHED_BEHAVIOR_REMOVE_FAILED_SERVERS, 1));
  418. assertOnError(memcached_pool_behavior_set(pool, MEMCACHED_BEHAVIOR_NO_BLOCK, 0));
  419. assertOnError(memcached_pool_behavior_set(pool, MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT, 100));//units of ms MORE: What should I set this to or get from?
  420. assertOnError(memcached_pool_behavior_set(pool, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 0));// Buffering does not work with the ecl runtime paradigm
  421. }
  422. void MemCachedPlugin::MCached::invokePoolSecurity(ICodeContext * ctx)
  423. {
  424. assertPool();
  425. assertOnError(memcached_pool_behavior_set(pool, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1));
  426. }
  427. void MemCachedPlugin::MCached::invokeConnectionSecurity(ICodeContext * ctx)
  428. {
  429. //NOTE: Whether to assert or just report? This depends on when this is called. If before checkServersUp() and
  430. //a server is down, it will cause the following to fail if asserted with only a 'poor' libmemcached error message.
  431. //Reporting means that these 'security' measures may not be carried out. Moving checkServersUp() to here is probably the best
  432. //soln. however, this comes with extra overhead.
  433. reportErrorOnFail(ctx, memcached_verbosity(connection, (uint32_t)(0)));
  434. //reportErrorOnFail(ctx, memcached_callback_set(connection, MEMCACHED_CALLBACK_PREFIX_KEY, "ecl"));//NOTE: MEMCACHED_CALLBACK_PREFIX_KEY is an alias of MEMCACHED_CALLBACK_NAMESPACE
  435. }
  436. void MemCachedPlugin::MCached::connect(ICodeContext * ctx)
  437. {
  438. assertPool();
  439. if (connection)
  440. memcached_pool_release(pool, connection);
  441. memcached_return_t error;
  442. connection = memcached_pool_fetch(pool, (struct timespec *)0 , &error);
  443. invokeConnectionSecurity(ctx);
  444. if (!alreadyInitialized)//Do this now rather than after assert. Better to have something even if it could be jiberish.
  445. {
  446. init(ctx);//doesn't necessarily initialize anything, instead outputs specs etc for debugging
  447. alreadyInitialized = true;
  448. }
  449. assertOnError(error);
  450. }
  451. //--------------------------------------------------------------------------------
  452. // ECL SERVICE ENTRYPOINTS
  453. //--------------------------------------------------------------------------------
  454. ECL_MEMCACHED_API bool ECL_MEMCACHED_CALL MClear(ICodeContext * ctx, const char * servers)
  455. {
  456. OwnedMCached serverPool = MemCachedPlugin::createConnection(ctx, servers);
  457. bool returnValue = serverPool->clear(ctx, 0);
  458. return returnValue;
  459. }
  460. ECL_MEMCACHED_API bool ECL_MEMCACHED_CALL MExist(ICodeContext * ctx, const char * servers, const char * key, const char * partitionKey)
  461. {
  462. OwnedMCached serverPool = MemCachedPlugin::createConnection(ctx, servers);
  463. bool returnValue = serverPool->exist(ctx, key, partitionKey);
  464. return returnValue;
  465. }
  466. ECL_MEMCACHED_API const char * ECL_MEMCACHED_CALL MKeyType(ICodeContext * ctx, const char * servers, const char * key, const char * partitionKey)
  467. {
  468. OwnedMCached serverPool = MemCachedPlugin::createConnection(ctx, servers);
  469. const char * keyType = enumToStr(serverPool->getKeyType(key, partitionKey));
  470. return keyType;
  471. }
  472. //-----------------------------------SET------------------------------------------
  473. //NOTE: These were all overloaded by 'value' type, however; this caused problems since ecl implicitly casts and doesn't type check.
  474. ECL_MEMCACHED_API bool ECL_MEMCACHED_CALL MSet(ICodeContext * ctx, const char * servers, const char * key, size32_t valueLength, const char * value, const char * partitionKey, unsigned expire /* = 0 (ECL default)*/)
  475. {
  476. return MemCachedPlugin::MSet(ctx, servers, partitionKey, key, valueLength, value, expire, MemCachedPlugin::ECL_STRING);
  477. }
  478. ECL_MEMCACHED_API bool ECL_MEMCACHED_CALL MSet(ICodeContext * ctx, const char * servers, const char * key, size32_t valueLength, const UChar * value, const char * partitionKey, unsigned expire /* = 0 (ECL default)*/)
  479. {
  480. return MemCachedPlugin::MSet(ctx, servers, partitionKey, key, (valueLength)*sizeof(UChar), value, expire, MemCachedPlugin::ECL_UNICODE);
  481. }
  482. ECL_MEMCACHED_API bool ECL_MEMCACHED_CALL MSet(ICodeContext * ctx, const char * servers, const char * key, signed __int64 value, const char * partitionKey, unsigned expire /* = 0 (ECL default)*/)
  483. {
  484. return MemCachedPlugin::MSet(ctx, servers, partitionKey, key, value, expire, MemCachedPlugin::ECL_INTEGER);
  485. }
  486. ECL_MEMCACHED_API bool ECL_MEMCACHED_CALL MSet(ICodeContext * ctx, const char * servers, const char * key, unsigned __int64 value, const char * partitionKey, unsigned expire /* = 0 (ECL default)*/)
  487. {
  488. return MemCachedPlugin::MSet(ctx, servers, partitionKey, key, value, expire, MemCachedPlugin::ECL_UNSIGNED);
  489. }
  490. ECL_MEMCACHED_API bool ECL_MEMCACHED_CALL MSet(ICodeContext * ctx, const char * servers, const char * key, double value, const char * partitionKey, unsigned expire /* = 0 (ECL default)*/)
  491. {
  492. return MemCachedPlugin::MSet(ctx, servers, partitionKey, key, value, expire, MemCachedPlugin::ECL_REAL);
  493. }
  494. ECL_MEMCACHED_API bool ECL_MEMCACHED_CALL MSet(ICodeContext * ctx, const char * servers, const char * key, bool value, const char * partitionKey, unsigned expire)
  495. {
  496. return MemCachedPlugin::MSet(ctx, servers, partitionKey, key, value, expire, MemCachedPlugin::ECL_BOOLEAN);
  497. }
  498. ECL_MEMCACHED_API bool ECL_MEMCACHED_CALL MSetData(ICodeContext * ctx, const char * servers, const char * key, size32_t valueLength, const void * value, const char * partitionKey, unsigned expire)
  499. {
  500. return MemCachedPlugin::MSet(ctx, servers, partitionKey, key, valueLength, value, expire, MemCachedPlugin::ECL_DATA);
  501. }
  502. ECL_MEMCACHED_API bool ECL_MEMCACHED_CALL MSetUtf8(ICodeContext * ctx, const char * servers, const char * key, size32_t valueLength, const char * value, const char * partitionKey, unsigned expire /* = 0 (ECL default)*/)
  503. {
  504. return MemCachedPlugin::MSet(ctx, servers, partitionKey, key, rtlUtf8Size(valueLength, value), value, expire, MemCachedPlugin::ECL_UTF8);
  505. }
  506. //-------------------------------------GET----------------------------------------
  507. ECL_MEMCACHED_API bool ECL_MEMCACHED_CALL MGetBool(ICodeContext * ctx, const char * servers, const char * key, const char * partitionKey)
  508. {
  509. bool value;
  510. MemCachedPlugin::MGet(ctx, servers, partitionKey, key, value, MemCachedPlugin::ECL_BOOLEAN);
  511. return value;
  512. }
  513. ECL_MEMCACHED_API double ECL_MEMCACHED_CALL MGetDouble(ICodeContext * ctx, const char * servers, const char * key, const char * partitionKey)
  514. {
  515. double value;
  516. MemCachedPlugin::MGet(ctx, servers, partitionKey, key, value, MemCachedPlugin::ECL_REAL);
  517. return value;
  518. }
  519. ECL_MEMCACHED_API signed __int64 ECL_MEMCACHED_CALL MGetInt8(ICodeContext * ctx, const char * servers, const char * key, const char * partitionKey)
  520. {
  521. signed __int64 value;
  522. MemCachedPlugin::MGet(ctx, servers, partitionKey, key, value, MemCachedPlugin::ECL_INTEGER);
  523. return value;
  524. }
  525. ECL_MEMCACHED_API unsigned __int64 ECL_MEMCACHED_CALL MGetUint8(ICodeContext * ctx, const char * servers, const char * key, const char * partitionKey)
  526. {
  527. unsigned __int64 value;
  528. MemCachedPlugin::MGet(ctx, servers, partitionKey, key, value, MemCachedPlugin::ECL_UNSIGNED);
  529. return value;
  530. }
  531. ECL_MEMCACHED_API void ECL_MEMCACHED_CALL MGetStr(ICodeContext * ctx, size32_t & returnLength, char * & returnValue, const char * servers, const char * key, const char * partitionKey)
  532. {
  533. size_t _returnLength;
  534. MemCachedPlugin::MGet(ctx, servers, partitionKey, key, _returnLength, returnValue, MemCachedPlugin::ECL_STRING);
  535. returnLength = static_cast<size32_t>(_returnLength);
  536. }
  537. ECL_MEMCACHED_API void ECL_MEMCACHED_CALL MGetUChar(ICodeContext * ctx, size32_t & returnLength, UChar * & returnValue, const char * servers, const char * key, const char * partitionKey)
  538. {
  539. size_t _returnSize;
  540. MemCachedPlugin::MGet(ctx, servers, partitionKey, key, _returnSize, returnValue, MemCachedPlugin::ECL_UNICODE);
  541. returnLength = static_cast<size32_t>(_returnSize/sizeof(UChar));
  542. }
  543. ECL_MEMCACHED_API void ECL_MEMCACHED_CALL MGetUtf8(ICodeContext * ctx, size32_t & returnLength, char * & returnValue, const char * servers, const char * key, const char * partitionKey)
  544. {
  545. size_t returnSize;
  546. MemCachedPlugin::MGet(ctx, servers, partitionKey, key, returnSize, returnValue, MemCachedPlugin::ECL_UTF8);
  547. returnLength = static_cast<size32_t>(rtlUtf8Length(returnSize, returnValue));
  548. }
  549. ECL_MEMCACHED_API void ECL_MEMCACHED_CALL MGetData(ICodeContext * ctx, size32_t & returnLength, void * & returnValue, const char * servers, const char * key, const char * partitionKey)
  550. {
  551. size_t _returnLength;
  552. MemCachedPlugin::MGetVoidPtrLenPair(ctx, servers, partitionKey, key, _returnLength, returnValue, MemCachedPlugin::ECL_DATA);
  553. returnLength = static_cast<size32_t>(_returnLength);
  554. }