From 4e3c9e3ceb902be178c93f7028cf3669f1cc52eb Mon Sep 17 00:00:00 2001 From: Adam Manzanares Date: Fri, 15 Feb 2013 15:25:42 -0800 Subject: lua: support cls_cxx_write and bufferlist::length Signed-off-by: Adam Manzanares Reviewed-by: Noah Watkins --- src/cls/lua/cls_lua.cc | 13 +++++++++++++ src/liblua/src/lua_bufferlist.cc | 11 +++++++++++ src/test/cls_lua/test_cls_lua.cc | 19 +++++++++++++++++++ src/test/cls_lua/test_script.lua | 9 +++++++++ 4 files changed, 52 insertions(+) diff --git a/src/cls/lua/cls_lua.cc b/src/cls/lua/cls_lua.cc index bf4dc6a6e16..d846df3c226 100644 --- a/src/cls/lua/cls_lua.cc +++ b/src/cls/lua/cls_lua.cc @@ -300,6 +300,18 @@ static int clslua_read(lua_State *L) return clslua_opresult(L, (ret >= 0), ret, 1); } +/* + * cls_cxx_write + */ +static int clslua_write(lua_State *L){ + cls_method_context_t hctx = clslua_get_hctx(L); + int offset = luaL_checkint(L, 1); + int length = luaL_checkint(L, 2); + bufferlist *bl = clslua_checkbufferlist(L, 3); + int ret = cls_cxx_write(hctx, offset, length, bl); + return clslua_opresult(L, (ret == 0), ret, 0); +} + /* * cls_cxx_map_get_val */ @@ -334,6 +346,7 @@ static const luaL_Reg clslua_lib[] = { {"remove", clslua_remove}, {"stat", clslua_stat}, {"read", clslua_read}, + {"write", clslua_write}, {"map_get_val", clslua_map_get_val}, {"map_set_val", clslua_map_set_val}, {NULL, NULL} diff --git a/src/liblua/src/lua_bufferlist.cc b/src/liblua/src/lua_bufferlist.cc index bb2b6084633..09abb755d25 100644 --- a/src/liblua/src/lua_bufferlist.cc +++ b/src/liblua/src/lua_bufferlist.cc @@ -77,6 +77,16 @@ static int bl_append(lua_State *L) return 0; } +/* + * Return the length in bytes of bufferlist + */ +static int bl_length(lua_State *L) +{ + bufferlist *bl = clslua_checkbufferlist(L); + lua_pushinteger(L, bl->length()); + return 1; +} + /* * Perform byte-for-byte bufferlist equality test */ @@ -104,6 +114,7 @@ static int bl_gc(lua_State *L) static const struct luaL_Reg bufferlist_methods[] = { {"str", bl_str}, {"append", bl_append}, + {"length", bl_length}, {"__gc", bl_gc}, {"__eq", bl_eq}, {NULL, NULL} diff --git a/src/test/cls_lua/test_cls_lua.cc b/src/test/cls_lua/test_cls_lua.cc index e73577f6a8a..666ffa3c2b4 100644 --- a/src/test/cls_lua/test_cls_lua.cc +++ b/src/test/cls_lua/test_cls_lua.cc @@ -123,6 +123,25 @@ librados::IoCtx ClsLua::ioctx; string ClsLua::pool_name; string ClsLua::test_script; +TEST_F(ClsLua, Write) { + /* write some data into object */ + string written = "Hello World"; + bufferlist inbl; + ::encode(written, inbl); + ASSERT_EQ(0, clslua_exec(test_script, &inbl, "write")); + + /* have Lua read out of the object */ + uint64_t size; + bufferlist outbl; + ASSERT_EQ(0, ioctx.stat(oid, &size, NULL)); + ASSERT_EQ(size, (uint64_t)ioctx.read(oid, outbl, size, 0) ); + + /* compare what Lua read to what we wrote */ + string read; + ::decode(read, outbl); + ASSERT_EQ(read, written); +} + TEST_F(ClsLua, SyntaxError) { ASSERT_EQ(-EIO, clslua_exec("-")); } diff --git a/src/test/cls_lua/test_script.lua b/src/test/cls_lua/test_script.lua index 73c602a9087..39f25974c3c 100644 --- a/src/test/cls_lua/test_script.lua +++ b/src/test/cls_lua/test_script.lua @@ -15,6 +15,15 @@ end cls.register(read) +-- +-- Write +-- +function write(input, output) + cls.write(0, input:length(), input) +end + +cls.register(write) + -- -- MsgPack -- -- cgit v1.2.1