From c92e382fc93d1acf4e7042e9f7a865a56d2e54cd Mon Sep 17 00:00:00 2001
From: ale <ale@incal.net>
Date: Mon, 24 May 2021 16:22:43 +0100
Subject: [PATCH] Do not instantiate a second Flexihash just to find the
 backend name

Fixes issue #3.
---
 lib/blogs.php | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)

diff --git a/lib/blogs.php b/lib/blogs.php
index 6978775..cf18bd3 100644
--- a/lib/blogs.php
+++ b/lib/blogs.php
@@ -29,6 +29,7 @@ function noblogs_get_blog($blogname) {
 function noblogs_get_backend_for_blog($blog_id) {
   global $wpdb;
   global $r2db_hash_map;
+  global $wpdb_reverse_backend_map;
 
   // Compatibility shim between old/new r2db.
   $hash_map = $r2db_hash_map;
@@ -36,16 +37,9 @@ function noblogs_get_backend_for_blog($blog_id) {
     $hash_map = $wpdb->hash_map;
   }
 
-  // Create a temporary hash object just to load the backends
-  // (we cannot use the map in $wpdb because it doesn't have the
-  // backend array anymore).
-  $hashmap = new Flexihash(null, R2DB_FLEXIHASH_REPLICAS);
-  $reversemap = noblogs_load_backends($hashmap);
-
-  // Lookup the blog ID using the $wpdb hash just to be safe (though
-  // we should get an identical result using $hashmap).
+  // Lookup the blog ID.
   $lookup = $hash_map->lookup($blog_id);
-  $backend = $reversemap[$lookup];
+  $backend = $wpdb_reverse_backend_map[$lookup];
 
   // Be nice and split the database host into 'host' and 'port' elements.
   $result = array();
-- 
GitLab