PageRenderTime 26ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/mongo_replicaset.cpp

https://github.com/xiaogongzhu/luamongo
C++ | 132 lines | 91 code | 29 blank | 12 comment | 3 complexity | 01f631aa7bd2dbc6b665ad9752aecf75 MD5 | raw file
  1. #include <client/dbclient.h>
  2. extern "C" {
  3. #include <lua.h>
  4. #include <lauxlib.h>
  5. #include <lualib.h>
  6. #if !defined(LUA_VERSION_NUM) || (LUA_VERSION_NUM < 501)
  7. #include <compat-5.1.h>
  8. #endif
  9. };
  10. #include "common.h"
  11. using namespace mongo;
  12. extern const luaL_Reg dbclient_methods[];
  13. namespace {
  14. inline DBClientReplicaSet* userdata_to_replicaset(lua_State* L, int index) {
  15. void *ud = luaL_checkudata(L, index, LUAMONGO_REPLICASET);
  16. DBClientReplicaSet *replicaset = *((DBClientReplicaSet **)ud);
  17. return replicaset;
  18. }
  19. } // anonymous namespace
  20. /*
  21. * db,err = mongo.ReplicaSet.New(name, {hostAndPort1, ...)
  22. */
  23. static int replicaset_new(lua_State *L) {
  24. luaL_checktype(L, 1, LUA_TSTRING);
  25. luaL_checktype(L, 2, LUA_TTABLE);
  26. int resultcount = 1;
  27. try {
  28. const char *rs_name = luaL_checkstring(L, 1);
  29. std::vector<mongo::HostAndPort> rs_servers;
  30. int i = 1;
  31. while (1) {
  32. lua_pushinteger(L, i++);
  33. lua_gettable(L, 2);
  34. const char* hostAndPort = lua_tostring(L, -1);
  35. lua_pop(L, 1);
  36. if (!hostAndPort)
  37. break;
  38. HostAndPort hp(hostAndPort);
  39. rs_servers.push_back(hp);
  40. }
  41. DBClientReplicaSet **replicaset = (DBClientReplicaSet **)lua_newuserdata(L, sizeof(DBClientReplicaSet *));
  42. *replicaset = new DBClientReplicaSet(rs_name, rs_servers);
  43. luaL_getmetatable(L, LUAMONGO_REPLICASET);
  44. lua_setmetatable(L, -2);
  45. } catch (std::exception &e) {
  46. lua_pushnil(L);
  47. lua_pushfstring(L, LUAMONGO_ERR_REPLICASET_FAILED, e.what());
  48. resultcount = 2;
  49. }
  50. return resultcount;
  51. }
  52. /*
  53. * ok,err = replicaset:connect()
  54. */
  55. static int replicaset_connect(lua_State *L) {
  56. DBClientReplicaSet *replicaset = userdata_to_replicaset(L, 1);
  57. try {
  58. replicaset->connect();
  59. } catch (std::exception &e) {
  60. lua_pushnil(L);
  61. lua_pushfstring(L, LUAMONGO_ERR_CONNECT_FAILED, LUAMONGO_REPLICASET, e.what());
  62. return 2;
  63. }
  64. lua_pushboolean(L, 1);
  65. return 1;
  66. }
  67. /*
  68. * __gc
  69. */
  70. static int replicaset_gc(lua_State *L) {
  71. DBClientReplicaSet *replicaset = userdata_to_replicaset(L, 1);
  72. delete replicaset;
  73. return 0;
  74. }
  75. /*
  76. * __tostring
  77. */
  78. static int replicaset_tostring(lua_State *L) {
  79. DBClientReplicaSet *replicaset = userdata_to_replicaset(L, 1);
  80. lua_pushfstring(L, "%s: %s", LUAMONGO_REPLICASET, replicaset->toString().c_str());
  81. return 1;
  82. }
  83. int mongo_replicaset_register(lua_State *L) {
  84. static const luaL_Reg replicaset_methods[] = {
  85. {"connect", replicaset_connect},
  86. {NULL, NULL}
  87. };
  88. static const luaL_Reg replicaset_class_methods[] = {
  89. {"New", replicaset_new},
  90. {NULL, NULL}
  91. };
  92. luaL_newmetatable(L, LUAMONGO_REPLICASET);
  93. luaL_register(L, NULL, dbclient_methods);
  94. luaL_register(L, NULL, replicaset_methods);
  95. lua_pushvalue(L,-1);
  96. lua_setfield(L, -2, "__index");
  97. lua_pushcfunction(L, replicaset_gc);
  98. lua_setfield(L, -2, "__gc");
  99. lua_pushcfunction(L, replicaset_tostring);
  100. lua_setfield(L, -2, "__tostring");
  101. luaL_register(L, LUAMONGO_REPLICASET, replicaset_class_methods);
  102. return 1;
  103. }