Permalink
Browse files

Added Connection.insert_batch(ns, {doc1, doc2,...})

Batching is more efficient for when there are many documents to insert at one time.
The protocol overhead is the same, but it is less kernel/userspace
transitions for the networking.

There may be a message size limit.  This is not checked, but simply delegates handling to the C++ API.



git-svn-id: http://luamongo.googlecode.com/svn/trunk@57 17e306f6-9439-11de-a13a-71b910e68cc8
  • Loading branch information...
neomantra
neomantra committed Oct 27, 2010
1 parent 668f223 commit f1ce76334169d758b08ca62d64f76b980c00664d
Showing with 33 additions and 0 deletions.
  1. +33 −0 mongo_connection.cpp
View
@@ -241,6 +241,38 @@ static int connection_insert(lua_State *L) {
return 1;
}
+/*
+ * ok,err = db:insert_batch(ns, lua_array_of_tables)
+ */
+static int connection_insert_batch(lua_State *L) {
+ DBClientConnection *connection = userdata_to_connection(L, 1);
+ const char *ns = luaL_checkstring(L, 2);
+ luaL_checktype(L, 3, LUA_TTABLE);
+
+ try {
+ std::vector<BSONObj> vdata;
+ size_t tlen = lua_objlen(L, 3) + 1;
+ for (size_t i = 1; i < tlen; ++i) {
+ vdata.push_back(BSONObj());
+ lua_rawgeti(L, 3, i);
+ lua_to_bson(L, -1, vdata.back());
+ lua_pop(L, -1);
+ }
+ connection->insert(ns, vdata);
+ } catch (std::exception &e) {
+ lua_pushboolean(L, 0);
+ lua_pushfstring(L, LUAMONGO_ERR_INSERT_FAILED, e.what());
+ return 2;
+ } catch (const char *err) {
+ lua_pushboolean(L, 0);
+ lua_pushstring(L, err);
+ return 2;
+ }
+
+ lua_pushboolean(L, 1);
+ return 1;
+}
+
/*
* cursor,err = db:query(ns, lua_table or json_str or query_obj, limit, skip, lua_table or json_str, options, batchsize)
*/
@@ -733,6 +765,7 @@ int mongo_connection_register(lua_State *L) {
{"get_indexes", connection_get_indexes},
{"get_server_address", connection_get_server_address},
{"insert", connection_insert},
+ {"insert_batch", connection_insert_batch},
{"is_failed", connection_is_failed},
{"mapreduce", connection_mapreduce},
{"query", connection_query},

0 comments on commit f1ce763

Please sign in to comment.