Skip to content

Commit 8d14649

Browse files
implement basic index (without search) (#3)
1 parent d2957a3 commit 8d14649

10 files changed

+300
-1
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.docker/data

PuceneAdapter.php

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Pucene.
5+
*
6+
* (c) asapo.at
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Pucene\SealAdapter;
13+
14+
use Pucene\Index\Driver\DriverFactoryInterface;
15+
use Pucene\Index\PuceneIndexFactory;
16+
use Schranz\Search\SEAL\Adapter\AdapterInterface;
17+
use Schranz\Search\SEAL\Adapter\ConnectionInterface;
18+
use Schranz\Search\SEAL\Adapter\SchemaManagerInterface;
19+
20+
class PuceneAdapter implements AdapterInterface
21+
{
22+
public static function createFromFactories(
23+
PuceneIndexFactory $indexFactory,
24+
DriverFactoryInterface $driverFactory,
25+
): self {
26+
return new self(
27+
new PuceneConnection($indexFactory),
28+
new PuceneSchemaManager($driverFactory),
29+
);
30+
}
31+
32+
private readonly ConnectionInterface $connection;
33+
34+
private readonly SchemaManagerInterface $schemaManager;
35+
36+
public function __construct(
37+
ConnectionInterface $connection,
38+
SchemaManagerInterface $schemaManager,
39+
) {
40+
$this->connection = $connection;
41+
$this->schemaManager = $schemaManager;
42+
}
43+
44+
public function getSchemaManager(): SchemaManagerInterface
45+
{
46+
return $this->schemaManager;
47+
}
48+
49+
public function getConnection(): ConnectionInterface
50+
{
51+
return $this->connection;
52+
}
53+
}

PuceneConnection.php

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Pucene.
5+
*
6+
* (c) asapo.at
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Pucene\SealAdapter;
13+
14+
use Pucene\Index\PuceneIndexFactory;
15+
use Schranz\Search\SEAL\Adapter\ConnectionInterface;
16+
use Schranz\Search\SEAL\Schema\Index;
17+
use Schranz\Search\SEAL\Search\Result;
18+
use Schranz\Search\SEAL\Search\Search;
19+
20+
class PuceneConnection implements ConnectionInterface
21+
{
22+
public function __construct(
23+
private PuceneIndexFactory $indexFactory,
24+
) {
25+
}
26+
27+
/**
28+
* @param mixed[] $document
29+
*
30+
* @return mixed[]
31+
*/
32+
public function save(Index $index, array $document): array
33+
{
34+
return $this->indexFactory->create($index)->save($document);
35+
}
36+
37+
public function delete(Index $index, string $identifier): void
38+
{
39+
$this->indexFactory->create($index)->delete($identifier);
40+
}
41+
42+
public function search(Search $search): Result
43+
{
44+
// TODO: Implement search() method.
45+
46+
return new Result($this->mockGenerator(), 1);
47+
}
48+
49+
private function mockGenerator(): \Generator
50+
{
51+
yield ['id' => 1];
52+
}
53+
}

PuceneSchemaManager.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Pucene.
5+
*
6+
* (c) asapo.at
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Pucene\SealAdapter;
13+
14+
use Pucene\Index\Driver\DriverFactoryInterface;
15+
use Schranz\Search\SEAL\Adapter\SchemaManagerInterface;
16+
use Schranz\Search\SEAL\Schema\Index;
17+
18+
class PuceneSchemaManager implements SchemaManagerInterface
19+
{
20+
public function __construct(
21+
private DriverFactoryInterface $driverFactory,
22+
) {
23+
}
24+
25+
public function existIndex(Index $index): bool
26+
{
27+
return $this->driverFactory->create($index)->isInitialized();
28+
}
29+
30+
public function dropIndex(Index $index): void
31+
{
32+
$this->driverFactory->create($index)->drop();
33+
}
34+
35+
public function createIndex(Index $index): void
36+
{
37+
$this->driverFactory->create($index)->initialize();
38+
}
39+
}

README.md

+42
Original file line numberDiff line numberDiff line change
@@ -1 +1,43 @@
11
# Pucene: Seal Adapter
2+
3+
Adapter package that glues pucene into SEAL universe.
4+
5+
> This is a subtree split of the `pucene/pucene` project create issues in the [main repository](https://github.com/pucene/pucene).
6+
7+
## Usage
8+
9+
```php
10+
<?php
11+
12+
use Doctrine\DBAL\DriverManager;
13+
use Pucene\Analysis\StandardAnalyzer;
14+
use Pucene\DbalDriver\DbalDriverFactory;
15+
use Pucene\Index\PuceneIndexFactory;
16+
use Pucene\SealAdapter\PuceneAdapter;
17+
use Schranz\Search\SEAL\Schema\Index;
18+
use Schranz\Search\SEAL\Schema\Schema;
19+
use Schranz\Search\SEAL\Schema\Field;
20+
21+
$dbalConnection = DriverManager::getConnection([
22+
'url' => 'mysql://[email protected]:3306/pucene?serverVersion=8.0',
23+
]);
24+
$driverFactory = new DbalDriverFactory($dbalConnection);
25+
26+
$adapter = PuceneAdapter::createFromFactories(
27+
new PuceneIndexFactory(
28+
$driverFactory,
29+
new StandardAnalyzer(),
30+
),
31+
$driverFactory,
32+
);
33+
34+
$engine = new Engine(
35+
$adapter,
36+
new Schema([
37+
'blog' => new Index('blog', [
38+
'id' => new Field\IdentifierField('id'),
39+
'title' => new Field\TextField('title'),
40+
]),
41+
]),
42+
);
43+
```

Tests/.gitkeep

Whitespace-only changes.

Tests/PuceneAdapterTest.php

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Pucene.
5+
*
6+
* (c) asapo.at
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Tests;
13+
14+
use Doctrine\DBAL\DriverManager;
15+
use Pucene\Analysis\StandardAnalyzer;
16+
use Pucene\DbalDriver\DbalDriverFactory;
17+
use Pucene\Index\PuceneIndexFactory;
18+
use Pucene\SealAdapter\PuceneAdapter;
19+
use Schranz\Search\SEAL\Testing\AbstractAdapterTestCase;
20+
21+
class PuceneAdapterTest extends AbstractAdapterTestCase
22+
{
23+
public static function setUpBeforeClass(): void
24+
{
25+
$dbalConnection = DriverManager::getConnection([
26+
'url' => 'mysql://[email protected]:3306/pucene?serverVersion=8.0',
27+
]);
28+
$driverFactory = new DbalDriverFactory($dbalConnection);
29+
30+
self::$adapter = PuceneAdapter::createFromFactories(
31+
new PuceneIndexFactory(
32+
$driverFactory,
33+
new StandardAnalyzer(),
34+
),
35+
$driverFactory,
36+
);
37+
}
38+
39+
public function testDocument(): void
40+
{
41+
$this->markTestSkipped();
42+
}
43+
}

Tests/PuceneSchemaManagerTest.php

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Pucene.
5+
*
6+
* (c) asapo.at
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Tests;
13+
14+
use Doctrine\DBAL\DriverManager;
15+
use Pucene\DbalDriver\DbalDriverFactory;
16+
use Pucene\SealAdapter\PuceneSchemaManager;
17+
use Schranz\Search\SEAL\Testing\AbstractSchemaManagerTestCase;
18+
19+
class PuceneSchemaManagerTest extends AbstractSchemaManagerTestCase
20+
{
21+
public static function setUpBeforeClass(): void
22+
{
23+
$dbalConnection = DriverManager::getConnection([
24+
'url' => 'mysql://[email protected]:3306/pucene?serverVersion=8.0',
25+
]);
26+
$driverFactory = new DbalDriverFactory($dbalConnection);
27+
28+
self::$schemaManager = new PuceneSchemaManager($driverFactory);
29+
}
30+
}

composer.json

+26-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
],
2525
"require": {
2626
"php": "^8.1",
27+
"pucene/index": "^0.1@dev",
28+
"pucene/dbal-driver": "^0.1@dev",
2729
"schranz-search/seal": "^0.1@dev"
2830
},
2931
"require-dev": {
@@ -34,5 +36,28 @@
3436
"test": "@phpunit Tests",
3537
"phpunit": "vendor/bin/phpunit --bootstrap vendor/autoload.php"
3638
},
37-
"minimum-stability": "dev"
39+
"minimum-stability": "dev",
40+
"repositories": [
41+
{
42+
"type": "path",
43+
"url": "../analysis",
44+
"options": {
45+
"symlink": true
46+
}
47+
},
48+
{
49+
"type": "path",
50+
"url": "../dbal-driver",
51+
"options": {
52+
"symlink": true
53+
}
54+
},
55+
{
56+
"type": "path",
57+
"url": "../index",
58+
"options": {
59+
"symlink": true
60+
}
61+
}
62+
]
3863
}

docker-compose.yaml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: '3'
2+
3+
services:
4+
database:
5+
image: mysql/mysql-server:8.0
6+
environment:
7+
MYSQL_DATABASE: pucene
8+
MYSQL_ALLOW_EMPTY_PASSWORD: 1
9+
MYSQL_ROOT_HOST: '%'
10+
ports:
11+
- 3306:3306
12+
volumes:
13+
- ./.docker/data/mysql:/var/lib/mysql

0 commit comments

Comments
 (0)