Skip to content

Commit

Permalink
Move packages for splitsh (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz authored Dec 28, 2022
0 parents commit 8688e27
Show file tree
Hide file tree
Showing 8 changed files with 222 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.gitattributes export-ignore
.gitignore export-ignore
composer.lock export-ignore
/Tests export-ignore
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/vendor/
/composer.phar
/phpunit.xml
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Alexander Schranz

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Schranz Search SEAL Read Write Adapter

The `ReadWriteAdapter` allows to use one adapter instance for reading
and one for writing. This is useful if you want to reindex something
without a downtime.

> This is a subtree split of the `schranz-search/schranz-search` project create issues in the [main repository](https://github.com/schranz-search/schranz-search).
## Usage

To use the adapter an instance of `ReadWriteAdapter` need to be created
which get a `$readAdapter` and `$writeAdapter` which are instances of the
`AdapterInterface`.

```php
<?php

use Schranz\Search\SEAL\Adapter\Elasticsearch\ElasticsearchAdapter;
use Schranz\Search\SEAL\Adapter\ReadWrite\ReadWriteAdapter;
use Schranz\Search\SEAL\Engine;

$readAdapter = new ElasticsearchAdapter(/* .. */); // can be any adapter
$writeAdapter = new ElasticsearchAdapter(/* .. */); // can be any adapter

$engine = new Engine(
new ReadWriteAdapter(
$readAdapter,
$writeAdapter
),
$schema,
);
```

> **Note**
> Read a document and partial update it based on the read document should be avoided
> when using this adapter, as the read document could already be outdated. So always
> fully update the document and never do based on read documents.
> Have a look at the `MultiAdapter` to write into read and write adapter.
34 changes: 34 additions & 0 deletions ReadWriteAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Schranz\Search\SEAL\Adapter\ReadWrite;

use Schranz\Search\SEAL\Adapter\AdapterInterface;
use Schranz\Search\SEAL\Adapter\ConnectionInterface;
use Schranz\Search\SEAL\Adapter\SchemaManagerInterface;

final class ReadWriteAdapter implements AdapterInterface
{
private ?ConnectionInterface $connection = null;

public function __construct(
private readonly AdapterInterface $readAdapter,
private readonly AdapterInterface $writeAdapter,
) {}

public function getSchemaManager(): SchemaManagerInterface
{
return $this->writeAdapter->getSchemaManager();
}

public function getConnection(): ConnectionInterface
{
if ($this->connection === null) {
$this->connection = new ReadWriteConnection(
$this->readAdapter->getConnection(),
$this->writeAdapter->getConnection(),
);
}

return $this->connection;
}
}
35 changes: 35 additions & 0 deletions ReadWriteConnection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Schranz\Search\SEAL\Adapter\ReadWrite;

use Schranz\Search\SEAL\Adapter\ConnectionInterface;
use Schranz\Search\SEAL\Schema\Index;
use Schranz\Search\SEAL\Search\Result;
use Schranz\Search\SEAL\Search\Search;

/**
* @internal This class should never be needed to be instanced manually.
*/
final class ReadWriteConnection implements ConnectionInterface
{
public function __construct(
public readonly ConnectionInterface $readConnection,
public readonly ConnectionInterface $writeConnection,
) {}

public function save(Index $index, array $document): array
{
return $this->writeConnection->save($index, $document);
}

public function delete(Index $index, string $identifier): void
{
$this->writeConnection->delete($index, $identifier);
}

public function search(Search $search): Result
{
return $this->readConnection->search($search);
}

}
31 changes: 31 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "schranz-search/seal-read-write-adapter",
"description": "An adapter to support to split read and write operations for the schranz-search/seal package.",
"type": "library",
"license": "MIT",
"autoload": {
"psr-4": {
"Schranz\\Search\\SEAL\\Adapter\\ReadWrite\\": ""
}
},
"authors": [
{
"name": "Alexander Schranz",
"email": "[email protected]"
}
],
"require": {
"php": "^8.1",
"schranz-search/seal": "^0.1"
},
"repositories": [
{
"type": "path",
"url": "./../seal",
"options": {
"symlink": true
}
}
],
"minimum-stability": "dev"
}
56 changes: 56 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8688e27

Please sign in to comment.