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

feat: Added clear and destroy function for all kind of storages #56

Merged
merged 1 commit into from
Mar 5, 2024
Merged
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
feat: Added clear and destroy function for all kind of storages
OliverSkroblin committed Mar 5, 2024
commit d6202f7384987a70aa1554afa4d21c66e0867690
10 changes: 10 additions & 0 deletions src/Array/ArrayKeyStorage.php
Original file line number Diff line number Diff line change
@@ -14,6 +14,16 @@ class ArrayKeyStorage implements Storage
*/
private array $storage = [];

public function destroy(): void
{
$this->storage = [];
}

public function clear(): void
{
$this->storage = [];
}

public function store(Documents $documents): void
{
foreach ($documents as $document) {
13 changes: 12 additions & 1 deletion src/Array/ArrayStorage.php
Original file line number Diff line number Diff line change
@@ -39,10 +39,11 @@
use Shopware\Storage\Common\Schema\Collection;
use Shopware\Storage\Common\Schema\FieldType;
use Shopware\Storage\Common\Schema\SchemaUtil;
use Shopware\Storage\Common\Storage;
use Shopware\Storage\Common\StorageContext;
use Shopware\Storage\Common\Total;

class ArrayStorage extends ArrayKeyStorage implements FilterAware, AggregationAware
class ArrayStorage implements Storage, FilterAware, AggregationAware
{
/**
* @var array<string, Document>
@@ -54,6 +55,16 @@ public function __construct(
private readonly Collection $collection
) {}

public function clear(): void
{
$this->storage = [];
}

public function destroy(): void
{
$this->storage = [];
}

public function setup(): void
{
$this->storage = [];
4 changes: 2 additions & 2 deletions src/Common/Storage.php
Original file line number Diff line number Diff line change
@@ -32,7 +32,7 @@ public function store(Documents $documents): void;
*/
public function setup(): void;

// public function clear(): void;
public function clear(): void;

// public function destroy();
public function destroy(): void;
}
11 changes: 11 additions & 0 deletions src/DynamoDB/DynamoDBKeyStorage.php
Original file line number Diff line number Diff line change
@@ -26,6 +26,11 @@ public function __construct(
private readonly Collection $collection
) {}

public function destroy(): void
{
$this->client->deleteTable(['TableName' => $this->collection->name]);
}

public function setup(): void
{
$table = new CreateTableInput([
@@ -49,6 +54,12 @@ public function setup(): void
}
}

public function clear(): void
{
$this->destroy();
$this->setup();
}

public function remove(array $keys): void
{
$mapped = [];
10 changes: 10 additions & 0 deletions src/Meilisearch/MeilisearchStorage.php
Original file line number Diff line number Diff line change
@@ -55,6 +55,16 @@ public function __construct(
private readonly Collection $collection
) {}

public function destroy(): void
{
$this->client->deleteIndex($this->collection->name);
}

public function clear(): void
{
$this->index()->deleteAllDocuments();
}

public function get(string $key, StorageContext $context): ?Document
{
try {
10 changes: 10 additions & 0 deletions src/MongoDB/MongoDBKeyStorage.php
Original file line number Diff line number Diff line change
@@ -19,6 +19,16 @@ public function __construct(
private readonly Client $client
) {}

public function clear(): void
{
$this->collection()->deleteMany([]);
}

public function destroy(): void
{
$this->collection()->drop();
}

public function mget(array $keys, StorageContext $context): Documents
{
$query['key'] = ['$in' => $keys];
10 changes: 10 additions & 0 deletions src/MongoDB/MongoDBStorage.php
Original file line number Diff line number Diff line change
@@ -60,6 +60,16 @@ public function __construct(
private readonly Client $client
) {}

public function destroy(): void
{
$this->collection()->drop();
}

public function clear(): void
{
$this->collection()->deleteMany([]);
}

public function setup(): void {}

public function mget(array $keys, StorageContext $context): Documents
14 changes: 14 additions & 0 deletions src/MySQL/MySQLKeyStorage.php
Original file line number Diff line number Diff line change
@@ -28,6 +28,20 @@ private function table(): string
return '`' . $this->collection->name . '`';
}

public function destroy(): void
{
$this->connection->executeStatement(
sql: 'DROP TABLE IF EXISTS ' . $this->table()
);
}

public function clear(): void
{
$this->connection->executeStatement(
sql: 'DELETE FROM ' . $this->table()
);
}

public function setup(): void
{
$table = new Table(name: $this->collection->name);
14 changes: 14 additions & 0 deletions src/MySQL/MySQLStorage.php
Original file line number Diff line number Diff line change
@@ -51,6 +51,20 @@ private static function escape(string $source): string
return '`' . $source . '`';
}

public function destroy(): void
{
$this->connection->executeStatement(
sql: 'DROP TABLE IF EXISTS ' . self::escape($this->collection->name)
);
}

public function clear(): void
{
$this->connection->executeStatement(
sql: 'DELETE FROM ' . self::escape($this->collection->name)
);
}

public function setup(): void
{
$table = new Table(name: $this->collection->name);
21 changes: 21 additions & 0 deletions src/Opensearch/OpensearchStorage.php
Original file line number Diff line number Diff line change
@@ -76,6 +76,27 @@ public function __construct(
private readonly Collection $collection
) {}

public function destroy(): void
{
if (!$this->exists()) {
return;
}

$this->client->indices()->delete(['index' => $this->collection->name]);
}

public function clear(): void
{
if (!$this->exists()) {
return;
}

$this->client->deleteByQuery([
'index' => $this->collection->name,
'body' => ['query' => ['match_all' => new \stdClass()]],
]);
}

public function setup(): void
{
$properties = $this->setupProperties($this->collection);
10 changes: 10 additions & 0 deletions src/Redis/RedisKeyStorage.php
Original file line number Diff line number Diff line change
@@ -17,6 +17,16 @@ public function __construct(
private readonly Hydrator $hydrator
) {}

public function clear(): void
{
$this->client->flushDB();
}

public function destroy(): void
{
$this->client->flushDB();
}

public function setup(): void {}

public function remove(array $keys): void
12 changes: 12 additions & 0 deletions tests/Meilisearch/MeilisearchLiveStorage.php
Original file line number Diff line number Diff line change
@@ -20,6 +20,18 @@ public function __construct(
private readonly Client $client
) {}

public function destroy(): void
{
$this->storage->destroy();
$this->wait();
}

public function clear(): void
{
$this->storage->clear();
$this->wait();
}

public function mget(array $keys, StorageContext $context): Documents
{
return $this->storage->mget($keys, $context);
56 changes: 13 additions & 43 deletions tests/Meilisearch/MeilisearchTestTrait.php
Original file line number Diff line number Diff line change
@@ -3,9 +3,6 @@
namespace Shopware\StorageTests\Meilisearch;

use Meilisearch\Client;
use Meilisearch\Contracts\TasksQuery;
use Meilisearch\Endpoints\Indexes;
use Meilisearch\Exceptions\ApiException;
use Shopware\Storage\Common\Aggregation\AggregationCaster;
use Shopware\Storage\Common\Document\Hydrator;
use Shopware\Storage\Meilisearch\MeilisearchStorage;
@@ -15,31 +12,25 @@ trait MeilisearchTestTrait
{
private static ?Client $client = null;

private function exists(): bool
public static function setUpBeforeClass(): void
{
try {
$this->getClient()->getIndex(TestSchema::getCollection()->name);
} catch (ApiException) {
return false;
}

return true;
parent::setUpBeforeClass();
self::createStorage()->setup();
}

protected function setUp(): void
{
parent::setUp();
$this->getStorage()->clear();
}

if ($this->exists()) {
$this->index()->deleteAllDocuments();
$this->wait();
return;
}

$this->getStorage()->setup();
public static function tearDownAfterClass(): void
{
self::createStorage()->destroy();
parent::tearDownAfterClass();
}

private function getClient(): Client
private static function getClient(): Client
{
if (self::$client === null) {
self::$client = new Client(
@@ -51,37 +42,16 @@ private function getClient(): Client
return self::$client;
}

public function createStorage(): MeilisearchLiveStorage
private static function createStorage(): MeilisearchLiveStorage
{
return new MeilisearchLiveStorage(
storage: new MeilisearchStorage(
caster: new AggregationCaster(),
hydrator: new Hydrator(),
client: $this->getClient(),
client: self::getClient(),
collection: TestSchema::getCollection()
),
client: $this->getClient(),
client: self::getClient(),
);
}

private function index(): Indexes
{
return $this->getClient()->index(TestSchema::getCollection()->name);
}

private function wait(): void
{
$tasks = new TasksQuery();
$tasks->setStatuses(['enqueued', 'processing']);

$tasks = $this->getClient()->getTasks($tasks);

$ids = array_map(fn($task) => $task['uid'], $tasks->getResults());

if (count($ids) === 0) {
return;
}

$this->getClient()->waitForTasks($ids);
}
}
5 changes: 3 additions & 2 deletions tests/MongoDB/MongoDBAggregateStorageTest.php
Original file line number Diff line number Diff line change
@@ -30,13 +30,14 @@ private function getClient(): Client
protected function setUp(): void
{
parent::setUp();
$this->getClient()->dropDatabase('test');
$this->getStorage()->destroy();
$this->getStorage()->setup();
}

protected function tearDown(): void
{
parent::tearDown();
$this->getClient()->dropDatabase('test');
$this->getStorage()->destroy();
}

public function getStorage(): AggregationAware&Storage
5 changes: 3 additions & 2 deletions tests/MongoDB/MongoDBFilterStorageTest.php
Original file line number Diff line number Diff line change
@@ -30,13 +30,14 @@ private function getClient(): Client
protected function setUp(): void
{
parent::setUp();
$this->getClient()->dropDatabase('test');
$this->getStorage()->destroy();
$this->getStorage()->setup();
}

protected function tearDown(): void
{
parent::tearDown();
$this->getClient()->dropDatabase('test');
$this->getStorage()->destroy();
}

public function getStorage(): FilterAware&Storage
6 changes: 2 additions & 4 deletions tests/MySQL/MySQLKeyValueStorageTest.php
Original file line number Diff line number Diff line change
@@ -21,8 +21,7 @@ protected function setUp(): void
{
parent::setUp();

$this->getConnection()
->executeStatement('DROP TABLE IF EXISTS `' . TestSchema::getCollection()->name . '`');
$this->getStorage()->destroy();

$this->getStorage()->setup();
}
@@ -31,8 +30,7 @@ protected function tearDown(): void
{
parent::tearDown();

$this->getConnection()
->executeStatement('DROP TABLE IF EXISTS `' . TestSchema::getCollection()->name . '`');
$this->getStorage()->destroy();
}

public function getStorage(): Storage
2 changes: 1 addition & 1 deletion tests/MySQL/MySQLTestTrait.php
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ protected function setUp(): void
{
parent::setUp();

self::getConnection()->executeStatement('DROP TABLE IF EXISTS ' . TestSchema::getCollection()->name);
$this->getStorage()->destroy();

$this->getStorage()->setup();
}
26 changes: 0 additions & 26 deletions tests/MySQL/test_storage.sql

This file was deleted.

31 changes: 27 additions & 4 deletions tests/Opensearch/OpensearchLiveStorage.php
Original file line number Diff line number Diff line change
@@ -20,6 +20,18 @@ public function __construct(
private readonly \Shopware\Storage\Common\Schema\Collection $collection
) {}

public function destroy(): void
{
$this->decorated->destroy();
$this->wait();
}

public function clear(): void
{
$this->decorated->clear();
$this->wait();
}

public function mget(array $keys, StorageContext $context): Documents
{
return $this->decorated->mget($keys, $context);
@@ -33,15 +45,13 @@ public function get(string $key, StorageContext $context): ?Document
public function remove(array $keys): void
{
$this->decorated->remove($keys);

$this->client->indices()->refresh(['index' => $this->collection->name]);
$this->wait();
}

public function store(Documents $documents): void
{
$this->decorated->store($documents);

$this->client->indices()->refresh(['index' => $this->collection->name]);
$this->wait();
}

public function filter(Criteria $criteria, StorageContext $context): Result
@@ -61,4 +71,17 @@ public function setup(): void
{
$this->decorated->setup();
}

private function wait(): void
{
if (!$this->exists()) {
return;
}
$this->client->indices()->refresh(['index' => $this->collection->name]);
}

private function exists(): bool
{
return $this->client->indices()->exists(['index' => $this->collection->name]);
}
}
2 changes: 1 addition & 1 deletion tests/Opensearch/OpensearchStorageAggregationTest.php
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ class OpensearchStorageAggregationTest extends AggregationStorageTestBase

public function getStorage(): AggregationAware&Storage
{
return $this->createStorage(
return self::createStorage(
collection: TestSchema::getCollection(),
);
}
2 changes: 1 addition & 1 deletion tests/Opensearch/OpensearchStorageFilterTest.php
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ class OpensearchStorageFilterTest extends FilterStorageTestBase

public function getStorage(): FilterAware&Storage
{
return $this->createStorage(
return self::createStorage(
collection: TestSchema::getCollection(),
);
}
34 changes: 6 additions & 28 deletions tests/Opensearch/OpensearchTestTrait.php
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ trait OpensearchTestTrait
{
private static ?Client $client = null;

private function getClient(): Client
private static function getClient(): Client
{
if (self::$client === null) {
$builder = ClientBuilder::create();
@@ -30,31 +30,18 @@ protected function setUp(): void
{
parent::setUp();

$exists = $this->getClient()
->indices()
->exists(['index' => TestSchema::getCollection()->name]);

if ($exists) {
//$this->getClient()->indices()->delete(['index' => TestSchema::getCollection()->name]);
// delete all documents from index
$this->getClient()->deleteByQuery([
'index' => TestSchema::getCollection()->name,
'body' => [
'query' => ['match_all' => new \stdClass()],
],
]);
}
$this->getStorage()->clear();

$this->getStorage()->setup();
}

public function createStorage(Collection $collection): OpensearchLiveStorage
private static function createStorage(Collection $collection): OpensearchLiveStorage
{
return new OpensearchLiveStorage(
client: $this->getClient(),
client: self::getClient(),
decorated: new OpensearchStorage(
caster: new AggregationCaster(),
client: $this->getClient(),
client: self::getClient(),
hydrator: new Hydrator(),
collection: $collection
),
@@ -64,15 +51,6 @@ public function createStorage(Collection $collection): OpensearchLiveStorage

public static function tearDownAfterClass(): void
{
$builder = ClientBuilder::create();
$builder->setHosts(['http://localhost:9200']);
$client = $builder->build();

$exists = $client->indices()
->exists(['index' => TestSchema::getCollection()->name]);

if ($exists) {
$client->indices()->delete(['index' => TestSchema::getCollection()->name]);
}
self::createStorage(TestSchema::getCollection())->destroy();
}
}