PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/docroot/transform/app/arc/store/ARC2_StoreTableManager.php

https://github.com/mterenzio/FollowThis
PHP | 287 lines | 232 code | 35 blank | 20 comment | 14 complexity | bcfba3e31524c2e41f3dcfcd0cfe93a0 MD5 | raw file
  1. <?php
  2. /**
  3. * ARC2 RDF Store Table Manager
  4. *
  5. * @license http://arc.semsol.org/license
  6. * @author Benjamin Nowack
  7. * @version 2009-09-07 Tweak: store_engine_type is a config option
  8. *
  9. */
  10. ARC2::inc('Store');
  11. class ARC2_StoreTableManager extends ARC2_Store {
  12. function __construct($a = '', &$caller) {
  13. parent::__construct($a, $caller);
  14. }
  15. function ARC2_StoreTableManager($a = '', &$caller) {
  16. $this->__construct($a, $caller);
  17. }
  18. function __init() {/* db_con */
  19. parent::__init();
  20. $this->engine_type = $this->v('store_engine_type', 'MyISAM', $this->a);
  21. }
  22. /* */
  23. function getTableOptionsCode() {
  24. $v = $this->getDBVersion();
  25. $r = "";
  26. $r .= (($v < '04-01-00') && ($v >= '04-00-18')) ? 'ENGINE' : (($v >= '04-01-02') ? 'ENGINE' : 'TYPE');
  27. $r .= "=" . $this->engine_type;
  28. $r .= ($v >= '04-00-00') ? " CHARACTER SET utf8" : "";
  29. $r .= ($v >= '04-01-00') ? " COLLATE utf8_unicode_ci" : "";
  30. $r .= " DELAY_KEY_WRITE = 1";
  31. return $r;
  32. }
  33. /* */
  34. function createTables() {
  35. $con = $this->getDBCon();
  36. if(!$this->createTripleTable()) {
  37. return $this->addError('Could not create "triple" table (' . mysql_error($con) . ').');
  38. }
  39. if(!$this->createG2TTable()) {
  40. return $this->addError('Could not create "g2t" table (' . mysql_error($con) . ').');
  41. }
  42. if(!$this->createID2ValTable()) {
  43. return $this->addError('Could not create "id2val" table (' . mysql_error($con) . ').');
  44. }
  45. if(!$this->createS2ValTable()) {
  46. return $this->addError('Could not create "s2val" table (' . mysql_error($con) . ').');
  47. }
  48. if(!$this->createO2ValTable()) {
  49. return $this->addError('Could not create "o2val" table (' . mysql_error($con) . ').');
  50. }
  51. if(!$this->createSettingTable()) {
  52. return $this->addError('Could not create "setting" table (' . mysql_error($con) . ').');
  53. }
  54. return 1;
  55. }
  56. /* */
  57. function createTripleTable($suffix = 'triple') {
  58. /* keep in sync with merge def in StoreQueryHandler ! */
  59. $indexes = $this->v('store_indexes', array('sp (s,p)', 'os (o,s)', 'po (p,o)'), $this->a);
  60. $index_code = $indexes ? 'KEY ' . join(', KEY ', $indexes) . ', ' : '';
  61. $sql = "
  62. CREATE TABLE IF NOT EXISTS " . $this->getTablePrefix() . $suffix . " (
  63. t mediumint UNSIGNED NOT NULL,
  64. s mediumint UNSIGNED NOT NULL,
  65. p mediumint UNSIGNED NOT NULL,
  66. o mediumint UNSIGNED NOT NULL,
  67. o_lang_dt mediumint UNSIGNED NOT NULL,
  68. o_comp char(35) NOT NULL, /* normalized value for ORDER BY operations */
  69. s_type tinyint(1) NOT NULL default 0, /* uri/bnode => 0/1 */
  70. o_type tinyint(1) NOT NULL default 0, /* uri/bnode/literal => 0/1/2 */
  71. misc tinyint(1) NOT NULL default 0, /* temporary flags */
  72. UNIQUE KEY (t), " . $index_code . " KEY (misc)
  73. ) ". $this->getTableOptionsCode() . "
  74. ";
  75. return mysql_query($sql, $this->getDBCon());
  76. }
  77. function extendTripleTableColumns($suffix = 'triple') {
  78. $sql = "
  79. ALTER TABLE " . $this->getTablePrefix() . $suffix . "
  80. MODIFY t int(10) UNSIGNED NOT NULL,
  81. MODIFY s int(10) UNSIGNED NOT NULL,
  82. MODIFY p int(10) UNSIGNED NOT NULL,
  83. MODIFY o int(10) UNSIGNED NOT NULL,
  84. MODIFY o_lang_dt int(10) UNSIGNED NOT NULL
  85. ";
  86. return mysql_query($sql, $this->getDBCon());
  87. }
  88. /* */
  89. function createG2TTable() {
  90. $sql = "
  91. CREATE TABLE IF NOT EXISTS " . $this->getTablePrefix() . "g2t (
  92. g mediumint UNSIGNED NOT NULL,
  93. t mediumint UNSIGNED NOT NULL,
  94. UNIQUE KEY gt (g,t), KEY tg (t,g)
  95. ) ". $this->getTableOptionsCode() . "
  96. ";
  97. return mysql_query($sql, $this->getDBCon());
  98. }
  99. function extendG2tTableColumns($suffix = 'g2t') {
  100. $sql = "
  101. ALTER TABLE " . $this->getTablePrefix() . $suffix . "
  102. MODIFY g int(10) UNSIGNED NOT NULL,
  103. MODIFY t int(10) UNSIGNED NOT NULL
  104. ";
  105. return mysql_query($sql, $this->getDBCon());
  106. }
  107. /* */
  108. function createID2ValTable() {
  109. $sql = "
  110. CREATE TABLE IF NOT EXISTS " . $this->getTablePrefix() . "id2val (
  111. id mediumint UNSIGNED NOT NULL,
  112. misc tinyint(1) NOT NULL default 0,
  113. val text NOT NULL,
  114. val_type tinyint(1) NOT NULL default 0, /* uri/bnode/literal => 0/1/2 */
  115. UNIQUE KEY (id,val_type), KEY v (val(64))
  116. ) ". $this->getTableOptionsCode() . "
  117. ";
  118. return mysql_query($sql, $this->getDBCon());
  119. }
  120. function extendId2valTableColumns($suffix = 'id2val') {
  121. $sql = "
  122. ALTER TABLE " . $this->getTablePrefix() . $suffix . "
  123. MODIFY id int(10) UNSIGNED NOT NULL
  124. ";
  125. return mysql_query($sql, $this->getDBCon());
  126. }
  127. /* */
  128. function createS2ValTable() {
  129. $sql = "
  130. CREATE TABLE IF NOT EXISTS " . $this->getTablePrefix() . "s2val (
  131. id mediumint UNSIGNED NOT NULL,
  132. misc tinyint(1) NOT NULL default 0,
  133. val text NOT NULL,
  134. UNIQUE KEY (id), KEY v (val(64))
  135. ) ". $this->getTableOptionsCode() . "
  136. ";
  137. return mysql_query($sql, $this->getDBCon());
  138. }
  139. function extendS2valTableColumns($suffix = 's2val') {
  140. $sql = "
  141. ALTER TABLE " . $this->getTablePrefix() . $suffix . "
  142. MODIFY id int(10) UNSIGNED NOT NULL
  143. ";
  144. return mysql_query($sql, $this->getDBCon());
  145. }
  146. /* */
  147. function createO2ValTable() {
  148. $sql = "
  149. CREATE TABLE IF NOT EXISTS " . $this->getTablePrefix() . "o2val (
  150. id mediumint UNSIGNED NOT NULL,
  151. misc tinyint(1) NOT NULL default 0,
  152. val text NOT NULL,
  153. UNIQUE KEY (id), KEY v (val(64))
  154. ) ". $this->getTableOptionsCode() . "
  155. ";
  156. return mysql_query($sql, $this->getDBCon());
  157. }
  158. function extendO2valTableColumns($suffix = 'o2val') {
  159. $sql = "
  160. ALTER TABLE " . $this->getTablePrefix() . $suffix . "
  161. MODIFY id int(10) UNSIGNED NOT NULL
  162. ";
  163. return mysql_query($sql, $this->getDBCon());
  164. }
  165. /* */
  166. function createSettingTable() {
  167. $sql = "
  168. CREATE TABLE IF NOT EXISTS " . $this->getTablePrefix() . "setting (
  169. k char(32) NOT NULL,
  170. val text NOT NULL,
  171. UNIQUE KEY (k)
  172. ) ". $this->getTableOptionsCode() . "
  173. ";
  174. return mysql_query($sql, $this->getDBCon());
  175. }
  176. /* */
  177. function extendColumns() {
  178. $con = $this->getDBCon();
  179. $tbl_prefix = $this->getTablePrefix();
  180. $tbls = $this->getTables();
  181. foreach ($tbls as $suffix) {
  182. if (preg_match('/^(triple|g2t|id2val|s2val|o2val)/', $suffix, $m)) {
  183. $mthd = 'extend' . ucfirst($m[1]) . 'TableColumns';
  184. $this->$mthd($suffix);
  185. }
  186. }
  187. }
  188. /* */
  189. function splitTables() {
  190. $old_ps = $this->getSetting('split_predicates', array());
  191. $new_ps = $this->retrieveSplitPredicates();
  192. $add_ps = array_diff($new_ps, $old_ps);
  193. $del_ps = array_diff($old_ps, $new_ps);
  194. $final_ps = array();
  195. foreach ($del_ps as $p) {
  196. if (!$this->unsplitPredicate($p)) $final_ps[] = $p;
  197. }
  198. foreach ($add_ps as $p) {
  199. if ($this->splitPredicate($p)) $final_ps[] = $p;
  200. }
  201. $this->setSetting('split_predicates', $new_ps);
  202. }
  203. function unsplitPredicate($p) {
  204. $suffix = 'triple_' . abs(crc32($p));
  205. $old_tbl = $this->getTablePrefix() . $suffix;
  206. $new_tbl = $this->getTablePrefix() . 'triple';
  207. $p_id = $this->getTermID($p, 'p');
  208. $con = $this->getDBCon();
  209. $sql = '
  210. INSERT IGNORE INTO ' . $new_tbl .'
  211. SELECT * FROM ' . $old_tbl . ' WHERE ' . $old_tbl . '.p = ' . $p_id . '
  212. ';
  213. if ($rs = mysql_query($sql, $con)) {
  214. mysql_query('DROP TABLE ' . $old_tbl, $con);
  215. return 1;
  216. }
  217. else {
  218. return 0;
  219. }
  220. }
  221. function splitPredicate($p) {
  222. $suffix = 'triple_' . abs(crc32($p));
  223. $this->createTripleTable($suffix);
  224. $old_tbl = $this->getTablePrefix() . 'triple';
  225. $new_tbl = $this->getTablePrefix() . $suffix;
  226. $p_id = $this->getTermID($p, 'p');
  227. $con = $this->getDBCon();
  228. $sql = '
  229. INSERT IGNORE INTO ' . $new_tbl .'
  230. SELECT * FROM ' . $old_tbl . ' WHERE ' . $old_tbl . '.p = ' . $p_id . '
  231. ';
  232. if ($rs = mysql_query($sql, $con)) {
  233. mysql_query('DELETE FROM ' . $old_tbl . ' WHERE ' . $old_tbl . '.p = ' . $p_id, $con);
  234. return 1;
  235. }
  236. else {
  237. mysql_query('DROP TABLE ' . $new_tbl, $con);
  238. return 0;
  239. }
  240. }
  241. function retrieveSplitPredicates() {
  242. $r = $this->split_predicates;
  243. $limit = $this->max_split_tables - count($r);
  244. $q = 'SELECT ?p COUNT(?p) AS ?pc WHERE { ?s ?p ?o } GROUP BY ?p ORDER BY DESC(?pc) LIMIT ' . $limit;
  245. $rows = $this->query($q, 'rows');
  246. foreach ($rows as $row) {
  247. $r[] = $row['p'];
  248. }
  249. return $r;
  250. }
  251. /* */
  252. }