Cfg: Add Xocfg_mgr

v3.3.4
gnosygnu 8 years ago
parent 364e72e869
commit 265388a889

@ -33,6 +33,4 @@ public class Xoa_cfg_addon implements Xoax_addon_itm, Xoax_addon_itm__special, X
}
public String Addon__key() {return ADDON__KEY;} private static final String ADDON__KEY = "xowa.app.cfg";
// public static void Init(Xoae_app app) {
// }
}

@ -0,0 +1,37 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa.addons.apps.cfgs; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.apps.*;
import gplx.dbs.*; import gplx.xowa.addons.apps.cfgs.mgrs.*;
public class Xocfg_mgr {
private final Xocfg_cache_mgr cache_mgr;
public Xocfg_mgr(Db_conn conn) {
this.cache_mgr = new Xocfg_cache_mgr(conn);
}
public void Clear() {
cache_mgr.Clear();
}
public void Sub_many(Gfo_evt_itm sub, String ctx, String... keys) {
cache_mgr.Sub_many(sub, ctx, keys);
}
public String Get_str(String ctx, String key) {return cache_mgr.Get(ctx, key);}
public void Set_str(String ctx, String key, String val) {cache_mgr.Set(ctx, key, val);}
public static Xocfg_mgr New(Db_conn conn) {
return new Xocfg_mgr(conn);
}
}

@ -34,10 +34,10 @@ public class Xocfg_db_mgr {
public Xoitm_data_tbl Tbl__itm_data() {return tbl__itm_data;} private final Xoitm_data_tbl tbl__itm_data;
public Xonde_i18n_tbl Tbl__nde_i18n() {return tbl__nde_i18n;} private final Xonde_i18n_tbl tbl__nde_i18n;
public String Get_str1(String ctx, String key) {
public String Get_str(String ctx, String key) {
Xoitm_meta_itm meta_itm = tbl__itm_meta.Select_by_key_or_null(key);
if (meta_itm == null) throw Err_.new_wo_type("cfg not defined", "ctx", ctx, "key", key);
Xoitm_data_itm data_itm = tbl__itm_data.Select_by_id_or_null(meta_itm.Id());
Xoitm_data_itm data_itm = tbl__itm_data.Select_one_by_id_or_null(meta_itm.Id(), ctx);
return data_itm == null ? meta_itm.Dflt() : data_itm.Val();
}
public void Set_str(String ctx, String key, String val) {

@ -31,18 +31,33 @@ public class Xoitm_data_tbl implements Db_tbl {
conn.Rls_reg(this);
}
public String Tbl_name() {return tbl_name;} private final String tbl_name;
public void Create_tbl() {conn.Meta_tbl_create(Dbmeta_tbl_itm.New(tbl_name, flds));}
public void Create_tbl() {
conn.Meta_tbl_create(Dbmeta_tbl_itm.New(tbl_name, flds
, Dbmeta_idx_itm.new_normal_by_tbl(tbl_name, "", fld__itm_id, fld__itm_ctx)
));
}
public void Upsert(int itm_id, String ctx, String itm_val, String itm_date) {
Db_tbl__crud_.Upsert(conn, tbl_name, flds, String_.Ary(fld__itm_id), itm_id, ctx, itm_val, itm_date);
}
public void Delete(int id, String ctx) {
conn.Stmt_delete(tbl_name, fld__itm_id, fld__itm_ctx).Crt_int(fld__itm_id, id).Crt_str(fld__itm_ctx, ctx).Exec_delete();
}
public Xoitm_data_itm Select_by_id_or_null(int id) {
Db_rdr rdr = conn.Stmt_select(tbl_name, flds, fld__itm_id).Exec_select__rls_auto();
public Xoitm_data_itm Select_one_by_id_or_null(int id, String ctx) {
Db_rdr rdr = conn.Stmt_select(tbl_name, flds, fld__itm_id, fld__itm_ctx).Crt_int(fld__itm_id, id).Crt_str(fld__itm_ctx, ctx).Exec_select__rls_auto();
try {return rdr.Move_next() ? Load(rdr) : null;}
finally {rdr.Rls();}
}
public Xoitm_data_itm[] Select_all_by_id(int id) {
List_adp list = List_adp_.New();
Db_rdr rdr = conn.Stmt_select(tbl_name, flds, fld__itm_id).Crt_int(fld__itm_id, id).Exec_select__rls_auto();
try {
while (rdr.Move_next()) {
list.Add(Load(rdr));
}
}
finally {rdr.Rls();}
return (Xoitm_data_itm[])list.To_ary_and_clear(Xoitm_data_itm.class);
}
private Xoitm_data_itm Load(Db_rdr rdr) {
return new Xoitm_data_itm
( rdr.Read_int(fld__itm_id)

@ -0,0 +1,77 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa.addons.apps.cfgs.mgrs; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.apps.*; import gplx.xowa.addons.apps.cfgs.*;
class Xocfg_cache_grp {
private final Hash_adp vals = Hash_adp_.New();
private final Hash_adp subs = Hash_adp_.New();
private final String dflt;
public Xocfg_cache_grp(String key, String dflt) {
this.key = key;
this.dflt = dflt;
}
public String Key() {return key;} private final String key;
public String Get(String ctx) {
// exact match; EX: "en.w|key_1"
Xocfg_cache_itm rv = (Xocfg_cache_itm)vals.Get_by(ctx);
if (rv != null) return rv.Val();
// global match; EX: "app|key_1"
rv = (Xocfg_cache_itm)vals.Get_by(gplx.xowa.addons.apps.cfgs.gui.Xogui_itm.Ctx__app);
if (rv != null) return rv.Val();
// dflt
return dflt;
}
public void Set(String ctx, String val) {
Xocfg_cache_itm rv = (Xocfg_cache_itm)vals.Get_by(ctx);
if (rv == null) {
rv = new Xocfg_cache_itm(ctx, key, val);
vals.Add(ctx, rv);
}
else {
rv.Val_(val);
}
}
public void Add(String ctx, Xocfg_cache_itm itm) {
vals.Add(ctx, itm);
}
public void Sub(Gfo_evt_itm sub, String ctx, String evt) {
List_adp list = (List_adp)subs.Get_by(ctx);
if (list == null) {
list = List_adp_.New();
subs.Add(ctx, list);
}
list.Add(new Xocfg_cache_sub(sub, ctx, evt, key));
}
public void Pub(String ctx, String val) {
// exact match; EX: "en.w|key_1"
List_adp list = (List_adp)subs.Get_by(ctx);
if (list == null) {// global match; EX: "app|key_1"
list = (List_adp)subs.Get_by(ctx);
if (list == null) return;
}
Pub(list, val);
}
private void Pub(List_adp list, String val) {
int len = list.Len();
for (int i = 0; i < len; i++) {
Xocfg_cache_sub sub = (Xocfg_cache_sub)list.Get_at(i);
Gfo_evt_mgr_.Pub_val(sub.Sub(), sub.Evt(), val);
}
}
}

@ -0,0 +1,29 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa.addons.apps.cfgs.mgrs; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.apps.*; import gplx.xowa.addons.apps.cfgs.*;
class Xocfg_cache_itm {
public Xocfg_cache_itm(String ctx, String key, String val) {
this.ctx = ctx;
this.key = key;
this.val = val;
}
public String Ctx() {return ctx;} private final String ctx;
public String Key() {return key;} private final String key;
public String Val() {return val;} private String val;
public void Val_(String val) {this.val = val;}
}

@ -0,0 +1,68 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa.addons.apps.cfgs.mgrs; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.apps.*; import gplx.xowa.addons.apps.cfgs.*;
import gplx.dbs.*; import gplx.xowa.addons.apps.cfgs.dbs.*;
public class Xocfg_cache_mgr {
private final Hash_adp grps = Hash_adp_.New();
public Xocfg_cache_mgr(Db_conn conn) {
this.db_mgr = new Xocfg_db_mgr(conn);
}
public Xocfg_db_mgr Db_mgr() {return db_mgr;} private final Xocfg_db_mgr db_mgr;
public void Clear() {grps.Clear();}
public String Get(String ctx, String key) {
Xocfg_cache_grp grp = Grps__get_or_load(key);
return grp.Get(ctx);
}
public void Set(String ctx, String key, String val) {
Xocfg_cache_grp grp = Grps__get_or_load(key);
grp.Set(ctx, val);
grp.Pub(ctx, val);
}
public void Sub_many(Gfo_evt_itm sub, String ctx, String... evts) {
int len = evts.length;
for (int i = 0; i < len; i++) {
String evt = evts[i];
if (!String_.Has_at_bgn(evt, "Evt__")) throw Err_.new_wo_type("cfg:event must start with Evt__", "evt", evt);
Sub(sub, ctx, String_.Mid(evt, 5), evt);
}
}
public void Sub(Gfo_evt_itm sub, String ctx, String key, String evt) {
Xocfg_cache_grp grp = Grps__get_or_load(key);
grp.Sub(sub, ctx, evt);
}
private Xocfg_cache_grp Grps__get_or_load(String key) {
Xocfg_cache_grp grp = (Xocfg_cache_grp)grps.Get_by(key);
return grp == null ? Load_grp(key) : grp;
}
private Xocfg_cache_grp Load_grp(String key) {
// get data from db
Xoitm_meta_itm meta_itm = db_mgr.Tbl__itm_meta().Select_by_key_or_null(key);
if (meta_itm == null) throw Err_.new_wo_type("cfg:grp not found;", "key", key);
Xoitm_data_itm[] itms = db_mgr.Tbl__itm_data().Select_all_by_id(meta_itm.Id());
// make
Xocfg_cache_grp rv = new Xocfg_cache_grp(key, meta_itm.Dflt());
int len = itms.length;
for (int i = 0; i < len; i++) {
Xoitm_data_itm itm = itms[0];
String itm_ctx = itm.Ctx();
rv.Add(itm_ctx, new Xocfg_cache_itm(itm_ctx, key, itm.Val()));
}
return rv;
}
}

@ -0,0 +1,44 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa.addons.apps.cfgs.mgrs; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.apps.*; import gplx.xowa.addons.apps.cfgs.*;
import org.junit.*; import gplx.core.tests.*; import gplx.dbs.*; import gplx.xowa.addons.apps.cfgs.dbs.*;
public class Xocfg_cache_mgr__tst {
private final Xocfg_cache_mgr__fxt fxt = new Xocfg_cache_mgr__fxt();
@Test public void Basic() {
// fxt.Init__db_add("en.w", "key_1", "val_1");
// fxt.Test__get("en.w", "key_1", "val_1");
}
}
class Xocfg_cache_mgr__fxt {
private final Xocfg_cache_mgr mgr;
private final Xocfg_itm_bldr cfg_bldr;
public Xocfg_cache_mgr__fxt() {
gplx.dbs.Db_conn_bldr.Instance.Reg_default_mem();
Db_conn conn = Db_conn_bldr.Instance.Get_or_autocreate(true, Io_url_.new_any_("mem/xowa/wiki/en.wikipedia.org/"));
this.mgr = new Xocfg_cache_mgr(conn);
cfg_bldr = new Xocfg_itm_bldr(mgr.Db_mgr());
}
public void Init__db_add(String ctx, String key, String val) {
cfg_bldr.Create_grp("", "test_grp", "", "");
cfg_bldr.Create_itm("test_grp", key, "wiki", "textbox", "", "dflt", "", "");
mgr.Db_mgr().Set_str(ctx, key, val);
}
public void Test__get(String ctx, String key, String expd) {
Gftest.Eq__str(expd, mgr.Get(ctx, key));
}
}

@ -0,0 +1,30 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa.addons.apps.cfgs.mgrs; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.apps.*; import gplx.xowa.addons.apps.cfgs.*;
class Xocfg_cache_sub {
public Xocfg_cache_sub(Gfo_evt_itm sub, String ctx, String evt, String key) {
this.ctx = ctx;
this.key = key;
this.evt = evt;
this.sub = sub;
}
public String Ctx() {return ctx;} private final String ctx;
public String Key() {return key;} private final String key;
public String Evt() {return evt;} private final String evt;
public Gfo_evt_itm Sub() {return sub;} private final Gfo_evt_itm sub;
}
Loading…
Cancel
Save