MDL-53213 caching: Faster databasemeta caching for all databases.
This commit is contained in:
@@ -58,6 +58,7 @@ $definitions = array(
|
||||
'requireidentifiers' => array(
|
||||
'dbfamily'
|
||||
),
|
||||
'simpledata' => true, // This is a read only class, so leaving references in place is safe.
|
||||
'staticacceleration' => true,
|
||||
'staticaccelerationsize' => 15
|
||||
),
|
||||
|
||||
@@ -226,11 +226,10 @@ abstract class sql_generator {
|
||||
$tablename = $table->getName();
|
||||
}
|
||||
|
||||
// get all tables in moodle database
|
||||
// Get all tables in moodle database.
|
||||
$tables = $this->mdb->get_tables();
|
||||
$exists = in_array($tablename, $tables);
|
||||
|
||||
return $exists;
|
||||
return isset($tables[$tablename]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -33,128 +33,110 @@ defined('MOODLE_INTERNAL') || die();
|
||||
* @package core_dml
|
||||
* @copyright 2008 Petr Skoda (http://skodak.org)
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*
|
||||
* @property-read string $name Name of column - lowercase.
|
||||
* @property-read string $type Driver dependent native data type. Not standardised, it's used to find meta_type.
|
||||
*
|
||||
* Max length:
|
||||
* character type - number of characters
|
||||
* blob - number of bytes
|
||||
* integer - number of digits
|
||||
* float - digits left from floating point
|
||||
* boolean - 1
|
||||
* @property-read int $max_length size of the database field, eg how much data can you put in there.
|
||||
*
|
||||
* @property-read int $scale Scale of field, decimal points (float), null otherwise.
|
||||
* @property-read bool $not_null true if the field is set to NOT NULL.
|
||||
* @property-read bool $primary_key true if the field is the primary key. (usually 'id').
|
||||
* @property-read bool $auto_increment True if field is autoincrementing or sequence.
|
||||
* @property-read bool $binary True if the field is binary.
|
||||
* @property-read bool $has_default True if the default value is defined.
|
||||
* @property-read string $default_value The default value (if defined).
|
||||
* @property-read bool $unique True if the field values are unique, false if not.
|
||||
*
|
||||
* Standardised one character column type, uppercased and enumerated as follows:
|
||||
* R - counter (integer primary key)
|
||||
* I - integers
|
||||
* N - numbers (floats)
|
||||
* C - characters and strings
|
||||
* X - texts
|
||||
* B - binary blobs
|
||||
* L - boolean (1 bit)
|
||||
* T - timestamp - unsupported
|
||||
* D - date - unsupported
|
||||
* @property-read string $meta_type Standardised one character column type, uppercased and enumerated: R,I,N,C,X,B,L,T,D
|
||||
*/
|
||||
class database_column_info {
|
||||
/**
|
||||
* Name of column - lowercase.
|
||||
* @var string
|
||||
*/
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* Driver dependent native data type.
|
||||
* Not standardised, its used to find meta_type.
|
||||
* @var string
|
||||
* @var array The internal storage of column data.
|
||||
*/
|
||||
public $type;
|
||||
protected $data;
|
||||
|
||||
/**
|
||||
* Max length:
|
||||
* character type - number of characters
|
||||
* blob - number of bytes
|
||||
* integer - number of digits
|
||||
* float - digits left from floating point
|
||||
* boolean - 1
|
||||
* @var int
|
||||
* Magic set function. This is a read only object and you aren't allowed to write to any variables.
|
||||
*
|
||||
* @param string $name ignored.
|
||||
* @param mixed $value ignored.
|
||||
* @throws coding_exception You are not allowed to set data on database_column_info
|
||||
*/
|
||||
public $max_length;
|
||||
public function __set($name, $value) {
|
||||
throw new coding_exception('database_column_info is a ready only object to allow for faster caching.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Scale
|
||||
* float - decimal points
|
||||
* other - null
|
||||
* @var int
|
||||
* Magic get function.
|
||||
*
|
||||
* @param string $variablename variable name to return the value of.
|
||||
* @return mixed The variable contents.
|
||||
*
|
||||
* @throws coding_exception You cannot request a variable that is not allowed.
|
||||
*/
|
||||
public $scale;
|
||||
public function __get($variablename) {
|
||||
if (isset($this->data[$variablename]) || array_key_exists($variablename, $this->data)) {
|
||||
return $this->data[$variablename];
|
||||
}
|
||||
throw new coding_exception('Asked for a variable that is not available . ('.$variablename.').');
|
||||
}
|
||||
|
||||
/**
|
||||
* True if not null, false otherwise
|
||||
* @var bool
|
||||
* Magic isset function.
|
||||
*
|
||||
* @param string $variablename The name of the property to test if isset().
|
||||
* @return bool Whether the value is set or not.
|
||||
*/
|
||||
public $not_null;
|
||||
|
||||
/**
|
||||
* True if column is primary key.
|
||||
* (usually 'id').
|
||||
* @var bool
|
||||
*/
|
||||
public $primary_key;
|
||||
|
||||
/**
|
||||
* True if filed autoincrementing
|
||||
* (usually 'id' only)
|
||||
* @var bool
|
||||
*/
|
||||
public $auto_increment;
|
||||
|
||||
/**
|
||||
* True if binary
|
||||
* @var bool
|
||||
*/
|
||||
public $binary;
|
||||
|
||||
/**
|
||||
* True if integer unsigned, false if signed.
|
||||
* Null for other types
|
||||
* @var integer
|
||||
* @deprecated since 2.3
|
||||
*/
|
||||
public $unsigned;
|
||||
|
||||
/**
|
||||
* True if the default value is defined.
|
||||
* @var bool
|
||||
*/
|
||||
public $has_default;
|
||||
|
||||
/**
|
||||
* The default value (if defined).
|
||||
* @var string
|
||||
*/
|
||||
public $default_value;
|
||||
|
||||
/**
|
||||
* True if field values are unique, false if not.
|
||||
* @var bool
|
||||
*/
|
||||
public $unique;
|
||||
|
||||
/**
|
||||
* Standardised one character column type, uppercased and enumerated as follows:
|
||||
* R - counter (integer primary key)
|
||||
* I - integers
|
||||
* N - numbers (floats)
|
||||
* C - characters and strings
|
||||
* X - texts
|
||||
* B - binary blobs
|
||||
* L - boolean (1 bit)
|
||||
* T - timestamp - unsupported
|
||||
* D - date - unsupported
|
||||
* @var string
|
||||
*/
|
||||
public $meta_type;
|
||||
public function __isset($variablename) {
|
||||
return isset($this->data[$variablename]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param mixed $data object or array with properties
|
||||
*/
|
||||
public function __construct($data) {
|
||||
foreach ($data as $key=>$value) {
|
||||
if (property_exists($this, $key)) {
|
||||
$this->$key = $value;
|
||||
// Initialize all the allowed variables to null so the array key exists.
|
||||
$validelements = array('name', 'type', 'max_length', 'scale', 'not_null', 'primary_key',
|
||||
'auto_increment', 'binary', 'has_default', 'default_value',
|
||||
'unique', 'meta_type');
|
||||
foreach ($validelements as $element) {
|
||||
if (isset($data->$element)) {
|
||||
$this->data[$element] = $data->$element;
|
||||
} else {
|
||||
$this->data[$element] = null;
|
||||
}
|
||||
}
|
||||
|
||||
switch ($this->meta_type) {
|
||||
switch ($this->data['meta_type']) {
|
||||
case 'R': // normalise counters (usually 'id')
|
||||
$this->binary = false;
|
||||
$this->has_default = false;
|
||||
$this->default_value = null;
|
||||
$this->unique = true;
|
||||
$this->data['binary'] = false;
|
||||
$this->data['has_default'] = false;
|
||||
$this->data['default_value'] = null;
|
||||
$this->data['unique'] = true;
|
||||
break;
|
||||
case 'C':
|
||||
$this->auto_increment = false;
|
||||
$this->binary = false;
|
||||
$this->data['auto_increment'] = false;
|
||||
$this->data['binary'] = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -324,6 +324,18 @@ abstract class moodle_database {
|
||||
return $this->settingshash;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the creation and caching of the databasemeta information for all databases.
|
||||
*
|
||||
* TODO MDL-53267 impelement caching of cache::make() results when it's safe to do so.
|
||||
*
|
||||
* @return cache_application The databasemeta cachestore to complete operations on.
|
||||
*/
|
||||
protected function get_metacache() {
|
||||
$properties = array('dbfamily' => $this->get_dbfamily(), 'settings' => $this->get_settings_hash());
|
||||
return cache::make('core', 'databasemeta', $properties);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to create the database
|
||||
* @param string $dbhost The database host.
|
||||
@@ -1032,9 +1044,9 @@ abstract class moodle_database {
|
||||
*/
|
||||
public function reset_caches() {
|
||||
$this->tables = null;
|
||||
// Purge MUC as well
|
||||
$identifiers = array('dbfamily' => $this->get_dbfamily(), 'settings' => $this->get_settings_hash());
|
||||
cache_helper::purge_by_definition('core', 'databasemeta', $identifiers);
|
||||
// Purge MUC as well.
|
||||
$this->get_metacache()->purge();
|
||||
$this->metacache = null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -90,7 +90,7 @@ class moodle_temptables {
|
||||
* @return array containing all the tablenames in the store (tablename both key and value)
|
||||
*/
|
||||
public function get_temptables() {
|
||||
return array_keys($this->temptables);
|
||||
return $this->temptables;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -438,9 +438,7 @@ class mssql_native_moodle_database extends moodle_database {
|
||||
public function get_columns($table, $usecache=true) {
|
||||
|
||||
if ($usecache) {
|
||||
$properties = array('dbfamily' => $this->get_dbfamily(), 'settings' => $this->get_settings_hash());
|
||||
$cache = cache::make('core', 'databasemeta', $properties);
|
||||
if ($data = $cache->get($table)) {
|
||||
if ($data = $this->get_metacache()->get($table)) {
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -536,7 +534,7 @@ class mssql_native_moodle_database extends moodle_database {
|
||||
$this->free_result($result);
|
||||
|
||||
if ($usecache) {
|
||||
$cache->set($table, $structure);
|
||||
$this->get_metacache()->set($table, $structure);
|
||||
}
|
||||
|
||||
return $structure;
|
||||
|
||||
@@ -578,9 +578,7 @@ class mysqli_native_moodle_database extends moodle_database {
|
||||
public function get_columns($table, $usecache=true) {
|
||||
|
||||
if ($usecache) {
|
||||
$properties = array('dbfamily' => $this->get_dbfamily(), 'settings' => $this->get_settings_hash());
|
||||
$cache = cache::make('core', 'databasemeta', $properties);
|
||||
if ($data = $cache->get($table)) {
|
||||
if ($data = $this->get_metacache()->get($table)) {
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -686,7 +684,7 @@ class mysqli_native_moodle_database extends moodle_database {
|
||||
}
|
||||
|
||||
if ($usecache) {
|
||||
$cache->set($table, $structure);
|
||||
$this->get_metacache()->set($table, $structure);
|
||||
}
|
||||
|
||||
return $structure;
|
||||
|
||||
@@ -470,9 +470,7 @@ class oci_native_moodle_database extends moodle_database {
|
||||
public function get_columns($table, $usecache=true) {
|
||||
|
||||
if ($usecache) {
|
||||
$properties = array('dbfamily' => $this->get_dbfamily(), 'settings' => $this->get_settings_hash());
|
||||
$cache = cache::make('core', 'databasemeta', $properties);
|
||||
if ($data = $cache->get($table)) {
|
||||
if ($data = $this->get_metacache()->get($table)) {
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -664,7 +662,7 @@ class oci_native_moodle_database extends moodle_database {
|
||||
}
|
||||
|
||||
if ($usecache) {
|
||||
$cache->set($table, $structure);
|
||||
$this->get_metacache()->set($table, $structure);
|
||||
}
|
||||
|
||||
return $structure;
|
||||
|
||||
@@ -390,9 +390,7 @@ class pgsql_native_moodle_database extends moodle_database {
|
||||
*/
|
||||
public function get_columns($table, $usecache=true) {
|
||||
if ($usecache) {
|
||||
$properties = array('dbfamily' => $this->get_dbfamily(), 'settings' => $this->get_settings_hash());
|
||||
$cache = cache::make('core', 'databasemeta', $properties);
|
||||
if ($data = $cache->get($table)) {
|
||||
if ($data = $this->get_metacache()->get($table)) {
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -596,7 +594,7 @@ class pgsql_native_moodle_database extends moodle_database {
|
||||
pg_free_result($result);
|
||||
|
||||
if ($usecache) {
|
||||
$cache->set($table, $structure);
|
||||
$this->get_metacache()->set($table, $structure);
|
||||
}
|
||||
|
||||
return $structure;
|
||||
|
||||
@@ -201,9 +201,7 @@ class sqlite3_pdo_moodle_database extends pdo_moodle_database {
|
||||
public function get_columns($table, $usecache=true) {
|
||||
|
||||
if ($usecache) {
|
||||
$properties = array('dbfamily' => $this->get_dbfamily(), 'settings' => $this->get_settings_hash());
|
||||
$cache = cache::make('core', 'databasemeta', $properties);
|
||||
if ($data = $cache->get($table)) {
|
||||
if ($data = $this->get_metacache()->get($table)) {
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -300,7 +298,7 @@ class sqlite3_pdo_moodle_database extends pdo_moodle_database {
|
||||
}
|
||||
|
||||
if ($usecache) {
|
||||
$cache->set($table, $structure);
|
||||
$this->get_metacache()->set($table, $structure);
|
||||
}
|
||||
|
||||
return $structure;
|
||||
|
||||
@@ -508,9 +508,7 @@ class sqlsrv_native_moodle_database extends moodle_database {
|
||||
*/
|
||||
public function get_columns($table, $usecache = true) {
|
||||
if ($usecache) {
|
||||
$properties = array('dbfamily' => $this->get_dbfamily(), 'settings' => $this->get_settings_hash());
|
||||
$cache = cache::make('core', 'databasemeta', $properties);
|
||||
if ($data = $cache->get($table)) {
|
||||
if ($data = $this->get_metacache()->get($table)) {
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -606,7 +604,7 @@ class sqlsrv_native_moodle_database extends moodle_database {
|
||||
$this->free_result($result);
|
||||
|
||||
if ($usecache) {
|
||||
$cache->set($table, $structure);
|
||||
$this->get_metacache()->set($table, $structure);
|
||||
}
|
||||
|
||||
return $structure;
|
||||
|
||||
Reference in New Issue
Block a user