Skip to content
This repository
tree: db2547fca0
Fetching contributors…

Cannot retrieve contributors at this time

file 131 lines (103 sloc) 3.225 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
#include <client/dbclient.h>

extern "C" {
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

#if !defined(LUA_VERSION_NUM) || (LUA_VERSION_NUM < 501)
#include <compat-5.1.h>
#endif
};

#include "common.h"

using namespace mongo;

extern const luaL_Reg dbclient_methods[];

namespace {
inline DBClientReplicaSet* userdata_to_replicaset(lua_State* L, int index) {
    void *ud = luaL_checkudata(L, index, LUAMONGO_REPLICASET);
    DBClientReplicaSet *replicaset = *((DBClientReplicaSet **)ud);
    return replicaset;
}

} // anonymous namespace


/*
* db,err = mongo.ReplicaSet.New(name, {hostAndPort1, ...)
*/
static int replicaset_new(lua_State *L) {
    luaL_checktype(L, 1, LUA_TSTRING);
    luaL_checktype(L, 2, LUA_TTABLE);

    int resultcount = 1;
    try {
        const char *rs_name = luaL_checkstring(L, 1);

        std::vector<mongo::HostAndPort> rs_servers;
        int i = 1;
        while (1) {
            lua_pushinteger(L, i++);
            lua_gettable(L, 2);
            const char* hostAndPort = lua_tostring(L, -1);
            lua_pop(L, 1);
            if (!hostAndPort)
                break;
            HostAndPort hp(hostAndPort);
            rs_servers.push_back(hp);
        }

        DBClientReplicaSet **replicaset = (DBClientReplicaSet **)lua_newuserdata(L, sizeof(DBClientReplicaSet *));
        *replicaset = new DBClientReplicaSet(rs_name, rs_servers);

        luaL_getmetatable(L, LUAMONGO_REPLICASET);
        lua_setmetatable(L, -2);
    } catch (std::exception &e) {
        lua_pushnil(L);
        lua_pushfstring(L, LUAMONGO_ERR_REPLICASET_FAILED, e.what());
        resultcount = 2;
    }

    return resultcount;
}

/*
* ok,err = replicaset:connect()
*/
static int replicaset_connect(lua_State *L) {
    DBClientReplicaSet *replicaset = userdata_to_replicaset(L, 1);

    try {
        replicaset->connect();
    } catch (std::exception &e) {
        lua_pushnil(L);
        lua_pushfstring(L, LUAMONGO_ERR_CONNECT_FAILED, LUAMONGO_REPLICASET, e.what());
        return 2;
    }

    lua_pushboolean(L, 1);
    return 1;
}


/*
* __gc
*/
static int replicaset_gc(lua_State *L) {
    DBClientReplicaSet *replicaset = userdata_to_replicaset(L, 1);
    delete replicaset;
    return 0;
}

/*
* __tostring
*/
static int replicaset_tostring(lua_State *L) {
    DBClientReplicaSet *replicaset = userdata_to_replicaset(L, 1);
    lua_pushfstring(L, "%s: %s", LUAMONGO_REPLICASET, replicaset->toString().c_str());
    return 1;
}


int mongo_replicaset_register(lua_State *L) {
    static const luaL_Reg replicaset_methods[] = {
        {"connect", replicaset_connect},
        {NULL, NULL}
    };

    static const luaL_Reg replicaset_class_methods[] = {
        {"New", replicaset_new},
        {NULL, NULL}
    };

    luaL_newmetatable(L, LUAMONGO_REPLICASET);
    luaL_register(L, NULL, dbclient_methods);
    luaL_register(L, NULL, replicaset_methods);
    lua_pushvalue(L,-1);
    lua_setfield(L, -2, "__index");

    lua_pushcfunction(L, replicaset_gc);
    lua_setfield(L, -2, "__gc");

    lua_pushcfunction(L, replicaset_tostring);
    lua_setfield(L, -2, "__tostring");

    luaL_register(L, LUAMONGO_REPLICASET, replicaset_class_methods);

    return 1;
}
Something went wrong with that request. Please try again.