Skip to content

Commit 5ab03ce

Browse files
feat[index]: Add support for inverted indexes (#19)
* feat[inverted-index]: strict typing for base index class * chore[indexes]: deprecate 'hash' and 'skiplist' index types * feat[indexes]: base implementation for 'inverted' index * tests[indexes]: remove ununsed imports * tests[indexes]: add tests for inverted index * feat[indexes]: add inverted index implementation
1 parent d168fdc commit 5ab03ce

File tree

11 files changed

+223
-101
lines changed

11 files changed

+223
-101
lines changed

src/Collection/Index/Factory.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public static function factory(array $attributes): IndexInterface
3838
'geo' => GeoSpatialIndex::class,
3939
'skiplist' => SkipListIndex::class,
4040
'ttl' => TTLIndex::class,
41+
'inverted' => InvertedIndex::class
4142
];
4243

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

65+
if ($attributes['type'] === 'inverted') {
66+
return new InvertedIndex($attributes['fields'], $attributes);
67+
}
68+
6469
$class = $indexes[$attributes['type']];
6570
return new $class($attributes['fields'], $attributes);
6671
}

src/Collection/Index/HashIndex.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*
1212
* @package ArangoDB\Collection\Index
1313
* @author Lucas S. Vieira
14+
* @deprecated
1415
*/
1516
class HashIndex extends Index
1617
{

src/Collection/Index/Index.php

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,42 +21,42 @@ class Index implements IndexInterface
2121
*
2222
* @var string
2323
*/
24-
protected $id;
24+
protected string $id;
2525

2626
/**
2727
* Index name.
2828
*
2929
* @var string
3030
*/
31-
protected $name;
31+
protected string $name;
3232

3333
/**
3434
* Sparse
3535
*
3636
* @var bool
3737
*/
38-
protected $sparse;
38+
protected bool $sparse;
3939

4040
/**
4141
* Index type
4242
*
4343
* @var string
4444
*/
45-
protected $type;
45+
protected string $type;
4646

4747
/**
4848
* Unique constraint
4949
*
5050
* @var bool
5151
*/
52-
protected $unique;
52+
protected bool $unique;
5353

5454
/**
5555
* Fields of index
5656
*
5757
* @var array
5858
*/
59-
protected $fields;
59+
protected array $fields;
6060

6161
/**
6262
* Collection where the index belongs to
@@ -71,21 +71,21 @@ class Index implements IndexInterface
7171
*
7272
* @var bool
7373
*/
74-
protected $isNew;
74+
protected bool $isNew;
7575

7676
/**
7777
* Valid indexes types
7878
*
7979
* @var array
8080
*/
81-
protected static $indexTypes = [
82-
'fulltext', 'general', 'geo', 'hash', 'persistent', 'skiplist', 'ttl', 'primary', 'edge'
81+
protected static array $indexTypes = [
82+
'fulltext', 'general', 'geo', 'hash', 'persistent', 'skiplist', 'ttl', 'primary', 'edge', 'inverted'
8383
];
8484

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

100+
$fieldNames = [];
101+
100102
foreach ($fields as $key => $field) {
101-
if (!is_string($field)) {
102-
throw new InvalidParameterException("fields[$key]", $field);
103+
if (is_string($field)) {
104+
array_push($fieldNames, $field);
105+
continue;
106+
}
107+
108+
if (is_array($field)) {
109+
array_push($fieldNames, $field['name']);
110+
continue;
103111
}
112+
113+
throw new InvalidParameterException("fields[$key]", $field);
104114
}
105115

106116
$this->type = $type;
107117
$this->fields = $fields;
108118

109119
// Default values;
110-
$this->id = isset($attributes['id']) ? $attributes['id'] : '';
111-
$this->name = isset($attributes['name']) ? $attributes['name'] : '';
112-
$this->unique = isset($attributes['unique']) ? $attributes['unique'] : false;
113-
$this->sparse = isset($attributes['sparse']) ? $attributes['sparse'] : false;
120+
$this->id = $attributes['id'] ?? '';
121+
$this->name = $attributes['name'] ?? '';
122+
$this->unique = $attributes['unique'] ?? false;
123+
$this->sparse = $attributes['sparse'] ?? false;
114124
$this->isNew = !isset($attributes['id']);
115125
}
116126

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace ArangoDB\Collection\Index;
4+
5+
use ArangoDB\Validation\Exceptions\InvalidParameterException;
6+
7+
/**
8+
* Inverted index representation
9+
*
10+
* @package ArangoDB\Collection\Index
11+
* @author Lucas S. Vieira
12+
*/
13+
final class InvertedIndex extends Index
14+
{
15+
/**
16+
* Default options for inverted index
17+
*
18+
* @var array
19+
*/
20+
protected array $defaultOptions = [
21+
'unique' => true,
22+
'sparse' => true,
23+
'analyzer' => "identity",
24+
'inBackground' => true, // Keep the collection available for writes during the index creation
25+
'parallelism' => 2,
26+
];
27+
28+
/**
29+
* InvertedIndex constructor
30+
*
31+
* @param array $fields Fields for which the index applies to
32+
* @param array $attributes Index $attributes
33+
*
34+
* @throws InvalidParameterException
35+
*/
36+
public function __construct(array $fields, array $attributes = [])
37+
{
38+
parent::__construct('inverted', $fields, $attributes);
39+
}
40+
}

src/Collection/Index/SkipListIndex.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*
1212
* @package ArangoDB\Collection\Index
1313
* @author Lucas S. Vieira
14+
* @deprecated
1415
*/
1516
final class SkipListIndex extends HashIndex
1617
{

0 commit comments

Comments
 (0)