db-hash.php 555 Bytes
Newer Older
root's avatar
root committed
1 2
<?php

3
require_once(dirname(__FILE__) . '/flexihash.php');
4
require_once( ABSPATH . 'wp-config.php' );
root's avatar
root committed
5

6
$wpdb->hash_map = new Flexihash(null, R2DB_FLEXIHASH_REPLICAS);
root's avatar
root committed
7 8 9 10 11 12

/* Hashing function to map blogs to databases.
 *
 * Implements a consistent hashing scheme using Flexihash.
 */
function noblogs_db_callback($query, $wpdb) {
13
  $wpdb_hash = $wpdb->hash_map;
root's avatar
root committed
14 15 16 17 18 19
  if (preg_match("/^{$wpdb->base_prefix}(\d+)_/", $wpdb->table, $matches)) {
    $blog_id = $matches[1];
    return $wpdb_hash->lookup($blog_id);
  }
}
$wpdb->add_callback('noblogs_db_callback');