return err for buffer functions

This commit is contained in:
Lion 2022-07-06 13:14:39 +08:00
parent f87bee3f20
commit 0b8146fa5f
3 changed files with 37 additions and 21 deletions

View File

@ -65,11 +65,15 @@ end
-- create the searcher based on the cache-policy -- create the searcher based on the cache-policy
local searcher, v_index, content local searcher, v_index, content
if cachePolicy == "file" then if cachePolicy == "file" then
searcher = xdb.new_with_file_only(dbFile) searcher, err = xdb.new_with_file_only(dbFile)
if err ~= nil then
print(string.format("failed to create searcher: %s", err))
return
end
elseif cachePolicy == "vectorIndex" then elseif cachePolicy == "vectorIndex" then
v_index = xdb.load_vector_index(dbFile) v_index, err = xdb.load_vector_index(dbFile)
if v_index == nil then if err ~= nil then
print(string.format("failed to load vector index from '%s'", dbFile)) print(string.format("failed to load vector index: %s", err))
return return
end end
@ -79,8 +83,8 @@ elseif cachePolicy == "vectorIndex" then
return return
end end
elseif cachePolicy == "content" then elseif cachePolicy == "content" then
content = xdb.load_content(dbFile) content, err = xdb.load_content(dbFile)
if content == nil then if err ~= nil then
print(string.format("failed to load xdb content from '%s'", dbFile)) print(string.format("failed to load xdb content from '%s'", dbFile))
return return
end end
@ -174,4 +178,4 @@ if count > 0 then
avg_costs = c_time / count avg_costs = c_time / count
end end
print(string.format("Bench finished, {cachePolicy: %s, total: %d, took: %.3f s, cost: %.3f μs/op}", print(string.format("Bench finished, {cachePolicy: %s, total: %d, took: %.3f s, cost: %.3f μs/op}",
cachePolicy, count, (xdb.now() - s_time)/1e6, c_time / count)) cachePolicy, count, (xdb.now() - s_time)/1e6, c_time / count))

View File

@ -68,9 +68,9 @@ if cachePolicy == "file" then
return return
end end
elseif cachePolicy == "vectorIndex" then elseif cachePolicy == "vectorIndex" then
v_index = xdb.load_vector_index(dbFile) v_index, err = xdb.load_vector_index(dbFile)
if v_index == nil then if err ~= nil then
print(string.format("failed to load vector index from '%s'", dbFile)) print(string.format("failed to load vector index: %s", err))
return return
end end
@ -80,8 +80,8 @@ elseif cachePolicy == "vectorIndex" then
return return
end end
elseif cachePolicy == "content" then elseif cachePolicy == "content" then
content = xdb.load_content(dbFile) content, err = xdb.load_content(dbFile)
if content == nil then if err ~= nil then
print(string.format("failed to load xdb content from '%s'", dbFile)) print(string.format("failed to load xdb content from '%s'", dbFile))
return return
end end

View File

@ -152,13 +152,16 @@ static int lua_xdb_load_header_from_file(lua_State *L) {
header = xdb_load_header_from_file(db_path); header = xdb_load_header_from_file(db_path);
if (header == NULL) { if (header == NULL) {
lua_pushnil(L); lua_pushnil(L);
return 1; lua_pushfstring(L, "load header from `%s`", db_path);
return 2;
} }
// alloc the buffer. // alloc the buffer.
buffer = (xdb_buffer_t *) lua_newuserdata(L, sizeof(xdb_buffer_t)); buffer = (xdb_buffer_t *) lua_newuserdata(L, sizeof(xdb_buffer_t));
if (buffer == NULL) { if (buffer == NULL) {
return luaL_error(L, "failed to alloc xdb buffer entry"); lua_pushnil(L);
lua_pushfstring(L, "failed to alloc xdb buffer entry");
return 2;
} }
// init the buffer // init the buffer
@ -170,8 +173,9 @@ static int lua_xdb_load_header_from_file(lua_State *L) {
// set the metatable of the header buffer object and push onto the stack // set the metatable of the header buffer object and push onto the stack
luaL_getmetatable(L, XDB_BUFFER_METATABLE_NAME); luaL_getmetatable(L, XDB_BUFFER_METATABLE_NAME);
lua_setmetatable(L, -2); lua_setmetatable(L, -2);
lua_pushnil(L);
return 1; return 2;
} }
static int lua_xdb_load_vector_index_from_file(lua_State *L) { static int lua_xdb_load_vector_index_from_file(lua_State *L) {
@ -184,13 +188,16 @@ static int lua_xdb_load_vector_index_from_file(lua_State *L) {
v_index = xdb_load_vector_index_from_file(db_path); v_index = xdb_load_vector_index_from_file(db_path);
if (v_index == NULL) { if (v_index == NULL) {
lua_pushnil(L); lua_pushnil(L);
return 1; lua_pushfstring(L, "load vector index from `%s`", db_path);
return 2;
} }
// alloc the buffer. // alloc the buffer.
buffer = (xdb_buffer_t *) lua_newuserdata(L, sizeof(xdb_buffer_t)); buffer = (xdb_buffer_t *) lua_newuserdata(L, sizeof(xdb_buffer_t));
if (buffer == NULL) { if (buffer == NULL) {
return luaL_error(L, "failed to alloc xdb buffer entry"); lua_pushnil(L);
lua_pushstring(L, "failed to alloc xdb buffer entry");
return 2;
} }
// init the buffer // init the buffer
@ -202,8 +209,9 @@ static int lua_xdb_load_vector_index_from_file(lua_State *L) {
// set the metatable of the header buffer object and push onto the stack // set the metatable of the header buffer object and push onto the stack
luaL_getmetatable(L, XDB_BUFFER_METATABLE_NAME); luaL_getmetatable(L, XDB_BUFFER_METATABLE_NAME);
lua_setmetatable(L, -2); lua_setmetatable(L, -2);
lua_pushnil(L);
return 1; return 2;
} }
static int lua_xdb_load_content_from_file(lua_State *L) { static int lua_xdb_load_content_from_file(lua_State *L) {
@ -216,13 +224,16 @@ static int lua_xdb_load_content_from_file(lua_State *L) {
content = xdb_load_content_from_file(db_path); content = xdb_load_content_from_file(db_path);
if (content == NULL) { if (content == NULL) {
lua_pushnil(L); lua_pushnil(L);
return 1; lua_pushfstring(L, "load xdb content from `%s`", db_path);
return 2;
} }
// alloc the buffer. // alloc the buffer.
buffer = (xdb_buffer_t *) lua_newuserdata(L, sizeof(xdb_buffer_t)); buffer = (xdb_buffer_t *) lua_newuserdata(L, sizeof(xdb_buffer_t));
if (buffer == NULL) { if (buffer == NULL) {
return luaL_error(L, "failed to alloc xdb buffer entry"); lua_pushnil(L);
lua_pushstring(L, "failed to alloc xdb buffer entry");
return 2;
} }
// init the buffer // init the buffer
@ -234,8 +245,9 @@ static int lua_xdb_load_content_from_file(lua_State *L) {
// set the metatable of the header buffer object and push onto the stack // set the metatable of the header buffer object and push onto the stack
luaL_getmetatable(L, XDB_BUFFER_METATABLE_NAME); luaL_getmetatable(L, XDB_BUFFER_METATABLE_NAME);
lua_setmetatable(L, -2); lua_setmetatable(L, -2);
lua_pushnil(L);
return 1; return 2;
} }
// --- End of buffer api // --- End of buffer api