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

Add option to use resumptionToken in REST #4225

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion config/vufind/searches.ini
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,8 @@ searchAccessPermission = access.api.Search

; This is the maximum number of results that can be returned in a single response:
maxLimit = 100

; This is the maximum number of results that can be returned with resumption token in a single response:
Copy link
Member

Choose a reason for hiding this comment

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

Should we specify the default value (which currently appears to be 1000) here? And is it possible that limit is too high? Even if the performance of the cursor improves speed, don't we still need to worry about potentially running out of memory if the index contains large records?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Adjusted the comment and set the default value for 200. (Modifiable in searches.ini)

Copy link
Member

Choose a reason for hiding this comment

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

I would suggest adding "(default = 200)" to the comments here so users know what they will get if they don't override the setting through the configuration.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe also worth adding a note to maxLimit above indicating that it only applies to results without resumption tokens.

;cursorMaxLimit = 500
; This section provides settings for optional caching of search requests. This
; affects the primary Solr backend only; for caching of other backends, see the
; appropriate backend-specific configuration files (Search2.ini, website.ini, etc.).
Expand Down
36 changes: 5 additions & 31 deletions module/VuFind/src/VuFind/OAI/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
*/
class Server
{
use \VuFind\ResumptionToken\ResumptionTokenTrait;

/**
* Repository base URL
*
Expand Down Expand Up @@ -1249,28 +1251,6 @@ protected function loadRecord($id)
return false;
}

/**
* Load parameters associated with a resumption token.
*
* @param string $token The resumption token to look up
*
* @return array Parameters associated with token
*/
protected function loadResumptionToken($token)
{
// Clean up expired records before doing our search:
$this->resumptionService->removeExpired();

// Load the requested token if it still exists:
if ($row = $this->resumptionService->findToken($token)) {
parse_str($row->getResumptionParameters(), $params);
return $params;
}

// If we got this far, the token is invalid or expired:
return false;
}

/**
* Normalize a date to a Unix timestamp.
*
Expand Down Expand Up @@ -1330,17 +1310,11 @@ protected function saveResumptionToken(
// Save the old cursor position before overwriting it for storage in the
// database!
$oldCursor = $params['cursor'];
$params['cursor'] = $currentCursor;
$params['cursorMark'] = $cursorMark;

// Save everything to the database:
$expire = time() + 24 * 60 * 60;
$token = $this->resumptionService->createAndPersistToken($params, $expire)->getId();

$resumptionToken = $this->createResumptionToken($params, $currentCursor, $cursorMark);
// Add details to the xml:
$token = $xml->addChild('resumptionToken', $token);
$token = $xml->addChild('resumptionToken', $resumptionToken->getId());
$token->addAttribute('cursor', $oldCursor);
$token->addAttribute('expirationDate', date($this->iso8601, $expire));
$token->addAttribute('expirationDate', date($this->iso8601, $resumptionToken->getExpiry()->getTimestamp()));
$token->addAttribute('completeListSize', $listSize);
}

Expand Down
106 changes: 106 additions & 0 deletions module/VuFind/src/VuFind/ResumptionToken/ResumptionTokenTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

/**
* Resumption token trait.
*
* PHP version 8
*
* Copyright (C) The National Library 2024.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category VuFind
* @package Controller
* @author Juha Luoma <[email protected]>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development:plugins:controllers Wiki
*/

namespace VuFind\ResumptionToken;

use VuFind\Db\Entity\OaiResumptionEntityInterface;
use VuFind\Db\Service\OaiResumptionServiceInterface;

/**
* Resumption token trait.
*
* @category VuFind
* @package Controller
* @author Juha Luoma <[email protected]>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development:plugins:controllers Wiki
*/
trait ResumptionTokenTrait
{
/**
* Resumption service for managing resumption tokens.
*
* @var OaiResumptionServiceInterface
*/
protected OaiResumptionServiceInterface $resumptionService;

/**
* Set resumption service
*
* @param OaiResumptionServiceInterface $resumptionService Resumption service
*
* @return void
*/
public function setResumptionService(OaiResumptionServiceInterface $resumptionService): void
demiankatz marked this conversation as resolved.
Show resolved Hide resolved
{
$this->resumptionService = $resumptionService;
}

/**
* Generate and return a new resumption token
*
* @param array $params Params to be saved for the resumption token
* @param int $cursor Cursor to be saved for the resumption token
* @param string $cursorMark Cursor mark to be saved for the resumption token
*
* @return OaiResumptionEntityInterface
*/
public function createResumptionToken(
array $params,
int $cursor,
string $cursorMark
): OaiResumptionEntityInterface {
$params['cursor'] = $cursor;
$params['cursorMark'] = $cursorMark;
$expire = time() + 24 * 60 * 60;
demiankatz marked this conversation as resolved.
Show resolved Hide resolved
return $this->resumptionService->createAndPersistToken($params, $expire);
}

/**
* Load parameters associated with a resumption token.
*
* @param string $token The resumption token to look up
*
* @return array|false Parameters associated with token or false if invalid or expired
demiankatz marked this conversation as resolved.
Show resolved Hide resolved
*/
protected function loadResumptionToken(string $token): array|false
{
// Clean up expired records before doing our search:
$this->resumptionService->removeExpired();

// Load the requested token if it still exists:
if ($row = $this->resumptionService->findToken($token)) {
parse_str($row->getResumptionParameters(), $params);
return $params;
}

// If we got this far, the token is invalid or expired:
return false;
}
}
2 changes: 1 addition & 1 deletion module/VuFind/src/VuFind/Search/SearchRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function run(
$params->initFromRequest($request);

if (is_callable($setupCallback)) {
$setupCallback($this, $params, $runningSearchId);
$setupCallback($this, $params, $runningSearchId, $results);
}

// Trigger the "configuration done" event.
Expand Down
Loading