MaxSem has uploaded a new change for review.
https://gerrit.wikimedia.org/r/79029
Change subject: WIP RFC: Generic data store
......................................................................
WIP RFC: Generic data store
Change-Id: Idae45f62d2e2af7b8e7e219adb8e7af997dd793a
---
A includes/store/DataStore.php
A includes/store/SqlDataStore.php
M maintenance/tables.sql
3 files changed, 80 insertions(+), 0 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core
refs/changes/29/79029/1
diff --git a/includes/store/DataStore.php b/includes/store/DataStore.php
new file mode 100644
index 0000000..4f65838
--- /dev/null
+++ b/includes/store/DataStore.php
@@ -0,0 +1,27 @@
+<?php
+
+abstract class DataStore {
+ const MAX_KEY_LENGTH = 255;
+ public abstract function get( $key, $latest = false );
+ public abstract function set( $key, $value );
+ public abstract function getByPrefix( $prefix, $callback, $latest =
false );
+
+ public function key( /*...*/ ) {
+ global $wgWikiID;
+ $keys = func_get_args();
+ array_unshift( $keys, $wgWikiID );
+ return $this->makeKey( $keys );
+ }
+
+ public function sharedKey( /*...*/ ) {
+ return $this->makeKey( func_get_args() );
+ }
+
+ protected function makeKey( $keys ) {
+ $key = implode( ':', $keys );
+ if ( strlen( $key ) > self::MAX_KEY_LENGTH ) {
+ throw new MWException( "Key '$key' is too long" );
+ }
+ return $key;
+ }
+};
diff --git a/includes/store/SqlDataStore.php b/includes/store/SqlDataStore.php
new file mode 100644
index 0000000..237252f
--- /dev/null
+++ b/includes/store/SqlDataStore.php
@@ -0,0 +1,45 @@
+<?php
+
+class SqlDataStore extends DataStore {
+ private $batchSize = 500;
+ public function __construct( $config ) {
+
+ }
+
+ public function get( $key, $latest = false ) {
+ wfProfileIn( __METHOD__ );
+ $db = $this->getDB( $latest );
+ $res = $db->selectField( 'store', 'store_value', array(
'store_key' => $key ), __METHOD__ );
+ wfProfileOut( __METHOD__ );
+
+ return $res === false ? null : $res;
+ }
+
+ public function set( $key, $value ) {
+ wfProfileIn( __METHOD__ );
+ $dbw = $this->getDB( true );
+ $dbw->insert( 'store', array( 'store_key' => $key,
'store_value' => $value ), __METHOD__, array( 'UPDATE' ) );
+ }
+
+ public function getByPrefix( $prefix, $callback, $latest = false ) {
+ wfProfileIn( __METHOD__ );
+
+ $db = $this->getDB( $latest );
+ do {
+ $res = $db->select( 'store',
+ array( 'store_key', 'store_value' ),
+ array( "store_key >= {$db->addQuotes( $prefix
)}" ),
+ __METHOD__,
+ array( 'ORDER BY' => 'store_key', 'LIMIT' =>
$this->batchSize + 1 )
+ );
+ } while ( true );
+ wfProfileOut( __METHOD__ );
+ }
+
+ protected function getDB( $master ) {
+ if ( $master ) {
+ return wfGetDB( DB_MASTER );
+ }
+ return wfGetDB( DB_SLAVE );
+ }
+};
diff --git a/maintenance/tables.sql b/maintenance/tables.sql
index 7fc6bb8..07ce427 100644
--- a/maintenance/tables.sql
+++ b/maintenance/tables.sql
@@ -1573,4 +1573,12 @@
CREATE INDEX /*i*/site_ids_site ON /*_*/site_identifiers (si_site);
CREATE INDEX /*i*/site_ids_key ON /*_*/site_identifiers (si_key);
+CREATE TABLE /*_*/store (
+ store_key varbinary(255) NOT NULL PRIMARY KEY,
+ store_value MEDIUMBLOB,
+ store_timestamp TIMESTAMP
+);
+
+CREATE INDEX /*i*/store_timestamp ON /*_*/store (store_timestamp);
+
-- vim: sw=2 sts=2 et
--
To view, visit https://gerrit.wikimedia.org/r/79029
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Idae45f62d2e2af7b8e7e219adb8e7af997dd793a
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: MaxSem <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits