redisplugin.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2015 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. #ifndef ECL_REDIS_INCL
  14. #define ECL_REDIS_INCL
  15. #ifdef _WIN32
  16. #define ECL_REDIS_CALL _cdecl
  17. #ifdef ECL_REDIS_EXPORTS
  18. #define ECL_REDIS_API __declspec(dllexport)
  19. #else
  20. #define ECL_REDIS_API __declspec(dllimport)
  21. #endif
  22. #else
  23. #define ECL_REDIS_CALL
  24. #define ECL_REDIS_API
  25. #endif
  26. #include "jhash.hpp"
  27. #include "hqlplugins.hpp"
  28. #include "eclhelper.hpp"
  29. #include "jexcept.hpp"
  30. #include "hiredis/hiredis.h"
  31. extern "C"
  32. {
  33. ECL_REDIS_API bool getECLPluginDefinition(ECLPluginDefinitionBlock *pb);
  34. ECL_REDIS_API void setPluginContext(IPluginContext * _ctx);
  35. }
  36. class StringBuffer;
  37. namespace RedisPlugin {
  38. #define setFailMsg "'Set' request failed - "
  39. #define getFailMsg "'Get<type>' request failed - "
  40. StringBuffer & appendExpire(StringBuffer & buffer, unsigned expire);
  41. class RedisServer : public CInterface
  42. {
  43. public :
  44. RedisServer(ICodeContext * ctx, const char * _options, const char * pswd)
  45. {
  46. serverIpPortPasswordHash = hashc((const unsigned char*)pswd, strlen(pswd), 0);
  47. serverIpPortPasswordHash = hashc((const unsigned char*)_options, strlen(_options), serverIpPortPasswordHash);
  48. options.set(_options);
  49. parseOptions(ctx, _options);
  50. }
  51. bool isSame(ICodeContext * ctx, unsigned hash) const
  52. {
  53. return (serverIpPortPasswordHash == hash);
  54. }
  55. const char * getIp() { return ip.str(); }
  56. int getPort() { return port; }
  57. void parseOptions(ICodeContext * ctx, const char * _options);
  58. private :
  59. unsigned serverIpPortPasswordHash;
  60. StringAttr options;
  61. StringAttr ip;
  62. int port;
  63. };
  64. class Connection : public CInterface
  65. {
  66. public :
  67. Connection(ICodeContext * ctx, const char * _options, const char * pswd, unsigned __int64 _timeout);
  68. Connection(ICodeContext * ctx, RedisServer * _server);
  69. bool isSameConnection(ICodeContext * ctx, unsigned hash) const;
  70. const char * ip() const { return server->getIp(); }
  71. int port() const { return server->getPort(); }
  72. protected :
  73. virtual void assertOnError(const redisReply * reply, const char * _msg) { }
  74. virtual void assertConnection() { }
  75. virtual void logServerStats(ICodeContext * ctx) { }
  76. virtual void updateTimeout(unsigned __int64 _timeout) { }
  77. const char * appendIfKeyNotFoundMsg(const redisReply * reply, const char * key, StringBuffer & target) const;
  78. void * allocateAndCopy(const char * src, size_t size);
  79. void init(ICodeContext * ctx);
  80. protected :
  81. Owned<RedisServer> server;
  82. unsigned __int64 timeout;
  83. unsigned __int64 database;
  84. bool alreadyInitialized;
  85. };
  86. class Reply : public CInterface
  87. {
  88. public :
  89. inline Reply() { reply = NULL; };
  90. inline Reply(void * _reply) { reply = (redisReply*)_reply; }
  91. inline Reply(redisReply * _reply) { reply = _reply; }
  92. inline ~Reply()
  93. {
  94. if (reply)
  95. freeReplyObject(reply);
  96. }
  97. static Reply * createReply(void * _reply) { return new Reply(_reply); }
  98. inline const redisReply * query() const { return reply; }
  99. private :
  100. redisReply * reply;
  101. };
  102. typedef Owned<RedisPlugin::Reply> OwnedReply;
  103. }//close namespace
  104. #endif