mirror of https://github.com/gnosygnu/xowa
parent
e4a2af026b
commit
232838c732
@ -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;
|
||||
import org.junit.*; import gplx.core.tests.*; import gplx.core.envs.*;
|
||||
public class Io_url__tst {
|
||||
@Before public void init() {fxt.Clear();} private final Io_url__fxt fxt = new Io_url__fxt();
|
||||
@Test public void Basic__lnx() {fxt.Test__New__http_or_null(Bool_.N, "file:///C:/a.txt", "C:/a.txt");}
|
||||
@Test public void Basic__wnt() {fxt.Test__New__http_or_null(Bool_.Y, "file:///C:/a.txt", "C:\\a.txt");}
|
||||
@Test public void Null() {fxt.Test__New__http_or_null(Bool_.N, "C:/a.txt", null);}
|
||||
}
|
||||
class Io_url__fxt {
|
||||
public void Clear() {Io_mgr.Instance.InitEngine_mem();}
|
||||
public void Test__New__http_or_null(boolean os_is_wnt, String raw, String expd) {
|
||||
Op_sys.Cur_(os_is_wnt ? Op_sys.Tid_wnt : Op_sys.Tid_lnx);
|
||||
Gftest.Eq__obj_or_null(expd, Io_url_.New__http_or_null(raw));
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
/*
|
||||
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.core.envs; import gplx.*; import gplx.core.*;
|
||||
public class Runtime_ {
|
||||
// *** Hardware-related
|
||||
public static int Cpu_count() {return Runtime.getRuntime().availableProcessors();}
|
||||
public static long Memory_max() {return Runtime.getRuntime().maxMemory();}
|
||||
public static long Memory_total() {return Runtime.getRuntime().totalMemory();}
|
||||
public static long Memory_free() {return Runtime.getRuntime().freeMemory();}
|
||||
}
|
@ -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.core.envs; import gplx.*; import gplx.core.*;
|
||||
public class System_ {
|
||||
// *** ticks
|
||||
public static long Ticks() {return Ticks__test_val >= 0 ? Ticks__test_val : System.currentTimeMillis();}
|
||||
public static int Ticks__elapsed_in_sec (long time_bgn) {return (int)(Ticks() - time_bgn) / 1000;}
|
||||
public static int Ticks__elapsed_in_frac (long time_bgn) {return (int)(Ticks() - time_bgn);}
|
||||
public static void Ticks__test_set(long v) {Ticks__test_val = v;}
|
||||
public static void Ticks__test_add(long v) {Ticks__test_val += v;}
|
||||
public static void Ticks__test_off() {Ticks__test_val = -1;}
|
||||
private static long Ticks__test_val = -1; // in milliseconds
|
||||
|
||||
// *** misc methods: Exit / GC
|
||||
public static void Exit() {Exit_by_code(0);}
|
||||
public static void Exit_by_code(int code) {System.exit(code);}
|
||||
public static void Garbage_collect() {if (Env_.Mode_testing()) return; System.gc();}
|
||||
|
||||
// *** java properties: getProperty; "-D" properties
|
||||
public static String Prop__user_language() {return Prop__get(Prop_key__user_language);}
|
||||
public static String Prop__user_name() {return Prop__get(Prop_key__user_name);}
|
||||
public static String Prop__java_version() {return Prop__get(Prop_key__java_version);}
|
||||
public static String Prop__java_home() {return Prop__get(Prop_key__java_home);}
|
||||
private static String Prop__get(String key) {
|
||||
return System.getProperty(key);
|
||||
}
|
||||
private static final String
|
||||
Prop_key__user_language = "user.language"
|
||||
, Prop_key__user_name = "user.name"
|
||||
, Prop_key__java_version = "java.version"
|
||||
, Prop_key__java_home = "java.version"
|
||||
;
|
||||
|
||||
// *** environment variables: getenv
|
||||
public static String Env__machine_name() {
|
||||
String rv = "UNKNOWN_MACHINE_NAME";
|
||||
rv = Env__get(Env_key__computername); if (String_.Len_gt_0(rv)) return rv;
|
||||
rv = Env__get(Env_key__hostname); if (String_.Len_gt_0(rv)) return rv;
|
||||
return rv;
|
||||
}
|
||||
private static String Env__get(String key) {
|
||||
return System.getenv(key);
|
||||
}
|
||||
private static final String
|
||||
Env_key__computername = "COMPUTERNAME"
|
||||
, Env_key__hostname = "HOSTNAME"
|
||||
;
|
||||
}
|
@ -1,43 +0,0 @@
|
||||
/*
|
||||
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.core.intls; import gplx.*; import gplx.core.*;
|
||||
import gplx.core.brys.fmtrs.*;
|
||||
class Gfo_i18n_itm {
|
||||
public Gfo_i18n_itm(int src, byte[] key, byte[] val, boolean val_fmt_exists, Gfo_i18n_val_cmd val_cmd) {
|
||||
this.src = src; this.key = key; this.val = val; this.val_fmt_exists = val_fmt_exists; this.val_cmd = val_cmd;
|
||||
}
|
||||
public int Src() {return src;} private final int src;
|
||||
public byte[] Key() {return key;} private final byte[] key;
|
||||
public byte[] Val() {return val;} private final byte[] val;
|
||||
public boolean Val_fmt_exists() {return val_fmt_exists;} private final boolean val_fmt_exists;
|
||||
public Gfo_i18n_val_cmd Val_cmd() {return val_cmd;} private final Gfo_i18n_val_cmd val_cmd;
|
||||
public byte[] Bld_none() {
|
||||
return val_cmd == null ? val : val_cmd.Process(src, key, val);
|
||||
}
|
||||
public byte[] Bld_many(Object... args) {
|
||||
byte[] rv = null;
|
||||
synchronized (tmp_fmtr) {
|
||||
tmp_fmtr.Fmt_(val);
|
||||
tmp_fmtr.Bld_bfr_many(tmp_bfr, args);
|
||||
rv = tmp_bfr.To_bry_and_clear();
|
||||
}
|
||||
return val_cmd == null ? rv : val_cmd.Process(src, key, rv);
|
||||
}
|
||||
private static final Bry_fmtr tmp_fmtr = Bry_fmtr.new_();
|
||||
private static final Bry_bfr tmp_bfr = Bry_bfr_.Reset(255);
|
||||
}
|
@ -1,41 +0,0 @@
|
||||
/*
|
||||
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.core.intls; import gplx.*; import gplx.core.*;
|
||||
public class Gfo_i18n_mgr {
|
||||
public String Dflt() {return dflt;} private String dflt = "en";
|
||||
public Gfo_i18n_mgr Add_txt_dflt(String key, String val) {return this;}
|
||||
public Gfo_i18n_mgr Add_txt_one(String key, String lng, String val) {return this;}
|
||||
public Gfo_i18n_mgr Add_txt_many(String key, String... ary) {return this;}
|
||||
}
|
||||
class Gfo_i18n_lng {
|
||||
private Hash_adp_bry hash = Hash_adp_bry.cs();
|
||||
public Gfo_i18n_lng(String lng) {this.lng = lng;}
|
||||
public String Lng() {return lng;} private final String lng;
|
||||
public void Add(int src, byte[] key, byte[] val, boolean val_fmt_exists, Gfo_i18n_val_cmd val_cmd) {
|
||||
Gfo_i18n_itm itm = new Gfo_i18n_itm(src, key, val, val_fmt_exists, val_cmd);
|
||||
hash.Add_bry_obj(key, itm);
|
||||
}
|
||||
public byte[] Bld_to_bry_none(byte[] key) {
|
||||
Gfo_i18n_itm itm = (Gfo_i18n_itm)hash.Get_by_bry(key);
|
||||
return itm.Bld_none();
|
||||
}
|
||||
public byte[] Bld_to_bry_many(byte[] key, Object... args) {
|
||||
Gfo_i18n_itm itm = (Gfo_i18n_itm)hash.Get_by_bry(key);
|
||||
return itm.Bld_many(args);
|
||||
}
|
||||
}
|
@ -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.langs.htmls; import gplx.*; import gplx.langs.*;
|
||||
public class Gfh_tag_meta {
|
||||
public Gfh_tag_meta(int id, String key_str) {
|
||||
this.id = id;
|
||||
this.key_str = key_str;
|
||||
this.key_bry = Bry_.new_u8(key_str);
|
||||
}
|
||||
public int Id() {return id;} private final int id;
|
||||
public String Key_str() {return key_str;} private final String key_str;
|
||||
public byte[] Key_bry() {return key_bry;} private final byte[] key_bry;
|
||||
}
|
@ -1,60 +0,0 @@
|
||||
/*
|
||||
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.bldrs.mass_parses.dbs; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.bldrs.*; import gplx.xowa.addons.bldrs.mass_parses.*;
|
||||
import gplx.dbs.*;
|
||||
public class Xomp_db_core {
|
||||
private final Object thread_lock = new Object();
|
||||
private final Io_url root_dir;
|
||||
Xomp_db_core(Io_url root_dir) {
|
||||
this.root_dir = root_dir;
|
||||
Io_url mgr_url = root_dir.GenSubFil("xomp.sqlite3");
|
||||
this.mgr_db = new Xomp_mgr_db(mgr_url);
|
||||
}
|
||||
public Db_conn Conn() {return mgr_db.Conn();}
|
||||
public Xomp_mgr_db Mgr_db() {return mgr_db;} private Xomp_mgr_db mgr_db;
|
||||
public Xomp_wkr_db Wkr_db(boolean delete, int idx) {
|
||||
Io_url wkr_url = root_dir.GenSubFil_nest("xomp_" + Int_.To_str_fmt(idx, "000"), "xomp_wkr.sqlite3");
|
||||
if (delete) Io_mgr.Instance.DeleteFil(wkr_url);
|
||||
return new Xomp_wkr_db(idx, wkr_url);
|
||||
}
|
||||
public int Wkr_count() {
|
||||
Io_url[] wkr_dirs = Io_mgr.Instance.QueryDir_args(root_dir).DirOnly_().ExecAsUrlAry();
|
||||
return wkr_dirs.length;
|
||||
}
|
||||
public void Update_wkr_id(int idx, Db_conn wkr_conn) {
|
||||
synchronized (thread_lock) {
|
||||
Db_attach_mgr attach_mgr = new Db_attach_mgr(mgr_db.Conn(), new Db_attach_itm("wkr_db", wkr_conn));
|
||||
attach_mgr.Exec_sql_w_msg("updating page_regy: wkr_id=" + idx, String_.Concat_lines_nl_skip_last // ANSI.Y
|
||||
( "UPDATE xomp_page"
|
||||
, "SET xomp_wkr_id = " + Int_.To_str(idx)
|
||||
, ", html_len = (SELECT length(body) FROM <wkr_db>html h WHERE h.page_id = xomp_page.page_id)"
|
||||
, "WHERE page_id IN (SELECT page_id FROM <wkr_db>html h)"
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
public static Xomp_db_core New__make(Xowe_wiki wiki) {
|
||||
Io_url root_dir = wiki.Fsys_mgr().Root_dir().GenSubDir_nest("tmp", "xomp");
|
||||
Io_mgr.Instance.DeleteDirDeep(root_dir);
|
||||
return new Xomp_db_core(root_dir);
|
||||
}
|
||||
public static Xomp_db_core New__load(Xowe_wiki wiki) {
|
||||
Io_url root_dir = wiki.Fsys_mgr().Root_dir().GenSubDir_nest("tmp", "xomp");
|
||||
return new Xomp_db_core(root_dir);
|
||||
}
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
/*
|
||||
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.bldrs.mass_parses.dbs; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.bldrs.*; import gplx.xowa.addons.bldrs.mass_parses.*;
|
||||
import gplx.dbs.*;
|
||||
public class Xomp_lock_req_tbl implements Db_tbl {
|
||||
private final String fld_machine_name, fld_req_time;
|
||||
private final Db_conn conn;
|
||||
public Xomp_lock_req_tbl(Db_conn conn) {
|
||||
this.conn = conn;
|
||||
this.tbl_name = "xomp_lock_req";
|
||||
this.fld_machine_name = flds.Add_str("machine_name", 255); // EX: "MACHINE1"
|
||||
this.fld_req_time = flds.Add_str("req_time", 32); // EX: 20160801 010203
|
||||
conn.Rls_reg(this);
|
||||
}
|
||||
public String Tbl_name() {return tbl_name;} private final String tbl_name;
|
||||
public Dbmeta_fld_list Flds() {return flds;} private final Dbmeta_fld_list flds = new Dbmeta_fld_list();
|
||||
public void Create_tbl() {
|
||||
conn.Meta_tbl_create(Dbmeta_tbl_itm.New(tbl_name, flds
|
||||
, Dbmeta_idx_itm.new_normal_by_tbl(tbl_name, "req_time", fld_req_time)
|
||||
));
|
||||
}
|
||||
public void Insert(String machine_name) {
|
||||
conn.Stmt_insert(tbl_name, flds).Clear().Val_str(fld_machine_name, machine_name).Val_str(fld_req_time, Datetime_now.Get_force().XtoStr_fmt_yyyyMMdd_HHmmss()).Exec_insert();
|
||||
}
|
||||
public String Select_1st() {
|
||||
String sql = String_.Format("SELECT * FROM {0} ORDER BY {1} DESC", tbl_name, fld_machine_name); // ANSI.y
|
||||
Db_rdr rdr = conn.Stmt_sql(sql).Exec_select__rls_auto();
|
||||
try {
|
||||
if (!rdr.Move_next()) throw Err_.new_wo_type("xomp_lock_req has no rows");
|
||||
return rdr.Read_str(fld_machine_name);}
|
||||
finally {rdr.Rls();}
|
||||
}
|
||||
public void Delete(String machine_name) {
|
||||
conn.Stmt_delete(tbl_name, fld_machine_name).Clear().Crt_str(fld_machine_name, machine_name).Exec_delete();
|
||||
}
|
||||
public void Rls() {}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
/*
|
||||
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.bldrs.mass_parses.dbs; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.bldrs.*; import gplx.xowa.addons.bldrs.mass_parses.*;
|
||||
import gplx.dbs.*;
|
||||
public class Xomp_lock_tbl implements Db_tbl {
|
||||
private final String fld_uid_prv;
|
||||
private final Db_conn conn;
|
||||
public Xomp_lock_tbl(Db_conn conn) {
|
||||
this.conn = conn;
|
||||
this.tbl_name = "xomp_lock";
|
||||
this.fld_uid_prv = flds.Add_int("uid_prv"); // EX: -1
|
||||
conn.Rls_reg(this);
|
||||
}
|
||||
public String Tbl_name() {return tbl_name;} private final String tbl_name;
|
||||
public Dbmeta_fld_list Flds() {return flds;} private final Dbmeta_fld_list flds = new Dbmeta_fld_list();
|
||||
public void Create_tbl() {
|
||||
conn.Meta_tbl_create(Dbmeta_tbl_itm.New(tbl_name, flds));
|
||||
conn.Stmt_insert(tbl_name, flds).Clear().Val_int(fld_uid_prv, -1).Exec_insert(); // always add default record when creating table
|
||||
}
|
||||
public int Select() {
|
||||
Db_rdr rdr = conn.Stmt_select(tbl_name, flds).Exec_select__rls_auto();
|
||||
try {
|
||||
if (!rdr.Move_next()) throw Err_.new_wo_type("xomp_lock has no rows");
|
||||
return rdr.Read_int(fld_uid_prv);}
|
||||
finally {rdr.Rls();}
|
||||
}
|
||||
public void Update(int uid_prv) {
|
||||
conn.Stmt_update(tbl_name, String_.Ary_empty, fld_uid_prv).Clear().Val_int(fld_uid_prv, uid_prv).Exec_update();
|
||||
}
|
||||
public void Rls() {}
|
||||
}
|
@ -0,0 +1,99 @@
|
||||
/*
|
||||
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.bldrs.mass_parses.dbs; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.bldrs.*; import gplx.xowa.addons.bldrs.mass_parses.*;
|
||||
import gplx.dbs.*;
|
||||
public class Xomp_wkr_tbl implements Db_tbl {
|
||||
private final String fld_wkr_uid, fld_wkr_url, fld_wkr_status, fld_wkr_status_time, fld_wkr_exec_count, fld_wkr_exec_time;
|
||||
private final Db_conn conn;
|
||||
private final Object thread_lock = new Object();
|
||||
public Xomp_wkr_tbl(Db_conn conn) {
|
||||
this.conn = conn;
|
||||
this.tbl_name = "xomp_wkr";
|
||||
fld_wkr_uid = flds.Add_int_pkey("wkr_uid"); // EX: 0
|
||||
fld_wkr_url = flds.Add_str("wkr_url", 255); // EX: //MACHINE/C:/xowa/wiki/en.wikipedia.org/tmp/xomp
|
||||
fld_wkr_status = flds.Add_int("wkr_status"); // EX: running; waiting
|
||||
fld_wkr_status_time = flds.Add_str("wkr_status_time", 255); // EX: 20160801 010203
|
||||
fld_wkr_exec_count = flds.Add_int("wkr_exec_count"); // EX: 1000
|
||||
fld_wkr_exec_time = flds.Add_int("wkr_exec_time"); // EX: 123
|
||||
conn.Rls_reg(this);
|
||||
}
|
||||
public String Tbl_name() {return tbl_name;} private final String tbl_name;
|
||||
public Dbmeta_fld_list Flds() {return flds;} private final Dbmeta_fld_list flds = new Dbmeta_fld_list();
|
||||
public void Create_tbl() {conn.Meta_tbl_create(Dbmeta_tbl_itm.New(tbl_name, flds));}
|
||||
|
||||
public int Init_wkrs(String wkr_url, int wkr_len) {
|
||||
// delete all with machine_name
|
||||
conn.Stmt_delete(tbl_name, fld_wkr_url).Crt_str(fld_wkr_url, wkr_url).Exec_delete();
|
||||
|
||||
// get bgn_uid / end_uid
|
||||
int bgn_uid = conn.Exec_select_max_as_int(tbl_name, fld_wkr_uid, -1) + 1;
|
||||
int end_uid = bgn_uid + wkr_len;
|
||||
|
||||
// insert into tbl
|
||||
Db_stmt insert_stmt = conn.Stmt_insert(tbl_name, flds);
|
||||
for (int i = bgn_uid; i < end_uid; ++i)
|
||||
Insert(insert_stmt, i, wkr_url);
|
||||
insert_stmt.Rls();
|
||||
return bgn_uid;
|
||||
}
|
||||
public int Select_count() {
|
||||
return conn.Exec_select_count_as_int(tbl_name, 0);
|
||||
}
|
||||
|
||||
private void Insert(Db_stmt stmt, int wkr_uid, String wkr_url) {
|
||||
stmt.Clear()
|
||||
.Val_int(fld_wkr_uid, wkr_uid)
|
||||
.Val_str(fld_wkr_url, wkr_url).Val_int(fld_wkr_status, Status__running).Val_str(fld_wkr_status_time, Datetime_now.Get_force().XtoStr_fmt_yyyyMMdd_HHmmss())
|
||||
.Val_int(fld_wkr_exec_count, 0).Val_int(fld_wkr_exec_time, 0)
|
||||
.Exec_insert();
|
||||
}
|
||||
public void Update_exec(int wkr_uid, int wkr_exec_count, long wkr_exec_time) {
|
||||
synchronized (thread_lock) { // LOCK:wkr_tbl is shared by multiple threads / machines
|
||||
int attempts = 0;
|
||||
while (true) {
|
||||
if (++attempts > 10) {
|
||||
Gfo_usr_dlg_.Instance.Warn_many("", "", "failed to update status; try=~{0}", attempts);
|
||||
break;
|
||||
}
|
||||
try {
|
||||
conn.Stmt_update(tbl_name, String_.Ary(fld_wkr_uid), fld_wkr_status, fld_wkr_status_time, fld_wkr_exec_count, fld_wkr_exec_time).Clear()
|
||||
.Val_int(fld_wkr_status, Status__running).Val_str(fld_wkr_status_time, Datetime_now.Get_force().XtoStr_fmt_yyyyMMdd_HHmmss())
|
||||
.Val_int(fld_wkr_exec_count, wkr_exec_count).Val_int(fld_wkr_exec_time, (int)(wkr_exec_time / 1000))
|
||||
.Crt_int(fld_wkr_uid, wkr_uid)
|
||||
.Exec_update();
|
||||
break; // exit loop
|
||||
} catch (Exception e) {
|
||||
Gfo_usr_dlg_.Instance.Warn_many("", "", "unable to update status; try=~{0} err=~{1}", attempts, Err_.Message_gplx_log(e));
|
||||
gplx.core.threads.Thread_adp_.Sleep(10000);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public void Update_status(int wkr_uid, int status) {
|
||||
synchronized (thread_lock) { // LOCK:wkr_tbl is shared by multiple threads
|
||||
conn.Stmt_update(tbl_name, String_.Ary(fld_wkr_uid), fld_wkr_status, fld_wkr_status_time).Clear()
|
||||
.Val_int(fld_wkr_status, status).Val_str(fld_wkr_status_time, Datetime_now.Get_force().XtoStr_fmt_yyyyMMdd_HHmmss())
|
||||
.Crt_int(fld_wkr_uid, wkr_uid)
|
||||
.Exec_update();
|
||||
}
|
||||
}
|
||||
public void Rls() {}
|
||||
|
||||
public static final int Status__running = 1, Status__sleeping = 2;
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
/*
|
||||
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.bldrs.mass_parses.inits; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.bldrs.*; import gplx.xowa.addons.bldrs.mass_parses.*;
|
||||
import gplx.xowa.wikis.nss.*;
|
||||
class Xomp_init_mgr_cfg implements Gfo_invk {
|
||||
public int[] Ns_ids() {return ns_ids;} private int[] ns_ids = new int[] {0, 4, 14, 100};
|
||||
public void Init(Xowe_wiki wiki) {
|
||||
if (ns_ids == null) ns_ids = Ns_ids(wiki.Ns_mgr());
|
||||
}
|
||||
private static int[] Ns_ids(Xow_ns_mgr ns_mgr) {
|
||||
int ns_ids_len = ns_mgr.Ids_len();
|
||||
int[] rv = new int[ns_ids_len];
|
||||
for (int i = 0; i < ns_ids_len; ++i)
|
||||
rv[i] = ns_mgr.Ids_get_at(i).Id();
|
||||
return rv;
|
||||
}
|
||||
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
|
||||
if (ctx.Match(k, Invk__ns_ids_)) ns_ids = Int_.Ary_parse(m.ReadStr("v"), "|");
|
||||
else return Gfo_invk_.Rv_unhandled;
|
||||
return this;
|
||||
}
|
||||
private static final String
|
||||
Invk__ns_ids_ = "ns_ids_"
|
||||
;
|
||||
}
|
@ -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.xowa.addons.bldrs.mass_parses.parses.locks; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.bldrs.*; import gplx.xowa.addons.bldrs.mass_parses.*; import gplx.xowa.addons.bldrs.mass_parses.parses.*;
|
||||
public interface Xomp_lock_mgr {
|
||||
void Remake();
|
||||
int Uid_prv__get(String machine_name);
|
||||
void Uid_prv__rls(String machine_name, int uid_prv);
|
||||
}
|
@ -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.xowa.addons.bldrs.mass_parses.parses.locks; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.bldrs.*; import gplx.xowa.addons.bldrs.mass_parses.*; import gplx.xowa.addons.bldrs.mass_parses.parses.*;
|
||||
import gplx.dbs.*; import gplx.xowa.addons.bldrs.mass_parses.dbs.*;
|
||||
public class Xomp_lock_mgr__db implements Xomp_lock_mgr {
|
||||
private final Db_conn conn;
|
||||
private final Xomp_lock_tbl lock_tbl;
|
||||
private final Xomp_lock_req_tbl req_tbl;
|
||||
private final int wait_time;
|
||||
public Xomp_lock_mgr__db(Db_conn conn, int wait_time) {
|
||||
this.conn = conn;
|
||||
this.lock_tbl = new Xomp_lock_tbl(conn);
|
||||
this.req_tbl = new Xomp_lock_req_tbl(conn);
|
||||
this.wait_time = wait_time;
|
||||
}
|
||||
public void Remake() {
|
||||
conn.Meta_tbl_remake(lock_tbl);
|
||||
conn.Meta_tbl_remake(req_tbl);
|
||||
}
|
||||
public int Uid_prv__get(String machine_name) {
|
||||
// insert into req_tbl
|
||||
req_tbl.Insert(machine_name);
|
||||
|
||||
// loop until req is 1st record in req_tbl
|
||||
while (true) {
|
||||
String machine_name_1st = req_tbl.Select_1st();
|
||||
if (String_.Eq(machine_name, machine_name_1st))
|
||||
break;
|
||||
else {
|
||||
Gfo_usr_dlg_.Instance.Note_many("", "", "waiting for lock: ~{0}", machine_name);
|
||||
gplx.core.threads.Thread_adp_.Sleep(wait_time);
|
||||
}
|
||||
}
|
||||
|
||||
// get next uid and fill pages
|
||||
return lock_tbl.Select();
|
||||
}
|
||||
public void Uid_prv__rls(String machine_name, int uid_prv) {
|
||||
lock_tbl.Update(uid_prv);
|
||||
req_tbl.Delete(machine_name);
|
||||
}
|
||||
}
|
@ -0,0 +1,91 @@
|
||||
/*
|
||||
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.bldrs.mass_parses.parses.locks; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.bldrs.*; import gplx.xowa.addons.bldrs.mass_parses.*; import gplx.xowa.addons.bldrs.mass_parses.parses.*;
|
||||
public class Xomp_lock_mgr__fsys implements Xomp_lock_mgr {
|
||||
private final Io_url root_dir, uid_fil, active_fil;
|
||||
private final int wait_time;
|
||||
private Io_url stop_fil;
|
||||
public Xomp_lock_mgr__fsys(int wait_time, Io_url root_dir) {
|
||||
this.wait_time = wait_time;
|
||||
this.root_dir = root_dir;
|
||||
this.uid_fil = root_dir.GenSubFil("xomp.semaphore.uid.txt");
|
||||
this.active_fil = root_dir.GenSubFil("xomp.semaphore.active.txt");
|
||||
}
|
||||
public void Remake() {
|
||||
Io_url[] fils = Io_mgr.Instance.QueryDir_fils(root_dir);
|
||||
for (Io_url fil : fils) {
|
||||
if (String_.Has_at_end(fil.NameAndExt(), ".sempahore.txt"))
|
||||
Io_mgr.Instance.DeleteFil(fil);
|
||||
}
|
||||
Io_mgr.Instance.SaveFilStr(uid_fil, Int_.To_str(Uid__bos));
|
||||
}
|
||||
public int Uid_prv__get(String machine_name) {
|
||||
// return -1 if stop file exists; note that -1 will stop machine
|
||||
if (stop_fil == null) this.stop_fil = root_dir.GenSubFil("xomp.semaphore.stop." + machine_name + ".txt");
|
||||
if (Io_mgr.Instance.ExistsFil(stop_fil)) return Uid__eos;
|
||||
|
||||
// loop until permit is acquired
|
||||
int tries = 0;
|
||||
while (true) {
|
||||
++tries;
|
||||
if (tries > 10) {
|
||||
Gfo_usr_dlg_.Instance.Warn_many("", "", "failed to acquire permit");
|
||||
return Uid__eos; // too many tries; just exit now
|
||||
}
|
||||
|
||||
// if active_file exists, then assume another machine is reading;
|
||||
if (Io_mgr.Instance.ExistsFil(active_fil)) {
|
||||
Sleep(machine_name, "active file exists");
|
||||
continue;
|
||||
}
|
||||
|
||||
// write to the active_file
|
||||
Io_mgr.Instance.SaveFilStr(active_fil, machine_name);
|
||||
|
||||
// now read it to make sure it's the same
|
||||
String cur_active = String_.new_u8(Io_mgr.Instance.LoadFilBryOr(active_fil, Bry_.Empty));
|
||||
if (!String_.Eq(cur_active, machine_name)) {
|
||||
Sleep(machine_name, "active file differs: " + cur_active);
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// get next uid and fill pages
|
||||
byte[] cur_uid_bry = Io_mgr.Instance.LoadFilBryOr(uid_fil, null);
|
||||
if (cur_uid_bry == null) return 0; // file is empty; should only occur on 1st run; return 0, which will start from beginning;
|
||||
|
||||
int cur_uid = Int_.Min_value;
|
||||
if (cur_uid_bry != null)
|
||||
cur_uid = Bry_.To_int_or(cur_uid_bry, Int_.Min_value);
|
||||
if (cur_uid == Int_.Min_value) {
|
||||
Gfo_usr_dlg_.Instance.Warn_many("", "", "uid fil has bad data: data:~{0}", cur_uid_bry);
|
||||
return Uid__eos;
|
||||
}
|
||||
return cur_uid;
|
||||
}
|
||||
private void Sleep(String machine_name, String reason) {
|
||||
Gfo_usr_dlg_.Instance.Note_many("", "", "waiting for permit: machine:~{0} reason:~{1}", machine_name, reason);
|
||||
gplx.core.threads.Thread_adp_.Sleep(wait_time);
|
||||
}
|
||||
public void Uid_prv__rls(String machine_name, int uid_prv) {
|
||||
Io_mgr.Instance.SaveFilStr(uid_fil, Int_.To_str(uid_prv));
|
||||
Io_mgr.Instance.DeleteFil(active_fil);
|
||||
}
|
||||
public static final int Uid__bos = 0, Uid__eos = -1;
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue