Skip to content

Commit

Permalink
refactor: store tags against object id instead of hash
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewhilton committed Aug 20, 2024
1 parent d1032bc commit d9edca4
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion classes/local/report/tag_count_report_builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function build_report($reportid) {
SUM(objects.filesize) as objectsum
FROM {tool_objectfs_objects} objects
LEFT JOIN {tool_objectfs_object_tags} object_tags
ON objects.contenthash = object_tags.contenthash
ON objects.id = object_tags.objectid
GROUP BY object_tags.tagkey, object_tags.tagvalue
";
$result = $DB->get_records_sql($sql);
Expand Down
7 changes: 5 additions & 2 deletions classes/local/tag/tag_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,17 @@ public static function gather_object_tags_for_upload(string $contenthash): array
public static function store_tags_locally(string $contenthash, array $tags) {
global $DB;

// Lookup object id.
$objectid = $DB->get_field('tool_objectfs_objects', 'id', ['contenthash' => $contenthash]);

// Purge any existing tags for this object.
$DB->delete_records('tool_objectfs_object_tags', ['contenthash' => $contenthash]);
$DB->delete_records('tool_objectfs_object_tags', ['objectid' => $objectid]);

// Store new records.
$recordstostore = [];
foreach ($tags as $key => $value) {
$recordstostore[] = [
'contenthash' => $contenthash,
'objectid' => $objectid,
'tagkey' => $key,
'tagvalue' => $value,
];
Expand Down
4 changes: 2 additions & 2 deletions db/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@
<TABLE NAME="tool_objectfs_object_tags" COMMENT="Object tag key value pairs">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="contenthash" TYPE="char" LENGTH="40" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="objectid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="tagkey" TYPE="char" LENGTH="32" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="tagvalue" TYPE="char" LENGTH="128" NOTNULL="true" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
</KEYS>
<INDEXES>
<INDEX NAME="objecttagkey_idx" UNIQUE="true" FIELDS="contenthash, tagkey"/>
<INDEX NAME="objecttagkey_idx" UNIQUE="true" FIELDS="objectid, tagkey"/>
</INDEXES>
</TABLE>
</TABLES>
Expand Down
4 changes: 2 additions & 2 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,15 @@ function xmldb_tool_objectfs_upgrade($oldversion) {

// Adding fields to table tool_objectfs_object_tags.
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('contenthash', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
$table->add_field('objectid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('tagkey', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
$table->add_field('tagvalue', XMLDB_TYPE_CHAR, '128', null, XMLDB_NOTNULL, null, null);

// Adding keys to table tool_objectfs_object_tags.
$table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);

// Adding indexes to table tool_objectfs_object_tags.
$table->add_index('objecttagkey_idx', XMLDB_INDEX_UNIQUE, ['contenthash', 'tagkey']);
$table->add_index('objecttagkey_idx', XMLDB_INDEX_UNIQUE, ['objectid', 'tagkey']);

// Conditionally launch create table for tool_objectfs_object_tags.
if (!$dbman->table_exists($table)) {
Expand Down
8 changes: 4 additions & 4 deletions tests/local/tagging_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,16 @@ public function test_store_tags_locally() {
'test1' => 'abc',
'test2' => 'xyz',
];
$hash = 'thisisatest';
$object = $this->create_remote_object();

// Ensure no tags for hash intially.
$this->assertEmpty($DB->get_records('tool_objectfs_object_tags', ['contenthash' => $hash]));
$this->assertEmpty($DB->get_records('tool_objectfs_object_tags', ['objectid' => $object->id]));

// Store.
tag_manager::store_tags_locally($hash, $tags);
tag_manager::store_tags_locally($object->contenthash, $tags);

// Confirm they are stored.
$queriedtags = $DB->get_records('tool_objectfs_object_tags', ['contenthash' => $hash]);
$queriedtags = $DB->get_records('tool_objectfs_object_tags', ['objectid' => $object->id]);
$this->assertCount(2, $queriedtags);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/object_file_system_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1093,14 +1093,14 @@ public function test_push_object_tags_replicated(bool $canoverride) {
$this->assertCount(count($testtags), $tags);

// But tags will not be stored locally (yet).
$localtags = $DB->get_records('tool_objectfs_object_tags', ['contenthash' => $object->contenthash]);
$localtags = $DB->get_records('tool_objectfs_object_tags', ['objectid' => $object->id]);
$this->assertCount(0, $localtags);

// Sync the file.
$this->filesystem->push_object_tags($object->contenthash);

// Tags should now be replicated locally.
$localtags = $DB->get_records('tool_objectfs_object_tags', ['contenthash' => $object->contenthash]);
$localtags = $DB->get_records('tool_objectfs_object_tags', ['objectid' => $object->id]);
$externaltags = $this->filesystem->get_external_client()->get_object_tags($object->contenthash);
$time = $DB->get_field('tool_objectfs_objects', 'tagslastpushed', ['id' => $object->id]);

Expand Down

0 comments on commit d9edca4

Please sign in to comment.