Skip to content
Browse files

+ Fix segfault on MongoDB restart (add check for lost connection)

+ Minor makeup
  • Loading branch information...
1 parent f46815e commit 1d1cfe2d601746c86a6abab99d616bb8ca3da554 @neoxic neoxic committed Jan 31, 2012
Showing with 50 additions and 30 deletions.
  1. +1 −0 common.h
  2. +9 −3 mongo_cursor.cpp
  3. +10 −4 mongo_dbclient.cpp
  4. +30 −23 mongo_gridfs.cpp
View
1 common.h
@@ -20,6 +20,7 @@
#define LUAMONGO_ERR_COUNT_FAILED "Count failed: %s"
#define LUAMONGO_ERR_REMOVE_FAILED "Remove failed: %s"
#define LUAMONGO_ERR_UPDATE_FAILED "Update failed: %s"
+#define LUAMONGO_ERR_CONNECTION_LOST "Connection lost"
#define LUAMONGO_UNSUPPORTED_BSON_TYPE "Unsupported BSON type `%s'"
#define LUAMONGO_UNSUPPORTED_LUA_TYPE "Unsupported Lua type `%s'"
#define LUAMONGO_REQUIRES_JSON_OR_TABLE "JSON string or Lua table required"
View
12 mongo_cursor.cpp
@@ -20,8 +20,7 @@ extern void bson_to_lua(lua_State *L, const BSONObj &obj);
namespace {
inline DBClientCursor* userdata_to_cursor(lua_State* L, int index) {
- void *ud = 0;
- ud = luaL_checkudata(L, index, LUAMONGO_CURSOR);
+ void *ud = luaL_checkudata(L, index, LUAMONGO_CURSOR);
DBClientCursor *cursor = *((DBClientCursor **)ud);
return cursor;
}
@@ -36,10 +35,17 @@ int cursor_create(lua_State *L, DBClientBase *connection, const char *ns,
int resultcount = 1;
try {
- DBClientCursor **cursor = (DBClientCursor **)lua_newuserdata(L, sizeof(DBClientCursor *));
auto_ptr<DBClientCursor> autocursor = connection->query(
ns, query, nToReturn, nToSkip,
fieldsToReturn, queryOptions, batchSize);
+
+ if (!autocursor.get()) {
+ lua_pushnil(L);
+ lua_pushstring(L, LUAMONGO_ERR_CONNECTION_LOST);
+ return 2;
+ }
+
+ DBClientCursor **cursor = (DBClientCursor **)lua_newuserdata(L, sizeof(DBClientCursor *));
*cursor = autocursor.get();
autocursor.release();
View
14 mongo_dbclient.cpp
@@ -40,7 +40,7 @@ DBClientBase* userdata_to_dbclient(lua_State *L, int stackpos)
lua_pop(L, 2);
return connection;
}
- lua_pop(L, 2);
+ lua_pop(L, 2);
}
else
lua_pop(L, 1);
@@ -55,7 +55,7 @@ DBClientBase* userdata_to_dbclient(lua_State *L, int stackpos)
lua_pop(L, 2); // remove both metatables
return replicaset;
}
- lua_pop(L, 2);
+ lua_pop(L, 2);
}
else
lua_pop(L, 1);
@@ -617,14 +617,20 @@ static int dbclient_gen_index_name(lua_State *L) {
}
/*
- * cursor = db:get_indexes(ns)
+ * cursor,err = db:get_indexes(ns)
*/
static int dbclient_get_indexes(lua_State *L) {
DBClientBase *dbclient = userdata_to_dbclient(L, 1);
const char *ns = luaL_checkstring(L, 2);
auto_ptr<DBClientCursor> autocursor = dbclient->getIndexes(ns);
+ if (!autocursor.get()) {
+ lua_pushnil(L);
+ lua_pushstring(L, LUAMONGO_ERR_CONNECTION_LOST);
+ return 2;
+ }
+
DBClientCursor **cursor = (DBClientCursor **)lua_newuserdata(L, sizeof(DBClientCursor *));
*cursor = autocursor.get();
autocursor.release();
@@ -758,7 +764,7 @@ static int dbclient_run_command(lua_State *L) {
} catch (std::exception &e) {
lua_pushboolean(L, 0);
lua_pushfstring(L, LUAMONGO_ERR_CALLING, LUAMONGO_CONNECTION,
- "run_command", e.what());
+ "run_command", e.what());
return 2;
} catch (const char *err) {
lua_pushboolean(L, 0);
View
53 mongo_gridfs.cpp
@@ -73,43 +73,50 @@ static int gridfs_new(lua_State *L) {
static int gridfs_find_file(lua_State *L) {
GridFS *gridfs = userdata_to_gridfs(L, 1);
int resultcount = 1;
-
+
if (!lua_isnoneornil(L, 2)) {
- try {
- int type = lua_type(L, 2);
- if (type == LUA_TTABLE) {
- BSONObj obj;
- lua_to_bson(L, 2, obj);
- GridFile gridfile = gridfs->findFile(obj);
- resultcount = gridfile_create(L, gridfile);
- } else {
- GridFile gridfile = gridfs->findFile(luaL_checkstring(L, 2));
- resultcount = gridfile_create(L, gridfile);
- }
-
- } catch (std::exception &e) {
- lua_pushboolean(L, 0);
- lua_pushfstring(L, LUAMONGO_ERR_CALLING, LUAMONGO_GRIDFS, "find_file", e.what());
- resultcount = 2;
- }
+ try {
+ int type = lua_type(L, 2);
+ if (type == LUA_TTABLE) {
+ BSONObj obj;
+ lua_to_bson(L, 2, obj);
+ GridFile gridfile = gridfs->findFile(obj);
+ resultcount = gridfile_create(L, gridfile);
+ } else {
+ GridFile gridfile = gridfs->findFile(luaL_checkstring(L, 2));
+ resultcount = gridfile_create(L, gridfile);
+ }
+
+ } catch (std::exception &e) {
+ lua_pushboolean(L, 0);
+ lua_pushfstring(L, LUAMONGO_ERR_CALLING, LUAMONGO_GRIDFS, "find_file", e.what());
+ resultcount = 2;
+ }
}
-
+
return resultcount;
-
+
}
/*
- * cursor = gridfs:list()
+ * cursor,err = gridfs:list()
*/
static int gridfs_list(lua_State *L) {
GridFS *gridfs = userdata_to_gridfs(L, 1);
- DBClientCursor **cursor = (DBClientCursor **)lua_newuserdata(L, sizeof(DBClientCursor *));
auto_ptr<DBClientCursor> autocursor = gridfs->list();
+
+ if (!autocursor.get()) {
+ lua_pushnil(L);
+ lua_pushstring(L, LUAMONGO_ERR_CONNECTION_LOST);
+ return 2;
+ }
+
+ DBClientCursor **cursor = (DBClientCursor **)lua_newuserdata(L, sizeof(DBClientCursor *));
*cursor = autocursor.get();
autocursor.release();
@@ -168,7 +175,7 @@ static int gridfs_store_file(lua_State *L) {
/*
* gridfile, err = gridfs:store_data(data[, remote_file], content_type]])
- * puts the file represented by data into the db
+ * puts the file represented by data into the db
*/
static int gridfs_store_data(lua_State *L) {
int resultcount = 1;

0 comments on commit 1d1cfe2

Please sign in to comment.
Something went wrong with that request. Please try again.