Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #30

Merged
merged 8 commits into from
May 19, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 38 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,62 @@

## Support

Submit issues to the <a href="https://github.com/moai/luamongo/issues">moai github site</a>.
Submit issues to the <a
href="https://github.com/moai/luamongo/issues">moai github site</a>.

There is a <a href="http://groups.google.com/group/luamongo">Google Groups mailing list</a>.
There is a <a href="http://groups.google.com/group/luamongo">Google
Groups mailing list</a>.

## 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 <a href="https://github.com/mongodb/mongo-cxx-driver">mongodb C++ API</a>.
luamongo is a Lua library that wraps the <a
href="https://github.com/mongodb/mongo-cxx-driver">mongodb C++
API</a>.

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"
28 changes: 28 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include <iostream>
#include <client/dbclient.h>
#include <sys/time.h>
#include "utils.h"
#include "common.h"

Expand All @@ -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<double>(wop.tv_sec) +
static_cast<double>(wop.tv_usec)*1e-6);
return 1;
}

/*
*
* library entry point
Expand All @@ -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);

Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion mongo_cursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ int cursor_create(lua_State *L, DBClientBase *connection, const char *ns,
int resultcount = 1;

try {
auto_ptr<DBClientCursor> autocursor = connection->query(
std::auto_ptr<DBClientCursor> autocursor = connection->query(
ns, query, nToReturn, nToSkip,
fieldsToReturn, queryOptions, batchSize);

Expand Down
2 changes: 1 addition & 1 deletion mongo_cxx_extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
16 changes: 9 additions & 7 deletions mongo_dbclient.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include <client/dbclient.h>
#include <string>
#include <list>
#include "utils.h"
#include "common.h"

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<DBClientCursor> autocursor = dbclient->getIndexes(ns);
std::auto_ptr<DBClientCursor> autocursor = dbclient->getIndexes(ns);

if (!autocursor.get()) {
lua_pushnil(L);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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<string> dbs = dbclient->getDatabaseNames();
std::list<std::string> dbs = dbclient->getDatabaseNames();
lua_newtable(L);
int i=1;
for (list<string>::iterator it=dbs.begin(); it!=dbs.end(); ++it, ++i) {
for (std::list<std::string>::iterator it=dbs.begin(); it!=dbs.end(); ++it, ++i) {
lua_pushnumber(L,i);
lua_pushstring(L,it->c_str());
lua_settable(L,-3);
Expand All @@ -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<string> dbs = dbclient->getCollectionNames(ns);
std::list<std::string> dbs = dbclient->getCollectionNames(ns);
lua_newtable(L);
int i=1;
for (list<string>::iterator it=dbs.begin(); it!=dbs.end(); ++it, ++i) {
for (std::list<std::string>::iterator it=dbs.begin(); it!=dbs.end(); ++it, ++i) {
lua_pushnumber(L,i);
lua_pushstring(L,it->c_str());
lua_settable(L,-3);
Expand Down
2 changes: 1 addition & 1 deletion mongo_gridfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ static int gridfs_list(lua_State *L) {
} else if (type == LUA_TTABLE) {
lua_to_bson(L, 2, query);
}
auto_ptr<DBClientCursor> autocursor = gridfs->list(query);
std::auto_ptr<DBClientCursor> autocursor = gridfs->list(query);

if (!autocursor.get()){
lua_pushnil(L);
Expand Down
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -15,7 +15,7 @@ description = {
}

dependencies = {
"lua >= 5.1"
"lua >= 5.2"
}

external_dependencies = {
Expand All @@ -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" } }
Expand Down
34 changes: 34 additions & 0 deletions rockspec/luamongo-v0.4-0.rockspec
Original file line number Diff line number Diff line change
@@ -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" } }
}
7 changes: 4 additions & 3 deletions utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "utils.h"
#include "common.h"
#include <limits.h>
#include <sstream>

using namespace mongo;

Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down