]> git.mxchange.org Git - friendica.git/commitdiff
Improve uniqueness loop by adding an exists() call in DisposableFullTextSearch
authorHypolite Petovan <hypolite@mrpetovan.com>
Mon, 29 Jan 2024 11:56:30 +0000 (06:56 -0500)
committerHypolite Petovan <hypolite@mrpetovan.com>
Mon, 29 Jan 2024 11:56:30 +0000 (06:56 -0500)
src/Database/DisposableFullTextSearch.php

index fb8ce900d59e0d62e158811fc6bf53cdddb60cf7..5ef8adebd2bcda448980878b0f54c26c3b3002c2 100644 (file)
@@ -37,12 +37,17 @@ class DisposableFullTextSearch
        {
                $this->db = $database;
 
+               // Unique identifier generation. Two DisposableFullTextSearch object should never have the same as the first object destruction
+               // would delete both check-full-text-search rows before the second object destruction is called, leading to unexpected behavior.
                do {
-                       // Unique identifier generation. Two DisposableFullTextSearch object should never have the same as the first object destruction
-                       // would delete both check-full-text-search rows before the second object destruction is called, leading to unexpected behavior.
                        // Maximum value is indicated by the INT UNSIGNED type of the check-full-text-search.pid field
                        $this->identifier = random_int(0, pow(2, 32) - 1);
-               } while($this->db->insert('check-full-text-search', ['pid' => $this->identifier, 'searchtext' => $haystack]));
+               } while($this->db->exists('check-full-text-search', ['pid' => $this->identifier, 'searchtext' => $haystack]));
+
+               // If the `exists()` call fails and return false because the database is unavailable, the `insert()` call will likely fail as well, which means
+               // all subsequent calls to `match()` will return false because the haystack won't have been inserted.
+               // However, at this point there may be bigger problems to worry about.
+               $this->db->insert('check-full-text-search', ['pid' => $this->identifier, 'searchtext' => $haystack]);
        }
 
        public function __destruct()