static int serialize_bloom_filter(lua_State *lua) { lsb_output_buffer* ob = lua_touserdata(lua, -1); const char *key = lua_touserdata(lua, -2); bloom_filter* bf = lua_touserdata(lua, -3); if (!(ob && key && bf)) { return 1; } if (lsb_outputf(ob, "if %s == nil then %s = bloom_filter.new(%u, %g) end\n", key, key, (unsigned)bf->items, bf->probability)) { return 1; } if (lsb_outputf(ob, "%s:fromstring(%u, \"", key, (unsigned)bf->cnt)) { return 1; } if (lsb_serialize_binary(ob, bf->data, bf->bytes)) return 1; if (lsb_outputs(ob, "\")\n", 3)) { return 1; } return 0; }
static char* test_serialize_binary() { size_t size = 512; lsb_output_buffer b; lsb_err_value ret = lsb_init_output_buffer(&b, size); mu_assert(ret == NULL, "received: %s", lsb_err_string(ret)); lsb_serialize_binary(&b, "a\r\n\\\"", 5); mu_assert(b.pos == 9, "received %d", (int)b.pos); mu_assert(memcmp(b.buf, "a\\r\\n\\\\\\\"", 5) == 0, "received %.*s", 9, b.buf); lsb_free_output_buffer(&b); return NULL; }
static int serialize_hyperloglog(lua_State *lua) { lsb_output_buffer *ob = lua_touserdata(lua, -1); const char *key = lua_touserdata(lua, -2); hyperloglog *hll = lua_touserdata(lua, -3); if (!(ob && key && hll)) return 1; if (lsb_outputf(ob, "if %s == nil then %s = hyperloglog.new() end\n", key, key)) { return 1; } if (lsb_outputf(ob, "%s:fromstring(\"", key)) return 1; if (lsb_serialize_binary(ob, hll, sizeof(hyperloglog) - 1)) return 1; if (lsb_outputs(ob, "\")\n", 3)) return 1; return 0; }