add init methods

This commit is contained in:
ghost 2024-02-12 15:36:13 +02:00
parent a178130500
commit 777d98c6a0

View File

@ -4,20 +4,22 @@ declare(strict_types=1);
namespace Kvazar\Index; namespace Kvazar\Index;
use Manticoresearch\Client;
use Manticoresearch\Index;
class Manticore class Manticore
{ {
private \Manticoresearch\Client $_client; private \Manticoresearch\Client $_client;
private \Manticoresearch\Index $_index;
private $_index;
public function __construct( public function __construct(
?string $name = 'kvazar', ?string $name = 'kvazar',
?array $meta = [],
?string $host = '127.0.0.1', ?string $host = '127.0.0.1',
?int $port = 9308, ?int $port = 9308
?bool $drop = false
) )
{ {
$this->_client = new \Manticoresearch\Client( $this->_client = new Client(
[ [
'host' => $host, 'host' => $host,
'port' => $port, 'port' => $port,
@ -28,39 +30,69 @@ class Manticore
$name $name
); );
if ($drop) $this->_index->create(
[
'block' =>
[
'type' => 'int'
],
'namespace' =>
[
'type' => 'text'
],
'transaction' =>
[
'type' => 'text'
],
'key' =>
[
'type' => 'text'
],
'value' =>
[
'type' => 'text'
]
],
$meta,
true
);
}
public function add(
int $block,
string $namespace,
string $transaction,
string $key,
string $value
): int
{
$crc32transaction = crc32(
$transaction
);
if (!$this->_index->getDocumentById($crc32transaction))
{ {
$this->_index->drop( $this->_index->addDocument(
true [
'block' => $block,
'namespace' => $namespace,
'transaction' => $transaction,
'key' => $key,
'value' => $value
],
$crc32transaction
); );
} }
if (!$this->_index->status()) return $crc32transaction;
{ }
$this->_index->create(
[ public function drop(
'block' => ?bool $silent = false
[ )
'type' => 'int' {
], return $this->_index->drop(
'namespace' => $silent
[ );
'type' => 'text'
],
'txid' =>
[
'type' => 'text'
],
'key' =>
[
'type' => 'text'
],
'value' =>
[
'type' => 'text'
]
]
);
}
} }
} }