From d9edca4275485eb9e7311eedf4d65d4f7f645e0b Mon Sep 17 00:00:00 2001 From: Matthew Hilton Date: Tue, 20 Aug 2024 14:47:10 +1000 Subject: [PATCH] refactor: store tags against object id instead of hash --- classes/local/report/tag_count_report_builder.php | 2 +- classes/local/tag/tag_manager.php | 7 +++++-- db/install.xml | 4 ++-- db/upgrade.php | 4 ++-- tests/local/tagging_test.php | 8 ++++---- tests/object_file_system_test.php | 4 ++-- 6 files changed, 16 insertions(+), 13 deletions(-) diff --git a/classes/local/report/tag_count_report_builder.php b/classes/local/report/tag_count_report_builder.php index 0364b3fc..bbc22f51 100644 --- a/classes/local/report/tag_count_report_builder.php +++ b/classes/local/report/tag_count_report_builder.php @@ -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); diff --git a/classes/local/tag/tag_manager.php b/classes/local/tag/tag_manager.php index ab7cdb5b..55a61219 100644 --- a/classes/local/tag/tag_manager.php +++ b/classes/local/tag/tag_manager.php @@ -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, ]; diff --git a/db/install.xml b/db/install.xml index 5c5fc2ca..0065b1d7 100644 --- a/db/install.xml +++ b/db/install.xml @@ -54,7 +54,7 @@ - + @@ -62,7 +62,7 @@ - +
diff --git a/db/upgrade.php b/db/upgrade.php index ec997e09..65327746 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -178,7 +178,7 @@ 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); @@ -186,7 +186,7 @@ function xmldb_tool_objectfs_upgrade($oldversion) { $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)) { diff --git a/tests/local/tagging_test.php b/tests/local/tagging_test.php index 23d660f9..d4fc7be6 100644 --- a/tests/local/tagging_test.php +++ b/tests/local/tagging_test.php @@ -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); } diff --git a/tests/object_file_system_test.php b/tests/object_file_system_test.php index bedb05fd..1fb6a785 100644 --- a/tests/object_file_system_test.php +++ b/tests/object_file_system_test.php @@ -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]);