/** * Commit new records. * * @return void */ public function commit() { $request = (object) array('pid' => getmypid(), 'threadid' => ZEND_THREAD_SAFE ? zend_thread_id() : null, 'uid' => getmyuid(), 'url' => $this->url->out_as_local_url(false), 'hostname' => gethostname(), 'memory' => memory_get_usage(), 'peakmemory' => memory_get_peak_usage()); // Not supported on Windows until PHP 7 if (function_exists('getrusage')) { $resourceusage = getrusage(); $request->numswaps = $resourceusage['ru_nswap']; $request->numpagefaults = $resourceusage['ru_majflt']; $request->usertime = $resourceusage['ru_utime.tv_usec']; } $request->id = $this->db->insert_record('telemetry_request', $request); foreach ($this->additionalstate as $collector) { $table = $collector->get_table(); $records = $collector->get_records(); foreach ($records as $record) { $record->requestid = $request->id; } $this->db->insert_records($table, $records); } }
/** * This function generates the unique index for a specific lock key. * Once an index is assigned to a key, it never changes - so this is * statically cached. * * @param string $key * @return int * @throws \moodle_exception */ protected function get_index_from_key($key) { if (isset(self::$lockidcache[$key])) { return self::$lockidcache[$key]; } $index = 0; $record = $this->db->get_record('lock_db', array('resourcekey' => $key)); if ($record) { $index = $record->id; } if (!$index) { $record = new \stdClass(); $record->resourcekey = $key; try { $index = $this->db->insert_record('lock_db', $record); } catch (\dml_exception $de) { // Race condition - never mind - now the value is guaranteed to exist. $record = $this->db->get_record('lock_db', array('resourcekey' => $key)); if ($record) { $index = $record->id; } } } if (!$index) { throw new \moodle_exception('Could not generate unique index for key'); } self::$lockidcache[$key] = $index; return $index; }
/** * Create and get a lock * @param string $resource - The identifier for the lock. Should use frankenstyle prefix. * @param int $timeout - The number of seconds to wait for a lock before giving up. * @param int $maxlifetime - Unused by this lock type. * @return boolean - true if a lock was obtained. */ public function get_lock($resource, $timeout, $maxlifetime = 86400) { $token = $this->generate_unique_token(); $now = time(); $giveuptime = $now + $timeout; $expires = $now + $maxlifetime; if (!$this->db->record_exists('lock_db', array('resourcekey' => $resource))) { $record = new \stdClass(); $record->resourcekey = $resource; $result = $this->db->insert_record('lock_db', $record); } $params = array('expires' => $expires, 'token' => $token, 'resourcekey' => $resource, 'now' => $now); $sql = 'UPDATE {lock_db} SET expires = :expires, owner = :token WHERE resourcekey = :resourcekey AND (owner IS NULL OR expires < :now)'; do { $now = time(); $params['now'] = $now; $this->db->execute($sql, $params); $countparams = array('owner' => $token, 'resourcekey' => $resource); $result = $this->db->count_records('lock_db', $countparams); $locked = $result === 1; if (!$locked) { usleep(rand(10000, 250000)); // Sleep between 10 and 250 milliseconds. } // Try until the giveup time. } while (!$locked && $now < $giveuptime); if ($locked) { $this->openlocks[$token] = 1; return new lock($token, $this); } return false; }
public function test_limits_and_offsets() { $DB = $this->tdb; $dbman = $DB->get_manager(); if (false) { $DB = new moodle_database(); } $table = $this->get_test_table(); $tablename = $table->getName(); $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, null); $table->add_field('content', XMLDB_TYPE_TEXT, 'big', XMLDB_UNSIGNED, XMLDB_NOTNULL); $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); $dbman->create_table($table); $this->assertNotEmpty($DB->insert_record($tablename, array('name' => 'a', 'content' => 'one'))); $this->assertNotEmpty($DB->insert_record($tablename, array('name' => 'b', 'content' => 'two'))); $this->assertNotEmpty($DB->insert_record($tablename, array('name' => 'c', 'content' => 'three'))); $this->assertNotEmpty($DB->insert_record($tablename, array('name' => 'd', 'content' => 'four'))); $this->assertNotEmpty($DB->insert_record($tablename, array('name' => 'e', 'content' => 'five'))); $this->assertNotEmpty($DB->insert_record($tablename, array('name' => 'f', 'content' => 'six'))); $sqlqm = "SELECT *\n FROM {{$tablename}}"; $this->assertNotEmpty($records = $DB->get_records_sql($sqlqm, null, 4)); $this->assertEquals(2, count($records)); $this->assertEquals('e', reset($records)->name); $this->assertEquals('f', end($records)->name); $sqlqm = "SELECT *\n FROM {{$tablename}}"; $this->assertEmpty($records = $DB->get_records_sql($sqlqm, null, 8)); $sqlqm = "SELECT *\n FROM {{$tablename}}"; $this->assertNotEmpty($records = $DB->get_records_sql($sqlqm, null, 0, 4)); $this->assertEquals(4, count($records)); $this->assertEquals('a', reset($records)->name); $this->assertEquals('d', end($records)->name); $sqlqm = "SELECT *\n FROM {{$tablename}}"; $this->assertNotEmpty($records = $DB->get_records_sql($sqlqm, null, 0, 8)); $this->assertEquals(6, count($records)); $this->assertEquals('a', reset($records)->name); $this->assertEquals('f', end($records)->name); $sqlqm = "SELECT *\n FROM {{$tablename}}"; $this->assertNotEmpty($records = $DB->get_records_sql($sqlqm, null, 1, 4)); $this->assertEquals(4, count($records)); $this->assertEquals('b', reset($records)->name); $this->assertEquals('e', end($records)->name); $sqlqm = "SELECT *\n FROM {{$tablename}}"; $this->assertNotEmpty($records = $DB->get_records_sql($sqlqm, null, 4, 4)); $this->assertEquals(2, count($records)); $this->assertEquals('e', reset($records)->name); $this->assertEquals('f', end($records)->name); $sqlqm = "SELECT t.*, t.name AS test\n FROM {{$tablename}} t\n ORDER BY t.id ASC"; $this->assertNotEmpty($records = $DB->get_records_sql($sqlqm, null, 4, 4)); $this->assertEquals(2, count($records)); $this->assertEquals('e', reset($records)->name); $this->assertEquals('f', end($records)->name); $sqlqm = "SELECT DISTINCT t.name, t.name AS test\n FROM {{$tablename}} t\n ORDER BY t.name DESC"; $this->assertNotEmpty($records = $DB->get_records_sql($sqlqm, null, 4, 4)); $this->assertEquals(2, count($records)); $this->assertEquals('b', reset($records)->name); $this->assertEquals('a', end($records)->name); $sqlqm = "SELECT 1\n FROM {{$tablename}} t\n WHERE t.name = 'a'"; $this->assertNotEmpty($records = $DB->get_records_sql($sqlqm, null, 0, 1)); $this->assertEquals(1, count($records)); $sqlqm = "SELECT 'constant'\n FROM {{$tablename}} t\n WHERE t.name = 'a'"; $this->assertNotEmpty($records = $DB->get_records_sql($sqlqm, null, 0, 8)); $this->assertEquals(1, count($records)); $this->assertNotEmpty($DB->insert_record($tablename, array('name' => 'a', 'content' => 'one'))); $this->assertNotEmpty($DB->insert_record($tablename, array('name' => 'b', 'content' => 'two'))); $this->assertNotEmpty($DB->insert_record($tablename, array('name' => 'c', 'content' => 'three'))); $sqlqm = "SELECT t.name, COUNT(DISTINCT t2.id) AS count, 'Test' AS teststring\n FROM {{$tablename}} t\n LEFT JOIN (\n SELECT t.id, t.name\n FROM {{$tablename}} t\n ) t2 ON t2.name = t.name\n GROUP BY t.name\n ORDER BY t.name ASC"; $this->assertNotEmpty($records = $DB->get_records_sql($sqlqm)); $this->assertEquals(6, count($records)); // a,b,c,d,e,f $this->assertEquals(2, reset($records)->count); // a has 2 records now $this->assertEquals(1, end($records)->count); // f has 1 record still $this->assertNotEmpty($records = $DB->get_records_sql($sqlqm, null, 0, 2)); $this->assertEquals(2, count($records)); $this->assertEquals(2, reset($records)->count); $this->assertEquals(2, end($records)->count); }