View source
<?php
class ARC_rdf_store_merge_table_creator {
var $version = "0.1.0";
var $created = array();
function __construct(&$api) {
$this->api =& $api;
$this->config = $this->api
->get_config();
if (!isset($this->config["prop_tables"])) {
$this->config["prop_tables"] = array();
}
}
function ARC_rdf_store_merge_table_creator(&$api) {
$this
->__construct($api);
}
function tables_created() {
return $this->created ? true : false;
}
function get_merge_tables() {
if ($this->config["store_type"] == "basic") {
return array();
}
return $this->config["store_type"] == "basic+" ? array(
"triple_all_wdup",
) : array(
"triple_all",
"triple_all_wdup",
"triple_dp_all",
"triple_op_all",
);
}
function create_merge_tables() {
mysql_query("FLUSH TABLES");
foreach ($this
->get_merge_tables() as $cur_tbl) {
$tbl_name = $this->config["prefix"] . "_" . $cur_tbl;
if (!mysql_query("SELECT 1 FROM " . $tbl_name . " LIMIT 0")) {
$cur_mthd = "create_" . $cur_tbl . "_table";
$tmp = $this
->{$cur_mthd}($tbl_name, $this->config["prefix"], $this->config["prop_tables"]);
}
}
$this->created = true;
return true;
}
function drop_merge_tables() {
mysql_query("FLUSH TABLES");
foreach ($this
->get_merge_tables() as $cur_tbl) {
$tbl_name = $this->config["prefix"] . "_" . $cur_tbl;
if (mysql_query("SELECT 1 FROM " . $tbl_name . " LIMIT 0") || mysql_error()) {
$tmp = mysql_query("DROP TABLE " . $tbl_name);
}
}
$this->created = false;
return true;
}
function create_merge_table($tbl_name, $engine_info = "") {
$id_type = $this->config["id_type"];
$id_code = strpos($id_type, "_int") ? "bigint(20)" : ($id_type == "hash_md5" ? "char(21) BINARY" : "char(26) BINARY");
$s_init_code = $this->config["reversible_consolidation"] ? "s_init " . $id_code . " NOT NULL," : "";
$o_init_code = $this->config["reversible_consolidation"] ? "o_init " . $id_code . " NOT NULL," : "";
$index_type = $this->config["index_type"];
$index_graph_iris = $this->config["index_graph_iris"];
if ($index_type == "basic" && !$index_graph_iris) {
$index_code = "KEY spo (s,p,o), KEY so (s,o), KEY po (p,o), KEY o (o)";
}
elseif ($index_type == "basic" && $index_graph_iris) {
$index_code = "KEY spog (s,p,o,g), KEY sog (s,o,g), KEY pog (p,o,g), KEY gsp (g,s,p),\tKEY pg (p,g), KEY og (o,g)";
}
elseif ($index_type == "advanced" && !$index_graph_iris) {
$index_code = "KEY spo (s,p,o), KEY so (s,o), KEY po (p,o), KEY sp (s,p), KEY s (s), KEY p (p), KEY o (o), KEY o_comp (o_comp)";
}
if ($index_type == "advanced" && $index_graph_iris) {
$index_code = " KEY spog (s,p,o,g), KEY sog (s,o,g), KEY pog (p,o,g), KEY gsp (g,s,p), KEY sg (s,g), KEY pg (p,g), KEY og (o,g), KEY o_comp (o_comp)";
}
$charset_code = isset($this->config["charset"]) && strlen($this->config["charset"]) ? " CHARACTER SET " . $this->config["charset"] : "";
$collation_code = isset($this->config["charset_collation"]) && strlen($this->config["charset_collation"]) ? " COLLATE " . $this->config["charset_collation"] : "";
$temporary = "";
$sql = "\n\t\t\tCREATE" . $temporary . " TABLE " . $tbl_name . " (\n\t\t\t\ts " . $id_code . " NOT NULL,\n\t\t\t\tp " . $id_code . " NOT NULL,\n\t\t\t\to " . $id_code . " NOT NULL,\n\t\t\t\tg " . $id_code . " NOT NULL,\n\t\t\t\ts_type tinyint(1) NOT NULL default '0',\n\t\t\t\t" . $s_init_code . "\n\t\t\t\to_type tinyint(1) NOT NULL default '0',\n\t\t\t\t" . $o_init_code . "\n\t\t\t\to_lang char(8) NULL,\n\t\t\t\to_dt " . $id_code . " NOT NULL,\n\t\t\t\to_comp char(35) NULL,\n\t\t\t\tmisc tinyint(2) NOT NULL default '0',\n\t\t\t\t" . $index_code . "\n\t\t\t) ENGINE=" . $engine_info . $charset_code . $collation_code . ";\n\t\t";
mysql_query($sql);
}
function create_triple_all_table($tbl_name, $prefix, $prop_tbl_infos) {
$engine_info = "MERGE UNION=(" . $prefix . "_triple_op," . $prefix . "_triple_dp";
foreach ($prop_tbl_infos as $cur_info) {
$engine_info .= $cur_info["prop_type"] == "obj" ? "," . $prefix . "_triple_op_" . $cur_info["name"] : "," . $prefix . "_triple_dp_" . $cur_info["name"];
}
$engine_info .= ")";
return $this
->create_merge_table($tbl_name, $engine_info);
}
function create_triple_all_wdup_table($tbl_name, $prefix, $prop_tbl_infos) {
if ($this->config["store_type"] == "basic+") {
$engine_info = "MERGE UNION=(" . $prefix . "_triple," . $prefix . "_triple_dup)";
}
else {
$engine_info = "MERGE UNION=(" . $prefix . "_triple_op," . $prefix . "_triple_dp";
foreach ($prop_tbl_infos as $cur_info) {
$engine_info .= $cur_info["prop_type"] == "obj" ? "," . $prefix . "_triple_op_" . $cur_info["name"] : "," . $prefix . "_triple_dp_" . $cur_info["name"];
}
$engine_info .= "," . $prefix . "_triple_dup)";
}
$this
->create_merge_table($tbl_name, $engine_info);
}
function create_triple_dp_all_table($tbl_name, $prefix, $prop_tbl_infos) {
$engine_info = "MERGE UNION=(" . $prefix . "_triple_dp";
foreach ($prop_tbl_infos as $cur_info) {
$engine_info .= $cur_info["prop_type"] == "dt" ? "," . $prefix . "_triple_dp_" . $cur_info["name"] : "";
}
$engine_info .= ")";
$this
->create_merge_table($tbl_name, $engine_info);
}
function create_triple_op_all_table($tbl_name, $prefix, $prop_tbl_infos) {
$engine_info = "MERGE UNION=(" . $prefix . "_triple_op";
foreach ($prop_tbl_infos as $cur_info) {
$engine_info .= $cur_info["prop_type"] == "obj" ? "," . $prefix . "_triple_op_" . $cur_info["name"] : "";
}
$engine_info .= ")";
$this
->create_merge_table($tbl_name, $engine_info);
}
}