Skip to content
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
5 changes: 5 additions & 0 deletions src/Collection/Index/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public static function factory(array $attributes): IndexInterface
'geo' => GeoSpatialIndex::class,
'skiplist' => SkipListIndex::class,
'ttl' => TTLIndex::class,
'inverted' => InvertedIndex::class
];

if (!array_key_exists($attributes['type'], $indexes)) {
Expand All @@ -61,6 +62,10 @@ public static function factory(array $attributes): IndexInterface
return new TTLIndex($attributes['fields'], $attributes['expireAfter'], $attributes);
}

if ($attributes['type'] === 'inverted') {
return new InvertedIndex($attributes['fields'], $attributes);
}

$class = $indexes[$attributes['type']];
return new $class($attributes['fields'], $attributes);
}
Expand Down
1 change: 1 addition & 0 deletions src/Collection/Index/HashIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*
* @package ArangoDB\Collection\Index
* @author Lucas S. Vieira
* @deprecated
*/
class HashIndex extends Index
{
Expand Down
42 changes: 26 additions & 16 deletions src/Collection/Index/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,42 +21,42 @@ class Index implements IndexInterface
*
* @var string
*/
protected $id;
protected string $id;

/**
* Index name.
*
* @var string
*/
protected $name;
protected string $name;

/**
* Sparse
*
* @var bool
*/
protected $sparse;
protected bool $sparse;

/**
* Index type
*
* @var string
*/
protected $type;
protected string $type;

/**
* Unique constraint
*
* @var bool
*/
protected $unique;
protected bool $unique;

/**
* Fields of index
*
* @var array
*/
protected $fields;
protected array $fields;

/**
* Collection where the index belongs to
Expand All @@ -71,21 +71,21 @@ class Index implements IndexInterface
*
* @var bool
*/
protected $isNew;
protected bool $isNew;

/**
* Valid indexes types
*
* @var array
*/
protected static $indexTypes = [
'fulltext', 'general', 'geo', 'hash', 'persistent', 'skiplist', 'ttl', 'primary', 'edge'
protected static array $indexTypes = [
'fulltext', 'general', 'geo', 'hash', 'persistent', 'skiplist', 'ttl', 'primary', 'edge', 'inverted'
];

/**
* Index constructor.
*
* @param string $type The index type. Must be one of following values: 'fulltext', 'general', 'geo', 'hash', 'persistent', 'skiplist' or 'ttl'
* @param string $type The index type. Must be one of following values: 'fulltext', 'general', 'geo', 'hash', 'persistent', 'skiplist', 'inverted' or 'ttl'
* @param array $fields An array of attribute names. Normally with just one attribute.
*
* @param array $attributes
Expand All @@ -97,20 +97,30 @@ public function __construct(string $type, array $fields, array $attributes = [])
throw new InvalidParameterException("type", $type);
}

$fieldNames = [];

foreach ($fields as $key => $field) {
if (!is_string($field)) {
throw new InvalidParameterException("fields[$key]", $field);
if (is_string($field)) {
array_push($fieldNames, $field);
continue;
}

if (is_array($field)) {
array_push($fieldNames, $field['name']);
continue;
}

throw new InvalidParameterException("fields[$key]", $field);
}

$this->type = $type;
$this->fields = $fields;

// Default values;
$this->id = isset($attributes['id']) ? $attributes['id'] : '';
$this->name = isset($attributes['name']) ? $attributes['name'] : '';
$this->unique = isset($attributes['unique']) ? $attributes['unique'] : false;
$this->sparse = isset($attributes['sparse']) ? $attributes['sparse'] : false;
$this->id = $attributes['id'] ?? '';
$this->name = $attributes['name'] ?? '';
$this->unique = $attributes['unique'] ?? false;
$this->sparse = $attributes['sparse'] ?? false;
$this->isNew = !isset($attributes['id']);
}

Expand Down
40 changes: 40 additions & 0 deletions src/Collection/Index/InvertedIndex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace ArangoDB\Collection\Index;

use ArangoDB\Validation\Exceptions\InvalidParameterException;

/**
* Inverted index representation
*
* @package ArangoDB\Collection\Index
* @author Lucas S. Vieira
*/
final class InvertedIndex extends Index
{
/**
* Default options for inverted index
*
* @var array
*/
protected array $defaultOptions = [
'unique' => true,
'sparse' => true,
'analyzer' => "identity",
'inBackground' => true, // Keep the collection available for writes during the index creation
'parallelism' => 2,
];

/**
* InvertedIndex constructor
*
* @param array $fields Fields for which the index applies to
* @param array $attributes Index $attributes
*
* @throws InvalidParameterException
*/
public function __construct(array $fields, array $attributes = [])
{
parent::__construct('inverted', $fields, $attributes);
}
}
1 change: 1 addition & 0 deletions src/Collection/Index/SkipListIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*
* @package ArangoDB\Collection\Index
* @author Lucas S. Vieira
* @deprecated
*/
final class SkipListIndex extends HashIndex
{
Expand Down
Loading