Skip to content

Commit

Permalink
Consolidate metastore retrieve/range methods (#3733)
Browse files Browse the repository at this point in the history
  • Loading branch information
dafeder authored Dec 29, 2021
1 parent 849c788 commit 2baa835
Show file tree
Hide file tree
Showing 21 changed files with 418 additions and 293 deletions.
9 changes: 7 additions & 2 deletions modules/datastore/src/Form/DashboardForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,14 @@ protected function getDatasets(array $filters): array {
// If no filter values were supplied, fetch from the list of all dataset
// UUIDs.
else {
$total = $this->metastore->count('dataset');
$total = $this->metastore->count('dataset', TRUE);
$currentPage = $this->pagerManager->createPager($total, $this->itemsPerPage)->getCurrentPage();
$datasets = $this->metastore->getRangeUuids('dataset', ($currentPage * $this->itemsPerPage), $this->itemsPerPage);
$datasets = $this->metastore->getIdentifiers(
'dataset',
($currentPage * $this->itemsPerPage),
$this->itemsPerPage,
TRUE
);
}

return $datasets;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ private function queryExample($exampleId = NULL, $format = NULL) {
* @todo Page through results in case 20 isn't enough.
*/
private function getExampleIdentifiers() {
$all = $this->metastore->getRange("dataset", 0, 20);
$all = $this->metastore->getAll("dataset", 0, 20);
$i = 0;
$datastore = FALSE;
$identifiers = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ public function retrieve(string $uuid) : ?string {
throw new MissingObjectException("Error retrieving published dataset: distribution {$uuid} not found.");
}

}
}
8 changes: 4 additions & 4 deletions modules/datastore/tests/src/Unit/Form/DashboardFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public function testBuildTableRowsWithAllDatasets() {

$container = $this->buildContainerChain()
->add(MetastoreService::class, 'count', 2)
->add(MetastoreService::class, 'getRangeUuids', [$datasetInfo['latest_revision']['uuid'], $nonHarvestDatasetInfo['latest_revision']['uuid']])
->add(MetastoreService::class, 'getIdentifiers', [$datasetInfo['latest_revision']['uuid'], $nonHarvestDatasetInfo['latest_revision']['uuid']])
->add(DatasetInfo::class, 'gather', $datasetInfoOptions);

\Drupal::setContainer($container->getMock());
Expand Down Expand Up @@ -244,7 +244,7 @@ public function testBuildTableRowsDatasetWithNoDistribution() {

$container = $this->buildContainerChain()
->add(MetastoreService::class, 'count', 1)
->add(MetastoreService::class, 'getRangeUuids', [$datasetInfo['latest_revision']['uuid']])
->add(MetastoreService::class, 'getIdentifiers', [$datasetInfo['latest_revision']['uuid']])
->add(DatasetInfo::class, 'gather', $datasetInfo)
->getMock();
\Drupal::setContainer($container);
Expand Down Expand Up @@ -290,7 +290,7 @@ public function testBuildTableRowsDatasetMultipleDistribution() {

$container = $this->buildContainerChain()
->add(MetastoreService::class, 'count', 1)
->add(MetastoreService::class, 'getRangeUuids', [$datasetInfo['latest_revision']['uuid']])
->add(MetastoreService::class, 'getIdentifiers', [$datasetInfo['latest_revision']['uuid']])
->add(DatasetInfo::class, 'gather', $datasetInfo)
->getMock();
\Drupal::setContainer($container);
Expand Down Expand Up @@ -341,7 +341,7 @@ private function buildContainerChain(): Chain {
->add(Harvest::class,'getAllHarvestRunInfo', ['test'])
->add(Harvest::class,'getHarvestRunInfo', $runInfo)
->add(MetastoreService::class, 'count', 0)
->add(MetastoreService::class, 'getRangeUuids', [])
->add(MetastoreService::class, 'getIdentifiers', [])
->add(PagerManagerInterface::class,'createPager', Pager::class)
->add(DateFormatter::class, 'format', '12/31/2021')
->add(PathValidator::class, 'getUrlIfValidWithoutAccessCheck', NULL)
Expand Down
2 changes: 1 addition & 1 deletion modules/dkan_js_frontend/dkan_js_frontend.info.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: dkan_js_frontend
name: DKAN JS Frontend
description: Provides the routing connection between Drupal and a decoupled JavaScript frontend.
type: module
core_version_requirement: ^9.1
Expand Down
2 changes: 1 addition & 1 deletion modules/dkan_js_frontend/dkan_js_frontend.module
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ function _dkan_js_frontend_add_dataset_links(array &$arbitrary_links, Route $dat
$url_generator = new UrlGenerator($routes, $request_context);

// Fetch dataset UUIDs.
$dataset_uuids = \Drupal::service('dkan.metastore.service')->getRangeUuids('dataset');
$dataset_uuids = \Drupal::service('dkan.metastore.service')->getIdentifiers('dataset');
// Add dataset routes using the fetched UUIDs.
foreach ($dataset_uuids as $uuid) {
$arbitrary_links[] = DKAN_JS_FRONTEND_DEFAULT_DATASET_LINK + [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use Symfony\Component\HttpFoundation\RequestStack;

$module_path = substr(__DIR__, 0, strpos(__DIR__, '/dkan_js_frontend/')) . '/dkan_js_frontend';
require_once($module_path . '/dkan_js_frontend.module');
require_once $module_path . '/dkan_js_frontend.module';

/**
* Test dkan_js_frontend_simple_sitemap_arbitrary_links_alter() function.
Expand Down Expand Up @@ -88,7 +88,7 @@ public function testSitemapDatasetLinks(): void {
$container = (new Chain($this))
->add(Container::class, 'get', $containerOptions)
->add(RequestStack::class, 'getCurrentRequest', (Request::create(self::BASE_URL)))
->add(Service::class, 'getRangeUuids', [1, 2])
->add(Service::class, 'getIdentifiers', [1, 2])
->getMock();
\Drupal::setContainer($container);

Expand All @@ -98,7 +98,6 @@ public function testSitemapDatasetLinks(): void {
->getMock();
dkan_js_frontend_simple_sitemap_arbitrary_links_alter($arbitrary_links, $simpleSitemap);

$host = \Drupal::request()->getSchemeAndHttpHost();
$this->assertEquals($arbitrary_links, [
DKAN_JS_FRONTEND_DEFAULT_DATASET_LINK + ['url' => self::BASE_URL . '/dataset/1'],
DKAN_JS_FRONTEND_DEFAULT_DATASET_LINK + ['url' => self::BASE_URL . '/dataset/2'],
Expand Down
24 changes: 8 additions & 16 deletions modules/metastore/metastore.module
Original file line number Diff line number Diff line change
Expand Up @@ -96,38 +96,30 @@ function metastore_data_lifecycle(EntityInterface $entity, string $stage) {
* Returns true if the entity is used by the metastore.
*/
function metastore_entity_is_valid_item(EntityInterface $entity) {
$storageClass = metastore_storage_class();
$storageClass = \Drupal::service('dkan.metastore.storage')::getStorageClass();

// If the storage class used implements the entity storage interface, continue.
if (!is_a($storageClass, MetastoreEntityStorageInterface::class, true)) {
return FALSE;
}

$storageEntityType = \Drupal::service('dkan.metastore.metastore_item_factory')::getEntityType();
$storageBundles = \Drupal::service('dkan.metastore.metastore_item_factory')::getBundles();

// If the type and bundle are correct, return true.
if ($entity->getEntityTypeId() != $storageClass::getEntityType()) {
if ($entity->getEntityTypeId() != $storageEntityType) {
return FALSE;
}
if (in_array($entity->bundle(), $storageClass::getBundles())) {
if (in_array($entity->bundle(), $storageBundles)) {
return TRUE;
}
}

/**
* Get the storage class currently in use.
*
* @return string
* Qualified storage class name.
*/
function metastore_storage_class() {
$storageFactory = \Drupal::service('dkan.metastore.storage');
return $storageFactory::getStorageClass();
}

/**
* Implements hook_form_alter().
*/
function metastore_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$fieldName = metastore_storage_class()::getMetadataField();
$fieldName = \Drupal::service('dkan.metastore.metastore_item_factory')::getMetadataField();
if (isset($form[$fieldName]['widget'][0]['value']['#default_value'])) {
$json = $form[$fieldName]['widget'][0]['value']['#default_value'];
if (empty($json)) {
Expand All @@ -144,7 +136,7 @@ function metastore_form_alter(&$form, FormStateInterface $form_state, $form_id)
* Implements hook_entity_view_alter.
*/
function metastore_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
$fieldName = metastore_storage_class()::getMetadataField();
$fieldName = \Drupal::service('dkan.metastore.metastore_item_factory')::getMetadataField();

if (isset($build[$fieldName][0]['#context']['value'])) {
$json = $build[$fieldName][0]['#context']['value'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,12 @@ public function testFacets() {
],
];

$expect2 = [(object) [
'type' => 'publisher__name',
'name' => 'Steve',
'total' => 0,
],
$expect2 = [
(object) [
'type' => 'publisher__name',
'name' => 'Steve',
'total' => 0,
],
];

$container = $this->getCommonMockChain($this)->getMock();
Expand Down Expand Up @@ -181,7 +182,8 @@ public static function getCommonMockChain(TestCase $case, Options $services = NU
$getAllOptions = (new Options())
->add('keyword', [])
->add('theme', [])
->add('publisher', [ServiceTest::getValidMetadataFactory($case)->get(json_encode($facet), 'publisher')]);
->add('publisher', [ServiceTest::getValidMetadataFactory($case)->get(json_encode($facet), 'publisher')])
->index(0);

$getData = ServiceTest::getValidMetadataFactory($case)->get(json_encode($collection), 'dummy_schema_id');

Expand Down
5 changes: 2 additions & 3 deletions modules/metastore/src/DatasetApiDocs.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,8 @@ public function __construct(DkanApiDocsGenerator $docsGenerator, Service $metast
* OpenAPI spec.
*/
public function getDatasetSpecific(string $identifier) {
$fullSpec = $this->docsGenerator->buildSpec(
['metastore_api_docs', 'datastore_api_docs']
)->{"$"};
$specs = ['metastore_api_docs', 'datastore_api_docs'];
$fullSpec = $this->docsGenerator->buildSpec($specs)->{"$"};

$datasetSpec = [
'openapi' => $fullSpec['openapi'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ private function schemaParameters($schemaId) {
* An identifier.
*/
private function getExampleIdentifier($schemaId) {
if ($first = $this->metastore->getRange($schemaId, 0, 1)) {
if ($first = $this->metastore->getAll($schemaId, 0, 1)) {
return $first[0]->{"$.identifier"};
}
return FALSE;
Expand Down
51 changes: 16 additions & 35 deletions modules/metastore/src/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,14 @@ private function getStorage(string $schema_id): MetastoreStorageInterface {
*
* @param string $schema_id
* The schema ID to be counted.
* @param bool $unpublished
* Whether to include unpublished items.
*
* @return int
* Object count.
*/
public function count(string $schema_id): int {
return $this->getStorage($schema_id)->count();
public function count(string $schema_id, bool $unpublished = FALSE): int {
return $this->getStorage($schema_id)->count($unpublished);
}

/**
Expand All @@ -137,54 +139,33 @@ public function count(string $schema_id): int {
* Schema object offset or null for all.
* @param int|null $length
* Number of objects to fetch or null for all.
* @param bool $unpublished
* Whether to include unpublished items.
*
* @return string[]
* Range of object UUIDs of the given schema_id.
*/
public function getRangeUuids(string $schema_id, ?int $start = NULL, ?int $length = NULL): array {
return $this->getStorage($schema_id)->retrieveRangeUuids($start, $length);
public function getIdentifiers(string $schema_id, ?int $start = NULL, ?int $length = NULL, $unpublished = FALSE): array {
return $this->getStorage($schema_id)->retrieveIds($start, $length, $unpublished);
}

/**
* Get all.
*
* @param string $schema_id
* The {schema_id} slug from the HTTP request.
*
* @return array
* All objects of the given schema_id.
*/
public function getAll($schema_id): array {
$jsonStringsArray = $this->getStorage($schema_id)->retrieveAll();
$objects = array_filter($this->jsonStringsArrayToObjects($jsonStringsArray, $schema_id));

return $this->dispatchEvent(self::EVENT_DATA_GET_ALL, $objects, function ($data) {
if (!is_array($data)) {
return FALSE;
}
if (count($data) == 0) {
return TRUE;
}
return reset($data) instanceof RootedJsonData;
});

}

/**
* Get a subset of metastore items according to a range.
* Get all items, with optional pagination and status filters.
*
* @param string $schema_id
* Schema ID.
* @param int $start
* Start offset.
* @param int $length
* @param int|null $start
* Start offset. Null for no range.
* @param int|null $length
* Number of items to retrieve.
* @param bool $unpublished
* Whether to include unpublished items. Should default to false.
*
* @return array
* Array of RootedJsonData objects.
*/
public function getRange(string $schema_id, int $start, int $length):array {
$jsonStringsArray = $this->getStorage($schema_id)->retrieveRange($start, $length);
public function getAll(string $schema_id, ?int $start = NULL, ?int $length = NULL, $unpublished = FALSE):array {
$jsonStringsArray = $this->getStorage($schema_id)->retrieveAll($start, $length, $unpublished);
$objects = array_filter($this->jsonStringsArrayToObjects($jsonStringsArray, $schema_id));

return $this->dispatchEvent(self::EVENT_DATA_GET_ALL, $objects, function ($data) {
Expand Down
Loading

0 comments on commit 2baa835

Please sign in to comment.