mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
Source: Restore broken commit
This commit is contained in:
54
400_xowa/src/gplx/dbs/Db_diff_bldr.java
Normal file
54
400_xowa/src/gplx/dbs/Db_diff_bldr.java
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
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.dbs; import gplx.*;
|
||||
import gplx.dbs.metas.*; import gplx.dbs.sqls.*; import gplx.dbs.sqls.wtrs.*;
|
||||
class Db_diff_bldr {
|
||||
private final Bry_bfr bfr = Bry_bfr_.New();
|
||||
private final Sql_schema_wtr sql_bldr = new Sql_schema_wtr();
|
||||
public Db_diff_bldr() {sql_bldr.Bfr_(bfr);}
|
||||
public String Compare_db(String src_str, String trg_str) {
|
||||
// Io_url src_url = Io_url_.new_fil_(src_str);
|
||||
// Io_url trg_url = Io_url_.new_fil_(trg_str);
|
||||
// Db_conn src_conn = Db_conn_bldr.Instance.Get_or_new(src_url).Conn();
|
||||
// Db_conn trg_conn = Db_conn_bldr.Instance.Get_or_new(trg_url).Conn();
|
||||
Dbmeta_tbl_mgr src_tbls = new Dbmeta_tbl_mgr(Dbmeta_reload_cmd_.Noop);
|
||||
Dbmeta_tbl_mgr trg_tbls = new Dbmeta_tbl_mgr(Dbmeta_reload_cmd_.Noop);
|
||||
return Compare_tbls(src_tbls, trg_tbls);
|
||||
}
|
||||
public String Compare_tbls(Dbmeta_tbl_mgr src_tbls, Dbmeta_tbl_mgr trg_tbls) {
|
||||
int src_len = src_tbls.Len();
|
||||
for (int i = 0; i < src_len; ++i) {
|
||||
Dbmeta_tbl_itm src_tbl = src_tbls.Get_at(i);
|
||||
Dbmeta_tbl_itm trg_tbl = trg_tbls.Get_by(src_tbl.Name());
|
||||
if (trg_tbl == null) Tbl_delete(src_tbl);
|
||||
}
|
||||
int trg_len = trg_tbls.Len();
|
||||
for (int i = 0; i < trg_len; ++i) {
|
||||
Dbmeta_tbl_itm trg_tbl = src_tbls.Get_at(i);
|
||||
Dbmeta_tbl_itm src_tbl = trg_tbls.Get_by(trg_tbl.Name());
|
||||
if (src_tbl == null) Tbl_create(trg_tbl);
|
||||
}
|
||||
return bfr.To_str_and_clear();
|
||||
}
|
||||
private void Tbl_delete(Dbmeta_tbl_itm tbl) {
|
||||
bfr.Add_str_a7("DROP TABLE ").Add_str_u8(tbl.Name()).Add_byte_nl();
|
||||
}
|
||||
private void Tbl_create(Dbmeta_tbl_itm tbl) {
|
||||
// sql_bldr.Bld_create_tbl(tbl);
|
||||
}
|
||||
}
|
||||
88
400_xowa/src/gplx/dbs/bulks/Db_bulk_exec_.java
Normal file
88
400_xowa/src/gplx/dbs/bulks/Db_bulk_exec_.java
Normal file
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
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.dbs.bulks; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.dbs.metas.*;
|
||||
public class Db_bulk_exec_ {
|
||||
public static void Insert(Db_bulk_prog prog_wkr, String msg, Dbmeta_fld_itm[] flds, Db_rdr src, Db_stmt trg, Db_conn trg_conn) {
|
||||
// init
|
||||
int flds_len = flds.length;
|
||||
String[] fld_names = Db_bulk_exec_utl_.To_fld_names(flds, flds_len);
|
||||
int[] fld_types = Db_bulk_exec_utl_.To_fld_types(flds, flds_len);
|
||||
|
||||
// loop all rows
|
||||
Gfo_log_.Instance.Prog(msg);
|
||||
trg_conn.Txn_bgn("bulk_insert");
|
||||
try {
|
||||
while (src.Move_next()) {
|
||||
// fill insert
|
||||
trg.Clear();
|
||||
int row_size = 0;
|
||||
for (int i = 0; i < flds_len; ++i) {
|
||||
String fld_name = fld_names[i];
|
||||
switch (fld_types[i]) {
|
||||
case Dbmeta_fld_tid.Tid__bool : trg.Val_bool_as_byte (fld_name, src.Read_bool_by_byte (fld_name)); row_size += 1; break;
|
||||
case Dbmeta_fld_tid.Tid__byte : trg.Val_byte (fld_name, src.Read_byte (fld_name)); row_size += 1; break;
|
||||
case Dbmeta_fld_tid.Tid__int : trg.Val_int (fld_name, src.Read_int (fld_name)); row_size += 4; break;
|
||||
case Dbmeta_fld_tid.Tid__long : trg.Val_long (fld_name, src.Read_long (fld_name)); row_size += 8; break;
|
||||
case Dbmeta_fld_tid.Tid__float : trg.Val_float (fld_name, src.Read_float (fld_name)); row_size += 4; break;
|
||||
case Dbmeta_fld_tid.Tid__double : trg.Val_double (fld_name, src.Read_double (fld_name)); row_size += 8; break;
|
||||
case Dbmeta_fld_tid.Tid__str : String src_str = src.Read_str(fld_name); trg.Val_str(fld_name, src_str); row_size += src_str == null ? 0 : String_.Len(src_str); break;
|
||||
case Dbmeta_fld_tid.Tid__bry : byte[] src_bry = src.Read_bry(fld_name); trg.Val_bry(fld_name, src_bry); row_size += src_bry == null ? 0 : src_bry.length; break;
|
||||
default : throw Err_.new_unhandled_default(fld_types[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// exec insert
|
||||
try {trg.Exec_insert();}
|
||||
catch (Exception e) {throw Db_bulk_exec_utl_.New_err(e, src, flds_len, fld_names, fld_types);}
|
||||
|
||||
// commit and notify if applicable
|
||||
if (prog_wkr.Prog__insert_and_stop_if_suspended(row_size)) break;
|
||||
}
|
||||
}
|
||||
catch (Exception e) {throw Err_.new_wo_type("dbs.bulk:insert failed", "err", e);}
|
||||
finally {
|
||||
trg_conn.Txn_end();
|
||||
}
|
||||
}
|
||||
public static final String Invk__bulk_insert_err = "bulk.insert.err", Invk__bulk_insert_prog = "bulk.insert.prog";
|
||||
}
|
||||
class Db_bulk_exec_utl_ {
|
||||
public static String[] To_fld_names(Dbmeta_fld_itm[] flds, int flds_len) {
|
||||
String[] rv = new String[flds_len];
|
||||
for (int i = 0; i < flds_len; ++i)
|
||||
rv[i] = flds[i].Name();
|
||||
return rv;
|
||||
}
|
||||
public static int[] To_fld_types(Dbmeta_fld_itm[] flds, int flds_len) {
|
||||
int[] rv = new int[flds_len];
|
||||
for (int i = 0; i < flds_len; ++i)
|
||||
rv[i] = flds[i].Type().Tid_ansi();
|
||||
return rv;
|
||||
}
|
||||
public static Err New_err(Exception e, Db_rdr rdr, int flds_len, String[] fld_names, int[] fld_types) {
|
||||
Object[] args = new Object[(flds_len * 2) + 2];
|
||||
for (int i = 0; i < flds_len; i += 2) {
|
||||
args[i ] = fld_names[i];
|
||||
args[i + 1] = rdr.Read_at(i);
|
||||
}
|
||||
args[flds_len - 2] = "err";
|
||||
args[flds_len - 1] = Err_.Message_gplx_log(e);
|
||||
return Err_.new_wo_type("dbs.bulk:insert row failed", args);
|
||||
}
|
||||
}
|
||||
21
400_xowa/src/gplx/dbs/bulks/Db_bulk_prog.java
Normal file
21
400_xowa/src/gplx/dbs/bulks/Db_bulk_prog.java
Normal file
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
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.dbs.bulks; import gplx.*; import gplx.dbs.*;
|
||||
public interface Db_bulk_prog {
|
||||
boolean Prog__insert_and_stop_if_suspended(int row_size);
|
||||
}
|
||||
56
400_xowa/src/gplx/dbs/bulks/Db_tbl_copy.java
Normal file
56
400_xowa/src/gplx/dbs/bulks/Db_tbl_copy.java
Normal file
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
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.dbs.bulks; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.dbs.*; import gplx.dbs.metas.*;
|
||||
public class Db_tbl_copy {
|
||||
private final Bry_bfr bfr = Bry_bfr_.New();
|
||||
private final Db_attach_mgr attach_mgr = new Db_attach_mgr();
|
||||
public void Copy_many(Db_conn src_conn, Db_conn trg_conn, String... tbl_names) {
|
||||
for (String tbl_name : tbl_names)
|
||||
Copy_one(src_conn, trg_conn, tbl_name, tbl_name);
|
||||
}
|
||||
public void Copy_one(Db_conn src_conn, Db_conn trg_conn, String src_tbl, String trg_tbl) {
|
||||
Dbmeta_tbl_itm tbl = src_conn.Meta_mgr().Get_by(src_tbl); if (tbl == null) throw Err_.new_wo_type("tbl does not exist", "tbl_name", src_tbl);
|
||||
trg_conn.Meta_tbl_remake(Dbmeta_tbl_itm.New(trg_tbl, tbl.Flds().To_ary(), tbl.Idxs().To_ary()));
|
||||
|
||||
// do copy
|
||||
attach_mgr.Conn_main_(trg_conn).Conn_links_(new Db_attach_itm("src_db", src_conn));
|
||||
attach_mgr.Exec_sql(Bld_sql(tbl, src_tbl, trg_tbl));
|
||||
}
|
||||
public String Bld_sql(Dbmeta_tbl_itm tbl, String src_tbl, String trg_tbl) {
|
||||
Dbmeta_fld_mgr flds = tbl.Flds();
|
||||
int flds_len = flds.Len();
|
||||
bfr.Add_str_a7("INSERT INTO ").Add_str_a7(trg_tbl).Add_byte_nl();
|
||||
bfr.Add_byte(Byte_ascii.Paren_bgn);
|
||||
for (int i = 0; i < flds_len; ++i) {
|
||||
Dbmeta_fld_itm fld = flds.Get_at(i);
|
||||
if (i != 0) bfr.Add_str_a7(", ");
|
||||
bfr.Add_str_a7(fld.Name());
|
||||
}
|
||||
bfr.Add_byte(Byte_ascii.Paren_end).Add_byte_nl();
|
||||
bfr.Add_str_a7("SELECT").Add_byte_nl().Add_byte_space();
|
||||
for (int i = 0; i < flds_len; ++i) {
|
||||
Dbmeta_fld_itm fld = flds.Get_at(i);
|
||||
if (i != 0) bfr.Add_str_a7(", ");
|
||||
bfr.Add_str_a7(fld.Name());
|
||||
}
|
||||
bfr.Add_byte_nl();
|
||||
bfr.Add_str_a7("FROM <src_db>").Add_str_a7(src_tbl);
|
||||
return bfr.To_str_and_clear();
|
||||
}
|
||||
}
|
||||
39
400_xowa/src/gplx/dbs/bulks/Db_tbl_copy_tst.java
Normal file
39
400_xowa/src/gplx/dbs/bulks/Db_tbl_copy_tst.java
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
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.dbs.bulks; import gplx.*; import gplx.dbs.*;
|
||||
import org.junit.*; import gplx.core.tests.*; import gplx.dbs.metas.*;
|
||||
public class Db_tbl_copy_tst {
|
||||
private final Db_tbl_copy_fxt fxt = new Db_tbl_copy_fxt();
|
||||
@Test public void Basic() {
|
||||
fxt.Test__bld_sql(fxt.Make_tbl("tbl_1", Dbmeta_fld_itm.new_int("fld_1"), Dbmeta_fld_itm.new_int("fld_2")),
|
||||
String_.Concat_lines_nl_skip_last
|
||||
( "INSERT INTO trg"
|
||||
, "(fld_1, fld_2)"
|
||||
, "SELECT"
|
||||
, " fld_1, fld_2"
|
||||
, "FROM <src_db>src"
|
||||
));
|
||||
}
|
||||
}
|
||||
class Db_tbl_copy_fxt {
|
||||
private final Db_tbl_copy mgr = new Db_tbl_copy();
|
||||
public Dbmeta_tbl_itm Make_tbl(String name, Dbmeta_fld_itm... flds) {return Dbmeta_tbl_itm.New(name, flds);}
|
||||
public void Test__bld_sql(Dbmeta_tbl_itm tbl, String expd) {
|
||||
Gftest.Eq__ary__lines(expd, mgr.Bld_sql(tbl, "src", "trg"), "sql");
|
||||
}
|
||||
}
|
||||
34
400_xowa/src/gplx/dbs/cfgs/Db_cfg_hash.java
Normal file
34
400_xowa/src/gplx/dbs/cfgs/Db_cfg_hash.java
Normal file
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
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.dbs.cfgs; import gplx.*; import gplx.dbs.*;
|
||||
public class Db_cfg_hash {
|
||||
private final String grp; private final Ordered_hash hash = Ordered_hash_.New();
|
||||
public Db_cfg_hash(String grp) {this.grp = grp;}
|
||||
public int Len() {return hash.Count();}
|
||||
public Db_cfg_itm Get_at(int i) {return (Db_cfg_itm)hash.Get_at(i);}
|
||||
public Db_cfg_itm Get_by(String key) {
|
||||
Db_cfg_itm rv = (Db_cfg_itm)hash.Get_by(key);
|
||||
return rv == null ? Db_cfg_itm.Empty : rv;
|
||||
}
|
||||
public void Set(String key, String val) {hash.Del(key); Add(key, val);}
|
||||
public void Add(String key, String val) {
|
||||
if (hash.Has(key)) throw Err_.new_wo_type("itm exists", "grp", grp, "key", key);
|
||||
Db_cfg_itm itm = new Db_cfg_itm(grp, key, val);
|
||||
hash.Add(key, itm);
|
||||
}
|
||||
}
|
||||
57
400_xowa/src/gplx/dbs/cfgs/Db_cfg_itm.java
Normal file
57
400_xowa/src/gplx/dbs/cfgs/Db_cfg_itm.java
Normal file
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
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.dbs.cfgs; import gplx.*; import gplx.dbs.*;
|
||||
public class Db_cfg_itm {
|
||||
public Db_cfg_itm(String grp, String key, String val) {this.grp = grp; this.key = key; this.val = val;}
|
||||
public String Grp() {return grp;} private final String grp;
|
||||
public String Key() {return key;} private final String key;
|
||||
public String Val() {return val;} public Db_cfg_itm Val_(String v) {val = v; return this;} private String val;
|
||||
public String To_str_or(String or) {return val == null ? or : val;}
|
||||
public byte[] To_bry_or(byte[] or) {try {return val == null ? or : Bry_.new_u8(val) ;} catch (Exception e) {throw err_parse(e, Bry_.Cls_val_name);}}
|
||||
public int To_int_or(int or) {try {return val == null ? or : Int_.parse_or(val, or) ;} catch (Exception e) {throw err_parse(e, Int_.Cls_val_name);}}
|
||||
public long To_long_or(long or) {try {return val == null ? or : Long_.parse_or(val, or) ;} catch (Exception e) {throw err_parse(e, Long_.Cls_val_name);}}
|
||||
public byte To_byte_or(byte or) {try {return val == null ? or : Byte_.parse_or(val, or) ;} catch (Exception e) {throw err_parse(e, Byte_.Cls_val_name);}}
|
||||
public boolean To_yn_or_n() {return To_yn_or(Bool_.N);}
|
||||
public boolean To_yn_or(boolean or) {try {return val == null ? or : Yn.parse_by_char_or(val, or);} catch (Exception e) {throw err_parse(e, Bool_.Cls_val_name);}}
|
||||
public DateAdp To_date_or(DateAdp or) {try {return val == null ? or : DateAdp_.parse_gplx(val) ;} catch (Exception e) {throw err_parse(e, DateAdp_.Cls_ref_name);}}
|
||||
public Guid_adp To_guid_or(Guid_adp or) {try {return val == null ? or : Guid_adp_.Parse(val) ;} catch (Exception e) {throw err_parse(e, Guid_adp_.Cls_ref_name);}}
|
||||
public boolean To_bool() {Fail_if_null(); try {return Yn.parse(val) ;} catch (Exception e) {throw err_parse(e, Bool_.Cls_val_name);}}
|
||||
public byte To_byte() {Fail_if_null(); try {return Byte_.parse(val) ;} catch (Exception e) {throw err_parse(e, Byte_.Cls_val_name);}}
|
||||
public int To_int() {Fail_if_null(); try {return Int_.parse(val) ;} catch (Exception e) {throw err_parse(e, Int_.Cls_val_name);}}
|
||||
public String To_str() {Fail_if_null(); return val;}
|
||||
private void Fail_if_null() {if (val == null) throw Err_.new_wo_type("cfg.val is empty", "grp", grp, "key", key); }
|
||||
private Err err_parse(Exception e, String type) {return Err_.new_wo_type("cfg.val is not parseable", "grp", grp, "key", key, "val", val, "type", type).Trace_ignore_add_1_();}
|
||||
private static final String Grp_none = "";
|
||||
public static Db_cfg_itm new_str (String key, String val) {return new Db_cfg_itm(Grp_none , key, val);}
|
||||
public static Db_cfg_itm new_str (String grp, String key, String val) {return new Db_cfg_itm(grp , key, val);}
|
||||
public static Db_cfg_itm new_bry (String key, byte[] val) {return new Db_cfg_itm(Grp_none , key, String_.new_u8(val));}
|
||||
public static Db_cfg_itm new_bry (String grp, String key, byte[] val) {return new Db_cfg_itm(grp , key, String_.new_u8(val));}
|
||||
public static Db_cfg_itm new_int (String key, int val) {return new Db_cfg_itm(Grp_none , key, Int_.To_str(val));}
|
||||
public static Db_cfg_itm new_int (String grp, String key, int val) {return new Db_cfg_itm(grp , key, Int_.To_str(val));}
|
||||
public static Db_cfg_itm new_long (String key, long val) {return new Db_cfg_itm(Grp_none , key, Long_.To_str(val));}
|
||||
public static Db_cfg_itm new_long (String grp, String key, long val) {return new Db_cfg_itm(grp , key, Long_.To_str(val));}
|
||||
public static Db_cfg_itm new_byte (String key, byte val) {return new Db_cfg_itm(Grp_none , key, Byte_.To_str(val));}
|
||||
public static Db_cfg_itm new_byte (String grp, String key, byte val) {return new Db_cfg_itm(grp , key, Byte_.To_str(val));}
|
||||
public static Db_cfg_itm new_yn (String key, boolean val) {return new Db_cfg_itm(Grp_none , key, Yn.To_str(val));}
|
||||
public static Db_cfg_itm new_yn (String grp, String key, boolean val) {return new Db_cfg_itm(grp , key, Yn.To_str(val));}
|
||||
public static Db_cfg_itm new_DateAdp (String key, DateAdp val) {return new Db_cfg_itm(Grp_none , key, val.XtoStr_fmt_yyyyMMdd_HHmmss());}
|
||||
public static Db_cfg_itm new_DateAdp (String grp, String key, DateAdp val) {return new Db_cfg_itm(grp , key, val.XtoStr_fmt_yyyyMMdd_HHmmss());}
|
||||
public static Db_cfg_itm new_guid (String key, Guid_adp val) {return new Db_cfg_itm(Grp_none , key, val.To_str());}
|
||||
public static Db_cfg_itm new_guid (String grp, String key, Guid_adp val) {return new Db_cfg_itm(grp , key, val.To_str());}
|
||||
public static final Db_cfg_itm Empty = new Db_cfg_itm("empty", "empty", null);
|
||||
}
|
||||
139
400_xowa/src/gplx/dbs/cfgs/Db_cfg_tbl.java
Normal file
139
400_xowa/src/gplx/dbs/cfgs/Db_cfg_tbl.java
Normal file
@@ -0,0 +1,139 @@
|
||||
/*
|
||||
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.dbs.cfgs; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.core.primitives.*;
|
||||
public class Db_cfg_tbl implements Db_tbl {
|
||||
private final String tbl_name; private final Dbmeta_fld_list flds = new Dbmeta_fld_list();
|
||||
private final String fld_grp, fld_key, fld_val;
|
||||
private Db_stmt stmt_insert, stmt_update, stmt_select;
|
||||
public Db_conn Conn() {return conn;} private final Db_conn conn;
|
||||
public Db_cfg_tbl(Db_conn conn, String tbl_name) {
|
||||
this.conn = conn; this.tbl_name = tbl_name;
|
||||
this.fld_grp = flds.Add_str("cfg_grp", 255);
|
||||
this.fld_key = flds.Add_str("cfg_key", 255);
|
||||
this.fld_val = flds.Add_str("cfg_val", 1024);
|
||||
conn.Rls_reg(this);
|
||||
}
|
||||
public void Rls() {
|
||||
stmt_insert = Db_stmt_.Rls(stmt_insert);
|
||||
stmt_update = Db_stmt_.Rls(stmt_update);
|
||||
stmt_select = Db_stmt_.Rls(stmt_select);
|
||||
}
|
||||
public String Tbl_name() {return tbl_name;}
|
||||
public void Create_tbl() {conn.Meta_tbl_create(Dbmeta_tbl_itm.New(tbl_name, flds, Dbmeta_idx_itm.new_unique_by_tbl(tbl_name, "main", fld_grp, fld_key, fld_val)));}
|
||||
public void Delete_val(String grp, String key) {conn.Stmt_delete(tbl_name, fld_grp, fld_key).Crt_str(fld_grp, grp).Crt_str(fld_key, key).Exec_delete();}
|
||||
public void Delete_grp(String grp) {conn.Stmt_delete(tbl_name, fld_grp).Crt_str(fld_grp, grp).Exec_delete();}
|
||||
public void Delete_all() {conn.Stmt_delete(tbl_name, Dbmeta_fld_itm.Str_ary_empty).Exec_delete();}
|
||||
public void Insert_yn (String grp, String key, boolean val) {Insert_str(grp, key, val ? "y" : "n");}
|
||||
public void Insert_byte (String grp, String key, byte val) {Insert_str(grp, key, Byte_.To_str(val));}
|
||||
public void Insert_int (String grp, String key, int val) {Insert_str(grp, key, Int_.To_str(val));}
|
||||
public void Insert_long (String grp, String key, long val) {Insert_str(grp, key, Long_.To_str(val));}
|
||||
public void Insert_date (String grp, String key, DateAdp val) {Insert_str(grp, key, val.XtoStr_fmt_yyyyMMdd_HHmmss());}
|
||||
public void Insert_guid (String grp, String key, Guid_adp val) {Insert_str(grp, key, val.To_str());}
|
||||
public void Insert_bry (String grp, String key, byte[] val) {Insert_str(grp, key, String_.new_u8(val));}
|
||||
public void Insert_str (String grp, String key, String val) {
|
||||
if (stmt_insert == null) stmt_insert = conn.Stmt_insert(tbl_name, flds);
|
||||
try {
|
||||
stmt_insert.Clear().Val_str(fld_grp, grp).Val_str(fld_key, key).Val_str(fld_val, val).Exec_insert();
|
||||
} catch (Exception e) {throw Err_.new_exc(e, "db", "db_cfg.insert failed", "grp", grp, "key", key, "val", val, "db", conn.Conn_info().Db_api());}
|
||||
}
|
||||
public void Update_yn (String grp, String key, boolean val) {Update_str(grp, key, val ? "y" : "n");}
|
||||
public void Update_byte (String grp, String key, byte val) {Update_str(grp, key, Byte_.To_str(val));}
|
||||
public void Update_int (String grp, String key, int val) {Update_str(grp, key, Int_.To_str(val));}
|
||||
public void Update_long (String grp, String key, long val) {Update_str(grp, key, Long_.To_str(val));}
|
||||
public void Update_date (String grp, String key, DateAdp val) {Update_str(grp, key, val.XtoStr_fmt_yyyyMMdd_HHmmss());}
|
||||
public void Update_guid (String grp, String key, Guid_adp val) {Update_str(grp, key, val.To_str());}
|
||||
public void Update_bry (String grp, String key, byte[] val) {Update_str(grp, key, String_.new_u8(val));}
|
||||
public void Update_str (String grp, String key, String val) {
|
||||
if (stmt_update == null) stmt_update = conn.Stmt_update_exclude(tbl_name, flds, fld_grp, fld_key);
|
||||
stmt_update.Clear().Val_str(fld_val, val).Crt_str(fld_grp, grp).Crt_str(fld_key, key).Exec_update();
|
||||
}
|
||||
public void Upsert_yn (String grp, String key, boolean val) {Upsert_str(grp, key, val ? "y" : "n");}
|
||||
public void Upsert_int (String grp, String key, int val) {Upsert_str(grp, key, Int_.To_str(val));}
|
||||
public void Upsert_date (String grp, String key, DateAdp val) {Upsert_str(grp, key, val.XtoStr_fmt_yyyyMMdd_HHmmss());}
|
||||
public void Upsert_guid (String grp, String key, Guid_adp val) {Upsert_str(grp, key, val.To_str());}
|
||||
public void Upsert_bry (String grp, String key, byte[] val) {Upsert_str(grp, key, String_.new_u8(val));}
|
||||
public void Upsert_str (String grp, String key, String val) {
|
||||
String cur_val = this.Select_str_or(grp, key, null);
|
||||
if (cur_val == null) this.Insert_str(grp, key, val);
|
||||
else this.Update_str(grp, key, val);
|
||||
}
|
||||
public boolean Select_yn (String grp, String key) {String val = Select_str(grp, key); return Parse_yn (grp, key, val);}
|
||||
public byte Select_byte (String grp, String key) {String val = Select_str(grp, key); return Parse_byte (grp, key, val);}
|
||||
public int Select_int (String grp, String key) {String val = Select_str(grp, key); return Parse_int (grp, key, val);}
|
||||
public long Select_long (String grp, String key) {String val = Select_str(grp, key); return Parse_long (grp, key, val);}
|
||||
public byte[] Select_bry (String grp, String key) {String val = Select_str(grp, key); return Parse_bry (grp, key, val);}
|
||||
public DateAdp Select_date (String grp, String key) {String val = Select_str(grp, key); return Parse_date (grp, key, val);}
|
||||
public Guid_adp Select_guid (String grp, String key) {String val = Select_str(grp, key); return Parse_guid (grp, key, val);}
|
||||
public boolean Select_yn_or (String grp, String key, boolean or) {String val = Select_str_or(grp, key, null) ; return val == null ? or : Parse_yn (grp, key, val);}
|
||||
public byte Select_byte_or (String grp, String key, byte or) {String val = Select_str_or(grp, key, null) ; return val == null ? or : Parse_byte (grp, key, val);}
|
||||
public int Select_int_or (String grp, String key, int or) {String val = Select_str_or(grp, key, null) ; return val == null ? or : Parse_int (grp, key, val);}
|
||||
public long Select_long_or (String grp, String key, long or) {String val = Select_str_or(grp, key, null) ; return val == null ? or : Parse_long (grp, key, val);}
|
||||
public byte[] Select_bry_or (String grp, String key, byte[] or) {String val = Select_str_or(grp, key, null) ; return val == null ? or : Parse_bry (grp, key, val);}
|
||||
public DateAdp Select_date_or (String grp, String key, DateAdp or) {String val = Select_str_or(grp, key, null) ; return val == null ? or : Parse_date (grp, key, val);}
|
||||
public Guid_adp Select_guid_or (String grp, String key, Guid_adp or) {String val = Select_str_or(grp, key, null) ; return val == null ? or : Parse_guid (grp, key, val);}
|
||||
public String Select_str (String grp, String key) {
|
||||
String rv = Select_str_or(grp, key, null); if (rv == null) throw Err_.new_wo_type("cfg.missing", "grp", grp, "key", key);
|
||||
return rv;
|
||||
}
|
||||
public String Select_str_or (String grp, String key, String or) {
|
||||
if (stmt_select == null) stmt_select = conn.Stmt_select(tbl_name, String_.Ary(fld_val), fld_grp, fld_key);
|
||||
Db_rdr rdr = stmt_select.Clear().Crt_str(fld_grp, grp).Crt_str(fld_key, key).Exec_select__rls_manual();
|
||||
try {return rdr.Move_next() ? rdr.Read_str(fld_val) : or;} finally {rdr.Rls();}
|
||||
}
|
||||
public Db_cfg_hash Select_as_hash(String grp) {
|
||||
Db_cfg_hash rv = new Db_cfg_hash(grp);
|
||||
Db_rdr rdr = conn.Stmt_select(tbl_name, flds, fld_grp).Crt_str(fld_grp, grp).Exec_select__rls_auto();
|
||||
try {
|
||||
while (rdr.Move_next()) {
|
||||
rv.Add(rdr.Read_str(fld_key), rdr.Read_str(fld_val));
|
||||
}
|
||||
}
|
||||
finally {rdr.Rls();}
|
||||
return rv;
|
||||
}
|
||||
public void Select_as_hash_bry(Hash_adp_bry rv, String grp) {
|
||||
rv.Clear();
|
||||
Db_rdr rdr = conn.Stmt_select(tbl_name, flds, fld_grp).Crt_str(fld_grp, grp).Exec_select__rls_auto();
|
||||
try {
|
||||
while (rdr.Move_next()) {
|
||||
rv.Add(rdr.Read_bry_by_str(fld_key), rdr.Read_bry_by_str(fld_val));
|
||||
}
|
||||
}
|
||||
finally {rdr.Rls();}
|
||||
}
|
||||
// NOTE: Assert guarantees that a value exists in database and returns it (Select + Insert); (1) String val = Assert('grp', 'key', 'val'); (2) Update('grp', 'key', 'val2');
|
||||
public boolean Assert_yn (String grp, String key, boolean or) {String val = Select_str_or(grp, key, null) ; if (val == null) {Insert_yn (grp, key, or); return or;} return Parse_yn (grp, key, val);}
|
||||
public byte Assert_byte (String grp, String key, byte or) {String val = Select_str_or(grp, key, null) ; if (val == null) {Insert_byte (grp, key, or); return or;} return Parse_byte (grp, key, val);}
|
||||
public int Assert_int (String grp, String key, int or) {String val = Select_str_or(grp, key, null) ; if (val == null) {Insert_int (grp, key, or); return or;} return Parse_int (grp, key, val);}
|
||||
public long Assert_long (String grp, String key, long or) {String val = Select_str_or(grp, key, null) ; if (val == null) {Insert_long (grp, key, or); return or;} return Parse_long (grp, key, val);}
|
||||
public byte[] Assert_bry (String grp, String key, byte[] or) {String val = Select_str_or(grp, key, null) ; if (val == null) {Insert_bry (grp, key, or); return or;} return Parse_bry (grp, key, val);}
|
||||
public DateAdp Assert_date (String grp, String key, DateAdp or) {String val = Select_str_or(grp, key, null) ; if (val == null) {Insert_date (grp, key, or); return or;} return Parse_date (grp, key, val);}
|
||||
public Guid_adp Assert_guid (String grp, String key, Guid_adp or) {String val = Select_str_or(grp, key, null) ; if (val == null) {Insert_guid (grp, key, or); return or;} return Parse_guid (grp, key, val);}
|
||||
public String Assert_str (String grp, String key, String or) {String val = Select_str_or(grp, key, null) ; if (val == null) {Insert_str (grp, key, or); return or;} return val;}
|
||||
private boolean Parse_yn (String grp, String key, String val) {try {return Yn.parse(val) ;} catch (Exception e) {throw err_parse(e, grp, key, val, Bool_.Cls_val_name);}}
|
||||
private byte Parse_byte (String grp, String key, String val) {try {return Byte_.parse(val) ;} catch (Exception e) {throw err_parse(e, grp, key, val, Byte_.Cls_val_name);}}
|
||||
private int Parse_int (String grp, String key, String val) {try {return Int_.parse(val) ;} catch (Exception e) {throw err_parse(e, grp, key, val, Int_.Cls_val_name);}}
|
||||
private long Parse_long (String grp, String key, String val) {try {return Long_.parse(val) ;} catch (Exception e) {throw err_parse(e, grp, key, val, Long_.Cls_val_name);}}
|
||||
private byte[] Parse_bry (String grp, String key, String val) {try {return Bry_.new_u8(val) ;} catch (Exception e) {throw err_parse(e, grp, key, val, Bry_.Cls_val_name);}}
|
||||
private DateAdp Parse_date (String grp, String key, String val) {try {return DateAdp_.parse_gplx(val) ;} catch (Exception e) {throw err_parse(e, grp, key, val, DateAdp_.Cls_ref_name);}}
|
||||
private Guid_adp Parse_guid (String grp, String key, String val) {try {return Guid_adp_.Parse(val) ;} catch (Exception e) {throw err_parse(e, grp, key, val, Guid_adp_.Cls_ref_name);}}
|
||||
private Err err_parse(Exception e, String grp, String key, String val, String type) {return Err_.new_exc(e, "db", "cfg.val is not parseable", "grp", grp, "key", key, "val", val, "type", type);}
|
||||
|
||||
public static Db_cfg_tbl Get_by_key(Db_tbl_owner owner, String key) {return (Db_cfg_tbl)owner.Tbls__get_by_key(key);}
|
||||
}
|
||||
28
400_xowa/src/gplx/dbs/metas/Schema_db_mgr.java
Normal file
28
400_xowa/src/gplx/dbs/metas/Schema_db_mgr.java
Normal file
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
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.dbs.metas; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.dbs.metas.updates.*;
|
||||
public class Schema_db_mgr {
|
||||
public Schema_loader_mgr Loader() {return loader;} public void Loader_(Schema_loader_mgr v) {loader = v;} private Schema_loader_mgr loader;
|
||||
public Schema_update_mgr Updater() {return updater;} private final Schema_update_mgr updater = new Schema_update_mgr();
|
||||
public Dbmeta_tbl_mgr Tbl_mgr() {return tbl_mgr;} private final Dbmeta_tbl_mgr tbl_mgr = new Dbmeta_tbl_mgr(Dbmeta_reload_cmd_.Noop);
|
||||
public void Init(Db_conn conn) {
|
||||
loader.Load(this, conn);
|
||||
updater.Update(this, conn);
|
||||
}
|
||||
}
|
||||
21
400_xowa/src/gplx/dbs/metas/Schema_loader_mgr.java
Normal file
21
400_xowa/src/gplx/dbs/metas/Schema_loader_mgr.java
Normal file
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
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.dbs.metas; import gplx.*; import gplx.dbs.*;
|
||||
public interface Schema_loader_mgr {
|
||||
void Load(Schema_db_mgr db_mgr, Db_conn conn);
|
||||
}
|
||||
50
400_xowa/src/gplx/dbs/metas/Schema_loader_mgr_.java
Normal file
50
400_xowa/src/gplx/dbs/metas/Schema_loader_mgr_.java
Normal file
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
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.dbs.metas; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.dbs.qrys.*;
|
||||
public class Schema_loader_mgr_ {
|
||||
public static final Schema_loader_mgr Null = new Schema_loader_mgr__null();
|
||||
public static final Schema_loader_mgr Sqlite = new Schema_loader_mgr__sqlite();
|
||||
}
|
||||
class Schema_loader_mgr__null implements Schema_loader_mgr {
|
||||
public void Load(Schema_db_mgr db_mgr, Db_conn conn) {}
|
||||
}
|
||||
class Schema_loader_mgr__sqlite implements Schema_loader_mgr {
|
||||
public void Load(Schema_db_mgr db_mgr, Db_conn conn) {
|
||||
Gfo_usr_dlg_.Instance.Log_many("", "", "db.schema.load.bgn: conn=~{0}", conn.Conn_info().Db_api());
|
||||
Dbmeta_tbl_mgr tbl_mgr = db_mgr.Tbl_mgr();
|
||||
Db_qry__select_in_tbl qry = Db_qry__select_in_tbl.new_("sqlite_master", String_.Ary_empty, String_.Ary("type", "name", "sql"), Db_qry__select_in_tbl.Order_by_null);
|
||||
Db_rdr rdr = conn.Stmt_new(qry).Exec_select__rls_auto();
|
||||
try {
|
||||
while (rdr.Move_next()) {
|
||||
String type_str = rdr.Read_str("type");
|
||||
String name = rdr.Read_str("name");
|
||||
int type_int = Dbmeta_itm_tid.Xto_int(type_str);
|
||||
switch (type_int) {
|
||||
case Dbmeta_itm_tid.Tid_table:
|
||||
Dbmeta_tbl_itm tbl_itm = Dbmeta_tbl_itm.New(name);
|
||||
tbl_mgr.Add(tbl_itm);
|
||||
break;
|
||||
case Dbmeta_itm_tid.Tid_index: break; // noop for now
|
||||
default: throw Err_.new_unhandled(type_str);
|
||||
}
|
||||
}
|
||||
} finally {rdr.Rls();}
|
||||
Gfo_usr_dlg_.Instance.Log_many("", "", "db.schema.load.end");
|
||||
}
|
||||
}
|
||||
23
400_xowa/src/gplx/dbs/metas/updates/Schema_update_cmd.java
Normal file
23
400_xowa/src/gplx/dbs/metas/updates/Schema_update_cmd.java
Normal file
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
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.dbs.metas.updates; import gplx.*; import gplx.dbs.*; import gplx.dbs.metas.*;
|
||||
public interface Schema_update_cmd {
|
||||
String Name();
|
||||
boolean Exec_is_done();
|
||||
void Exec(Schema_db_mgr mgr, Db_conn conn);
|
||||
}
|
||||
37
400_xowa/src/gplx/dbs/metas/updates/Schema_update_cmd_.java
Normal file
37
400_xowa/src/gplx/dbs/metas/updates/Schema_update_cmd_.java
Normal file
@@ -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.dbs.metas.updates; import gplx.*; import gplx.dbs.*; import gplx.dbs.metas.*;
|
||||
import gplx.dbs.engines.sqlite.*;
|
||||
public class Schema_update_cmd_ {
|
||||
public static Schema_update_cmd Make_tbl_create(String tbl_name, String tbl_sql, Db_idx_itm... tbl_idxs) {return new Schema_update_cmd__tbl_create(tbl_name, tbl_sql, tbl_idxs);}
|
||||
}
|
||||
class Schema_update_cmd__tbl_create implements Schema_update_cmd {
|
||||
private final String tbl_sql; private final Db_idx_itm[] tbl_idxs;
|
||||
public Schema_update_cmd__tbl_create(String tbl_name, String tbl_sql, Db_idx_itm... tbl_idxs) {
|
||||
this.tbl_name = tbl_name; this.tbl_sql = tbl_sql; this.tbl_idxs = tbl_idxs;
|
||||
}
|
||||
public String Name() {return "schema.tbl.create." + tbl_name;} private final String tbl_name;
|
||||
public boolean Exec_is_done() {return exec_is_done;} private boolean exec_is_done;
|
||||
public void Exec(Schema_db_mgr db_mgr, Db_conn conn) {
|
||||
if (db_mgr.Tbl_mgr().Has(tbl_name)) return;
|
||||
Gfo_usr_dlg_.Instance.Log_many("", "", "schema.tbl.create: tbl=~{0}", tbl_name);
|
||||
Sqlite_engine_.Tbl_create(conn, tbl_name, tbl_sql);
|
||||
Sqlite_engine_.Idx_create(conn, tbl_idxs);
|
||||
exec_is_done = true;
|
||||
}
|
||||
}
|
||||
32
400_xowa/src/gplx/dbs/metas/updates/Schema_update_mgr.java
Normal file
32
400_xowa/src/gplx/dbs/metas/updates/Schema_update_mgr.java
Normal file
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
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.dbs.metas.updates; import gplx.*; import gplx.dbs.*; import gplx.dbs.metas.*;
|
||||
public class Schema_update_mgr {
|
||||
private List_adp cmds = List_adp_.New();
|
||||
public void Add(Schema_update_cmd cmd) {cmds.Add(cmd);}
|
||||
public void Update(Schema_db_mgr schema_mgr, Db_conn conn) {
|
||||
int cmds_len = cmds.Count();
|
||||
for (int i = 0; i < cmds_len; ++i) {
|
||||
Schema_update_cmd cmd = (Schema_update_cmd)cmds.Get_at(i);
|
||||
try {cmd.Exec(schema_mgr, conn);}
|
||||
catch (Exception e) {
|
||||
Gfo_usr_dlg_.Instance.Warn_many("", "", "failed to run update cmd; name=~{0} err=~{1}", cmd.Name(), Err_.Message_gplx_full(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
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.dbs.metas.updates; import gplx.*; import gplx.dbs.*; import gplx.dbs.metas.*;
|
||||
import org.junit.*; import gplx.dbs.*;
|
||||
public class Schema_update_mgr_tst {
|
||||
@Before public void init() {fxt.Clear();} private Schema_update_mgr_fxt fxt = new Schema_update_mgr_fxt();
|
||||
@Test public void Create() {
|
||||
fxt.Test_exec_y(new Schema_update_cmd__mock());
|
||||
}
|
||||
@Test public void Delete() {
|
||||
fxt.Init_itm(Dbmeta_itm_tid.Tid_table, Schema_update_cmd__mock.Tbl_name);
|
||||
fxt.Test_exec_n(new Schema_update_cmd__mock());
|
||||
}
|
||||
}
|
||||
class Schema_update_mgr_fxt {
|
||||
private Schema_update_mgr update_mgr; private Schema_db_mgr db_mgr;
|
||||
public void Clear() {
|
||||
update_mgr = new Schema_update_mgr();
|
||||
db_mgr = new Schema_db_mgr();
|
||||
}
|
||||
public void Init_itm(int tid, String name) {
|
||||
db_mgr.Tbl_mgr().Add(Dbmeta_tbl_itm.New(name));
|
||||
}
|
||||
public void Test_exec_y(Schema_update_cmd cmd) {Test_exec(cmd, Bool_.Y);}
|
||||
public void Test_exec_n(Schema_update_cmd cmd) {Test_exec(cmd, Bool_.N);}
|
||||
private void Test_exec(Schema_update_cmd cmd, boolean expd) {
|
||||
update_mgr.Add(cmd);
|
||||
update_mgr.Update(db_mgr, Db_conn_.Noop);
|
||||
Tfds.Eq(expd, cmd.Exec_is_done());
|
||||
}
|
||||
}
|
||||
class Schema_update_cmd__mock implements Schema_update_cmd {
|
||||
public String Name() {return "xowa.user.cfg_regy.create";}
|
||||
public boolean Exec_is_done() {return exec_is_done;} private boolean exec_is_done;
|
||||
public void Exec(Schema_db_mgr mgr, Db_conn conn) {
|
||||
if (mgr.Tbl_mgr().Has(Tbl_name)) return;
|
||||
exec_is_done = true;
|
||||
}
|
||||
public static final String Tbl_name = "tbl_mock";
|
||||
}
|
||||
74
400_xowa/src/gplx/dbs/percentiles/Log_tbl_fmtr.java
Normal file
74
400_xowa/src/gplx/dbs/percentiles/Log_tbl_fmtr.java
Normal file
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
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.dbs.percentiles; import gplx.*; import gplx.dbs.*;
|
||||
class Log_tbl_fmtr {
|
||||
private final Bry_bfr bfr = Bry_bfr_.New();
|
||||
private final List_adp itms = List_adp_.New();
|
||||
private Log_fld_itm[] ary;
|
||||
public Log_tbl_fmtr Add_str(String key, int len) {ary = null; itms.Add(new Log_fld_itm__bry(Type_adp_.Tid__bry, key, len)); return this;}
|
||||
public Log_tbl_fmtr Add_int(String key, int bgn, int end) {ary = null; itms.Add(new Log_fld_itm__int(Type_adp_.Tid__int, key, bgn, end)); return this;}
|
||||
public void Log(Object... vals) {
|
||||
if (ary == null)
|
||||
ary = (Log_fld_itm[])itms.To_ary_and_clear(Log_fld_itm.class);
|
||||
int len = ary.length;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Log_fld_itm itm = ary[i];
|
||||
Object val = vals[i];
|
||||
if (i != 0) bfr.Add_byte_pipe();
|
||||
itm.Fmt(bfr, val);
|
||||
}
|
||||
bfr.Add_byte_nl();
|
||||
}
|
||||
public String To_str_and_clear() {return bfr.To_str_and_clear();}
|
||||
}
|
||||
interface Log_fld_itm {
|
||||
void Fmt(Bry_bfr bfr, Object val);
|
||||
}
|
||||
abstract class Log_fld_itm__base implements Log_fld_itm {
|
||||
public Log_fld_itm__base(int tid, String key, int len) {
|
||||
this.tid = tid; this.key = key; this.len = len;
|
||||
}
|
||||
public int Tid() {return tid;} private final int tid;
|
||||
public String Key() {return key;} private final String key;
|
||||
public int Len() {return len;} protected int len;
|
||||
public abstract void Fmt(Bry_bfr bfr, Object val);
|
||||
}
|
||||
class Log_fld_itm__bry extends Log_fld_itm__base {
|
||||
public Log_fld_itm__bry(int tid, String key, int len) {super(tid, key, len);}
|
||||
@Override public void Fmt(Bry_bfr bfr, Object val) {
|
||||
byte[] val_bry = Bry_.cast(val);
|
||||
int val_bry_len = val_bry.length;
|
||||
int pad_len = this.len - val_bry_len;
|
||||
bfr.Add(val_bry);
|
||||
if (pad_len > 0)
|
||||
bfr.Add_byte_repeat(Byte_ascii.Space, pad_len);
|
||||
}
|
||||
}
|
||||
class Log_fld_itm__int extends Log_fld_itm__base {
|
||||
public Log_fld_itm__int(int tid, String key, int bgn, int end) {super(tid, key, 0);
|
||||
this.bgn = bgn; this.end = end;
|
||||
this.len = Int_.DigitCount(end);
|
||||
}
|
||||
public int Bgn() {return bgn;} private final int bgn;
|
||||
public int End() {return end;} private final int end;
|
||||
@Override public void Fmt(Bry_bfr bfr, Object val) {
|
||||
int val_int = Int_.cast(val);
|
||||
String val_str = String_.PadBgn(Int_.To_str(val_int), this.Len(), " ");
|
||||
bfr.Add_str_u8(val_str);
|
||||
}
|
||||
}
|
||||
87
400_xowa/src/gplx/dbs/percentiles/Percentile_rng.java
Normal file
87
400_xowa/src/gplx/dbs/percentiles/Percentile_rng.java
Normal file
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
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.dbs.percentiles; import gplx.*; import gplx.dbs.*;
|
||||
public class Percentile_rng {
|
||||
private long total_max; private int total_needed;
|
||||
private int score_max, score_len_max;
|
||||
private long prv_time;
|
||||
public int Score_bgn() {return score_bgn;} private int score_bgn;
|
||||
public int Score_end() {return score_end;} private int score_end;
|
||||
public int Score_len() {return score_len;} private int score_len;
|
||||
public int Found_rdr() {return found_rdr;} private int found_rdr;
|
||||
public int Found_all() {return found_all;} private int found_all;
|
||||
public int Elapsed() {return elapsed;} private int elapsed;
|
||||
public Percentile_rng Init(long total_max, int score_max) {
|
||||
this.total_max = total_max;
|
||||
this.score_max = score_max;
|
||||
this.score_len_max = score_max / 20; // limit to 5%
|
||||
return this;
|
||||
}
|
||||
public void Select_init(int total_needed, int prv_score_bgn, int prv_score_len, int score_len_adj) {
|
||||
this.total_needed = total_needed;
|
||||
this.found_all = 0;
|
||||
this.prv_time = gplx.core.envs.System_.Ticks();
|
||||
int score_unit = Calc_score_unit(total_needed, total_max, score_max);
|
||||
if (prv_score_bgn == Score_null) {
|
||||
score_len = score_unit + (score_unit * score_len_adj);
|
||||
score_bgn = score_max;
|
||||
Rng_len_(Bool_.Y);
|
||||
}
|
||||
else {
|
||||
score_len = prv_score_len;
|
||||
score_bgn = prv_score_bgn;
|
||||
score_end = score_bgn + score_len;
|
||||
}
|
||||
}
|
||||
public void Update(int found_rdr) {
|
||||
this.found_rdr = found_rdr;
|
||||
this.found_all += found_rdr;
|
||||
|
||||
// calc rng_multiplier based on found_rdr and total_needed; EX: 100=total_needed; 10=found_rdr; 40=found_all -> 6=rng_multiplier; (100 - 40 / 10)
|
||||
int rng_multiplier = 1;
|
||||
if (found_rdr == 0) {
|
||||
rng_multiplier = 4;
|
||||
} else {
|
||||
int total_remaining = total_needed - found_all;
|
||||
rng_multiplier = total_remaining == 0 ? 1 : Math_.Ceil_as_int(total_remaining / found_rdr);
|
||||
}
|
||||
|
||||
// calc new score_len
|
||||
int new_score_len = score_len * rng_multiplier;
|
||||
if (new_score_len < 1) new_score_len = score_len;
|
||||
else if (new_score_len > score_len_max) new_score_len = score_len_max;
|
||||
score_len = new_score_len;
|
||||
Rng_len_(Bool_.N);
|
||||
|
||||
// update times
|
||||
long new_time = gplx.core.envs.System_.Ticks();
|
||||
this.elapsed = Int_.Subtract_long(new_time, prv_time);
|
||||
prv_time = new_time;
|
||||
}
|
||||
private void Rng_len_(boolean first) {
|
||||
score_end = score_bgn + (first ? 1 : 0); // + 1 to include rows with scores at max; EX: > 999,998 AND < 1,000,001
|
||||
score_bgn = score_end - score_len;
|
||||
if (score_bgn < 0) score_bgn = 0; // make sure score is not negative
|
||||
}
|
||||
@gplx.Internal protected static int Calc_score_unit(int total_needed, long total_max, int score_max) {// TEST:
|
||||
int rv = (int)Math_.Ceil(Math_.Div_safe_as_double(total_needed, Math_.Div_safe_as_double(total_max, score_max))); // EX: 100 needed / (16 M / 1 M) -> 7 units to fill 100
|
||||
if (rv > score_max) rv = score_max; // never allow score_unit to be > score_max; occurs when total_needed > total_max; EX: 50 needed; 10 available
|
||||
return rv;
|
||||
}
|
||||
public static final int Score_null = -1;
|
||||
}
|
||||
43
400_xowa/src/gplx/dbs/percentiles/Percentile_rng_log.java
Normal file
43
400_xowa/src/gplx/dbs/percentiles/Percentile_rng_log.java
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
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.dbs.percentiles; import gplx.*; import gplx.dbs.*;
|
||||
public class Percentile_rng_log {
|
||||
private final Log_tbl_fmtr fmtr = new Log_tbl_fmtr();
|
||||
private byte[] search; private int rslts_needed;
|
||||
private int rdr_idx;
|
||||
public Percentile_rng_log(int score_max) {
|
||||
fmtr.Add_str("search" , 50)
|
||||
.Add_int("rslts_needed" , 1, 999)
|
||||
.Add_int("rdr_idx" , 0, 100) // warn if more than 100 sql queries
|
||||
.Add_int("score_bgn" , 0, score_max)
|
||||
.Add_int("score_end" , 0, score_max)
|
||||
.Add_int("score_len" , 1, 100000)
|
||||
.Add_int("rdr_found" , 0, 9999) // warn if more than 9.999 seconds
|
||||
.Add_int("total_found" , 0, 999)
|
||||
.Add_int("total_needed" , 1, 999)
|
||||
;
|
||||
}
|
||||
public void Init(byte[] search, int rslts_needed) {
|
||||
this.search = search; this.rslts_needed = rslts_needed;
|
||||
rdr_idx = -1;
|
||||
}
|
||||
public void Log(int score_bgn, int score_end, int rdr_found, int total_found, int pass_time) {
|
||||
fmtr.Log(search, rslts_needed, ++rdr_idx, score_bgn, score_end, score_end - score_bgn, rdr_found, total_found, pass_time);
|
||||
}
|
||||
public String To_str_and_clear() {return fmtr.To_str_and_clear();}
|
||||
}
|
||||
63
400_xowa/src/gplx/dbs/percentiles/Percentile_rng_tst.java
Normal file
63
400_xowa/src/gplx/dbs/percentiles/Percentile_rng_tst.java
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
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.dbs.percentiles; import gplx.*; import gplx.dbs.*;
|
||||
import org.junit.*;
|
||||
public class Percentile_rng_tst {
|
||||
private final Percentile_rng_fxt fxt = new Percentile_rng_fxt();
|
||||
@Before public void init() {fxt.Clear();}
|
||||
@Test public void Found__000() {
|
||||
fxt.Test__rng(999994, 1000001);
|
||||
fxt.Exec__update( 0).Test__rng(999966, 999994);
|
||||
fxt.Exec__update( 0).Test__rng(999854, 999966);
|
||||
}
|
||||
@Test public void Found__025() {
|
||||
fxt.Test__rng(999994, 1000001);
|
||||
fxt.Exec__update( 25).Test__rng(999973, 999994);
|
||||
fxt.Exec__update( 25).Test__rng(999931, 999973);
|
||||
fxt.Exec__update( 25).Test__rng(999889, 999931);
|
||||
fxt.Exec__update( 25).Test__rng(999847, 999889);
|
||||
}
|
||||
@Test public void Calc_score_unit() {
|
||||
fxt.Test__calc_score_unit(50, 16000000, 1000000, 4); // to fill 50 -> 16 pages per point -> read every 4 points to get 64 pages
|
||||
fxt.Test__calc_score_unit(50, 1000, 1000000, 50000); // to fill 50 -> 1000 points per page -> read every 50k points to get 50 pages
|
||||
fxt.Test__calc_score_unit(50, 25, 1000000, 1000000); // range bounds check; to fill 50, always read full amount
|
||||
}
|
||||
}
|
||||
class Percentile_rng_fxt {
|
||||
private final Percentile_rng rng = new Percentile_rng();
|
||||
public void Clear() {
|
||||
this.Exec__init_for_wiki(16000000, 1000000);
|
||||
this.Exec__init_for_search(100, 0);
|
||||
}
|
||||
public Percentile_rng_fxt Exec__init_for_wiki (int pages_max, int score_max) {
|
||||
rng.Init(pages_max, score_max); return this;
|
||||
}
|
||||
public Percentile_rng_fxt Exec__init_for_search(int request_count, int score_len_adj) {
|
||||
rng.Select_init(request_count, Percentile_rng.Score_null, Percentile_rng.Score_null, score_len_adj); return this;
|
||||
}
|
||||
public Percentile_rng_fxt Exec__update(int rdr_found) {
|
||||
rng.Update(rdr_found); return this;
|
||||
}
|
||||
public void Test__rng(int expd_bgn, int expd_end) {
|
||||
Tfds.Eq(expd_end, rng.Score_end(), "rng_end");
|
||||
Tfds.Eq(expd_bgn, rng.Score_bgn(), "rng_bgn");
|
||||
}
|
||||
public void Test__calc_score_unit(int request_count, long pages_max, int score_max, int expd) {
|
||||
Tfds.Eq(expd, Percentile_rng.Calc_score_unit(request_count, pages_max, score_max));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
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.dbs.percentiles; import gplx.*; import gplx.dbs.*;
|
||||
public abstract class Percentile_select_base { // SELECT * FROM x ORDER BY y LIMIT 10;
|
||||
protected Cancelable cxl;
|
||||
protected Percentile_rng rng;
|
||||
protected Percentile_rng_log rng_log;
|
||||
protected void Select() {
|
||||
Db_rdr rdr = null;
|
||||
try {
|
||||
int rdr_found = 0;
|
||||
while (true) {
|
||||
if (cxl.Canceled()) return;
|
||||
if (rdr == null) {
|
||||
rdr = Rdr__init(); // EXPENSIVE
|
||||
rdr_found = 0;
|
||||
if (cxl.Canceled()) return;
|
||||
}
|
||||
if (!Row__read(rdr)) { // EXPENSIVE
|
||||
if (cxl.Canceled()) return;
|
||||
rng_log.Log(rng.Score_bgn(), rng.Score_end(), rng.Found_rdr(), rng.Found_all(), rng.Elapsed());
|
||||
rdr = Rdr__term(rdr);
|
||||
Rng__update(rdr_found);
|
||||
boolean found_enough = Found_enough();
|
||||
boolean none_left = rng.Score_bgn() == 0;
|
||||
Rdr__done(found_enough, none_left);
|
||||
if (found_enough || none_left)
|
||||
break;
|
||||
else
|
||||
continue; // resume from top; will create new rdrd
|
||||
}
|
||||
if (Row__eval()) ++rdr_found;
|
||||
}
|
||||
}
|
||||
catch (Exception exc) {
|
||||
Gfo_usr_dlg_.Instance.Warn_many("", "", "error during percentile; err=~{0}", Err_.Message_gplx_log(exc));
|
||||
}
|
||||
finally {
|
||||
rdr = Rdr__term(rdr);
|
||||
}
|
||||
}
|
||||
protected abstract Db_rdr Rdr__init();
|
||||
@gplx.Virtual protected void Rdr__done(boolean found_enough, boolean none_left) {}
|
||||
@gplx.Virtual protected Db_rdr Rdr__term(Db_rdr rdr) {
|
||||
if (rdr != null) rdr.Rls();
|
||||
return null;
|
||||
}
|
||||
@gplx.Virtual protected void Rng__update(int rdr_found) {rng.Update(rdr_found);}
|
||||
@gplx.Virtual protected boolean Row__read(Db_rdr rdr) {return true;}
|
||||
@gplx.Virtual protected boolean Row__eval() {return true;} // NOTE: return true by default; DEPENDENCY: Srch_word_count_wkr
|
||||
@gplx.Virtual protected boolean Found_enough() {return false;}
|
||||
}
|
||||
55
400_xowa/src/gplx/dbs/updates/Sql_runner.java
Normal file
55
400_xowa/src/gplx/dbs/updates/Sql_runner.java
Normal file
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
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.dbs.updates; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.dbs.stmts.*;
|
||||
/*
|
||||
sql =
|
||||
UPDATE page
|
||||
SET page_score = page_len
|
||||
WHERE page_id >= ?
|
||||
AND page_id < ?
|
||||
state = -1|100000
|
||||
*/
|
||||
public class Sql_runner {
|
||||
private final Db_stmt_arg_list list = new Db_stmt_arg_list();
|
||||
public Db_conn Conn() {return conn;} public void Conn_(Db_conn v) {conn = v;} private Db_conn conn;
|
||||
public boolean Quiet() {return quiet;} public void Quiet_(boolean v) {quiet = v;} private boolean quiet;
|
||||
public String Sql_fmt() {return sql_fmt;} public void Sql_fmt_(String v) {sql_fmt = v;} private String sql_fmt;
|
||||
// public Db_stmt_arg[] Sql_args() {return sql_args;} public void Sql_args_(Db_stmt_arg[] v) {sql_args = v;} private Db_stmt_arg[] sql_args;
|
||||
public String Msg() {return msg;} public void Msg_(String v) {msg = v;} private String msg;
|
||||
public String Fill_next(String state) {
|
||||
String[] vals = String_.Split(state, "|");
|
||||
int val_lo = Int_.parse(vals[0]);
|
||||
int interval = Int_.parse(vals[1]);
|
||||
int val_hi = val_lo + interval;
|
||||
|
||||
Db_stmt_arg arg = list.Get_at(0);
|
||||
arg.Val = val_lo;
|
||||
arg = list.Get_at(1);
|
||||
arg.Val = val_hi;
|
||||
|
||||
return String_.Concat_with_str("|", Int_.To_str(val_hi), vals[1]);
|
||||
}
|
||||
public void Run() {
|
||||
Db_stmt stmt = conn.Stmt_sql(sql_fmt);
|
||||
// foreach (itme) Db_stmt_arg_list list = Db_stmt_arg_list
|
||||
Gfo_usr_dlg_.Instance.Note_many("", "", msg);
|
||||
stmt.Exec_update();
|
||||
// increment ranges
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user