...
 
Commits (2)
......@@ -11,7 +11,7 @@ use Minds\Core\Data\Cassandra\Prepared;
use Cassandra;
use Minds\Core\Time;
class Repository
class CassandraRepository
{
/** @var Client $client */
private $client;
......
......@@ -5,6 +5,7 @@
namespace Minds\Core\Boost\Network;
use Minds\Common\Repository\Response;
use Minds\Core\Data\ElasticSearch\Client;
use Minds\Core\Di\Di;
use Minds\Core\Data\ElasticSearch\Prepared;
use Minds\Core\Util\BigNumber;
......@@ -82,7 +83,7 @@ class ElasticRepository
];
}
if ($opts['state'] === 'approved') {
if ($opts['state'] === Manager::OPT_STATEQUERY_APPROVED) {
$must[] = [
'exists' => [
'field' => '@reviewed',
......@@ -95,26 +96,48 @@ class ElasticRepository
],
],
];
}
if ($opts['offchain']) {
$must[] = [
'term' => [
'token_method' => 'offchain',
$must_not[] = [
'exists' => [
'field' => '@completed',
],
];
$must_not[] = [
'exists' => [
'field' => '@rejected',
],
];
$must_not[] = [
'exists' => [
'field' => '@revoked',
],
];
}
if ($opts['state'] === 'review') {
if ($opts['state'] === Manager::OPT_STATEQUERY_REVIEW) {
$must_not[] = [
'exists' => [
'field' => '@reviewed',
],
];
$must_not[] = [
'exists' => [
'field' => '@completed',
],
];
$must_not[] = [
'exists' => [
'field' => '@rejected',
],
];
$must_not[] = [
'exists' => [
'field' => '@revoked',
],
];
$sort = ['@timestamp' => 'asc'];
}
if ($opts['state'] === 'approved' || $opts['state'] === 'review' || $opts['state'] === 'active') {
if ($opts['state'] === Manager::OPT_STATEQUERY_ACTIVE) {
$must_not[] = [
'exists' => [
'field' => '@completed',
......@@ -132,6 +155,14 @@ class ElasticRepository
];
}
if ($opts['offchain']) {
$must[] = [
'term' => [
'token_method' => 'offchain',
],
];
}
$body = [
'query' => [
'bool' => [
......
......@@ -116,7 +116,7 @@ class Iterator implements \Iterator
'type' => $this->type,
'limit' => $this->limit,
'offset' => $this->offset,
'state' => Boost::STATE_APPROVED,
'state' => Manager::OPT_STATEQUERY_APPROVED,
'rating' => $this->rating,
]);
......
......@@ -12,10 +12,10 @@ use Minds\Entities\Entity;
class Manager
{
/** @var Repository $repository */
private $repository;
/** @var CassandraRepository $cassandraRepository */
private $cassandraRepository;
/** @var ElasticRepository $repository */
/** @var ElasticRepository $elasticRepository */
private $elasticRepository;
/** @var EntitiesBuilder $entitiesBuilder */
......@@ -27,14 +27,24 @@ class Manager
/** @var Config $config */
private $config;
const OPT_STATEQUERY_ACTIVE = 'active';
const OPT_STATEQUERY_REVIEW = 'review';
const OPT_STATEQUERY_APPROVED = 'approved';
const VALID_OPT_STATEQUERY = [
self::OPT_STATEQUERY_ACTIVE,
self::OPT_STATEQUERY_REVIEW,
self::OPT_STATEQUERY_APPROVED
];
public function __construct(
$repository = null,
$cassandraRepository = null,
$elasticRepository = null,
$entitiesBuilder = null,
$guidBuilder = null,
$config = null
) {
$this->repository = $repository ?: new Repository;
$this->cassandraRepository = $cassandraRepository ?: new CassandraRepository;
$this->elasticRepository = $elasticRepository ?: new ElasticRepository;
$this->entitiesBuilder = $entitiesBuilder ?: Di::_()->get('EntitiesBuilder');
$this->guidBuilder = $guidBuilder ?: new GuidBuilder;
......@@ -53,14 +63,14 @@ class Manager
'state' => null,
], $opts);
if ($opts['state'] == 'review' || $opts['state'] == 'active') {
if ($this->optStateIsUsedAndValid($opts)) {
$opts['useElastic'] = true;
}
if ($opts['useElastic']) {
$response = $this->elasticRepository->getList($opts);
if ($opts['state'] === 'review' || $opts['state'] === 'active') {
if ($this->optStateIsUsedAndValid($opts)) {
$opts['guids'] = array_map(function ($boost) {
return $boost->getGuid();
}, $response->toArray());
......@@ -70,11 +80,11 @@ class Manager
}
$loadNext = $response->getPagingToken();
$response = $this->repository->getList($opts);
$response = $this->cassandraRepository->getList($opts);
$response->setPagingToken($loadNext);
}
} else {
$response = $this->repository->getList($opts);
$response = $this->cassandraRepository->getList($opts);
}
/**
......@@ -92,6 +102,21 @@ class Manager
return $response;
}
protected function optStateIsUsedAndValid(array $opts): bool
{
return $this->optStateIsUsed($opts) && $this->optStateIsValid($opts);
}
protected function optStateIsUsed(array $opts): bool
{
return !is_null($opts['state']);
}
protected function optStateIsValid(array $opts): bool
{
return in_array($opts['state'], self::VALID_OPT_STATEQUERY);
}
/**
* Get a single boost
* @param string $urn
......@@ -104,7 +129,7 @@ class Manager
'hydrate' => false,
], $opts);
$boost = $this->repository->get($urn);
$boost = $this->cassandraRepository->get($urn);
if ($boost && $opts['hydrate']) {
$boost = $this->hydrate($boost);
......@@ -123,14 +148,14 @@ class Manager
if (!$boost->getGuid()) {
$boost->setGuid($this->guidBuilder->build());
}
$this->repository->add($boost);
$this->cassandraRepository->add($boost);
$this->elasticRepository->add($boost);
return true;
}
public function update($boost, $fields = [])
{
$this->repository->update($boost, $fields);
$this->cassandraRepository->update($boost, $fields);
$this->resync($boost, $fields);
}
......@@ -147,8 +172,7 @@ class Manager
public function checkExisting(Boost $boost): bool
{
$existingBoost = $this->getList([
'useElastic' => true,
'state' => 'review',
'state' => self::OPT_STATEQUERY_REVIEW,
'type' => $boost->getType(),
'entity_guid' => $boost->getEntityGuid(),
'limit' => 1
......@@ -196,8 +220,7 @@ class Manager
public function getOffchainBoosts(string $type, int $ownerGuid, $limit = 10): Response
{
$offchainBoosts = $this->getList([
'useElastic' => true,
'state' => 'active',
'state' => self::OPT_STATEQUERY_ACTIVE,
'type' => $type,
'limit' => $limit,
'order' => 'desc',
......@@ -222,7 +245,7 @@ class Manager
public function expire(Boost $boost): void
{
if ($boost->getState() == 'completed') {
if ($boost->getState() === Boost::STATE_COMPLETED) {
$this->resync($boost);
}
......
......@@ -204,7 +204,7 @@ class Review implements BoostReviewInterface
{
return $this->manager->getList([
'type' => $this->type,
'state' => 'review',
'state' => Manager::OPT_STATEQUERY_REVIEW,
'limit' => $limit,
'offset' => $offset,
]);
......
......@@ -2,14 +2,14 @@
namespace Spec\Minds\Core\Boost\Network;
use Minds\Core\Boost\Network\Repository;
use Minds\Core\Boost\Network\CassandraRepository;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
class RepositorySpec extends ObjectBehavior
class CassandraRepositorySpec extends ObjectBehavior
{
public function it_is_initializable()
{
$this->shouldHaveType(Repository::class);
$this->shouldHaveType(CassandraRepository::class);
}
}
......@@ -6,7 +6,7 @@ use Minds\Common\Repository\Response;
use Minds\Core\Boost\Network\Boost;
use Minds\Core\Boost\Network\ElasticRepository;
use Minds\Core\Boost\Network\Manager;
use Minds\Core\Boost\Network\Repository;
use Minds\Core\Boost\Network\CassandraRepository;
use Minds\Core\EntitiesBuilder;
use Minds\Core\GuidBuilder;
use Minds\Entities\Activity;
......@@ -18,7 +18,7 @@ use Minds\Core\Di\Di;
class ManagerSpec extends ObjectBehavior
{
/** @var Repository */
/** @var CassandraRepository */
private $repository;
/** @var ElasticRepository */
private $elasticRepository;
......@@ -28,7 +28,7 @@ class ManagerSpec extends ObjectBehavior
private $guidBuilder;
public function let(
Repository $repository,
CassandraRepository $repository,
ElasticRepository $elasticRepository,
EntitiesBuilder $entitiesBuilder,
GuidBuilder $guidBuilder
......