Skip to content

Commit

Permalink
Fix issue with double indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
dantleech committed Nov 16, 2014
1 parent cf81595 commit 7dfb444
Show file tree
Hide file tree
Showing 7 changed files with 235 additions and 69 deletions.
54 changes: 31 additions & 23 deletions src/Transport/Fs/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,29 +98,6 @@ public function __construct($factory, $parameters = array())
$this->registerEventSubscribers();
}

private function getFilesystemAdapter($adapterName)
{
if ($adapterName instanceof AdapterInterface) {
return $adapterName;
}

if (null === $adapterName) {
return new LocalAdapter($this->path);
}

switch ($adapterName) {
case 'local':
return new LocalAdapter($this->path);
case 'array':
return new ArrayAdapter();
}

throw new \InvalidArgumentException(sprintf(
'Unknown filesystem adapter "%s", must be one of "%s"',
implode('", "', array('local', 'array'))
));
}

private function getSearchAdapter()
{
$this->searchAdapter = new ZendSearchAdapter($this->path, $this->nodeTypeManager, $this->zendHideDestructException);
Expand Down Expand Up @@ -715,4 +692,35 @@ private function validatePath($workspaceName, $path)
));
}
}

/**
* Return the filesystem adapter indicated by $adapterName
* Defaults to LocalAdapter when null.
*
* @param string|AdapterInterface $adapterName
* @return AdapterInterface
* @throws InvalidArgumentException
*/
private function getFilesystemAdapter($adapterName)
{
if ($adapterName instanceof AdapterInterface) {
return $adapterName;
}

if (null === $adapterName) {
return new LocalAdapter($this->path);
}

switch ($adapterName) {
case 'local':
return new LocalAdapter($this->path);
case 'array':
return new ArrayAdapter();
}

throw new \InvalidArgumentException(sprintf(
'Unknown filesystem adapter "%s", must be one of "%s"',
implode('", "', array('local', 'array'))
));
}
}
20 changes: 18 additions & 2 deletions src/Transport/Fs/Search/Adapter/ZendSearchAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public function __construct($path, NodeTypeManagerInterface $nodeTypeManager = n
public function index($workspace, $path, Node $node)
{
$index = $this->getIndex($workspace);
$this->removeExisting($index, $node);

$document = new Document();
$nodeName = PathHelper::getNodeName($path);
$localNodeName = $nodeName; // PathHelper::getLocalNodeName($path);
Expand Down Expand Up @@ -127,7 +129,6 @@ public function index($workspace, $path, Node $node)
$document->addField(Field::Text($propertyName, $value));
break;
}

};

$index->addDocument($document);
Expand Down Expand Up @@ -203,7 +204,6 @@ public function query($workspace, QueryObjectModelInterface $qom)
);
}


$properties[$selectorName] = array();
foreach ($document->getFieldNames() as $fieldName) {
$field = $document->getField($fieldName);
Expand Down Expand Up @@ -318,4 +318,20 @@ private function normalizeValue($value)

return $value;
}

/**
* Remove any existing references to this node in the index
*
* @param Index $index Zend search index object
* @param Node $node Jackalope FS node object
*/
private function removeExisting(Index $index, Node $node)
{
$internalUuid = $node->getPropertyValue(Storage::INTERNAL_UUID);
$hits = $index->find(str_replace(':', "\\:", Storage::INTERNAL_UUID) . ':' . $internalUuid);

foreach ($hits as $hit) {
$index->delete($hit->id);
}
}
}
44 changes: 0 additions & 44 deletions tests/Transport/Fs/ClientTest.php

This file was deleted.

35 changes: 35 additions & 0 deletions tests/Transport/Fs/FunctionalTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Transport\Fs;

use Prophecy\PhpUnit\ProphecyTestCase;
use Jackalope\RepositoryFactoryFilesystem;
use PHPCR\SimpleCredentials;
use Symfony\Component\Filesystem\Filesystem;

abstract class FunctionalTestCase extends ProphecyTestCase
{
protected $path;

public function getSession($parameters)
{
$this->path = __DIR__ . '/../../data';

$fs = new Filesystem();

if (file_exists($this->path)) {
$fs->remove($this->path);
}

$parameters = array_merge(array(
'path' => $this->path,
), $parameters);

$factory = new RepositoryFactoryFilesystem();
$repository = $factory->getRepository($parameters);
$credentials = new SimpleCredentials('admin', 'admin');
$session = $repository->login($credentials);

return $session;
}
}
135 changes: 135 additions & 0 deletions tests/Transport/Fs/Search/Adapter/AdapterTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

namespace Jackalope\Transport\Fs\Search\Adapter;

use Prophecy\PhpUnit\ProphecyTestCase;
use PHPCR\Util\QOM\Sql2ToQomQueryConverter;
use Transport\Fs\FunctionalTestCase;
use Jackalope\Transport\Fs\Model\Node;
use Jackalope\Transport\Fs\Filesystem\Storage;

abstract class AdapterTestCase extends FunctionalTestCase
{
protected $nodeTypeManager;
protected $adapter;
protected $queryManager;

public function setUp()
{
$this->session = $this->getSession(array(
'filesystem.adapter' => 'array'
));
$this->nodeTypeManager = $this->session->getWorkspace()->getNodeTypeManager();
$this->queryManager = $this->session->getWorkspace()->getQueryManager();
$this->adapter = $this->getAdapter();
}

abstract protected function getAdapter();

public function provideQuery()
{
return array(
array('simple', 'SELECT * FROM [nt:unstructured]', 2),
array('simple', 'SELECT * FROM [nt:unstructured] WHERE field1 = "value 1"', 1)
);
}

/**
* @dataProvider provideQuery
*/
public function testQuery($nodeDataName, $query, $expectedNbResults)
{
$this->indexNodeData($nodeDataName);

$res = $this->adapter->query('workspace', $this->queryToQOM($query));
$this->assertCount($expectedNbResults, $res);
}

/**
* @dataProvider provideQuery
*/
public function testQueryDoubleIndex($nodeDataName, $query, $expectedNbResults)
{
$this->indexNodeData($nodeDataName);
$this->indexNodeData($nodeDataName);

$res = $this->adapter->query('workspace', $this->queryToQOM($query));
$this->assertCount($expectedNbResults, $res);
}

public function testQueryNewIndex()
{
$this->indexNodeData('simple');
$this->indexNodeData('article');

$query = 'SELECT * FROM [nt:unstructured] WHERE title = "Article Title"';
$res = $this->adapter->query('workspace', $this->queryToQOM($query));
$this->assertCount(1, $res);
}

protected function getNodeData($name)
{
switch ($name) {
case 'article':
return array(
array(
'/node/node-new',
array(
Storage::INTERNAL_UUID => 'article',
'jcr:primaryType' => 'nt:unstructured',
'title' => 'Article Title',
'body' => 'This is the article body',
),
),
);
case 'simple':
default:
return array(
array(
'/node/node1',
array(
Storage::INTERNAL_UUID => 'simple1',
'jcr:primaryType' => 'nt:unstructured',
'field1' => 'value 1',
'field2' => 'value 2',
),
),
array(
'/node/node2',
array(
Storage::INTERNAL_UUID => 'simple2',
'jcr:primaryType' => 'nt:unstructured',
'field1' => 'value 3',
'field2' => 'value 4',
),
),
);
}
}

protected function indexNodeData($nodeDataName)
{
$nodeData = $this->getNodeData($nodeDataName);

foreach ($nodeData as $nodeDatum) {
list($path, $properties) = $nodeDatum;
$node = new Node();

foreach ($properties as $key => $value) {
$node->setProperty($key, $value);
}

$this->adapter->index('workspace', $path, $node);
}
}

protected function queryToQOM($query)
{
$parser = new Sql2ToQomQueryConverter($this->queryManager->getQOMFactory());
$qom = $parser->parse($query);

return $qom;
}
}


14 changes: 14 additions & 0 deletions tests/Transport/Fs/Search/Adapter/ZendSearchAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Jackalope\Transport\Fs\Search\Adapter;

use Jackalope\Transport\Fs\Search\Adapter\ZendSearchAdapter;

class ZendSearchAdapterTest extends AdapterTestCase
{
public function getAdapter()
{
return new ZendSearchAdapter($this->path, $this->nodeTypeManager);
}
}

2 changes: 2 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@
require_once __DIR__ . '/../vendor/phpcr/phpcr-api-tests/inc/AbstractLoader.php';
require_once __DIR__ . '/../vendor/phpcr/phpcr-api-tests/inc/FixtureLoaderInterface.php';
require_once __DIR__ . '/ImplementationLoader.php';
require_once __DIR__ . '/Transport/Fs/FunctionalTestCase.php';
require_once __DIR__ . '/Transport/Fs/Filesystem/Adapter/AdapterTestCase.php';
require_once __DIR__ . '/Transport/Fs/Search/Adapter/AdapterTestCase.php';

function dodefine($name, $value)
{
Expand Down

0 comments on commit 7dfb444

Please sign in to comment.