From ae61755e5ae4cb230b7ae7182f29564400ce7fc9 Mon Sep 17 00:00:00 2001 From: Paco Zamora Martinez Date: Wed, 7 May 2014 18:43:32 +0200 Subject: [PATCH 1/8] Added sleep function as mongo.sleep, using nanosleep --- main.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/main.cpp b/main.cpp index 95e9865..3cc928e 100644 --- a/main.cpp +++ b/main.cpp @@ -23,6 +23,7 @@ #include #include +#include #include "utils.h" #include "common.h" @@ -36,6 +37,16 @@ extern int mongo_gridfile_register(lua_State *L); extern int mongo_gridfschunk_register(lua_State *L); extern int mongo_gridfilebuilder_register(lua_State *L); +int mongo_sleep(lua_State *L) { + double sleeptime = luaL_checknumber(L, 1); + double seconds = floor(sleeptime); + struct timespec req; + req.tv_sec = (time_t)seconds; + req.tv_nsec = (long)((sleeptime-seconds)*1e6); + nanosleep(&req, 0); + return 0; +} + /* * * library entry point @@ -45,6 +56,11 @@ extern int mongo_gridfilebuilder_register(lua_State *L); extern "C" { LM_EXPORT int luaopen_mongo(lua_State *L) { + static const luaL_Reg static_functions[] = { + {"sleep", mongo_sleep}, + {NULL, NULL} + }; + // bsontypes is the root table mongo_bsontypes_register(L); @@ -92,6 +108,9 @@ LM_EXPORT int luaopen_mongo(lua_State *L) { lua_pushstring(L, LUAMONGO_VERSION); lua_setfield(L, -2, LUAMONGO_VERSION_STRING); + // add static functions + luaL_setfuncs(L, static_functions, 0); + return 1; } From 0360332bac6c1a6bb88a0d056bc39b3d0fff3527 Mon Sep 17 00:00:00 2001 From: Paco Zamora Martinez Date: Thu, 8 May 2014 12:44:45 +0200 Subject: [PATCH 2/8] Added mongo.time --- main.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/main.cpp b/main.cpp index 3cc928e..40fba7f 100644 --- a/main.cpp +++ b/main.cpp @@ -47,6 +47,14 @@ int mongo_sleep(lua_State *L) { return 0; } +int mongo_time(lua_State *L) { + struct timeval wop; + gettimeofday(&wop, 0); + lua_pushnumber(L, static_cast(wop.tv_sec) + + static_cast(wop.tv_usec)*1e-6); + return 1; +} + /* * * library entry point @@ -58,6 +66,7 @@ extern "C" { LM_EXPORT int luaopen_mongo(lua_State *L) { static const luaL_Reg static_functions[] = { {"sleep", mongo_sleep}, + {"time", mongo_time}, {NULL, NULL} }; From 8b4a7a3a6a892807a0805f699e53433bba2c4a25 Mon Sep 17 00:00:00 2001 From: pakozm Date: Sat, 10 May 2014 23:53:22 +0200 Subject: [PATCH 3/8] Added luarock spec --- README.md | 29 +++++++++++----- .../luamongo-last.rockspec | 13 +++----- rockspec/luamongo-unstable.rockspec | 33 +++++++++++++++++++ 3 files changed, 58 insertions(+), 17 deletions(-) rename luamongo-scm-0.rockspec => rockspec/luamongo-last.rockspec (67%) create mode 100644 rockspec/luamongo-unstable.rockspec diff --git a/README.md b/README.md index 1decba5..6cb4579 100644 --- a/README.md +++ b/README.md @@ -16,12 +16,15 @@ ## Support -Submit issues to the moai github site. +Submit issues to the moai github site. -There is a Google Groups mailing list. +There is a Google +Groups mailing list. ## Example +```Lua local mongo = require('mongo') -- Create a connection object @@ -47,20 +50,28 @@ There is a Google Groups maili -- loop through the result set for result in q:results() do - print(result.a) - print(result.b) + print(result.a) + print(result.b) end - +``` ## How It Works -luamongo is a Lua library that wraps the mongodb C++ API. +luamongo is a Lua library that wraps the mongodb C++ +API. -The current implementation does not give you raw access to the BSON objects. BSON objects are passed to the API using a Lua table or a JSON string representation. Every returned BSON document is fully marshalled to a Lua table. +The current implementation does not give you raw access to the BSON +objects. BSON objects are passed to the API using a Lua table or a +JSON string representation. Every returned BSON document is fully +marshalled to a Lua table. ## Installing -luarocks can be used to install luamongo. +luarocks can be used to install luamongo last version: + + luarocks install "https://github.com/moai/raw/master/rockspec/luamongo-last.rockspec" - luarocks install "https://github.com/moai/raw/master/rockspec/luamongo-scm-0.rockspec" +or to install the unstable version (master branch): + luarocks install "https://github.com/moai/raw/master/rockspec/luamongo-unstable.rockspec" diff --git a/luamongo-scm-0.rockspec b/rockspec/luamongo-last.rockspec similarity index 67% rename from luamongo-scm-0.rockspec rename to rockspec/luamongo-last.rockspec index 147c07b..6ca3d6e 100644 --- a/luamongo-scm-0.rockspec +++ b/rockspec/luamongo-last.rockspec @@ -1,8 +1,9 @@ package = "luamongo" -version = "scm-0" +version = "v0.4" source = { - url = "git://github.com/moai/luamongo.git" + url = "git://github.com/moai/luamongo.git", + tag = "v0.4-beta" } description = { @@ -15,22 +16,18 @@ description = { } dependencies = { - "lua >= 5.1" + "lua >= 5.2" } external_dependencies = { LIBMONGOCLIENT = { header = "mongo/client/dbclient.h", - library = "mongoclient", + library = "mongo-client", } } build = { type = "make", - build_variables = { - CC="g++", - CFLAGS="-g -O2 -shared -fPIC -I/usr/include/mongo -I/usr/include/lua", - }, copy_directories = {}, install_pass = false, install = { lib = { "mongo.so" } } diff --git a/rockspec/luamongo-unstable.rockspec b/rockspec/luamongo-unstable.rockspec new file mode 100644 index 0000000..3ea7c71 --- /dev/null +++ b/rockspec/luamongo-unstable.rockspec @@ -0,0 +1,33 @@ +package = "luamongo" +version = "unstable" + +source = { + url = "git://github.com/moai/luamongo.git", +} + +description = { + summary = "Lua client library for mongodb", + detailed = [[ + luamongo: Lua mongo client library + ]], + homepage = "https://github.com/moai/luamongo", + license = "MIT/X11" +} + +dependencies = { + "lua >= 5.2" +} + +external_dependencies = { + LIBMONGOCLIENT = { + header = "mongo/client/dbclient.h", + library = "mongo-client", + } +} + +build = { + type = "make", + copy_directories = {}, + install_pass = false, + install = { lib = { "mongo.so" } } +} From e2d4f2ae7056aad5c2aab5b46a306dcfdd09e6e8 Mon Sep 17 00:00:00 2001 From: pakozm Date: Sun, 11 May 2014 00:04:43 +0200 Subject: [PATCH 4/8] Updated rock spec --- ...uamongo-unstable.rockspec => luamongo-unstable-0.rockspec} | 2 +- rockspec/{luamongo-last.rockspec => luamongo-v0.4-0.rockspec} | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) rename rockspec/{luamongo-unstable.rockspec => luamongo-unstable-0.rockspec} (96%) rename rockspec/{luamongo-last.rockspec => luamongo-v0.4-0.rockspec} (93%) diff --git a/rockspec/luamongo-unstable.rockspec b/rockspec/luamongo-unstable-0.rockspec similarity index 96% rename from rockspec/luamongo-unstable.rockspec rename to rockspec/luamongo-unstable-0.rockspec index 3ea7c71..80a183f 100644 --- a/rockspec/luamongo-unstable.rockspec +++ b/rockspec/luamongo-unstable-0.rockspec @@ -1,5 +1,5 @@ package = "luamongo" -version = "unstable" +version = "unstable-0" source = { url = "git://github.com/moai/luamongo.git", diff --git a/rockspec/luamongo-last.rockspec b/rockspec/luamongo-v0.4-0.rockspec similarity index 93% rename from rockspec/luamongo-last.rockspec rename to rockspec/luamongo-v0.4-0.rockspec index 6ca3d6e..c54c993 100644 --- a/rockspec/luamongo-last.rockspec +++ b/rockspec/luamongo-v0.4-0.rockspec @@ -1,9 +1,9 @@ package = "luamongo" -version = "v0.4" +version = "v0.4-0" source = { url = "git://github.com/moai/luamongo.git", - tag = "v0.4-beta" + tag = "v0.4-0" } description = { From 69ba8214d229b91dcee87e14c3ef7d8fa34b62b6 Mon Sep 17 00:00:00 2001 From: pakozm Date: Sun, 11 May 2014 00:06:06 +0200 Subject: [PATCH 5/8] Updated readme --- README.md | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 6cb4579..4d8b3e0 100644 --- a/README.md +++ b/README.md @@ -25,34 +25,34 @@ Groups mailing list. ## Example ```Lua - local mongo = require('mongo') +local mongo = require('mongo') - -- Create a connection object - local db = assert(mongo.Connection.New()) +-- Create a connection object +local db = assert(mongo.Connection.New()) - -- connect to the server on localhost - assert(db:connect('localhost')) +-- connect to the server on localhost +assert(db:connect('localhost')) - -- insert a value into the namespace 'test.values' - assert(db:insert('test.values', {a = 10, b = 'str1'})) +-- insert a value into the namespace 'test.values' +assert(db:insert('test.values', {a = 10, b = 'str1'})) - -- the same using a JSON string - assert(db:insert('test.values', "{'a': 20, 'b': 'str2'}")) +-- the same using a JSON string +assert(db:insert('test.values', "{'a': 20, 'b': 'str2'}")) - -- insert a multiple values into the namespace 'test.values' - assert(db:insert_batch('test.values', {{a = 10, b = 'str1'}, {c = 11, d = 'str2'}})) +-- insert a multiple values into the namespace 'test.values' +assert(db:insert_batch('test.values', {{a = 10, b = 'str1'}, {c = 11, d = 'str2'}})) - -- print the number of rows in the namespace 'test.values' - print(db:count('test.values')) +-- print the number of rows in the namespace 'test.values' +print(db:count('test.values')) - -- query all the values in the namespace 'test.values' - local q = assert(db:query('test.values', {})) +-- query all the values in the namespace 'test.values' +local q = assert(db:query('test.values', {})) - -- loop through the result set - for result in q:results() do - print(result.a) - print(result.b) - end +-- loop through the result set +for result in q:results() do + print(result.a) + print(result.b) +end ``` ## How It Works @@ -70,8 +70,8 @@ marshalled to a Lua table. luarocks can be used to install luamongo last version: - luarocks install "https://github.com/moai/raw/master/rockspec/luamongo-last.rockspec" + luarocks install "https://github.com/moai/raw/master/rockspec/luamongo-v0.4-0.rockspec" or to install the unstable version (master branch): - luarocks install "https://github.com/moai/raw/master/rockspec/luamongo-unstable.rockspec" + luarocks install "https://github.com/moai/raw/master/rockspec/luamongo-unstable-0.rockspec" From 66766f51a43034ca426c1cfdb065d468c9aba0d6 Mon Sep 17 00:00:00 2001 From: pakozm Date: Mon, 12 May 2014 09:18:55 +0200 Subject: [PATCH 6/8] Removed compiler errors with clang --- mongo_cursor.cpp | 2 +- mongo_dbclient.cpp | 16 +++++++++------- mongo_gridfs.cpp | 2 +- utils.cpp | 7 ++++--- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/mongo_cursor.cpp b/mongo_cursor.cpp index c384f35..e74e1ba 100644 --- a/mongo_cursor.cpp +++ b/mongo_cursor.cpp @@ -24,7 +24,7 @@ int cursor_create(lua_State *L, DBClientBase *connection, const char *ns, int resultcount = 1; try { - auto_ptr autocursor = connection->query( + std::auto_ptr autocursor = connection->query( ns, query, nToReturn, nToSkip, fieldsToReturn, queryOptions, batchSize); diff --git a/mongo_dbclient.cpp b/mongo_dbclient.cpp index acc9a63..b74b773 100644 --- a/mongo_dbclient.cpp +++ b/mongo_dbclient.cpp @@ -1,4 +1,6 @@ #include +#include +#include #include "utils.h" #include "common.h" @@ -662,7 +664,7 @@ static int dbclient_exists(lua_State *L) { static int dbclient_gen_index_name(lua_State *L) { DBClientBase *dbclient = userdata_to_dbclient(L, 1); - string name = ""; + std::string name = ""; try { int type = lua_type(L, 2); @@ -699,7 +701,7 @@ static int dbclient_get_indexes(lua_State *L) { DBClientBase *dbclient = userdata_to_dbclient(L, 1); const char *ns = luaL_checkstring(L, 2); - auto_ptr autocursor = dbclient->getIndexes(ns); + std::auto_ptr autocursor = dbclient->getIndexes(ns); if (!autocursor.get()) { lua_pushnil(L); @@ -801,7 +803,7 @@ static int dbclient_reset_index_cache(lua_State *L) { static int dbclient_get_last_error(lua_State *L) { DBClientBase *dbclient = userdata_to_dbclient(L, 1); - string result = dbclient->getLastError(); + std::string result = dbclient->getLastError(); lua_pushlstring(L, result.c_str(), result.size()); return 1; } @@ -862,10 +864,10 @@ static int dbclient_run_command(lua_State *L) { static int dbclient_get_dbnames(lua_State *L) { DBClientBase *dbclient = userdata_to_dbclient(L, 1); try { - list dbs = dbclient->getDatabaseNames(); + std::list dbs = dbclient->getDatabaseNames(); lua_newtable(L); int i=1; - for (list::iterator it=dbs.begin(); it!=dbs.end(); ++it, ++i) { + for (std::list::iterator it=dbs.begin(); it!=dbs.end(); ++it, ++i) { lua_pushnumber(L,i); lua_pushstring(L,it->c_str()); lua_settable(L,-3); @@ -890,10 +892,10 @@ static int dbclient_get_collections(lua_State *L) { DBClientBase *dbclient = userdata_to_dbclient(L, 1); const char *ns = luaL_checkstring(L, 2); try { - list dbs = dbclient->getCollectionNames(ns); + std::list dbs = dbclient->getCollectionNames(ns); lua_newtable(L); int i=1; - for (list::iterator it=dbs.begin(); it!=dbs.end(); ++it, ++i) { + for (std::list::iterator it=dbs.begin(); it!=dbs.end(); ++it, ++i) { lua_pushnumber(L,i); lua_pushstring(L,it->c_str()); lua_settable(L,-3); diff --git a/mongo_gridfs.cpp b/mongo_gridfs.cpp index 7c5a53d..56f57d5 100644 --- a/mongo_gridfs.cpp +++ b/mongo_gridfs.cpp @@ -104,7 +104,7 @@ static int gridfs_list(lua_State *L) { } else if (type == LUA_TTABLE) { lua_to_bson(L, 2, query); } - auto_ptr autocursor = gridfs->list(query); + std::auto_ptr autocursor = gridfs->list(query); if (!autocursor.get()){ lua_pushnil(L); diff --git a/utils.cpp b/utils.cpp index 13127d7..ffa3a10 100644 --- a/utils.cpp +++ b/utils.cpp @@ -3,6 +3,7 @@ #include "utils.h" #include "common.h" #include +#include using namespace mongo; @@ -151,7 +152,7 @@ static void lua_append_bson(lua_State *L, const char *key, int stackpos, BSONObj if (dense) { for (int i = 0; i < len; i++) { lua_rawgeti(L, stackpos, i+1); - stringstream ss; + std::stringstream ss; ss << i; lua_append_bson(L, ss.str().c_str(), -1, &b, ref); @@ -163,7 +164,7 @@ static void lua_append_bson(lua_State *L, const char *key, int stackpos, BSONObj for (lua_pushnil(L); lua_next(L, stackpos); lua_pop(L, 1)) { switch (lua_type(L, -2)) { // key type case LUA_TNUMBER: { - stringstream ss; + std::stringstream ss; ss << lua_tonumber(L, -2); lua_append_bson(L, ss.str().c_str(), -1, &b, ref); break; @@ -271,7 +272,7 @@ void lua_to_bson(lua_State *L, int stackpos, BSONObj &obj) { for (lua_pushnil(L); lua_next(L, stackpos); lua_pop(L, 1)) { switch (lua_type(L, -2)) { // key type case LUA_TNUMBER: { - ostringstream ss; + std::ostringstream ss; ss << lua_tonumber(L, -2); lua_append_bson(L, ss.str().c_str(), -1, &builder, ref); break; From 84155e31fadd788e8788bbb3e85451f26e98ec35 Mon Sep 17 00:00:00 2001 From: Paco Zamora Martinez Date: Mon, 12 May 2014 10:51:56 +0200 Subject: [PATCH 7/8] Modified indentation --- mongo_cxx_extension.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mongo_cxx_extension.cpp b/mongo_cxx_extension.cpp index bda2852..822cb29 100644 --- a/mongo_cxx_extension.cpp +++ b/mongo_cxx_extension.cpp @@ -122,7 +122,7 @@ namespace mongo_cxx_extension { } BSONObj GridFileBuilder::buildFile(const string &name, - const string& content_type) { + const string& content_type) { privateAppendPendingData(); /* from gridfs.cpp at https://github.com/mongodb/mongo-cxx-driver/blob/legacy/src/mongo/client/gridfs.cpp */ From b35d8272af945dd17bbcdf0615c0e74dda35d2f4 Mon Sep 17 00:00:00 2001 From: Paco Zamora Martinez Date: Wed, 14 May 2014 10:06:42 +0200 Subject: [PATCH 8/8] Updated rockspeck --- rockspec/luamongo-unstable-0.rockspec | 2 +- rockspec/luamongo-v0.4-0.rockspec | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/rockspec/luamongo-unstable-0.rockspec b/rockspec/luamongo-unstable-0.rockspec index 80a183f..4530acb 100644 --- a/rockspec/luamongo-unstable-0.rockspec +++ b/rockspec/luamongo-unstable-0.rockspec @@ -21,7 +21,7 @@ dependencies = { external_dependencies = { LIBMONGOCLIENT = { header = "mongo/client/dbclient.h", - library = "mongo-client", + library = "mongoclient", } } diff --git a/rockspec/luamongo-v0.4-0.rockspec b/rockspec/luamongo-v0.4-0.rockspec index c54c993..4854429 100644 --- a/rockspec/luamongo-v0.4-0.rockspec +++ b/rockspec/luamongo-v0.4-0.rockspec @@ -22,7 +22,7 @@ dependencies = { external_dependencies = { LIBMONGOCLIENT = { header = "mongo/client/dbclient.h", - library = "mongo-client", + library = "mongoclient", } }