From 5b798c398550e99b652ae4fcce00361c7e462de4 Mon Sep 17 00:00:00 2001
From: ale <ale@incal.net>
Date: Tue, 16 Feb 2021 21:13:58 +0000
Subject: [PATCH] Update r2db with the most recent code

The configuration has been refactored to be a JSON file. This is
the codebase that has been running in production for the last 3
years, from ai3/docker/noblogs.
---
 db-backends.php | 109 +++++++++++++++++++++++-------------------------
 db-config.php   |   8 ++--
 2 files changed, 57 insertions(+), 60 deletions(-)

diff --git a/db-backends.php b/db-backends.php
index e99f2df..d4cb9f8 100644
--- a/db-backends.php
+++ b/db-backends.php
@@ -1,65 +1,62 @@
 <?php
 
-function noblogs_split_db($file) {
-    $db_url = trim(file_get_contents($file));
-    return parse_url($db_url);
-}
-
-function noblogs_load_backends($db_config_file, $hashptr) {
+function noblogs_load_backends($hashptr) {
   global $wpdb;
-
+  global $noblogs_config;
   $backend_map = array();
-
-  $fp = @fopen($db_config_file, "r");
-  if (!$fp) {
-    die("Database backends not configured!");
-  }
-  while (($line = fgets($fp, 1024)) !== false) {
-    $wline = rtrim($line);
-    if ($wline == "" || $wline[0] == '#') {
-      continue;
-    }
-    $line_parts = explode(" ", $wline);
-    $server_id = $line_parts[0];
-    $dataset = "backend_" . $server_id;
-    $backend_url = $line_parts[2];
-    $backend_url_data = parse_url($backend_url);
-    $backend = array(
-        "host" => $backend_url_data["host"] . ":" . $backend_url_data["port"],
-        "user" => $backend_url_data["user"],
-        "password" => $backend_url_data["pass"],
-        "name" => substr($backend_url_data["path"], 1),
-        "dataset" => $dataset,
-        "read" => 1, "write" => 1, "timeout" => 10
-        );
-    $wpdb->add_database($backend);
-    $hashptr->addTarget($dataset);
-    $backend_map[$dataset] = $backend;
+  foreach ($noblogs_config['db_config']['backends'] as $backend_name => $backend) {
+    $b = array(
+       "host" => $backend['host'] . ":" . $backend['port'],
+       "user" => $backend['user'],
+       "password" => $backend['password'],
+       "name" => $backend['name'],
+       "dataset" => $backend_name,
+       "read" => 1,
+       "write" => 1,
+       "timeout" => 10
+    );
+    $wpdb->add_database($b);
+    $hashptr->addTarget($backend_name);
+    $backend_map[$backend_name] = $b;
   }
-  fclose($fp);
   return $backend_map;
 }
 
-function noblogs_load_global_dataset($master_file, $ip_file) {
-    global $wpdb;
-    $mdata = noblogs_split_db($master_file);
-    $ldata = trim(file_get_contents($ip_file));
-    $globaldb = array(
-        "host" => $mdata["host"] . ":" . $mdata["port"],
-        "user" => $mdata["user"],
-        "password" => $mdata["pass"],
-        "name" => substr($mdata["path"], 1),
-        "dataset" => "global",
-        "read" => 1, "write" => 1, "timeout" => 2
-    );
-    if ($mdata['host'] == $ldata) {
-        $wpdb->add_database($globaldb);
-    } else {
-        $globaldb['read'] = 0;
-        $wpdb->add_database($globaldb);
-        $globaldb['host'] = $ldata . ":" . $mdata["port"];
-        $globaldb['read'] = 1;
-        $globaldb['write'] = 0;
-        $wpdb->add_database($globaldb);
-    }
+function noblogs_load_global_dataset() {
+  global $wpdb;
+  global $noblogs_config;
+  $master = $noblogs_config['db_config']['master'];
+  if ($noblogs_config['db_config']['is_master']) {
+    $wpdb->add_database(array(
+       "host" => "127.0.0.1:" . $master['port'],
+       "user" => $master['user'],
+       "password" => $master['password'],
+       "name" => $master['name'],
+       "dataset" => "global",
+       "read" => 1,
+       "write" => 1,
+       "timeout" => 2
+    ));
+  } else {
+    $wpdb->add_database(array(
+       "host" => $master['host'] . ":" . $master['port'],
+       "user" => $master['user'],
+       "password" => $master['password'],
+       "name" => $master['name'],
+       "dataset" => "global",
+       "read" => 0,
+       "write" => 1,
+       "timeout" => 2
+    ));
+    $wpdb->add_database(array(
+       "host" => "127.0.0.1:" . $master['port'],
+       "user" => $master['user'],
+       "password" => $master['password'],
+       "name" => $master['name'],
+       "dataset" => "global",
+       "read" => 1,
+       "write" => 0,
+       "timeout" => 2
+    ));
+  }
 }
diff --git a/db-config.php b/db-config.php
index 4d71694..85806f0 100644
--- a/db-config.php
+++ b/db-config.php
@@ -8,11 +8,11 @@ $wpdb->max_connections = 30;
  connection errors for the wrong implementation */
 $wpdb->check_tcp_responsiveness = false;
 
-include_once('../r2db/db-hash.php');
-include_once('../r2db/db-backends.php');
+include_once('r2db/db-hash.php');
+include_once('r2db/db-backends.php');
 
 // Set up global dataset with master databases
-noblogs_load_global_dataset(NOBLOGS_MASTER_CONFIG, NOBLOGS_HOST_FILE);
+noblogs_load_global_dataset();
 
 // Add all the sharded blog databases.
-$wpdb_reverse_backend_map = noblogs_load_backends(NOBLOGS_BACKEND_CONFIG, $wpdb->hash_map);
+$wpdb_reverse_backend_map = noblogs_load_backends($wpdb->hash_map);
-- 
GitLab