Skip to content

Commit

Permalink
MDL-53213 caching: Faster databasemeta caching for all databases.
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-russ committed Feb 28, 2016
1 parent 03b8b55 commit d22a01f
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 127 deletions.
1 change: 1 addition & 0 deletions lib/db/caches.php
Expand Up @@ -58,6 +58,7 @@
'requireidentifiers' => array(
'dbfamily'
),
'simpledata' => true, // This is a read only class, so leaving references in place is safe.
'staticacceleration' => true,
'staticaccelerationsize' => 15
),
Expand Down
5 changes: 2 additions & 3 deletions lib/ddl/sql_generator.php
Expand Up @@ -226,11 +226,10 @@ public function table_exists($table) {
$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]);
}

/**
Expand Down
174 changes: 78 additions & 96 deletions lib/dml/database_column_info.php
Expand Up @@ -33,128 +33,110 @@
* @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
*/
public $type;

/**
* 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
*/
public $max_length;

/**
* Scale
* float - decimal points
* other - null
* @var int
*/
public $scale;

/**
* True if not null, false otherwise
* @var bool
*/
public $not_null;

/**
* True if column is primary key.
* (usually 'id').
* @var bool
* @var array The internal storage of column data.
*/
public $primary_key;
protected $data;

/**
* True if filed autoincrementing
* (usually 'id' only)
* @var bool
* 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 $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;
public function __set($name, $value) {
throw new coding_exception('database_column_info is a ready only object to allow for faster caching.');
}

/**
* True if field values are unique, false if not.
* @var bool
* 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 $unique;
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.').');
}

/**
* 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
* 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 $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;
}
}
Expand Down
18 changes: 15 additions & 3 deletions lib/dml/moodle_database.php
Expand Up @@ -324,6 +324,18 @@ protected function get_settings_hash() {
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.
Expand Down Expand Up @@ -1032,9 +1044,9 @@ protected abstract function normalise_value($column, $value);
*/
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;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/dml/moodle_temptables.php
Expand Up @@ -90,7 +90,7 @@ public function delete_temptable($tablename) {
* @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;
}

/**
Expand Down
6 changes: 2 additions & 4 deletions lib/dml/mssql_native_moodle_database.php
Expand Up @@ -438,9 +438,7 @@ public function get_indexes($table) {
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;
}
}
Expand Down Expand Up @@ -536,7 +534,7 @@ public function get_columns($table, $usecache=true) {
$this->free_result($result);

if ($usecache) {
$cache->set($table, $structure);
$this->get_metacache()->set($table, $structure);
}

return $structure;
Expand Down
6 changes: 2 additions & 4 deletions lib/dml/mysqli_native_moodle_database.php
Expand Up @@ -578,9 +578,7 @@ public function get_indexes($table) {
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;
}
}
Expand Down Expand Up @@ -686,7 +684,7 @@ public function get_columns($table, $usecache=true) {
}

if ($usecache) {
$cache->set($table, $structure);
$this->get_metacache()->set($table, $structure);
}

return $structure;
Expand Down
6 changes: 2 additions & 4 deletions lib/dml/oci_native_moodle_database.php
Expand Up @@ -470,9 +470,7 @@ public function get_indexes($table) {
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;
}
}
Expand Down Expand Up @@ -664,7 +662,7 @@ public function get_columns($table, $usecache=true) {
}

if ($usecache) {
$cache->set($table, $structure);
$this->get_metacache()->set($table, $structure);
}

return $structure;
Expand Down
6 changes: 2 additions & 4 deletions lib/dml/pgsql_native_moodle_database.php
Expand Up @@ -390,9 +390,7 @@ public function get_indexes($table) {
*/
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;
}
}
Expand Down Expand Up @@ -596,7 +594,7 @@ public function get_columns($table, $usecache=true) {
pg_free_result($result);

if ($usecache) {
$cache->set($table, $structure);
$this->get_metacache()->set($table, $structure);
}

return $structure;
Expand Down
6 changes: 2 additions & 4 deletions lib/dml/sqlite3_pdo_moodle_database.php
Expand Up @@ -201,9 +201,7 @@ public function get_indexes($table) {
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;
}
}
Expand Down Expand Up @@ -300,7 +298,7 @@ public function get_columns($table, $usecache=true) {
}

if ($usecache) {
$cache->set($table, $structure);
$this->get_metacache()->set($table, $structure);
}

return $structure;
Expand Down
6 changes: 2 additions & 4 deletions lib/dml/sqlsrv_native_moodle_database.php
Expand Up @@ -508,9 +508,7 @@ public function get_indexes($table) {
*/
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;
}
}
Expand Down Expand Up @@ -606,7 +604,7 @@ public function get_columns($table, $usecache = true) {
$this->free_result($result);

if ($usecache) {
$cache->set($table, $structure);
$this->get_metacache()->set($table, $structure);
}

return $structure;
Expand Down

0 comments on commit d22a01f

Please sign in to comment.