|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ArangoClient\Schema; |
|
6
|
|
|
|
|
7
|
|
|
use ArangoClient\ArangoClient; |
|
8
|
|
|
use ArangoClient\Exceptions\ArangoException; |
|
9
|
|
|
use stdClass; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Manage collection indexes |
|
13
|
|
|
* |
|
14
|
|
|
* @see https://www.arangodb.com/docs/stable/http/indexes.html |
|
15
|
|
|
*/ |
|
16
|
|
|
trait ManagesIndexes |
|
17
|
|
|
{ |
|
18
|
|
|
protected ArangoClient $arangoClient; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @see https://www.arangodb.com/docs/stable/http/indexes-working-with.html#read-all-indexes-of-a-collection |
|
22
|
|
|
* |
|
23
|
|
|
* @return array<mixed> |
|
24
|
|
|
* |
|
25
|
|
|
* @throws ArangoException |
|
26
|
|
|
*/ |
|
27
|
|
|
public function getIndexes(string $collection): array |
|
28
|
|
|
{ |
|
29
|
|
|
$options = [ |
|
30
|
|
|
'query' => [ |
|
31
|
|
|
'collection' => $collection, |
|
32
|
|
|
], |
|
33
|
|
|
]; |
|
34
|
|
|
$results = $this->arangoClient->request('get', '/_api/index', $options); |
|
35
|
|
|
|
|
36
|
|
|
return (array) $results->indexes; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @see https://www.arangodb.com/docs/stable/http/indexes-working-with.html#read-index |
|
41
|
|
|
* |
|
42
|
|
|
* @throws ArangoException |
|
43
|
|
|
*/ |
|
44
|
|
|
public function getIndex(string $id): stdClass |
|
45
|
|
|
{ |
|
46
|
|
|
$uri = '/_api/index/' . $id; |
|
47
|
|
|
|
|
48
|
|
|
return $this->arangoClient->request('get', $uri); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @see https://www.arangodb.com/docs/stable/http/indexes-working-with.html#read-index |
|
53
|
|
|
* |
|
54
|
|
|
* @throws ArangoException |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getIndexByName(string $collection, string $name): stdClass|false |
|
57
|
|
|
{ |
|
58
|
|
|
$indexes = $this->getIndexes($collection); |
|
59
|
|
|
$searchResult = array_search($name, array_column($indexes, 'name')); |
|
60
|
|
|
|
|
61
|
|
|
if (is_int($searchResult)) { |
|
62
|
|
|
return (object) $indexes[$searchResult]; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return false; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @see https://www.arangodb.com/docs/stable/http/indexes-working-with.html#create-index |
|
70
|
|
|
* |
|
71
|
|
|
* @param array<mixed> $index |
|
72
|
|
|
* |
|
73
|
|
|
* @throws ArangoException |
|
74
|
|
|
*/ |
|
75
|
|
|
public function createIndex(string $collection, array $index): stdClass |
|
76
|
|
|
{ |
|
77
|
|
|
$indexType = 'persistent'; |
|
78
|
|
|
|
|
79
|
|
|
if (isset($index['type'])) { |
|
80
|
|
|
$indexType = (string) $index['type']; |
|
81
|
|
|
} |
|
82
|
|
|
$uri = '/_api/index#' . $indexType; |
|
83
|
|
|
|
|
84
|
|
|
$options = ['body' => $index]; |
|
85
|
|
|
$options['query']['collection'] = $collection; |
|
86
|
|
|
|
|
87
|
|
|
return $this->arangoClient->request('post', $uri, $options); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @see https://www.arangodb.com/docs/stable/http/indexes-working-with.html#delete-index |
|
92
|
|
|
* |
|
93
|
|
|
* @throws ArangoException |
|
94
|
|
|
*/ |
|
95
|
|
|
public function deleteIndex(string $id): stdClass |
|
96
|
|
|
{ |
|
97
|
|
|
$uri = '/_api/index/' . $id; |
|
98
|
|
|
|
|
99
|
|
|
return $this->arangoClient->request('delete', $uri); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|