1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2026-03-02 03:49:30 +00:00
This commit is contained in:
gnosygnu
2016-01-03 21:27:38 -05:00
parent 9509363f46
commit 096045614c
647 changed files with 11693 additions and 7648 deletions

View File

@@ -0,0 +1,24 @@
/*
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.diffs; import gplx.*; import gplx.dbs.*;
public class Gfdb_diff_db {
public Gfdb_diff_db(Db_conn conn) {
this.conn = conn;
}
public Db_conn Conn() {return conn;} private final Db_conn conn;
}

View 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.diffs; import gplx.*; import gplx.dbs.*;
public class Gfdb_diff_db_ {
public static final String
Fld__diff_site = "diff_site" // -1 for single-site merge; 0+ for multiple-site merges where 0+ is defined in a registry
, Fld__diff_time = "diff_time" // -1 for single-time merge; 0+ for multiple-time merges where 0+ is defined in a registry
, Fld__diff_db_trg = "diff_db_trg" // -1 for single-db tables; 0+ for multiple-db tables
, Fld__diff_db_src = "diff_db_src" // -1 for I,U,D; 0+ for M
, Fld__diff_type = "diff_type" // I,U,D,M
, Fld__diff_uid = "diff_uid" // 0+
;
public static final byte
Tid__insert = 0
, Tid__update = 1
, Tid__delete = 2
, Tid__move = 3
;
}

View 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.diffs; import gplx.*; import gplx.dbs.*;
public class Gfdb_diff_tbl {
public Gfdb_diff_tbl(String name, Db_meta_fld[] keys, Db_meta_fld[] vals, Db_rdr rdr) {
this.name = name; this.keys = keys; this.vals = vals; this.rdr = rdr;
int keys_len = keys.length; int vals_len = vals.length;
this.flds = new Db_meta_fld[keys_len + vals_len];
for (int i = 0; i < keys_len; ++i)
flds[i] = keys[i];
for (int i = 0; i < vals_len; ++i)
flds[i + keys_len] = vals[i];
}
public String Name() {return name;} private final String name;
public Db_meta_fld[] Flds() {return flds;} private final Db_meta_fld[] flds;
public Db_meta_fld[] Keys() {return keys;} private final Db_meta_fld[] keys;
public Db_meta_fld[] Vals() {return vals;} private final Db_meta_fld[] vals;
public Db_rdr Rdr() {return rdr;} private final Db_rdr rdr;
}

View 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.diffs; import gplx.*; import gplx.dbs.*;
public class Gfdb_rdr_utl_ {
public static int Compare(Db_meta_fld[] flds, int len, Db_rdr lhs_rdr, Db_rdr rhs_rdr) {
int comp = CompareAble_.Same;
for (int i = 0; i < len; ++i) {
Db_meta_fld fld = flds[i];
String fld_name = fld.Name();
switch (fld.Tid()) {
case Db_meta_fld.Tid_bool: comp = Bool_.Compare (lhs_rdr.Read_bool_by_byte(fld_name), rhs_rdr.Read_bool_by_byte(fld_name)); break;
case Db_meta_fld.Tid_int: comp = Int_.Compare (lhs_rdr.Read_int(fld_name) , rhs_rdr.Read_int(fld_name)); break;
case Db_meta_fld.Tid_long: comp = Long_.Compare (lhs_rdr.Read_long(fld_name) , rhs_rdr.Read_long(fld_name)); break;
case Db_meta_fld.Tid_float: comp = Float_.Compare (lhs_rdr.Read_float(fld_name) , rhs_rdr.Read_float(fld_name)); break;
case Db_meta_fld.Tid_double: comp = Double_.Compare (lhs_rdr.Read_double(fld_name) , rhs_rdr.Read_double(fld_name)); break;
case Db_meta_fld.Tid_str: comp = String_.Compare (lhs_rdr.Read_str(fld_name) , rhs_rdr.Read_str(fld_name)); break;
case Db_meta_fld.Tid_bry: comp = Bry_.Compare (lhs_rdr.Read_bry(fld_name) , rhs_rdr.Read_bry(fld_name)); break;
default: throw Err_.new_unhandled(fld.Tid());
}
if (comp != CompareAble_.Same) return comp;
}
return CompareAble_.Same;
}
public static void Stmt_args(Db_stmt stmt, Db_meta_fld[] flds, int len, Db_rdr rdr) {
for (int i = 0; i < len; ++i) {
Db_meta_fld fld = flds[i];
String fld_name = fld.Name();
switch (fld.Tid()) {
case Db_meta_fld.Tid_bool: stmt.Val_bool_as_byte (fld_name, rdr.Read_bool_by_byte(fld_name)); break;
case Db_meta_fld.Tid_byte: stmt.Val_byte (fld_name, rdr.Read_byte(fld_name)); break;
case Db_meta_fld.Tid_int: stmt.Val_int (fld_name, rdr.Read_int(fld_name)); break;
case Db_meta_fld.Tid_long: stmt.Val_long (fld_name, rdr.Read_long(fld_name)); break;
case Db_meta_fld.Tid_float: stmt.Val_float (fld_name, rdr.Read_float(fld_name)); break;
case Db_meta_fld.Tid_double: stmt.Val_double (fld_name, rdr.Read_double(fld_name)); break;
case Db_meta_fld.Tid_str: stmt.Val_str (fld_name, rdr.Read_str(fld_name)); break;
case Db_meta_fld.Tid_bry: stmt.Val_bry (fld_name, rdr.Read_bry(fld_name)); break;
default: throw Err_.new_unhandled(fld.Tid());
}
}
}
}

View File

@@ -0,0 +1,38 @@
/*
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.diffs.builds; import gplx.*; import gplx.dbs.*; import gplx.dbs.diffs.*;
import gplx.dbs.*;
public class Gfdb_diff_bldr {
private Gfdb_diff_rdr_comparer rdr_comparer = new Gfdb_diff_rdr_comparer();
private Gfdb_diff_wkr diff_wkr;
public void Init(Gfdb_diff_wkr diff_wkr) {this.diff_wkr = diff_wkr;}
public void Compare(Gfdb_diff_tbl lhs_tbl, Gfdb_diff_tbl rhs_tbl) {
rdr_comparer.Init(lhs_tbl, rhs_tbl);
diff_wkr.Init_tbls(lhs_tbl, rhs_tbl);
boolean loop = true;
while (loop) {
int rslt = rdr_comparer.Compare();
switch (rslt) {
case Gfdb_diff_rdr_comparer.Rslt__same: diff_wkr.Handle_same(); break;
case Gfdb_diff_rdr_comparer.Rslt__lhs_missing: diff_wkr.Handle_lhs_missing(); break;
case Gfdb_diff_rdr_comparer.Rslt__rhs_missing: diff_wkr.Handle_rhs_missing(); break;
case Gfdb_diff_rdr_comparer.Rslt__done: loop = false; break;
}
}
}
}

View File

@@ -0,0 +1,123 @@
/*
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.diffs.builds; import gplx.*; import gplx.dbs.*; import gplx.dbs.diffs.*;
import org.junit.*;
import gplx.dbs.*; import gplx.dbs.engines.mems.*;
public class Gfdb_diff_bldr_tst {
private final Gfdb_diff_bldr_fxt fxt = new Gfdb_diff_bldr_fxt();
@Test public void Same() {
fxt.Init__tbl__lhs(Object_.Ary(1, "A") , Object_.Ary(2, "B"));
fxt.Init__tbl__rhs(Object_.Ary(1, "A") , Object_.Ary(2, "B"));
fxt.Test__bld();
}
@Test public void Update() {
fxt.Init__tbl__lhs(Object_.Ary(1, "A") , Object_.Ary(2, "B"));
fxt.Init__tbl__rhs(Object_.Ary(1, "A1") , Object_.Ary(2, "B1"));
fxt.Test__bld("U|1|A1", "U|2|B1");
}
@Test public void Insert() {
fxt.Init__tbl__lhs(Object_.Ary(1, "A"));
fxt.Init__tbl__rhs(Object_.Ary(1, "A") , Object_.Ary(2, "B"));
fxt.Test__bld("I|2|B");
}
@Test public void Delete() {
fxt.Init__tbl__lhs(Object_.Ary(1, "A") , Object_.Ary(2, "B"));
fxt.Init__tbl__rhs(Object_.Ary(1, "A"));
fxt.Test__bld("D|2");
}
@Test public void Basic() {
fxt.Init__tbl__lhs
( Object_.Ary(1, "A")
, Object_.Ary(2, "B")
, Object_.Ary(3, "C")
);
fxt.Init__tbl__rhs
( Object_.Ary(1, "A")
, Object_.Ary(2, "B1")
, Object_.Ary(4, "D")
);
fxt.Test__bld("U|2|B1", "D|3", "I|4|D");
}
}
class Gfdb_diff_bldr_fxt {
private final Gfdb_diff_bldr bldr = new Gfdb_diff_bldr();
private final Gfdb_diff_wkr__test wkr = new Gfdb_diff_wkr__test();
private final Db_meta_fld[] key_flds, val_flds;
private Gfdb_diff_tbl lhs_tbl, rhs_tbl;
public Gfdb_diff_bldr_fxt() {
Db_meta_fld_list fld_list = new Db_meta_fld_list();
fld_list.Add_int("id");
key_flds = fld_list.To_fld_ary();
fld_list.Clear();
fld_list.Add_str("val", 255);
val_flds = fld_list.To_fld_ary();
bldr.Init(wkr);
}
public void Init__tbl__lhs(Object[]... rows) {this.lhs_tbl = Make__tbl(key_flds, val_flds, rows);}
public void Init__tbl__rhs(Object[]... rows) {this.rhs_tbl = Make__tbl(key_flds, val_flds, rows);}
public void Test__bld(String... expd) {
bldr.Compare(lhs_tbl, rhs_tbl);
Tfds.Eq_ary_str(expd, wkr.To_str_ary());
}
private static Gfdb_diff_tbl Make__tbl(Db_meta_fld[] keys, Db_meta_fld[] vals, Object[][] rows) {
int keys_len = keys.length; int vals_len = vals.length;
int cols_len = keys_len + vals_len;
String[] cols = new String[cols_len];
for (int i = 0; i < keys_len; ++i)
cols[i] = keys[i].Name();
for (int i = 0; i < vals_len; ++i)
cols[i + keys_len] = vals[i].Name();
int rows_len = rows.length;
Mem_row[] mem_rows = new Mem_row[rows_len];
for (int i = 0; i < rows_len; ++i) {
Object[] row = rows[i];
Mem_row mem_row = new Mem_row();
mem_rows[i] = mem_row;
for (int j = 0; j < cols_len; ++j) {
Object cell = row[j];
mem_row.Add(cols[j], cell);
}
}
Db_rdr rdr = new Db_rdr__mem(cols, mem_rows);
return new Gfdb_diff_tbl("tbl1", keys, vals, rdr);
}
}
class Gfdb_diff_wkr__test implements Gfdb_diff_wkr {
private final List_adp list = List_adp_.new_();
private final Bry_bfr bfr = Bry_bfr.new_();
private Db_rdr lhs_rdr, rhs_rdr;
public void Init_tbls(Gfdb_diff_tbl lhs_tbl, Gfdb_diff_tbl rhs_tbl) {
this.lhs_rdr = lhs_tbl.Rdr(); this.rhs_rdr = rhs_tbl.Rdr();
}
public void Term_tbls() {}
public void Handle_same() {
String lhs_val = lhs_rdr.Read_str("val");
String rhs_val = rhs_rdr.Read_str("val");
if (!String_.Eq(lhs_val, rhs_val))
list.Add(bfr.Add_str_a7("U").Add_byte_pipe().Add_obj(lhs_rdr.Read_obj("id")).Add_byte_pipe().Add_str_a7(rhs_val).To_str_and_clear());
}
public void Handle_lhs_missing() {
String rhs_val = rhs_rdr.Read_str("val");
list.Add(bfr.Add_str_a7("I").Add_byte_pipe().Add_obj(rhs_rdr.Read_obj("id")).Add_byte_pipe().Add_str_a7(rhs_val).To_str_and_clear());
}
public void Handle_rhs_missing() {
list.Add(bfr.Add_str_a7("D").Add_byte_pipe().Add_obj(lhs_rdr.Read_obj("id")).To_str_and_clear());
}
public String[] To_str_ary() {return list.To_str_ary_and_clear();}
}

View File

@@ -0,0 +1,66 @@
/*
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.diffs.builds; import gplx.*; import gplx.dbs.*; import gplx.dbs.diffs.*;
class Gfdb_diff_rdr_comparer {
private Db_rdr lhs_rdr, rhs_rdr;
private boolean lhs_rdr_move, rhs_rdr_move;
private boolean lhs_rdr_done, rhs_rdr_done;
private Db_meta_fld[] key_flds; private int key_flds_len;
public void Init(Gfdb_diff_tbl lhs_tbl, Gfdb_diff_tbl rhs_tbl) {
this.lhs_rdr = lhs_tbl.Rdr(); this.rhs_rdr = rhs_tbl.Rdr();
this.lhs_rdr_move = rhs_rdr_move = Bool_.Y;
this.lhs_rdr_done = rhs_rdr_done = Bool_.N;
this.key_flds = rhs_tbl.Keys(); key_flds_len = key_flds.length;
}
public int Compare() {
if (lhs_rdr_move) {
lhs_rdr_move = lhs_rdr.Move_next();
if (!lhs_rdr_move) lhs_rdr_done = true;
}
if (rhs_rdr_move) {
rhs_rdr_move = rhs_rdr.Move_next();
if (!rhs_rdr_move) rhs_rdr_done = true;
}
if (lhs_rdr_done && rhs_rdr_done) return Gfdb_diff_rdr_comparer.Rslt__done;
else if (lhs_rdr_done) {rhs_rdr_move = true; return Gfdb_diff_rdr_comparer.Rslt__lhs_missing;}
else if (rhs_rdr_done) {lhs_rdr_move = true; return Gfdb_diff_rdr_comparer.Rslt__rhs_missing;}
else {
int comp = Gfdb_rdr_utl_.Compare(key_flds, key_flds_len, lhs_rdr, rhs_rdr);
switch (comp) {
case CompareAble_.Same: // lhs == rhs; move both
lhs_rdr_move = rhs_rdr_move = true;
return Gfdb_diff_rdr_comparer.Rslt__same;
case CompareAble_.Less: // lhs < rhs; EX: lhs == 2; rhs == 3
lhs_rdr_move = true;
rhs_rdr_move = false;
return Gfdb_diff_rdr_comparer.Rslt__rhs_missing;
case CompareAble_.More: // lhs > rhs; EX: lhs == 4; rhs == 3
lhs_rdr_move = false;
rhs_rdr_move = true;
return Gfdb_diff_rdr_comparer.Rslt__lhs_missing;
default: throw Err_.new_unhandled(comp);
}
}
}
public static final int
Rslt__same = 0
, Rslt__lhs_missing = 1
, Rslt__rhs_missing = 2
, Rslt__done = 3
;
}

View File

@@ -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.dbs.diffs.builds; import gplx.*; import gplx.dbs.*; import gplx.dbs.diffs.*;
public interface Gfdb_diff_wkr {
void Init_tbls(Gfdb_diff_tbl lhs, Gfdb_diff_tbl rhs);
void Term_tbls();
void Handle_same();
void Handle_lhs_missing();
void Handle_rhs_missing();
}

View File

@@ -0,0 +1,82 @@
/*
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.diffs.builds; import gplx.*; import gplx.dbs.*; import gplx.dbs.diffs.*;
class Gfdb_diff_wkr__db implements Gfdb_diff_wkr {
private Gfdb_diff_tbl lhs_tbl, rhs_tbl;
private Db_meta_fld[] val_flds; private int val_flds_len;
private Db_conn diff_conn; private Db_stmt stmt;
private int uid__upsert, uid__delete; private int prog_interval, prog_count;
public void Init_conn(Gfdb_diff_db diff_db, int prog_interval) {this.diff_conn = diff_db.Conn(); this.prog_interval = prog_interval;}
public void Init_tbls(Gfdb_diff_tbl lhs_tbl, Gfdb_diff_tbl rhs_tbl) {
this.lhs_tbl = lhs_tbl; this.rhs_tbl = rhs_tbl;
this.val_flds = lhs_tbl.Vals(); val_flds_len = val_flds.length;
this.uid__upsert = 0; uid__delete = 0; this.prog_count = 0;
String tbl_name = rhs_tbl.Name();
Db_meta_fld[] diff_flds = Gfdb_diff_wkr__db_.New_diff_flds(rhs_tbl.Flds());
if (!diff_conn.Meta_tbl_exists(tbl_name)) diff_conn.Ddl_create_tbl(Db_meta_tbl.new_(tbl_name, diff_flds));
this.stmt = diff_conn.Stmt_insert(tbl_name, Gfdb_diff_wkr__db_.To_str_ary(diff_flds));
diff_conn.Txn_bgn("diff_db");
}
public void Term_tbls() {
diff_conn.Txn_end();
}
public void Handle_same() {
if (Gfdb_rdr_utl_.Compare(val_flds, val_flds_len, lhs_tbl.Rdr(), rhs_tbl.Rdr()) != CompareAble_.Same)
Insert(Gfdb_diff_db_.Tid__update, uid__upsert++, rhs_tbl.Flds(), rhs_tbl.Rdr());
}
public void Handle_lhs_missing() {Insert(Gfdb_diff_db_.Tid__insert, uid__upsert++, rhs_tbl.Flds(), rhs_tbl.Rdr());}
public void Handle_rhs_missing() {Insert(Gfdb_diff_db_.Tid__delete, uid__delete++, lhs_tbl.Keys(), lhs_tbl.Rdr());}
private void Insert(byte diff_type, int uid, Db_meta_fld[] flds, Db_rdr rdr) {
stmt.Val_int (Gfdb_diff_db_.Fld__diff_site , -1)
.Val_int (Gfdb_diff_db_.Fld__diff_time , -1)
.Val_int (Gfdb_diff_db_.Fld__diff_db_trg , -1)
.Val_int (Gfdb_diff_db_.Fld__diff_db_src , -1)
.Val_byte (Gfdb_diff_db_.Fld__diff_type , diff_type)
.Val_int (Gfdb_diff_db_.Fld__diff_type , uid)
;
Gfdb_rdr_utl_.Stmt_args(stmt, flds, flds.length, rdr);
stmt.Exec_insert();
if ((++prog_count % prog_interval) == 0) diff_conn.Txn_sav();
}
}
class Gfdb_diff_wkr__db_ {
public static Db_meta_fld[] New_diff_flds(Db_meta_fld[] all_flds) {
int len = all_flds.length;
int system_flds = 6;
Db_meta_fld[] rv = new Db_meta_fld[len + system_flds];
rv[0] = Db_meta_fld.new_int (Gfdb_diff_db_.Fld__diff_site);
rv[1] = Db_meta_fld.new_int (Gfdb_diff_db_.Fld__diff_time);
rv[2] = Db_meta_fld.new_int (Gfdb_diff_db_.Fld__diff_db_trg);
rv[3] = Db_meta_fld.new_int (Gfdb_diff_db_.Fld__diff_db_src);
rv[4] = Db_meta_fld.new_byte(Gfdb_diff_db_.Fld__diff_type);
rv[5] = Db_meta_fld.new_int (Gfdb_diff_db_.Fld__diff_uid);
for (int i = 0; i < len; ++i) {
Db_meta_fld orig_fld = all_flds[i];
Db_meta_fld diff_fld = new Db_meta_fld(orig_fld.Name(), orig_fld.Tid(), orig_fld.Len()).Nullable_y_(); // keep same name and type, but make nullable
all_flds[i + system_flds] = diff_fld;
}
return rv;
}
public static String[] To_str_ary(Db_meta_fld[] ary) {
int len = ary.length;
String[] rv = new String[len];
for (int i = 0; i < len; ++i)
rv[i] = ary[i].Name();
return rv;
}
}

View File

@@ -0,0 +1,123 @@
/*
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.diffs.merges; import gplx.*; import gplx.dbs.*; import gplx.dbs.diffs.*;
import gplx.core.brys.fmtrs.*;
interface Gfdb_diff_cmd {
void Merge_undo();
void Merge_exec();
}
class Gfdb_diff_cmd__insert {
// else if I
// // txn_bgn
// // audit
// INSERT INTO db_temp.page (diff_type, diff, *)
// SELECT 'D', d.page_id, *
// FROM db_diff.page d
// JOIN db_main.page m ON d.page_id = m.page_id
// WHERE d.diff_type = 0
// AND d.diff_idx BETWEEN lo and hi
//
// // update
// INSERT INTO db_main.page
// SELECT d.page_title
// FROM db_diff.page d
// JOIN db_main.page m
// // txn_end
private Db_conn conn;
private String exec_sql;
public void Init(Db_conn main_conn, Db_conn diff_conn, Db_conn temp_conn, Gfdb_diff_tbl tbl) {
this.conn = temp_conn;
this.exec_sql = "";
// this.exec_sql = String_.Format(String_.Concat_lines_nl_skip_last
// ( "INSERT INTO db_curr.{tbl}"
// ), Gfdb_diff_cmd_ctx.Alias__curr);
}
public void Merge_exec() {
conn.Exec_sql(exec_sql);
}
}
class Gfdb_diff_cmd_sql_bldr {
private final Bry_fmtr fmtr = Bry_fmtr.new_();
private final Bry_bfr tmp_bfr = Bry_bfr.new_();
public void Bld_insert(Bry_bfr bfr, String tbl_name, String[] keys, String[] vals, int rng_bgn, int rng_end) {
fmtr.Fmt_(Insert__fmt).Keys_(Insert__keys);
fmtr.Bld_bfr_many(bfr, tbl_name, Bld_flds(tmp_bfr, ", ", "d.", keys, vals), Bld_join(keys), rng_bgn, rng_end);
}
public void Bld_update(Bry_bfr bfr, String tbl_name, String[] keys, String[] vals, int rng_bgn, int rng_end) {
fmtr.Fmt_(Update__fmt).Keys_(Update__keys);
fmtr.Bld_bfr_many(bfr, tbl_name, Bld_flds(tmp_bfr, ", ", "d.", keys, vals), Bld_join(keys), rng_bgn, rng_end);
}
public void Bld_delete(Bry_bfr bfr, String tbl_name, String[] keys, int rng_bgn, int rng_end) {
fmtr.Fmt_(Delete__fmt).Keys_(Delete__keys);
fmtr.Bld_bfr_many(bfr, tbl_name, Bld_flds(tmp_bfr, " || '|' || ", "", keys, String_.Ary_empty), Bld_flds(tmp_bfr, " || '|' || ", "k.", keys, String_.Ary_empty), Bld_join(keys), rng_bgn, rng_end);
}
private static String Bld_flds(Bry_bfr tmp_bfr, String dlm, String alias, String[] keys, String[] vals) {
int keys_len = keys.length;
for (int i = 0; i < keys_len; ++i) {
String key = keys[i];
if (i != 0) tmp_bfr.Add_str_a7(dlm);
tmp_bfr.Add_str_a7(alias).Add_str_a7(key);
}
int flds_len = vals.length;
for (int i = 0; i < flds_len; ++i) {
String val = vals[i];
tmp_bfr.Add_str_a7(dlm);
tmp_bfr.Add_str_a7(alias).Add_str_a7(val);
}
return tmp_bfr.To_str_and_clear();
}
private String Bld_join(String[] keys) {
int len = keys.length;
for (int i = 0; i < len; ++i) {
String key = keys[i];
tmp_bfr.Add_str_a7(i == 0 ? " ON " : " AND ");
tmp_bfr.Add_str_a7("k.").Add_str_a7(key).Add_str_a7(" = ");
tmp_bfr.Add_str_a7("d.").Add_str_a7(key);
}
return tmp_bfr.To_str_and_clear();
}
private static final String[] Insert__keys = String_.Ary("tbl", "flds", "join", "rng_bgn", "rng_end");
private static final String Insert__fmt = String_.Concat_lines_nl_skip_last
( "INSERT INTO db_curr.~{tbl}"
, "SELECT ~{flds}"
, "FROM db_temp.~{tbl}_pkey k"
, " JOIN db_diff.~{tbl} d~{join}"
, "WHERE k.diff_type = 1"
, "AND k.diff_uid BETWEEN ~{rng_bgn} AND ~{rng_end};"
);
private static final String[] Update__keys = String_.Ary("tbl", "flds", "join", "rng_bgn", "rng_end");
private static final String Update__fmt = String_.Concat_lines_nl_skip_last
( "REPLACE INTO db_curr.~{tbl}"
, "SELECT ~{flds}"
, "FROM db_temp.~{tbl}_pkey k"
, " JOIN db_diff.~{tbl} d~{join}"
, "WHERE k.diff_type = 2"
, "AND k.diff_uid BETWEEN ~{rng_bgn} AND ~{rng_end};"
);
private static final String[] Delete__keys = String_.Ary("tbl", "pkey_where", "pkey_select", "join", "rng_bgn", "rng_end");
private static final String Delete__fmt = String_.Concat_lines_nl_skip_last
( "DELETE db_curr.~{tbl}"
, "WHERE ~{pkey_where} IN"
, "( SELECT ~{pkey_select}"
, " FROM db_temp.~{tbl}_pkey k"
, " JOIN db_diff.~{tbl} d~{join}"
, " WHERE k.diff_type = 0"
, " AND k.diff_uid BETWEEN ~{rng_bgn} AND ~{rng_end}"
, ");"
);
}

View File

@@ -0,0 +1,71 @@
/*
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.diffs.merges; import gplx.*; import gplx.dbs.*; import gplx.dbs.diffs.*;
import org.junit.*;
import gplx.dbs.*; import gplx.dbs.engines.mems.*;
public class Gfdb_diff_cmd_sql_bldr_tst {
private final Gfdb_diff_cmd_sql_bldr_fxt fxt = new Gfdb_diff_cmd_sql_bldr_fxt();
@Test public void Insert() {
fxt.Test__insert("tbl1", String_.Ary("key1", "key2"), String_.Ary("fld1", "fld2"), 0, 99, String_.Concat_lines_nl_skip_last
( "INSERT INTO db_curr.tbl1"
, "SELECT d.key1, d.key2, d.fld1, d.fld2"
, "FROM db_temp.tbl1_pkey k"
, " JOIN db_diff.tbl1 d ON k.key1 = d.key1 AND k.key2 = d.key2"
, "WHERE k.diff_type = 1"
, "AND k.diff_uid BETWEEN 0 AND 99;"
));
}
@Test public void Update() {
fxt.Test__update("tbl1", String_.Ary("key1", "key2"), String_.Ary("fld1", "fld2"), 0, 99, String_.Concat_lines_nl_skip_last
( "REPLACE INTO db_curr.tbl1"
, "SELECT d.key1, d.key2, d.fld1, d.fld2"
, "FROM db_temp.tbl1_pkey k"
, " JOIN db_diff.tbl1 d ON k.key1 = d.key1 AND k.key2 = d.key2"
, "WHERE k.diff_type = 2"
, "AND k.diff_uid BETWEEN 0 AND 99;"
));
}
@Test public void Delete() {
fxt.Test__delete("tbl1", String_.Ary("key1", "key2"), 0, 99, String_.Concat_lines_nl_skip_last
( "DELETE db_curr.tbl1"
, "WHERE key1 || '|' || key2 IN"
, "( SELECT k.key1 || '|' || k.key2"
, " FROM db_temp.tbl1_pkey k"
, " JOIN db_diff.tbl1 d ON k.key1 = d.key1 AND k.key2 = d.key2"
, " WHERE k.diff_type = 0"
, " AND k.diff_uid BETWEEN 0 AND 99"
, ");"
));
}
}
class Gfdb_diff_cmd_sql_bldr_fxt {
private Gfdb_diff_cmd_sql_bldr bldr = new Gfdb_diff_cmd_sql_bldr();
private final Bry_bfr bfr = Bry_bfr.new_();
public void Test__insert(String tbl_name, String[] keys, String[] flds, int rng_bgn, int rng_end, String expd) {
bldr.Bld_insert(bfr, tbl_name, keys, flds, 0, 99);
Tfds.Eq_str_lines(expd, bfr.To_str_and_clear());
}
public void Test__update(String tbl_name, String[] keys, String[] flds, int rng_bgn, int rng_end, String expd) {
bldr.Bld_update(bfr, tbl_name, keys, flds, 0, 99);
Tfds.Eq_str_lines(expd, bfr.To_str_and_clear());
}
public void Test__delete(String tbl_name, String[] keys, int rng_bgn, int rng_end, String expd) {
bldr.Bld_delete(bfr, tbl_name, keys, 0, 99);
Tfds.Eq_str_lines(expd, bfr.To_str_and_clear());
}
}