Skip to content

Commit 4a2b253

Browse files
committed
Merge pull request #221 from cloudwu/dev
release 0.9.3
2 parents f15cd1d + f65ecdf commit 4a2b253

File tree

10 files changed

+151
-26
lines changed

10 files changed

+151
-26
lines changed

HISTORY.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
v0.9.3 (2014-1-5)
2+
-----------
3+
* Add : mongo createIndex
4+
* Update : sproto
5+
* bugfix : sharedata check dirty flag when len/pairs metamethod
6+
* bugfix : multicast
7+
18
v0.9.2 (2014-12-8)
29
-----------
310
* Simplify the message queue

lualib-src/lua-multicast.c

+8-5
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ mc_unpacklocal(lua_State *L) {
8383
if (sz != sizeof(*pack)) {
8484
return luaL_error(L, "Invalid multicast package size %d", sz);
8585
}
86-
lua_settop(L, 1);
86+
lua_pushlightuserdata(L, *pack);
8787
lua_pushlightuserdata(L, (*pack)->data);
8888
lua_pushunsigned(L, (*pack)->size);
8989
return 3;
@@ -92,6 +92,8 @@ mc_unpacklocal(lua_State *L) {
9292
/*
9393
lightuserdata struct mc_package **
9494
integer reference
95+
96+
return mc_package *
9597
*/
9698
static int
9799
mc_bindrefer(lua_State *L) {
@@ -102,16 +104,17 @@ mc_bindrefer(lua_State *L) {
102104
}
103105
(*pack)->reference = ref;
104106

105-
return 0;
107+
lua_pushlightuserdata(L, *pack);
108+
109+
return 1;
106110
}
107111

108112
/*
109-
lightuserdata struct mc_package **
113+
lightuserdata struct mc_package *
110114
*/
111115
static int
112116
mc_closelocal(lua_State *L) {
113-
struct mc_package **ptr = lua_touserdata(L,1);
114-
struct mc_package *pack = *ptr;
117+
struct mc_package *pack = lua_touserdata(L,1);
115118

116119
int ref = __sync_sub_and_fetch(&pack->reference, 1);
117120
if (ref <= 0) {

lualib/mongo.lua

+21-7
Original file line numberDiff line numberDiff line change
@@ -284,25 +284,39 @@ end
284284

285285
-- collection:createIndex({username = 1}, {unique = true})
286286
function mongo_collection:createIndex(keys, option)
287-
local name
288-
for k, v in pairs(keys) do
289-
assert(v == 1)
290-
name = (name == nil) and k or (name .. "_" .. k)
287+
local name = option.name
288+
option.name = nil
289+
290+
if not name then
291+
for k, v in pairs(keys) do
292+
name = (name == nil) and k or (name .. "_" .. k)
293+
name = name .. "_" .. v
294+
end
291295
end
292296

297+
293298
local doc = {};
294299
doc.name = name
295300
doc.key = keys
296301
for k, v in pairs(option) do
297-
if v then
298-
doc[k] = true
299-
end
302+
doc[k] = v
300303
end
301304
return self.database:runCommand("createIndexes", self.name, "indexes", {doc})
302305
end
303306

304307
mongo_collection.ensureIndex = mongo_collection.createIndex;
305308

309+
310+
function mongo_collection:drop()
311+
return self.database:runCommand("drop", self.name)
312+
end
313+
314+
-- collection:dropIndex("age_1")
315+
-- collection:dropIndex("*")
316+
function mongo_collection:dropIndex(indexName)
317+
return self.database:runCommand("dropIndexes", self.name, "index", indexName)
318+
end
319+
306320
-- collection:findAndModify({query = {name = "userid"}, update = {["$inc"] = {nextid = 1}}, })
307321
-- keys, value type
308322
-- query, table

lualib/sharedata/corelib.lua

+10-4
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ local function genkey(self)
5353
return key
5454
end
5555

56-
function meta:__index(key)
56+
local function getcobj(self)
5757
local obj = self.__obj
5858
if isdirty(obj) then
5959
local newobj, newtbl = needupdate(self.__gcobj)
@@ -67,6 +67,11 @@ function meta:__index(key)
6767
obj = self.__obj
6868
end
6969
end
70+
return obj
71+
end
72+
73+
function meta:__index(key)
74+
local obj = getcobj(self)
7075
local v = index(obj, key)
7176
if type(v) == "userdata" then
7277
local r = setmetatable({
@@ -83,11 +88,11 @@ function meta:__index(key)
8388
end
8489

8590
function meta:__len()
86-
return len(self.__obj)
91+
return len(getcobj(self))
8792
end
8893

8994
local function conf_ipairs(self, index)
90-
local obj = self.__obj
95+
local obj = getcobj(self)
9196
index = index + 1
9297
local value = rawget(self, index)
9398
if value then
@@ -109,7 +114,8 @@ function meta:__pairs()
109114
end
110115

111116
function conf.next(obj, key)
112-
local nextkey = core.nextkey(obj.__obj, key)
117+
local cobj = getcobj(obj)
118+
local nextkey = core.nextkey(cobj, key)
113119
if nextkey then
114120
return nextkey, obj[nextkey]
115121
end

lualib/socket.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ socket_message[1] = function(id, size, data)
5858
end
5959
else
6060
if s.buffer_limit and sz > s.buffer_limit then
61-
skynet.error(string.format("socket buffer overlow: fd=%d size=%d", id , sz))
61+
skynet.error(string.format("socket buffer overflow: fd=%d size=%d", id , sz))
6262
driver.clear(s.buffer,buffer_pool)
6363
driver.close(id)
6464
return

lualib/sprotoparser.lua

+4-1
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,12 @@ local function packgroup(t,p)
313313
local tt, tp
314314
local alltypes = {}
315315
for name in pairs(t) do
316-
alltypes[name] = #alltypes
317316
table.insert(alltypes, name)
318317
end
318+
table.sort(alltypes) -- make result stable
319+
for idx, name in ipairs(alltypes) do
320+
alltypes[name] = idx - 1
321+
end
319322
tt = {}
320323
for _,name in ipairs(alltypes) do
321324
table.insert(tt, packtype(name, t[name], alltypes))

service/multicastd.lua

+4-2
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,20 @@ end
6868
local function publish(c , source, pack, size)
6969
local group = channel[c]
7070
if group == nil then
71-
-- dead channel, delete the pack
72-
mc.bind(pack, 1)
71+
-- dead channel, delete the pack. mc.bind returns the pointer in pack
72+
local pack = mc.bind(pack, 1)
7373
mc.close(pack)
7474
return
7575
end
7676
mc.bind(pack, channel_n[c])
7777
local msg = skynet.tostring(pack, size)
7878
for k in pairs(group) do
79+
-- the msg is a pointer to the real message, publish pointer in local is ok.
7980
skynet.redirect(k, source, "multicast", c , msg)
8081
end
8182
local remote = channel_remote[c]
8283
if remote then
84+
-- remote publish should unpack the pack, because we should not publish the pointer out.
8385
local _, msg, sz = mc.unpack(pack, size)
8486
local msg = skynet.tostring(msg,sz)
8587
for node in pairs(remote) do

skynet-src/skynet_mq.c

-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ static struct global_queue *Q = NULL;
4242
#define LOCK(q) while (__sync_lock_test_and_set(&(q)->lock,1)) {}
4343
#define UNLOCK(q) __sync_lock_release(&(q)->lock);
4444

45-
#define GP(p) ((p) % MAX_GLOBAL_MQ)
46-
4745
void
4846
skynet_globalmq_push(struct message_queue * queue) {
4947
struct global_queue *q= Q;

test/testmongodb.lua

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
local skynet = require "skynet"
2+
local mongo = require "mongo"
3+
local bson = require "bson"
4+
5+
local host, db_name = ...
6+
7+
function test_insert_without_index()
8+
local db = mongo.client({host = host})
9+
10+
db[db_name].testdb:dropIndex("*")
11+
db[db_name].testdb:drop()
12+
13+
local ret = db[db_name].testdb:safe_insert({test_key = 1});
14+
assert(ret and ret.n == 1)
15+
16+
local ret = db[db_name].testdb:safe_insert({test_key = 1});
17+
assert(ret and ret.n == 1)
18+
end
19+
20+
function test_insert_with_index()
21+
local db = mongo.client({host = host})
22+
23+
db[db_name].testdb:dropIndex("*")
24+
db[db_name].testdb:drop()
25+
26+
db[db_name].testdb:ensureIndex({test_key = 1}, {unique = true, name = "test_key_index"})
27+
28+
local ret = db[db_name].testdb:safe_insert({test_key = 1})
29+
assert(ret and ret.n == 1)
30+
31+
local ret = db[db_name].testdb:safe_insert({test_key = 1})
32+
assert(ret and ret.n == 0)
33+
end
34+
35+
function test_find_and_remove()
36+
local db = mongo.client({host = host})
37+
38+
db[db_name].testdb:dropIndex("*")
39+
db[db_name].testdb:drop()
40+
41+
db[db_name].testdb:ensureIndex({test_key = 1}, {unique = true, name = "test_key_index"})
42+
43+
local ret = db[db_name].testdb:safe_insert({test_key = 1})
44+
assert(ret and ret.n == 1)
45+
46+
local ret = db[db_name].testdb:findOne({test_key = 1})
47+
assert(ret and ret.test_key == 1)
48+
49+
db[db_name].testdb:delete({test_key = 1})
50+
51+
local ret = db[db_name].testdb:findOne({test_key = 1})
52+
assert(ret == nil)
53+
end
54+
55+
56+
function test_expire_index()
57+
local db = mongo.client({host = host})
58+
59+
db[db_name].testdb:dropIndex("*")
60+
db[db_name].testdb:drop()
61+
62+
db[db_name].testdb:ensureIndex({test_key = 1}, {unique = true, name = "test_key_index", expireAfterSeconds = 1, })
63+
db[db_name].testdb:ensureIndex({test_date = 1}, {expireAfterSeconds = 1, })
64+
65+
local ret = db[db_name].testdb:safe_insert({test_key = 1, test_date = bson.date(os.time())})
66+
assert(ret and ret.n == 1)
67+
68+
local ret = db[db_name].testdb:findOne({test_key = 1})
69+
assert(ret and ret.test_key == 1)
70+
71+
for i = 1, 1000 do
72+
skynet.sleep(11);
73+
74+
local ret = db[db_name].testdb:findOne({test_key = 1})
75+
if ret == nil then
76+
return
77+
end
78+
end
79+
80+
assert(false, "test expire index failed");
81+
end
82+
83+
skynet.start(function()
84+
test_insert_without_index()
85+
test_insert_with_index()
86+
test_find_and_remove()
87+
test_expire_index()
88+
89+
print("mongodb test finish.");
90+
end)

test/testmysql.lua

+6-4
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ local function test3( db)
6363
local res = db:query("select * from cats order by id asc")
6464
print ( "test3 loop times=" ,i,"\n","query result=",dump( res ) )
6565
res = db:query("select * from cats order by id asc")
66-
print ( "test3 loop times=" ,i,"\n","query result=",dump( res ) )
66+
print ( "test3 loop times=" ,i,"\n","query result=",dump( res ) )
6767
skynet.sleep(1000)
6868
i=i+1
6969
end
7070
end
7171
skynet.start(function()
7272

73-
local db=mysql.connect{
73+
local db=mysql.connect{
7474
host="127.0.0.1",
7575
port=3306,
7676
database="skynet",
@@ -83,8 +83,10 @@ skynet.start(function()
8383
end
8484
print("testmysql success to connect to mysql server")
8585

86+
db:query("set names utf8")
87+
8688
local res = db:query("drop table if exists cats")
87-
res = db:query("create table cats "
89+
res = db:query("create table cats "
8890
.."(id serial primary key, ".. "name varchar(5))")
8991
print( dump( res ) )
9092

@@ -112,7 +114,7 @@ skynet.start(function()
112114
while true do
113115
local res = db:query("select * from cats order by id asc")
114116
print ( "test1 loop times=" ,i,"\n","query result=",dump( res ) )
115-
117+
116118
res = db:query("select * from cats order by id asc")
117119
print ( "test1 loop times=" ,i,"\n","query result=",dump( res ) )
118120

0 commit comments

Comments
 (0)