[MediaWiki-commits] [Gerrit] Added InternalEntityIdInterpreter - change (mediawiki...Wikibase)

2013-05-30 Thread Daniel Werner (Code Review)
Daniel Werner has submitted this change and it was merged.

Change subject: Added InternalEntityIdInterpreter
..


Added InternalEntityIdInterpreter

This interface has a method for translating internal store entity ids to 
EntityId objects

Change-Id: Ie5a05b5764944722d43ec3941f83a73af88978fa
---
M QueryEngine/includes/SQLStore/EntityIdTransformer.php
A QueryEngine/includes/SQLStore/InternalEntityIdInterpreter.php
M QueryEngine/tests/phpunit/SQLStore/EntityIdTransformerTest.php
3 files changed, 134 insertions(+), 14 deletions(-)

Approvals:
  Daniel Werner: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/QueryEngine/includes/SQLStore/EntityIdTransformer.php 
b/QueryEngine/includes/SQLStore/EntityIdTransformer.php
index c73b517..36c1668 100644
--- a/QueryEngine/includes/SQLStore/EntityIdTransformer.php
+++ b/QueryEngine/includes/SQLStore/EntityIdTransformer.php
@@ -2,6 +2,7 @@
 
 namespace Wikibase\QueryEngine\SQLStore;
 
+use OutOfBoundsException;
 use Wikibase\EntityId;
 
 /**
@@ -31,15 +32,16 @@
  * @author Jeroen De Dauw  jeroended...@gmail.com 
  * @author Denny Vrandecic
  */
-class EntityIdTransformer implements InternalEntityIdFinder {
+class EntityIdTransformer implements InternalEntityIdFinder, 
InternalEntityIdInterpreter {
 
-   protected $idMap;
+   protected $stringTypeToInt;
+   protected $intTypeToString;
 
/**
 * @param int[] $idMap Maps entity types (strings) to a unique one 
digit integer
 */
public function __construct( array $idMap ) {
-   $this-idMap = $idMap;
+   $this-stringTypeToInt = $idMap;
}
 
/**
@@ -50,19 +52,56 @@
 * @return int
 */
public function getInternalIdForEntity( EntityId $entityId ) {
-   $this-ensureEntityTypeIsKnown( $entityId-getEntityType() );
+   $this-ensureEntityStringTypeIsKnown( 
$entityId-getEntityType() );
 
return $this-getComputedId( $entityId );
}
 
-   protected function ensureEntityTypeIsKnown( $entityType ) {
-   if ( !array_key_exists( $entityType, $this-idMap ) ) {
-   throw new \OutOfBoundsException( Id of unknown entity 
type '$entityType' cannot be transformed );
+   protected function ensureEntityStringTypeIsKnown( $entityType ) {
+   if ( !array_key_exists( $entityType, $this-stringTypeToInt ) ) 
{
+   throw new OutOfBoundsException( Id of unknown entity 
type '$entityType' cannot be transformed );
}
}
 
protected function getComputedId( EntityId $entityId ) {
-   return $entityId-getNumericId() * 10 + 
$this-idMap[$entityId-getEntityType()];
+   return $entityId-getNumericId() * 10 + 
$this-stringTypeToInt[$entityId-getEntityType()];
+   }
+
+   /**
+* @see InternalEntityIdInterpreter::getExternalIdForEntity
+*
+* @param int $internalEntityId
+*
+* @return EntityId
+*/
+   public function getExternalIdForEntity( $internalEntityId ) {
+   $this-buildIntToStringMap();
+
+   $numericId = (int)floor( $internalEntityId / 10 );
+   $typeId = $internalEntityId % 10;
+
+   $this-ensureEntityIntTypeIsKnown( $typeId );
+   $typeId = $this-intTypeToString[$typeId];
+
+   return new EntityId( $typeId, $numericId );
+   }
+
+   protected function buildIntToStringMap() {
+   if ( is_array( $this-intTypeToString ) ) {
+   return;
+   }
+
+   $this-intTypeToString = array();
+
+   foreach ( $this-stringTypeToInt as $string = $int ) {
+   $this-intTypeToString[$int] = $string;
+   }
+   }
+
+   protected function ensureEntityIntTypeIsKnown( $intType ) {
+   if ( !array_key_exists( $intType, $this-intTypeToString ) ) {
+   throw new OutOfBoundsException( Id of unknown entity 
type '$intType' cannot be interpreted );
+   }
}
 
 }
diff --git a/QueryEngine/includes/SQLStore/InternalEntityIdInterpreter.php 
b/QueryEngine/includes/SQLStore/InternalEntityIdInterpreter.php
new file mode 100644
index 000..6bff3c3
--- /dev/null
+++ b/QueryEngine/includes/SQLStore/InternalEntityIdInterpreter.php
@@ -0,0 +1,42 @@
+?php
+
+namespace Wikibase\QueryEngine\SQLStore;
+
+use Wikibase\EntityId;
+
+/**
+ * Finds the external entity id for the given internal entity id.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY 

[MediaWiki-commits] [Gerrit] Added InternalEntityIdInterpreter - change (mediawiki...Wikibase)

2013-05-29 Thread Jeroen De Dauw (Code Review)
Jeroen De Dauw has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/66008


Change subject: Added InternalEntityIdInterpreter
..

Added InternalEntityIdInterpreter

Change-Id: Ie5a05b5764944722d43ec3941f83a73af88978fa
---
M QueryEngine/includes/SQLStore/EntityIdTransformer.php
A QueryEngine/includes/SQLStore/InternalEntityIdInterpreter.php
M QueryEngine/tests/phpunit/SQLStore/EntityIdTransformerTest.php
3 files changed, 134 insertions(+), 14 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/08/66008/1

diff --git a/QueryEngine/includes/SQLStore/EntityIdTransformer.php 
b/QueryEngine/includes/SQLStore/EntityIdTransformer.php
index c73b517..36c1668 100644
--- a/QueryEngine/includes/SQLStore/EntityIdTransformer.php
+++ b/QueryEngine/includes/SQLStore/EntityIdTransformer.php
@@ -2,6 +2,7 @@
 
 namespace Wikibase\QueryEngine\SQLStore;
 
+use OutOfBoundsException;
 use Wikibase\EntityId;
 
 /**
@@ -31,15 +32,16 @@
  * @author Jeroen De Dauw  jeroended...@gmail.com 
  * @author Denny Vrandecic
  */
-class EntityIdTransformer implements InternalEntityIdFinder {
+class EntityIdTransformer implements InternalEntityIdFinder, 
InternalEntityIdInterpreter {
 
-   protected $idMap;
+   protected $stringTypeToInt;
+   protected $intTypeToString;
 
/**
 * @param int[] $idMap Maps entity types (strings) to a unique one 
digit integer
 */
public function __construct( array $idMap ) {
-   $this-idMap = $idMap;
+   $this-stringTypeToInt = $idMap;
}
 
/**
@@ -50,19 +52,56 @@
 * @return int
 */
public function getInternalIdForEntity( EntityId $entityId ) {
-   $this-ensureEntityTypeIsKnown( $entityId-getEntityType() );
+   $this-ensureEntityStringTypeIsKnown( 
$entityId-getEntityType() );
 
return $this-getComputedId( $entityId );
}
 
-   protected function ensureEntityTypeIsKnown( $entityType ) {
-   if ( !array_key_exists( $entityType, $this-idMap ) ) {
-   throw new \OutOfBoundsException( Id of unknown entity 
type '$entityType' cannot be transformed );
+   protected function ensureEntityStringTypeIsKnown( $entityType ) {
+   if ( !array_key_exists( $entityType, $this-stringTypeToInt ) ) 
{
+   throw new OutOfBoundsException( Id of unknown entity 
type '$entityType' cannot be transformed );
}
}
 
protected function getComputedId( EntityId $entityId ) {
-   return $entityId-getNumericId() * 10 + 
$this-idMap[$entityId-getEntityType()];
+   return $entityId-getNumericId() * 10 + 
$this-stringTypeToInt[$entityId-getEntityType()];
+   }
+
+   /**
+* @see InternalEntityIdInterpreter::getExternalIdForEntity
+*
+* @param int $internalEntityId
+*
+* @return EntityId
+*/
+   public function getExternalIdForEntity( $internalEntityId ) {
+   $this-buildIntToStringMap();
+
+   $numericId = (int)floor( $internalEntityId / 10 );
+   $typeId = $internalEntityId % 10;
+
+   $this-ensureEntityIntTypeIsKnown( $typeId );
+   $typeId = $this-intTypeToString[$typeId];
+
+   return new EntityId( $typeId, $numericId );
+   }
+
+   protected function buildIntToStringMap() {
+   if ( is_array( $this-intTypeToString ) ) {
+   return;
+   }
+
+   $this-intTypeToString = array();
+
+   foreach ( $this-stringTypeToInt as $string = $int ) {
+   $this-intTypeToString[$int] = $string;
+   }
+   }
+
+   protected function ensureEntityIntTypeIsKnown( $intType ) {
+   if ( !array_key_exists( $intType, $this-intTypeToString ) ) {
+   throw new OutOfBoundsException( Id of unknown entity 
type '$intType' cannot be interpreted );
+   }
}
 
 }
diff --git a/QueryEngine/includes/SQLStore/InternalEntityIdInterpreter.php 
b/QueryEngine/includes/SQLStore/InternalEntityIdInterpreter.php
new file mode 100644
index 000..6bff3c3
--- /dev/null
+++ b/QueryEngine/includes/SQLStore/InternalEntityIdInterpreter.php
@@ -0,0 +1,42 @@
+?php
+
+namespace Wikibase\QueryEngine\SQLStore;
+
+use Wikibase\EntityId;
+
+/**
+ * Finds the external entity id for the given internal entity id.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied