diff --git a/README.md b/README.md index 1decba5..4d8b3e0 100644 --- a/README.md +++ b/README.md @@ -16,51 +16,62 @@ ## 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 - local mongo = require('mongo') +```Lua +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', {})) - - -- loop through the result set - for result in q:results() do - print(result.a) - print(result.b) - end +-- 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 +``` ## 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-v0.4-0.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-0.rockspec" diff --git a/main.cpp b/main.cpp index 95e9865..40fba7f 100644 --- a/main.cpp +++ b/main.cpp @@ -23,6 +23,7 @@ #include #include +#include #include "utils.h" #include "common.h" @@ -36,6 +37,24 @@ 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; +} + +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 @@ -45,6 +64,12 @@ 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}, + {"time", mongo_time}, + {NULL, NULL} + }; + // bsontypes is the root table mongo_bsontypes_register(L); @@ -92,6 +117,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; } 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_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 */ 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/luamongo-scm-0.rockspec b/rockspec/luamongo-unstable-0.rockspec similarity index 71% rename from luamongo-scm-0.rockspec rename to rockspec/luamongo-unstable-0.rockspec index 147c07b..4530acb 100644 --- a/luamongo-scm-0.rockspec +++ b/rockspec/luamongo-unstable-0.rockspec @@ -1,8 +1,8 @@ package = "luamongo" -version = "scm-0" +version = "unstable-0" source = { - url = "git://github.com/moai/luamongo.git" + url = "git://github.com/moai/luamongo.git", } description = { @@ -15,7 +15,7 @@ description = { } dependencies = { - "lua >= 5.1" + "lua >= 5.2" } external_dependencies = { @@ -27,10 +27,6 @@ external_dependencies = { 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-v0.4-0.rockspec b/rockspec/luamongo-v0.4-0.rockspec new file mode 100644 index 0000000..4854429 --- /dev/null +++ b/rockspec/luamongo-v0.4-0.rockspec @@ -0,0 +1,34 @@ +package = "luamongo" +version = "v0.4-0" + +source = { + url = "git://github.com/moai/luamongo.git", + tag = "v0.4-0" +} + +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 = "mongoclient", + } +} + +build = { + type = "make", + copy_directories = {}, + install_pass = false, + install = { lib = { "mongo.so" } } +} 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;