redisplugin.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. #include "platform.h"
  14. #include "eclrtl.hpp"
  15. #include "jstring.hpp"
  16. #include "redisplugin.hpp"
  17. #define REDIS_VERSION "redis plugin 1.0.0"
  18. ECL_REDIS_API bool getECLPluginDefinition(ECLPluginDefinitionBlock *pb)
  19. {
  20. if (pb->size != sizeof(ECLPluginDefinitionBlock))
  21. return false;
  22. pb->magicVersion = PLUGIN_VERSION;
  23. pb->version = REDIS_VERSION;
  24. pb->moduleName = "lib_redis";
  25. pb->ECL = NULL;
  26. pb->flags = PLUGIN_IMPLICIT_MODULE;
  27. pb->description = "ECL plugin library for the C API hiredis\n";
  28. return true;
  29. }
  30. namespace RedisPlugin {
  31. StringBuffer & appendExpire(StringBuffer & buffer, unsigned expire)
  32. {
  33. if (expire > 0)
  34. buffer.append(" EX ").append(expire);
  35. return buffer;
  36. }
  37. void RedisServer::parseOptions(ICodeContext * ctx, const char * _options)
  38. {
  39. StringArray optionStrings;
  40. optionStrings.appendList(_options, " ");
  41. ForEachItemIn(idx, optionStrings)
  42. {
  43. const char *opt = optionStrings.item(idx);
  44. if (strncmp(opt, "--SERVER=", 9) == 0)
  45. {
  46. opt += 9;
  47. StringArray splitPort;
  48. splitPort.appendList(opt, ":");
  49. if (splitPort.ordinality()==2)
  50. {
  51. ip.set(splitPort.item(0));
  52. port = atoi(splitPort.item(1));
  53. }
  54. }
  55. else
  56. {
  57. VStringBuffer err("RedisPlugin: unsupported option string %s", opt);
  58. rtlFail(0, err.str());
  59. }
  60. }
  61. if (ip.isEmpty())
  62. {
  63. ip.set("localhost");
  64. port = 6379;
  65. if (ctx)
  66. {
  67. VStringBuffer msg("Redis Plugin: WARNING - using default server (%s:%d)", ip.str(), port);
  68. ctx->logString(msg.str());
  69. }
  70. }
  71. return;
  72. }
  73. Connection::Connection(ICodeContext * ctx, const char * _options, const char * pswd, unsigned __int64 _timeout) : alreadyInitialized(false), database(0), timeout(_timeout)
  74. {
  75. server.setown(new RedisServer(ctx, _options, pswd));
  76. }
  77. Connection::Connection(ICodeContext * ctx, RedisServer * _server) : alreadyInitialized(false), database(0), timeout(0)
  78. {
  79. server.setown(_server);
  80. }
  81. bool Connection::isSameConnection(ICodeContext * ctx, unsigned hash) const
  82. {
  83. return server->isSame(ctx, hash);
  84. }
  85. void * Connection::allocateAndCopy(const char * src, size_t size)
  86. {
  87. void * value = rtlMalloc(size);
  88. return memcpy(value, src, size);
  89. }
  90. const char * Connection::appendIfKeyNotFoundMsg(const redisReply * reply, const char * key, StringBuffer & target) const
  91. {
  92. if (reply && reply->type == REDIS_REPLY_NIL)
  93. target.append("(key: '").append(key).append("') ");
  94. return target.str();
  95. }
  96. void Connection::init(ICodeContext * ctx)
  97. {
  98. logServerStats(ctx);
  99. alreadyInitialized = true;
  100. }
  101. }//close namespace