Skip to content

Commit

Permalink
UserDbReg : add db_pool_ (#320)
Browse files Browse the repository at this point in the history
* UserDbReg : add db_pool_

Signed-off-by: shewer <[email protected]>

* 改用 script 製作 db_pool_

Signed-off-by: shewer <[email protected]>

---------

Signed-off-by: shewer <[email protected]>
  • Loading branch information
shewer authored Mar 8, 2024
1 parent 0f69bfa commit 7404699
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 21 deletions.
73 changes: 73 additions & 0 deletions sample/lua/userdb.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#! /usr/bin/env lua
--
-- userdb.lua
-- Copyright (C) 2024 Shewer Lu <[email protected]>
--
-- Distributed under terms of the MIT license.
--
--[[
example:
local userdb = require 'userdb'
local ldb=userdb.LevelDb('ecdict')
ldb:open()
for k,v in ldb:query('a'):iter() do print(k,v) end
--]]
local db_pool_ = {}
local vars_get= {
_loaded=true,
read_only=true,
disabled=true,
name=true,
file_name=true,
}
local vars_set= {}
local userdb_mt = {}
function userdb_mt.__newindex(tab,key,value)
local db = db_pool_[tab._db_key]
if not db then
db = UserDb(tab._db_name, tab._db_class)
db_pool_[tab._db_key] = db
end
if db and vars_set[key] then
db[key]= value
end
end

function userdb_mt.__index(tab,key)
local db = db_pool_[tab._db_key]
if not db then
db = UserDb(tab._db_name, tab._db_class)
db_pool_[tab._db_key] = db
end

if db and vars_get[key] then
return db[key]
else
return function (tab, ...)
return db[key](db,...)
end
end
end

local userdb= {}

function userdb.UserDb(db_name, db_class)
local db_key = db_name .. "." .. db_class
local db = {
_db_key = db_key,
_db_name= db_name,
_db_class = db_class,
}
db_pool_[db_key] = UserDb(db_name, db_class)
return setmetatable(db , userdb_mt)
end

function userdb.LevelDb(db_name)
return userdb.UserDb(db_name, "userdb")
end
function userdb.TableDb(db_name)
return userdb.UserDb(db_name, "plain_userdb")
end

return userdb
30 changes: 9 additions & 21 deletions src/types_ext.cc
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,10 @@ namespace UserDbReg{
using A = DbAccessor;

an<T> make(const string& db_name, const string& db_class) {
if (auto comp= UserDb::Require(db_class)){
return an<T>( comp->Create(db_name)) ;
}
else {
return {};
if (auto comp= Db::Require(db_class)) {
return an<T>(comp->Create(db_name));
}
return {};
}

an<T> make_leveldb(const string& db_name) {
Expand All @@ -275,16 +273,6 @@ namespace UserDbReg{
return {};
}

bool Open(T &t) { return t.Open(); }
bool Close(T &t) { return t.Close(); }
bool OpenReadOnly(T &t) { return t.OpenReadOnly(); }
bool Erase(T &t, const string &key) { return t.Erase(key); }
bool Update(T &t, const string &key, const string &value) {
return t.Update(key, value);
}

an<A> Query(T &t, const string& key) { return t.Query(key); }

static const luaL_Reg funcs[] = {
{"UserDb", WRAP(make)},// an<Db> LevelDb( db_file, db_name)
{"LevelDb", WRAP(make_leveldb)},// an<Db> LevelDb( db_file, db_name)
Expand All @@ -293,13 +281,13 @@ namespace UserDbReg{
};

static const luaL_Reg methods[] = {
{"open", WRAP(Open)},
{"open_read_only", WRAP(OpenReadOnly)},
{"close", WRAP(Close)},
{"query", WRAP(Query)}, // query(prefix_key) return DbAccessor
{"open", WRAPMEM(T, Open)},
{"open_read_only", WRAPMEM(T, OpenReadOnly)},
{"close", WRAPMEM(T, Close)},
{"query", WRAPMEM(T, Query)}, // query(prefix_key) return DbAccessor
{"fetch", WRAP(fetch)}, // fetch(key) return value
{"update", WRAP(Update)}, // update(key,value) return bool
{"erase", WRAP(Erase)}, // erase(key) return bool
{"update", WRAPMEM(T, Update)}, // update(key,value) return bool
{"erase", WRAPMEM(T, Erase)}, // erase(key) return bool

{"loaded",WRAPMEM(T, loaded)},
{"disable", WRAPMEM(T, disable)},
Expand Down

0 comments on commit 7404699

Please sign in to comment.