Skip to content

Commit ff33335

Browse files
author
epriestley
committedNov 25, 2016
Create and populate a stopwords table for InnoDB fulltext indexes to use in the future
Summary: Ref T11741. InnoDB uses a stopwords table instead of a stopwords file. During `storage upgrade`, synchronize the table from the stopwords file on disk. Test Plan: - Ran `storage upgrade`. - Ran `select * from stopwords`, saw stopwords. - Added some garbage to the table. - Ran `storage upgrade`, saw it remove it. Reviewers: chad Reviewed By: chad Maniphest Tasks: T11741 Differential Revision: https://secure.phabricator.com/D16940
1 parent a956047 commit ff33335

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CREATE TABLE {$NAMESPACE}_search.stopwords (
2+
value VARCHAR(32) NOT NULL COLLATE {$COLLATE_SORT}
3+
) ENGINE=InnoDB, COLLATE {$COLLATE_TEXT};

‎src/applications/search/storage/PhabricatorSearchSchemaSpec.php

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ final class PhabricatorSearchSchemaSpec
55

66
public function buildSchemata() {
77
$this->buildEdgeSchemata(new PhabricatorProfilePanelConfiguration());
8+
9+
$this->buildRawSchema(
10+
'search',
11+
PhabricatorSearchDocument::STOPWORDS_TABLE,
12+
array(
13+
'value' => 'sort32',
14+
),
15+
array());
816
}
917

1018
}

‎src/applications/search/storage/document/PhabricatorSearchDocument.php

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ final class PhabricatorSearchDocument extends PhabricatorSearchDAO {
77
protected $documentCreated;
88
protected $documentModified;
99

10+
const STOPWORDS_TABLE = 'stopwords';
11+
1012
protected function getConfiguration() {
1113
return array(
1214
self::CONFIG_TIMESTAMPS => false,

‎src/infrastructure/storage/management/workflow/PhabricatorStorageManagementUpgradeWorkflow.php

+53
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ public function didExecute(PhutilArgumentParser $args) {
7777

7878
$this->upgradeSchemata($apis, $apply_only, $no_quickstart, $init_only);
7979

80+
if ($apply_only || $init_only) {
81+
echo tsprintf(
82+
"%s\n",
83+
pht('Declining to synchronize static tables.'));
84+
} else {
85+
echo tsprintf(
86+
"%s\n",
87+
pht('Synchronizing static tables...'));
88+
$this->synchronizeSchemata();
89+
}
90+
8091
if ($no_adjust || $init_only || $apply_only) {
8192
$console->writeOut(
8293
"%s\n",
@@ -93,4 +104,46 @@ public function didExecute(PhutilArgumentParser $args) {
93104
return 0;
94105
}
95106

107+
private function synchronizeSchemata() {
108+
// Synchronize the InnoDB fulltext stopwords table from the stopwords file
109+
// on disk.
110+
111+
$table = new PhabricatorSearchDocument();
112+
$conn = $table->establishConnection('w');
113+
$table_ref = PhabricatorSearchDocument::STOPWORDS_TABLE;
114+
115+
$stopwords_database = queryfx_all(
116+
$conn,
117+
'SELECT value FROM %T',
118+
$table_ref);
119+
$stopwords_database = ipull($stopwords_database, 'value', 'value');
120+
121+
$stopwords_path = phutil_get_library_root('phabricator');
122+
$stopwords_path = $stopwords_path.'/../resources/sql/stopwords.txt';
123+
$stopwords_file = Filesystem::readFile($stopwords_path);
124+
$stopwords_file = phutil_split_lines($stopwords_file, false);
125+
$stopwords_file = array_fuse($stopwords_file);
126+
127+
$rem_words = array_diff_key($stopwords_database, $stopwords_file);
128+
if ($rem_words) {
129+
queryfx(
130+
$conn,
131+
'DELETE FROM %T WHERE value IN (%Ls)',
132+
$table_ref,
133+
$rem_words);
134+
}
135+
136+
$add_words = array_diff_key($stopwords_file, $stopwords_database);
137+
if ($add_words) {
138+
foreach ($add_words as $word) {
139+
queryfx(
140+
$conn,
141+
'INSERT IGNORE INTO %T (value) VALUES (%s)',
142+
$table_ref,
143+
$word);
144+
}
145+
}
146+
}
147+
148+
96149
}

0 commit comments

Comments
 (0)
Failed to load comments.