Skip to content
This repository has been archived by the owner on Sep 20, 2019. It is now read-only.

Commit

Permalink
Add a builder that constructs a MultipleBatchingFetcher
Browse files Browse the repository at this point in the history
This is to not force users to have to construct a fetcher for
each type of entity themselves
  • Loading branch information
JeroenDeDauw committed Jul 19, 2014
1 parent c584358 commit 5cb2d6b
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 13 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -11,7 +11,7 @@
"require": {
"php": ">=5.3.2",
"wikibase/wikibase": "dev-master",
"jeroen-de-dauw/batching-iterator": "~2.0"
"jeroen-de-dauw/batching-iterator": "~2.1"
},
"autoload": {
"psr-4": {
Expand Down
49 changes: 49 additions & 0 deletions src/BatchingEntityIdFetcherBuilder.php
@@ -0,0 +1,49 @@
<?php

namespace Wikibase\EntityStore;

use BatchingIterator\Fetchers\MultipleBatchingFetcher;
use Wikibase\DataModel\Entity\EntityId;
use Wikibase\EntityPerPage;

/**
* @since 0.2
* @licence GNU GPL v2+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/
class BatchingEntityIdFetcherBuilder {

private $entityPerPageTable;
private $previousId;

private $fetchers = array();

public function __construct( EntityPerPage $entityPerPageTable, EntityId $previousId = null ) {
$this->entityPerPageTable = $entityPerPageTable;
$this->previousId = $previousId;

$this->addFetcherForEntityType( 'item' );
$this->addFetcherForEntityType( 'property' );
}

private function addFetcherForEntityType( $entityType ) {
$this->fetchers[] = new TypedEntityIdFetcher(
$this->entityPerPageTable,
$entityType,
$this->getPreviousIdForEntityType( $entityType )
);
}

private function getPreviousIdForEntityType( $entityType ) {
if ( $this->previousId !== null && $this->previousId->getEntityType() === $entityType ) {
return $this->previousId;
}

return null;
}

public function getFetcher() {
return new MultipleBatchingFetcher( $this->fetchers );
}

}
Expand Up @@ -3,16 +3,15 @@
namespace Wikibase\EntityStore;

use BatchingIterator\BatchingFetcher;
use Wikibase\DataModel\Entity\Entity;
use Wikibase\DataModel\Entity\EntityId;
use Wikibase\EntityPerPage;

/**
* @since 0.1
* @since 0.2
* @licence GNU GPL v2+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/
class BatchingEntityIdFetcher implements BatchingFetcher {
class TypedEntityIdFetcher implements BatchingFetcher {

private $entityPerPageTable;
private $entityType;
Expand All @@ -37,7 +36,7 @@ public function __construct( EntityPerPage $entityPerPageTable, $entityType, Ent
*
* @param int $maxFetchCount
*
* @return Entity[]
* @return EntityId[]
*/
public function fetchNext( $maxFetchCount ) {
$ids = $this->entityPerPageTable->listEntities(
Expand Down
7 changes: 3 additions & 4 deletions tests/integration/BatchingEntityFetcherTest.php
Expand Up @@ -6,7 +6,7 @@
use Wikibase\DataModel\Entity\Item;
use Wikibase\DataModel\Entity\ItemId;
use Wikibase\EntityStore\BatchingEntityFetcher;
use Wikibase\EntityStore\BatchingEntityIdFetcher;
use Wikibase\EntityStore\BatchingEntityIdFetcherBuilder;
use Wikibase\Repo\WikibaseRepo;

/**
Expand Down Expand Up @@ -38,14 +38,13 @@ private function insertEntities() {
private function getFetcherThatContinuesFrom( EntityId $previousId = null ) {
$repo = WikibaseRepo::getDefaultInstance();

$entityIdFetcher = new BatchingEntityIdFetcher(
$fetcherBuilder = new BatchingEntityIdFetcherBuilder(
$repo->getStore()->newEntityPerPage(),
'item',
$previousId
);

return new BatchingEntityFetcher(
$entityIdFetcher,
$fetcherBuilder->getFetcher(),
$repo->getEntityLookup()
);
}
Expand Down
Expand Up @@ -3,15 +3,15 @@
namespace Wikibase\EntityStore\Tests;

use Wikibase\DataModel\Entity\ItemId;
use Wikibase\EntityStore\BatchingEntityIdFetcher;
use Wikibase\EntityStore\TypedEntityIdFetcher;

/**
* @covers Wikibase\EntityStore\BatchingEntityIdFetcher
* @covers Wikibase\EntityStore\TypedEntityIdFetcher
*
* @licence GNU GPL v2+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/
class BatchingEntityIdFetcherTest extends \PHPUnit_Framework_TestCase {
class TypedEntityIdFetcherTest extends \PHPUnit_Framework_TestCase {

public function testEntityPerPageIsCalledAndResultIsReturned() {
$entityType = 'kittens';
Expand All @@ -35,7 +35,7 @@ public function testEntityPerPageIsCalledAndResultIsReturned() {
)
->will( $this->returnValue( $expectedReturnValue ) );

$idFetcher = new BatchingEntityIdFetcher(
$idFetcher = new TypedEntityIdFetcher(
$entityPerPageTable,
$entityType,
$entityId
Expand Down

0 comments on commit 5cb2d6b

Please sign in to comment.