PageRenderTime 47ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/v1/mod_semanticweb/arc/store/ARC2_StoreDeleteQueryHandler.php

https://code.google.com/p/goodrelations-for-joomla/
PHP | 196 lines | 150 code | 20 blank | 26 comment | 28 complexity | cccac8af38ae953b896ca56fab6c4c60 MD5 | raw file
  1. <?php
  2. /*
  3. homepage: http://arc.semsol.org/
  4. license: http://arc.semsol.org/license
  5. class: ARC2 RDF Store DELETE Query Handler
  6. author: Benjamin Nowack
  7. version: 2008-08-31 (Tweak: improved cleanTableReferences method (~3 times faster) + necessity check)
  8. */
  9. ARC2::inc('StoreQueryHandler');
  10. class ARC2_StoreDeleteQueryHandler extends ARC2_StoreQueryHandler {
  11. function __construct($a = '', &$caller) {/* caller has to be a store */
  12. parent::__construct($a, $caller);
  13. }
  14. function ARC2_StoreDeleteQueryHandler($a = '', &$caller) {
  15. $this->__construct($a, $caller);
  16. }
  17. function __init() {/* db_con */
  18. parent::__init();
  19. $this->store =& $this->caller;
  20. $this->handler_type = 'delete';
  21. }
  22. /* */
  23. function runQuery($infos) {
  24. $this->infos = $infos;
  25. $con = $this->store->getDBCon();
  26. $t1 = ARC2::mtime();
  27. /* delete */
  28. $this->refs_deleted = false;
  29. if (!$this->v('construct_triples', array(), $this->infos['query'])) {
  30. $tc = $this->deleteTargetGraphs();
  31. }
  32. elseif (!$this->v('pattern', array(), $this->infos['query'])) {
  33. $tc = $this->deleteTriples();
  34. }
  35. else {
  36. $tc = $this->deleteConstructedGraph();
  37. }
  38. $t2 = ARC2::mtime();
  39. /* clean up */
  40. if ($tc && ($this->refs_deleted || (rand(1, 100) == 1))) $this->cleanTableReferences();
  41. if ($tc && (rand(1, 50) == 1)) $this->store->optimizeTables();
  42. if ($tc && (rand(1, 500) == 1)) $this->cleanValueTables();
  43. $t3 = ARC2::mtime();
  44. $index_dur = round($t3 - $t2, 4);
  45. $dur = round($t3 - $t1, 4);
  46. return array(
  47. 't_count' => $tc,
  48. 'delete_time' => $dur,
  49. 'index_update_time' => $index_dur,
  50. );
  51. }
  52. /* */
  53. function deleteTargetGraphs() {
  54. $tbl_prefix = $this->store->getTablePrefix();
  55. $r = 0;
  56. $con = $this->store->getDBCon();
  57. foreach ($this->infos['query']['target_graphs'] as $g) {
  58. if ($g_id = $this->getTermID($g, 'g')) {
  59. $rs = mysql_query('DELETE FROM ' . $tbl_prefix . 'g2t WHERE g = ' .$g_id, $con);
  60. $r += mysql_affected_rows($con);
  61. }
  62. }
  63. $this->refs_deleted = $r ? 1 : 0;
  64. return $r;
  65. }
  66. /* */
  67. function deleteTriples() {
  68. $r = 0;
  69. $dbv = $this->store->getDBVersion();
  70. $tbl_prefix = $this->store->getTablePrefix();
  71. $con = $this->store->getDBCon();
  72. /* graph restriction */
  73. $tgs = $this->infos['query']['target_graphs'];
  74. $gq = '';
  75. foreach ($tgs as $g) {
  76. if ($g_id = $this->getTermID($g, 'g')) {
  77. $gq .= $gq ? ', ' . $g_id : $g_id;
  78. }
  79. }
  80. $gq = $gq ? ' AND G.g IN (' . $gq . ')' : '';
  81. /* triples */
  82. foreach ($this->infos['query']['construct_triples'] as $t) {
  83. $q = '';
  84. $skip = 0;
  85. foreach (array('s', 'p', 'o') as $term) {
  86. if (isset($t[$term . '_type']) && preg_match('/(var)/', $t[$term . '_type'])) {
  87. //$skip = 1;
  88. }
  89. else {
  90. $term_id = $this->getTermID($t[$term], $term);
  91. $q .= $q ? ' AND ' : '';
  92. $q .= 'T.' . $term . '=' . $term_id;
  93. }
  94. }
  95. if ($skip) {
  96. continue;
  97. }
  98. if ($gq) {
  99. $sql = ($dbv < '04-01') ? 'DELETE ' . $tbl_prefix . 'g2t' : 'DELETE G';
  100. $sql .= '
  101. FROM ' . $tbl_prefix . 'g2t G
  102. JOIN ' . $this->getTripleTable() . ' T ON (T.t = G.t' . $gq . ')
  103. WHERE ' . $q . '
  104. ';
  105. $this->refs_deleted = 1;
  106. }
  107. else {/* triples only */
  108. $sql = ($dbv < '04-01') ? 'DELETE ' . $this->getTripleTable() : 'DELETE T';
  109. $sql .= ' FROM ' . $this->getTripleTable() . ' T WHERE ' . $q;
  110. }
  111. $rs = mysql_query($sql, $con);
  112. if ($er = mysql_error($con)) {
  113. $this->addError($er .' in ' . $sql);
  114. }
  115. $r += mysql_affected_rows($con);
  116. }
  117. return $r;
  118. }
  119. /* */
  120. function deleteConstructedGraph() {
  121. ARC2::inc('StoreConstructQueryHandler');
  122. $h =& new ARC2_StoreConstructQueryHandler($this->a, $this->store);
  123. $sub_r = $h->runQuery($this->infos);
  124. $triples = ARC2::getTriplesFromIndex($sub_r);
  125. $tgs = $this->infos['query']['target_graphs'];
  126. $this->infos = array('query' => array('construct_triples' => $triples, 'target_graphs' => $tgs));
  127. return $this->deleteTriples();
  128. }
  129. /* */
  130. function cleanTableReferences() {
  131. /* lock */
  132. if (!$this->store->getLock()) return $this->addError('Could not get lock in "cleanTableReferences"');
  133. $con = $this->store->getDBCon();
  134. $tbl_prefix = $this->store->getTablePrefix();
  135. $dbv = $this->store->getDBVersion();
  136. /* check for unconnected triples */
  137. $sql = '
  138. SELECT T.t FROM '. $tbl_prefix . 'triple T LEFT JOIN '. $tbl_prefix . 'g2t G ON ( G.t = T.t )
  139. WHERE G.t IS NULL LIMIT 1
  140. ';
  141. if (($rs = mysql_query($sql, $con)) && mysql_num_rows($rs)) {
  142. /* delete unconnected triples */
  143. $sql = ($dbv < '04-01') ? 'DELETE ' . $tbl_prefix . 'triple' : 'DELETE T';
  144. $sql .= '
  145. FROM ' . $tbl_prefix . 'triple T
  146. LEFT JOIN ' . $tbl_prefix . 'g2t G ON (G.t = T.t)
  147. WHERE G.t IS NULL
  148. ';
  149. mysql_query($sql, $con);
  150. }
  151. /* check for unconnected graph refs */
  152. if ((rand(1, 100) == 1)) {
  153. $sql = '
  154. SELECT G.g FROM '. $tbl_prefix . 'g2t G LEFT JOIN '. $tbl_prefix . 'triple T ON ( T.t = G.t )
  155. WHERE T.t IS NULL LIMIT 1
  156. ';
  157. if (($rs = mysql_query($sql, $con)) && mysql_num_rows($rs)) {
  158. /* delete unconnected graph refs */
  159. $sql = ($dbv < '04-01') ? 'DELETE ' . $tbl_prefix . 'g2t' : 'DELETE G';
  160. $sql .= '
  161. FROM ' . $tbl_prefix . 'g2t G
  162. LEFT JOIN ' . $tbl_prefix . 'triple T ON (T.t = G.t)
  163. WHERE T.t IS NULL
  164. ';
  165. mysql_query($sql, $con);
  166. }
  167. }
  168. /* release lock */
  169. $this->store->releaseLock();
  170. }
  171. /* */
  172. function cleanValueTables() {
  173. }
  174. /* */
  175. }