Skip to content

Commit d5585ac

Browse files
committed
Profilequery Plan test
1 parent b11f0d2 commit d5585ac

File tree

6 files changed

+337
-64
lines changed

6 files changed

+337
-64
lines changed

src/Neo4jQueryAPI.php

Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
use GuzzleHttp\Client;
77
use GuzzleHttp\Exception\GuzzleException;
88
use GuzzleHttp\Exception\RequestException;
9+
use Neo4j\QueryAPI\Objects\QueryArguments;
910
use Neo4j\QueryAPI\Objects\ResultCounters;
11+
use Neo4j\QueryAPI\Objects\ProfiledQueryPlan;
1012
use Neo4j\QueryAPI\Results\ResultRow;
1113
use Neo4j\QueryAPI\Results\ResultSet;
1214
use Neo4j\QueryAPI\Exception\Neo4jException;
@@ -25,14 +27,13 @@ public function __construct(Client $client)
2527

2628
public static function login(string $address, string $username, string $password): self
2729
{
28-
2930
$client = new Client([
3031
'base_uri' => rtrim($address, '/'),
3132
'timeout' => 10.0,
3233
'headers' => [
3334
'Authorization' => 'Basic ' . base64_encode("$username:$password"),
3435
'Content-Type' => 'application/vnd.neo4j.query',
35-
'Accept'=>'application/vnd.neo4j.query',
36+
'Accept' => 'application/vnd.neo4j.query',
3637
],
3738
]);
3839

@@ -62,6 +63,7 @@ public function run(string $cypher, array $parameters = [], string $database = '
6263
$data = json_decode($response->getBody()->getContents(), true);
6364
$ogm = new OGM();
6465

66+
// Extract result rows
6567
$keys = $data['data']['fields'];
6668
$values = $data['data']['values'];
6769
$rows = array_map(function ($resultRow) use ($ogm, $keys) {
@@ -73,22 +75,64 @@ public function run(string $cypher, array $parameters = [], string $database = '
7375
return new ResultRow($data);
7476
}, $values);
7577

76-
return new ResultSet($rows, new ResultCounters(
77-
containsUpdates: $data['counters']['containsUpdates'],
78-
nodesCreated: $data['counters']['nodesCreated'],
79-
nodesDeleted: $data['counters']['nodesDeleted'],
80-
propertiesSet: $data['counters']['propertiesSet'],
81-
relationshipsCreated: $data['counters']['relationshipsCreated'],
82-
relationshipsDeleted: $data['counters']['relationshipsDeleted'],
83-
labelsAdded: $data['counters']['labelsAdded'],
84-
labelsRemoved: $data['counters']['labelsRemoved'],
85-
indexesAdded: $data['counters']['indexesAdded'],
86-
indexesRemoved: $data['counters']['indexesRemoved'],
87-
constraintsAdded: $data['counters']['constraintsAdded'],
88-
constraintsRemoved: $data['counters']['constraintsRemoved'],
89-
containsSystemUpdates: $data['counters']['containsSystemUpdates'],
90-
systemUpdates: $data['counters']['systemUpdates']
91-
));
78+
// Extract profile data, if available
79+
$profiledQueryPlan = null;
80+
if (isset($data['profiledQueryPlan'])) {
81+
$profiledQueryPlan = new ProfiledQueryPlan(
82+
$data['profile']['dbHits'],
83+
$data['profile']['records'],
84+
$data['profile']['hasPageCacheStats'],
85+
$data['profile']['pageCacheHits'],
86+
$data['profile']['pageCacheMisses'],
87+
$data['profile']['pageCacheHitRatio'],
88+
$data['profile']['time'],
89+
$data['profile']['operatorType'],
90+
$data['profile']['arguments']
91+
);
92+
}
93+
$queryArguments = null;
94+
if (isset($data['profiledQueryPlan']['arguments'])) {
95+
$queryArguments = new QueryArguments(
96+
$data['profile']['globalMemory'] ?? 0,
97+
$data['profile']['plannerImpl'] ?? '',
98+
$data['profile']['memory'] ?? 0,
99+
$data['profile']['stringRepresentation'] ?? '',
100+
$data['profile']['runtime'] ?? '',
101+
$data['profile']['runtimeImpl'] ?? '',
102+
$data['profile']['dbHits'] ?? 0,
103+
$data['profile']['batchSize'] ?? 0,
104+
$data['profile']['details'] ?? '',
105+
$data['profile']['plannerVersion'] ?? '',
106+
$data['profile']['pipelineInfo'] ?? '',
107+
$data['profile']['runtimeVersion'] ?? '',
108+
$data['profile']['id'] ?? 0,
109+
$data['profile']['estimatedRows'] ?? 0.0,
110+
$data['profile']['planner'] ?? '',
111+
$data['profile']['rows'] ?? 0
112+
);
113+
}
114+
115+
// Return a ResultSet containing rows, counters, and the profiled query plan
116+
return new ResultSet(
117+
$rows,
118+
new ResultCounters(
119+
containsUpdates: $data['counters']['containsUpdates'],
120+
nodesCreated: $data['counters']['nodesCreated'],
121+
nodesDeleted: $data['counters']['nodesDeleted'],
122+
propertiesSet: $data['counters']['propertiesSet'],
123+
relationshipsCreated: $data['counters']['relationshipsCreated'],
124+
relationshipsDeleted: $data['counters']['relationshipsDeleted'],
125+
labelsAdded: $data['counters']['labelsAdded'],
126+
labelsRemoved: $data['counters']['labelsRemoved'],
127+
indexesAdded: $data['counters']['indexesAdded'],
128+
indexesRemoved: $data['counters']['indexesRemoved'],
129+
constraintsAdded: $data['counters']['constraintsAdded'],
130+
constraintsRemoved: $data['counters']['constraintsRemoved'],
131+
containsSystemUpdates: $data['counters']['containsSystemUpdates'],
132+
systemUpdates: $data['counters']['systemUpdates']
133+
),
134+
$profiledQueryPlan // Pass the profiled query plan here
135+
);
92136
} catch (RequestExceptionInterface $e) {
93137
$response = $e->getResponse();
94138
if ($response !== null) {
@@ -110,8 +154,6 @@ public function beginTransaction(string $database = 'neo4j'): Transaction
110154
$responseData = json_decode($response->getBody(), true);
111155
$transactionId = $responseData['transaction']['id'];
112156

113-
114-
115157
return new Transaction($this->client, $clusterAffinity, $transactionId);
116158
}
117159
}

src/Objects/ProfiledQueryPlan.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace Neo4j\QueryAPI\Objects;
4+
5+
class ProfiledQueryPlan
6+
{
7+
private int $dbHits;
8+
private int $records;
9+
private bool $hasPageCacheStats;
10+
private int $pageCacheHits;
11+
private int $pageCacheMisses;
12+
private float $pageCacheHitRatio;
13+
private int $time;
14+
private string $operatorType;
15+
private array $arguments;
16+
17+
public function __construct(
18+
?int $dbHits = 0, // Default to 0 if null
19+
?int $records = 0,
20+
?bool $hasPageCacheStats = false,
21+
?int $pageCacheHits = 0,
22+
?int $pageCacheMisses = 0,
23+
?float $pageCacheHitRatio = 0.0,
24+
?int $time = 0,
25+
?string $operatorType = '',
26+
?array $arguments = []
27+
) {
28+
$this->dbHits = $dbHits ?? 0;
29+
$this->records = $records ?? 0;
30+
$this->hasPageCacheStats = $hasPageCacheStats ?? false;
31+
$this->pageCacheHits = $pageCacheHits ?? 0;
32+
$this->pageCacheMisses = $pageCacheMisses ?? 0;
33+
$this->pageCacheHitRatio = $pageCacheHitRatio ?? 0.0;
34+
$this->time = $time ?? 0;
35+
$this->operatorType = $operatorType ?? '';
36+
$this->arguments = $arguments ?? [];
37+
}
38+
39+
public function getDbHits(): int
40+
{
41+
return $this->dbHits;
42+
}
43+
44+
public function getRecords(): int
45+
{
46+
return $this->records;
47+
}
48+
49+
public function hasPageCacheStats(): bool
50+
{
51+
return $this->hasPageCacheStats;
52+
}
53+
54+
public function getPageCacheHits(): int
55+
{
56+
return $this->pageCacheHits;
57+
}
58+
59+
public function getPageCacheMisses(): int
60+
{
61+
return $this->pageCacheMisses;
62+
}
63+
64+
public function getPageCacheHitRatio(): float
65+
{
66+
return $this->pageCacheHitRatio;
67+
}
68+
69+
public function getTime(): int
70+
{
71+
return $this->time;
72+
}
73+
74+
public function getOperatorType(): string
75+
{
76+
return $this->operatorType;
77+
}
78+
79+
public function getArguments(): array
80+
{
81+
return $this->arguments;
82+
}
83+
}

src/Objects/QueryArguments.php

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php
2+
3+
namespace Neo4j\QueryAPI\Objects;
4+
5+
class QueryArguments
6+
{
7+
private int $globalMemory;
8+
private string $plannerImpl;
9+
private int $memory;
10+
private string $stringRepresentation;
11+
private string $runtime;
12+
private string $runtimeImpl;
13+
private int $dbHits;
14+
private int $batchSize;
15+
private string $details;
16+
private string $plannerVersion;
17+
private string $pipelineInfo;
18+
private string $runtimeVersion;
19+
private int $id;
20+
private float $estimatedRows;
21+
private string $planner;
22+
private int $rows;
23+
24+
public function __construct(
25+
?int $globalMemory = 0,
26+
?string $plannerImpl = '',
27+
?int $memory = 0,
28+
?string $stringRepresentation = '',
29+
?string $runtime = '',
30+
?string $runtimeImpl = '',
31+
?int $dbHits = 0,
32+
?int $batchSize = 0,
33+
?string $details = '',
34+
?string $plannerVersion = '',
35+
?string $pipelineInfo = '',
36+
?string $runtimeVersion = '',
37+
?int $id = 0,
38+
?float $estimatedRows = 0.0,
39+
?string $planner = '',
40+
?int $rows = 0
41+
) {
42+
$this->globalMemory = $globalMemory ?? 0;
43+
$this->plannerImpl = $plannerImpl ?? '';
44+
$this->memory = $memory ?? 0;
45+
$this->stringRepresentation = $stringRepresentation ?? '';
46+
$this->runtime = $runtime ?? '';
47+
$this->runtimeImpl = $runtimeImpl ?? '';
48+
$this->dbHits = $dbHits ?? 0;
49+
$this->batchSize = $batchSize ?? 0;
50+
$this->details = $details ?? '';
51+
$this->plannerVersion = $plannerVersion ?? '';
52+
$this->pipelineInfo = $pipelineInfo ?? '';
53+
$this->runtimeVersion = $runtimeVersion ?? '';
54+
$this->id = $id ?? 0;
55+
$this->estimatedRows = $estimatedRows ?? 0.0;
56+
$this->planner = $planner ?? '';
57+
$this->rows = $rows ?? 0;
58+
}
59+
60+
public function getGlobalMemory(): int
61+
{
62+
return $this->globalMemory;
63+
}
64+
65+
public function getPlannerImpl(): string
66+
{
67+
return $this->plannerImpl;
68+
}
69+
70+
public function getMemory(): int
71+
{
72+
return $this->memory;
73+
}
74+
75+
public function getStringRepresentation(): string
76+
{
77+
return $this->stringRepresentation;
78+
}
79+
80+
public function getRuntime(): string
81+
{
82+
return $this->runtime;
83+
}
84+
85+
public function getRuntimeImpl(): string
86+
{
87+
return $this->runtimeImpl;
88+
}
89+
90+
public function getDbHits(): int
91+
{
92+
return $this->dbHits;
93+
}
94+
95+
public function getBatchSize(): int
96+
{
97+
return $this->batchSize;
98+
}
99+
100+
public function getDetails(): string
101+
{
102+
return $this->details;
103+
}
104+
105+
public function getPlannerVersion(): string
106+
{
107+
return $this->plannerVersion;
108+
}
109+
110+
public function getPipelineInfo(): string
111+
{
112+
return $this->pipelineInfo;
113+
}
114+
115+
public function getRuntimeVersion(): string
116+
{
117+
return $this->runtimeVersion;
118+
}
119+
120+
public function getId(): int
121+
{
122+
return $this->id;
123+
}
124+
125+
public function getEstimatedRows(): float
126+
{
127+
return $this->estimatedRows;
128+
}
129+
130+
public function getPlanner(): string
131+
{
132+
return $this->planner;
133+
}
134+
135+
public function getRows(): int
136+
{
137+
return $this->rows;
138+
}
139+
}

0 commit comments

Comments
 (0)