mirror of
https://github.com/gnosygnu/xowa.git
synced 2024-10-27 20:34:16 +00:00
File_shrink: Add initial classes for file_shrink
This commit is contained in:
parent
063cc43047
commit
2dc6d8c20b
@ -20,16 +20,16 @@ import gplx.core.primitives.*; import gplx.core.envs.*;
|
||||
import gplx.dbs.*; import gplx.core.ios.*; import gplx.core.ios.streams.*;
|
||||
import gplx.dbs.engines.sqlite.*;
|
||||
public class Fsd_bin_tbl implements Rls_able {
|
||||
private final String fld_owner_id, fld_owner_tid, fld_part_id, fld_data_url, fld_data;
|
||||
public final String fld__owner_id, fld__owner_tid, fld__part_id, fld__data_url, fld__data;
|
||||
private Db_conn conn; private Db_stmt stmt_insert, stmt_select, stmt_select_itm; private Bry_bfr tmp_bfr;
|
||||
private final Bool_obj_ref saved_in_parts = Bool_obj_ref.n_();
|
||||
public Fsd_bin_tbl(Db_conn conn, boolean schema_is_1) {
|
||||
this.conn = conn;
|
||||
fld_owner_id = flds.Add_int_pkey ("bin_owner_id");
|
||||
fld_owner_tid = flds.Add_byte ("bin_owner_tid");
|
||||
fld_part_id = flds.Add_int ("bin_part_id");
|
||||
fld_data_url = flds.Add_str ("bin_data_url", 255);
|
||||
fld_data = flds.Add_bry ("bin_data"); // mediumblob
|
||||
fld__owner_id = flds.Add_int_pkey ("bin_owner_id");
|
||||
fld__owner_tid = flds.Add_byte ("bin_owner_tid");
|
||||
fld__part_id = flds.Add_int ("bin_part_id");
|
||||
fld__data_url = flds.Add_str ("bin_data_url", 255);
|
||||
fld__data = flds.Add_bry ("bin_data"); // mediumblob
|
||||
conn.Rls_reg(this);
|
||||
}
|
||||
public String Tbl_name() {return tbl_name;} private final String tbl_name = "fsdb_bin";
|
||||
@ -50,13 +50,16 @@ public class Fsd_bin_tbl implements Rls_able {
|
||||
}
|
||||
byte[] bin_ary = Io_stream_rdr_.Load_all_as_bry(tmp_bfr, bin_rdr);
|
||||
stmt_insert.Clear()
|
||||
.Val_int(fld_owner_id, id)
|
||||
.Val_byte(fld_owner_tid, tid)
|
||||
.Val_int(fld_part_id, Part_id_null)
|
||||
.Val_str(fld_data_url, Data_url_null)
|
||||
.Val_bry(fld_data, bin_ary)
|
||||
.Val_int(fld__owner_id, id)
|
||||
.Val_byte(fld__owner_tid, tid)
|
||||
.Val_int(fld__part_id, Part_id_null)
|
||||
.Val_str(fld__data_url, Data_url_null)
|
||||
.Val_bry(fld__data, bin_ary)
|
||||
.Exec_insert();
|
||||
}
|
||||
public void Update(Db_stmt stmt, int id, byte[] data) {
|
||||
stmt.Clear().Val_bry(fld__data, data).Crt_int(fld__owner_id, id).Exec_update();
|
||||
}
|
||||
public Io_stream_rdr Select_as_rdr(int owner_id) {
|
||||
byte[] rv = Select(owner_id, null);
|
||||
return rv == null
|
||||
@ -72,18 +75,18 @@ public class Fsd_bin_tbl implements Rls_able {
|
||||
return true;
|
||||
}
|
||||
private byte[] Select(int owner_id, Io_url url) {
|
||||
if (stmt_select == null) stmt_select = conn.Stmt_select(tbl_name, String_.Ary(fld_data), fld_owner_id);
|
||||
Db_rdr rdr = stmt_select.Clear().Crt_int(fld_owner_id, owner_id).Exec_select__rls_manual();
|
||||
if (stmt_select == null) stmt_select = conn.Stmt_select(tbl_name, String_.Ary(fld__data), fld__owner_id);
|
||||
Db_rdr rdr = stmt_select.Clear().Crt_int(fld__owner_id, owner_id).Exec_select__rls_manual();
|
||||
try {
|
||||
if (rdr.Move_next()) {
|
||||
byte[] rv = null;
|
||||
try {rv = rdr.Read_bry(fld_data);}
|
||||
try {rv = rdr.Read_bry(fld__data);}
|
||||
catch (Exception e) {
|
||||
if ( Op_sys.Cur().Tid_is_drd() // drd error when selecting large blobs (> 4 MB?)
|
||||
&& url != null // called by Select_to_url
|
||||
&& String_.Has(Err_.Message_lang(e), "get field slot from row") // get field slot from row 0 col 0 failed
|
||||
) {
|
||||
rdr.Save_bry_in_parts(url, tbl_name, fld_data, fld_owner_id, owner_id);
|
||||
rdr.Save_bry_in_parts(url, tbl_name, fld__data, fld__owner_id, owner_id);
|
||||
saved_in_parts.Val_y_();
|
||||
}
|
||||
}
|
||||
@ -95,16 +98,16 @@ public class Fsd_bin_tbl implements Rls_able {
|
||||
finally {rdr.Rls();}
|
||||
}
|
||||
public Fsd_bin_itm Select_as_itm(int owner_id) {
|
||||
if (stmt_select_itm == null) stmt_select_itm = conn.Stmt_select(tbl_name, flds, fld_owner_id);
|
||||
Db_rdr rdr = stmt_select_itm.Clear().Crt_int(fld_owner_id, owner_id).Exec_select__rls_manual();
|
||||
if (stmt_select_itm == null) stmt_select_itm = conn.Stmt_select(tbl_name, flds, fld__owner_id);
|
||||
Db_rdr rdr = stmt_select_itm.Clear().Crt_int(fld__owner_id, owner_id).Exec_select__rls_manual();
|
||||
try {
|
||||
if (rdr.Move_next()) {
|
||||
return new Fsd_bin_itm
|
||||
( rdr.Read_int(fld_owner_id)
|
||||
, rdr.Read_byte(fld_owner_tid)
|
||||
, rdr.Read_int(fld_part_id)
|
||||
, rdr.Read_str(fld_data_url)
|
||||
, rdr.Read_bry(fld_data)
|
||||
( rdr.Read_int(fld__owner_id)
|
||||
, rdr.Read_byte(fld__owner_tid)
|
||||
, rdr.Read_int(fld__part_id)
|
||||
, rdr.Read_str(fld__data_url)
|
||||
, rdr.Read_bry(fld__data)
|
||||
);
|
||||
}
|
||||
else
|
||||
|
@ -19,7 +19,6 @@ package gplx.fsdb.meta; import gplx.*; import gplx.fsdb.*;
|
||||
import gplx.core.ios.*; import gplx.core.ios.streams.*; import gplx.dbs.*;
|
||||
import gplx.fsdb.data.*;
|
||||
public class Fsm_bin_fil {
|
||||
private final Fsd_bin_tbl tbl;
|
||||
public Fsm_bin_fil(boolean schema_is_1, int id, Io_url url, String url_rel, Db_conn conn, long bin_len) {
|
||||
this.id = id; this.url = url; this.url_rel = url_rel; this.conn = conn; this.bin_len = bin_len;
|
||||
this.tbl = new Fsd_bin_tbl(conn, schema_is_1);
|
||||
@ -29,6 +28,7 @@ public class Fsm_bin_fil {
|
||||
public String Url_rel() {return url_rel;} private final String url_rel;
|
||||
public long Bin_len() {return bin_len;} public void Bin_len_(long v) {bin_len = v;} private long bin_len;
|
||||
public Db_conn Conn() {return conn;} private final Db_conn conn;
|
||||
public Fsd_bin_tbl Tbl() {return tbl;} private final Fsd_bin_tbl tbl;
|
||||
public boolean Select_to_url(int id, Io_url url) {return tbl.Select_to_url(id, url);}
|
||||
public Io_stream_rdr Select_as_rdr(int id) {return tbl.Select_as_rdr(id);}
|
||||
public Fsd_bin_itm Select_as_itm(int id) {return tbl.Select_as_itm(id);}
|
||||
|
@ -0,0 +1,30 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.xowa.addons.bldrs.files.shrinks; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.bldrs.*; import gplx.xowa.addons.bldrs.files.*;
|
||||
import gplx.xowa.bldrs.*; import gplx.xowa.bldrs.wkrs.*;
|
||||
public class Xoshrink_cmd extends Xob_cmd__base {
|
||||
public Xoshrink_cmd(Xob_bldr bldr, Xowe_wiki wiki) {super(bldr, wiki);}
|
||||
@Override public void Cmd_run() {
|
||||
wiki.Init_assert();
|
||||
new Xoshrink_mgr().Exec(wiki);
|
||||
}
|
||||
|
||||
@Override public String Cmd_key() {return BLDR_CMD_KEY;} private static final String BLDR_CMD_KEY = "fsdb.shrink";
|
||||
public static final Xob_cmd Prototype = new Xoshrink_cmd(null, null);
|
||||
@Override public Xob_cmd Cmd_clone(Xob_bldr bldr, Xowe_wiki wiki) {return new Xoshrink_cmd(bldr, wiki);}
|
||||
}
|
@ -0,0 +1,73 @@
|
||||
/*
|
||||
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.files.shrinks; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.bldrs.*; import gplx.xowa.addons.bldrs.files.*;
|
||||
import gplx.core.envs.*;
|
||||
import gplx.dbs.*;
|
||||
import gplx.fsdb.*; import gplx.fsdb.data.*; import gplx.fsdb.meta.*;
|
||||
class Xoshrink_mgr {
|
||||
private Io_url src_url, trg_url;
|
||||
private Process_adp convert_cmd;
|
||||
public void Exec(Xowe_wiki wiki) {
|
||||
// init
|
||||
src_url = wiki.Fsys_mgr().Root_dir().GenSubFil_nest("tmp", "shrink", "src.file");
|
||||
trg_url = wiki.Fsys_mgr().Root_dir().GenSubFil_nest("tmp", "shrink", "trg.file");
|
||||
Io_url convert_exe_url = wiki.Appe().Prog_mgr().App_resize_img().Exe_url();
|
||||
convert_cmd = Process_adp.New(Gfo_usr_dlg_.Instance, wiki.Appe().Url_cmd_eval(), Process_adp.Run_mode_sync_timeout, 1 * 60, convert_exe_url.Raw(), "-resample ~{w}x~{h}");
|
||||
|
||||
// get bin_mgr
|
||||
Fsm_bin_mgr bin_mgr = wiki.File__mnt_mgr().Mnts__get_main().Bin_mgr();
|
||||
int len = bin_mgr.Dbs__len();
|
||||
|
||||
// loop bin_dbs
|
||||
for (int i = 0; i < len; i++) {
|
||||
Shrink(bin_mgr.Dbs__get_at(i));
|
||||
}
|
||||
}
|
||||
private void Shrink(Fsm_bin_fil fil) {
|
||||
// init
|
||||
Fsd_bin_tbl tbl = fil.Tbl();
|
||||
Db_conn conn = fil.Conn();
|
||||
|
||||
// prep for update
|
||||
conn.Txn_bgn("tbl_update");
|
||||
Db_stmt stmt = conn.Stmt_update(tbl.Tbl_name(), String_.Ary(tbl.fld__owner_id), tbl.fld__data);
|
||||
|
||||
// get rdr
|
||||
Db_rdr rdr = conn.Stmt_select_all(tbl.Tbl_name(), tbl.Flds()).Exec_select__rls_auto();
|
||||
try {
|
||||
// loop each row and convert
|
||||
while (rdr.Move_next()) {
|
||||
// db.read and fs.save
|
||||
int id = rdr.Read_int(tbl.fld__owner_id);
|
||||
byte[] data = rdr.Read_bry(tbl.fld__data);
|
||||
Io_mgr.Instance.SaveFilBry(src_url, data);
|
||||
|
||||
// convert
|
||||
convert_cmd.Run();// get w and h
|
||||
|
||||
// fs.load and db.save
|
||||
data = Io_mgr.Instance.LoadFilBry(trg_url);
|
||||
tbl.Update(stmt, id, data);
|
||||
}
|
||||
} finally {
|
||||
conn.Txn_end();
|
||||
rdr.Rls();
|
||||
stmt.Rls();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user