Initial code to support backend id as virtual guid.

This commit is contained in:
Abdulmhsen B. A. A
2022-05-28 08:22:24 +03:00
parent 56e62028e8
commit aa03d21f40
6 changed files with 70 additions and 25 deletions

View File

@@ -75,7 +75,7 @@ final class ListCommand extends Command
)
->setDescription('List Database entries.');
foreach (array_keys(Guid::SUPPORTED) as $guid) {
foreach (array_keys(Guid::getSupported(includeVirtual: false)) as $guid) {
$guid = afterLast($guid, 'guid_');
$this->addOption(
$guid,
@@ -153,7 +153,7 @@ final class ListCommand extends Command
}
if ($input->getOption('parent')) {
foreach (array_keys(Guid::SUPPORTED) as $guid) {
foreach (array_keys(Guid::getSupported(includeVirtual: false)) as $guid) {
if (null === ($val = $input->getOption(afterLast($guid, 'guid_')))) {
continue;
}
@@ -161,7 +161,7 @@ final class ListCommand extends Command
$params[$guid] = $val;
}
} else {
foreach (array_keys(Guid::SUPPORTED) as $guid) {
foreach (array_keys(Guid::getSupported(includeVirtual: false)) as $guid) {
if (null === ($val = $input->getOption(afterLast($guid, 'guid_')))) {
continue;
}

View File

@@ -326,9 +326,11 @@ class ImportCommand extends Command
file_put_contents($config, Yaml::dump(Config::get('servers', []), 8, 2));
}
$this->logger->notice(
sprintf('SYSTEM: Memory Usage (Now: %s) - (Peak: %s).', getMemoryUsage(), getPeakMemoryUsage())
);
if ($inTradeMode) {
$this->logger->notice(
sprintf('SYSTEM: Memory Usage (Now: %s) - (Peak: %s).', getMemoryUsage(), getPeakMemoryUsage())
);
}
return self::SUCCESS;
}

View File

@@ -154,7 +154,12 @@ final class StateEntity implements iFace
public function getPointers(array|null $guids = null): array
{
return Guid::fromArray(array_intersect_key($this->guids, Guid::SUPPORTED))->getPointers();
return Guid::fromArray(
array_intersect_key(
$this->guids,
Guid::getSupported(includeVirtual: true)
)
)->getPointers();
}
public function hasParentGuid(): bool
@@ -199,7 +204,7 @@ final class StateEntity implements iFace
$list[$key] = $val . '/' . $this->season . '/' . $this->episode;
}
return array_intersect_key($list, Guid::SUPPORTED);
return array_intersect_key($list, Guid::getSupported(includeVirtual: true));
}
public function getRelativePointers(): array

View File

@@ -17,7 +17,7 @@ final class Guid
public const GUID_TVRAGE = 'guid_tvrage';
public const GUID_ANIDB = 'guid_anidb';
public const SUPPORTED = [
private const SUPPORTED = [
Guid::GUID_PLEX => 'string',
Guid::GUID_IMDB => 'string',
Guid::GUID_TVDB => 'string',
@@ -27,6 +27,8 @@ final class Guid
Guid::GUID_ANIDB => 'string',
];
private const BACKEND_GUID = 'guidv_';
private const LOOKUP_KEY = '%s://%s';
private array $data = [];
@@ -35,13 +37,16 @@ final class Guid
* Create List of db => external id list.
*
* @param array $guids Key/value pair of db => external id. For example, [ "guid_imdb" => "tt123456789" ]
* @param bool $includeVirtual Whether to consider virtual Guids.
*
* @throws RuntimeException if key/value is of unexpected type or unsupported.
*/
public function __construct(array $guids)
public function __construct(array $guids, bool $includeVirtual = true)
{
$supported = self::getSupported(includeVirtual: $includeVirtual);
foreach ($guids as $key => $value) {
if (null === $value || null === (Guid::SUPPORTED[$key] ?? null)) {
if (null === $value || null === ($supported[$key] ?? null)) {
continue;
}
@@ -52,28 +57,28 @@ final class Guid
if (!is_string($key)) {
throw new RuntimeException(
sprintf(
'Unexpected offset type was given. Was expecting \'string\' but got \'%s\' instead.',
'Unexpected key type was given. Was expecting \'string\' but got \'%s\' instead.',
get_debug_type($key)
),
);
}
if (null === (Guid::SUPPORTED[$key] ?? null)) {
if (null === ($supported[$key] ?? null)) {
throw new RuntimeException(
sprintf(
'Unexpected key. Was expecting one of \'%s\', but got \'%s\' instead.',
implode(', ', array_keys(Guid::SUPPORTED)),
$key
'Unexpected key \'%s\'. Expecting \'%s\'.',
$key,
implode(', ', array_keys($supported))
),
);
}
if (Guid::SUPPORTED[$key] !== ($valueType = get_debug_type($value))) {
if ($supported[$key] !== ($valueType = get_debug_type($value))) {
throw new RuntimeException(
sprintf(
'Unexpected value type for \'%s\'. Was Expecting \'%s\' but got \'%s\' instead.',
'Unexpected value type for \'%s\'. Expecting \'%s\' but got \'%s\' instead.',
$key,
Guid::SUPPORTED[$key],
$supported[$key],
$valueType
)
);
@@ -83,6 +88,32 @@ final class Guid
}
}
public static function makeVirtualGuid(string $backend, string $value): string
{
return self::BACKEND_GUID . $backend . '://' . $value;
}
public static function getSupported(bool $includeVirtual = false): array
{
static $list = null;
if (false === $includeVirtual) {
return self::SUPPORTED;
}
if (null !== $list) {
return $list;
}
$list = self::SUPPORTED;
foreach (array_keys((array)Config::get('servers', [])) as $name) {
$list[self::BACKEND_GUID . $name] = 'string';
}
return $list;
}
/**
* Create new instance from array payload.
*
@@ -125,7 +156,7 @@ final class Guid
}
/**
* Return list of External ids.
* Return list of external ids.
*
* @return array
*/

View File

@@ -580,7 +580,7 @@ final class PDOAdapter implements StorageInterface
'type' => $entity->type,
];
foreach (array_keys(Guid::SUPPORTED) as $key) {
foreach (array_keys(Guid::getSupported(includeVirtual: true)) as $key) {
if (null === ($entity->guids[$key] ?? null)) {
continue;
}

View File

@@ -145,11 +145,15 @@ final class PDODataMigration
iFace::COLUMN_SEASON => ag($row['meta'], iFace::COLUMN_SEASON, null),
iFace::COLUMN_EPISODE => ag($row['meta'], iFace::COLUMN_EPISODE, null),
iFace::COLUMN_PARENT => json_encode(
value: array_intersect_key($row, ag($row['meta'], iFace::COLUMN_PARENT, []), Guid::SUPPORTED),
value: array_intersect_key(
$row,
ag($row['meta'], iFace::COLUMN_PARENT, []),
Guid::getSupported(includeVirtual: false)
),
flags: $this->jFlags
),
iFace::COLUMN_GUIDS => json_encode(
value: array_intersect_key($row, Guid::SUPPORTED),
value: array_intersect_key($row, Guid::getSupported(includeVirtual: false)),
flags: $this->jFlags
),
iFace::COLUMN_META_DATA => json_encode(
@@ -325,11 +329,14 @@ final class PDODataMigration
iFace::COLUMN_SEASON => $row[iFace::COLUMN_SEASON] ?? null,
iFace::COLUMN_EPISODE => $row[iFace::COLUMN_EPISODE] ?? null,
iFace::COLUMN_PARENT => json_encode(
value: array_intersect_key($row[iFace::COLUMN_PARENT] ?? [], Guid::SUPPORTED),
value: array_intersect_key(
$row[iFace::COLUMN_PARENT] ?? [],
Guid::getSupported(includeVirtual: false)
),
flags: $this->jFlags
),
iFace::COLUMN_GUIDS => json_encode(
value: array_intersect_key($row[iFace::COLUMN_GUIDS], Guid::SUPPORTED),
value: array_intersect_key($row[iFace::COLUMN_GUIDS], Guid::getSupported(includeVirtual: false)),
flags: $this->jFlags
),
iFace::COLUMN_META_DATA => json_encode(