Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable adapter #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ Connect as follows:
Options:

- **path**: (required) Path to store data, indexes, etc.
- **search_enabled**: If search should be enabled or not (default true)
- **search.enabled**: If search should be enabled or not (default true)
- **search.zend.hide_destruct_exception**: Hack to hide exceptions from the
ZendSearch engine when running functional tests.
- **filesystem.adapter**: Filesystem adapter to use, either `local` for using
the filesystem or `array` to use in-memory storage (for testing)

Limitations
-----------
Expand Down
1 change: 1 addition & 0 deletions src/RepositoryFactoryFilesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function getConfigurationKeys()
'path',
'search.enabled',
'search.zend.hide_destruct_exception',
'filesystem.adapter'
);
}
}
Expand Down
47 changes: 44 additions & 3 deletions src/Transport/Fs/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
use PHPCR\PathNotFoundException;
use PHPCR\ReferentialIntegrityException;
use Jackalope\Transport\Fs\Model\Node;
use Jackalope\Transport\Fs\Filesystem\Adapter\ArrayAdapter;
use Jackalope\Transport\Fs\Filesystem\AdapterInterface;

/**
*/
Expand Down Expand Up @@ -67,20 +69,28 @@ class Client extends BaseTransport implements WorkspaceManagementInterface, Writ
*/
protected $path;

public function __construct($factory, $parameters = array(), Filesystem $filesystem = null)
public function __construct($factory, $parameters = array())
{
if (!isset($parameters['path'])) {
throw new \InvalidArgumentException(
'You must provide the "path" parameter for the filesystem jackalope repository'
);
}

$this->path = $parameters['path'];

$parameters = array_merge(array(
'search.enabled' => true,
'search.zend.hide_destruct_exception',
'filesystem.adapter' => null,
), $parameters);

$this->zendHideDestructException = isset($parameters['search.zend.hide_destruct_exception']) ? $parameters['search.zend.hide_destruct_exception'] : false;
$this->searchEnabled = isset($parameters['search.enabled']) ? $parameters['search.enabled'] : true;
$this->eventDispatcher = new EventDispatcher();
$adapter = $filesystem ? : new LocalAdapter($this->path);

$adapter = $this->getFilesystemAdapter($parameters['filesystem.adapter']);
$this->storage = new Storage(new Filesystem($adapter), $this->eventDispatcher);

$this->valueConverter = new ValueConverter();
$this->nodeSerializer = new YamlNodeSerializer();
$this->factory = $factory;
Expand Down Expand Up @@ -682,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';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. We should really upgrade the autoloading in the Jackalope tests.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1


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