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 Support for autocreate of an Index for Loupe and Memory Adapter #471

Merged
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
40 changes: 24 additions & 16 deletions packages/seal-loupe-adapter/src/LoupeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,7 @@ public function dropIndex(Index $index): void

public function createIndex(Index $index): void
{
$indexDirectory = $this->getIndexDirectory($index);
if (!\file_exists($indexDirectory)) {
\mkdir($indexDirectory, recursive: true);
}

// dumping the configuration allows us to search and index without knowing the configuration
// this way when a similar class like this would be part of loupe only the createIndex method
// would require then to know the configuration
$configuration = $this->createConfiguration($index);
$configuration = $this->createAndDumpConfiguration($index);
\file_put_contents($this->getConfigurationFile($index), \serialize($configuration));
$this->loupe[$index->name] = $this->createLoupe($index, $configuration);
}
Expand All @@ -112,14 +104,14 @@ private function createLoupe(Index $index, Configuration|null $configuration = n
$configurationFile = $this->getConfigurationFile($index);

if (!\file_exists($configurationFile)) {
throw new \LogicException('Configuration need to exist before creating Loupe instance.');
}
$configuration = $this->createAndDumpConfiguration($index);
} else {
/** @var string $configurationContent */
$configurationContent = \file_get_contents($configurationFile);

/** @var string $configurationContent */
$configurationContent = \file_get_contents($configurationFile);

/** @var Configuration $configuration */
$configuration = \unserialize($configurationContent);
/** @var Configuration $configuration */
$configuration = \unserialize($configurationContent);
}
}

if ('' === $this->directory) {
Expand All @@ -129,6 +121,22 @@ private function createLoupe(Index $index, Configuration|null $configuration = n
return $this->loupeFactory->create($this->getIndexDirectory($index), $configuration);
}

private function createAndDumpConfiguration(Index $index): Configuration
{
$indexDirectory = $this->getIndexDirectory($index);
if (!\file_exists($indexDirectory)) {
\mkdir($indexDirectory, recursive: true);
}

// dumping the configuration allows us to search and index without knowing the configuration
// this way when a similar class like this would be part of loupe only the createIndex method
// would require then to know the configuration
$configuration = $this->createConfiguration($index);
\file_put_contents($this->getConfigurationFile($index), \serialize($configuration));

return $configuration;
}

private function createConfiguration(Index $index): Configuration
{
return Configuration::create()
Expand Down
6 changes: 3 additions & 3 deletions packages/seal-memory-adapter/src/MemoryStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ final class MemoryStorage
public static function getDocuments(Index $index): array
{
if (!\array_key_exists($index->name, self::$indexes)) {
throw new \RuntimeException('Index "' . $index->name . '" does not exist.');
self::createIndex($index);
}

return self::$documents[$index->name];
Expand All @@ -50,7 +50,7 @@ public static function getDocuments(Index $index): array
public static function save(Index $index, array $document): array
{
if (!\array_key_exists($index->name, self::$indexes)) {
throw new \RuntimeException('Index "' . $index->name . '" does not exist.');
self::createIndex($index);
}

$identifierField = $index->getIdentifierField();
Expand All @@ -66,7 +66,7 @@ public static function save(Index $index, array $document): array
public static function delete(Index $index, string $identifier): void
{
if (!\array_key_exists($index->name, self::$indexes)) {
throw new \RuntimeException('Index "' . $index->name . '" does not exist.');
self::createIndex($index);
}

unset(self::$documents[$index->name][$identifier]);
Expand Down
Loading