27
27
use Phpfastcache \Exceptions \PhpfastcacheInvalidArgumentException ;
28
28
use Phpfastcache \Exceptions \PhpfastcacheUnsupportedMethodException ;
29
29
use RavenDB \Documents \DocumentStore ;
30
+ use RavenDB \Documents \Operations \CollectionStatistics ;
30
31
use RavenDB \Documents \Operations \DeleteByQueryOperation ;
32
+ use RavenDB \Documents \Operations \DetailedDatabaseStatistics ;
33
+ use RavenDB \Documents \Operations \GetCollectionStatisticsOperation ;
34
+ use RavenDB \Documents \Operations \GetDetailedStatisticsOperation ;
31
35
use RavenDB \Documents \Queries \IndexQuery ;
32
36
use RavenDB \Documents \Session \DocumentSession ;
33
- use RavenDB \Documents \Session \QueryStatistics ;
34
37
use RavenDB \Exceptions \RavenException ;
35
38
use RavenDB \Http \ServerNode ;
36
39
use RavenDB \ServerWide \Operations \BuildNumber ;
40
+ use RavenDB \ServerWide \Operations \Configuration \GetDatabaseSettingsOperation ;
37
41
use RavenDB \ServerWide \Operations \GetBuildNumberOperation ;
42
+ use RavenDB \Type \Duration ;
38
43
39
44
/**
40
45
* Class Driver
41
46
* @property DocumentSession $instance Instance of driver service
42
47
* @method Config getConfig()
48
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
43
49
*/
44
50
class Driver implements AggregatablePoolInterface
45
51
{
@@ -85,10 +91,15 @@ protected function driverConnect(): bool
85
91
if ($ authOptions ) {
86
92
$ this ->documentStorage ->setAuthOptions ($ authOptions );
87
93
}
88
- $ this ->documentStorage ->getConventions ()->setFindCollectionName (fn () => $ this ->getConfig ()-> getCollectionName ());
94
+ $ this ->documentStorage ->getConventions ()->setFindCollectionName (fn () => $ this ->getCollectionName ());
89
95
$ this ->documentStorage ->getConventions ()->setFindIdentityProperty (static fn () => 'key ' );
96
+ $ this ->documentStorage ->getConventions ()->setRequestTimeout (Duration::ofSeconds (1 ));
90
97
$ this ->documentStorage ->initialize ();
98
+ $ this ->documentStorage ->getRequestExecutor ()->setDefaultTimeout (Duration::ofSeconds (1 ));
91
99
$ this ->instance = $ this ->documentStorage ->openSession ();// @phpstan-ignore-line
100
+ if ($ this ->documentStorage ->maintenance ()->send (new GetDatabaseSettingsOperation ($ this ->getDatabaseName ())) === null ) {
101
+ throw new RavenException ('Unable to fetch databases metadata. ' );
102
+ }
92
103
} catch (RavenException $ e ) {
93
104
throw new PhpfastcacheDriverConnectException ('Unable to connect to Raven server: ' . $ e ->getMessage ());
94
105
}
@@ -234,7 +245,7 @@ protected function driverDeleteMultiple(array $keys): bool
234
245
protected function driverClear (): bool
235
246
{
236
247
$ this ->documentStorage ->operations ()->send (
237
- new DeleteByQueryOperation (new IndexQuery (sprintf ('from %s ' , $ this ->getConfig ()-> getCollectionName ())))
248
+ new DeleteByQueryOperation (new IndexQuery (sprintf ('from %s ' , $ this ->getCollectionName ())))
238
249
);
239
250
240
251
$ this ->instance ->clear ();
@@ -249,12 +260,16 @@ public function getStats(): DriverStatistic
249
260
$ nodes = $ this ->instance ->getRequestExecutor ()->getTopology ()->getNodes ();
250
261
/** @var BuildNumber|null $buildNumber */
251
262
$ buildNumber = $ this ->documentStorage ->maintenance ()->server ()->send (new GetBuildNumberOperation ());
263
+ /** @var CollectionStatistics $collectionStats */
264
+ $ collectionStats = $ this ->documentStorage ->maintenance ()->send (new GetCollectionStatisticsOperation ());
265
+ /** @var DetailedDatabaseStatistics $databaseStats */
266
+ $ databaseStats = $ this ->documentStorage ->maintenance ()->send (new GetDetailedStatisticsOperation ());
252
267
253
- return (new DriverStatistic ())
254
- ->setRawData ([ ' build ' => $ buildNumber , 'nodes ' => $ nodes ] )
268
+ $ driverStats = (new DriverStatistic ())
269
+ ->setRawData (compact ( ' nodes ' , ' buildNumber ' , 'collectionStats ' , ' databaseStats ' ) )
255
270
->setInfo (
256
271
sprintf (
257
- 'Ravendb server v%s (%s), client v%s with %s node%s configured: %s ' ,
272
+ 'Ravendb server v%s (%s), client v%s with %s node%s configured: %s. Database/Collection: "%s"/"%s". ' ,
258
273
$ buildNumber ?->getFullVersion() ?? 'Unknown version ' ,
259
274
$ buildNumber ?->getCommitHash() ?? '******** ' ,
260
275
InstalledVersions::getPrettyVersion ('ravendb/ravendb-php-client ' ),
@@ -263,9 +278,19 @@ public function getStats(): DriverStatistic
263
278
implode (', ' , array_map (
264
279
fn (ServerNode $ node ) => 'Node # ' . $ node ->getClusterTag () . ' ( ' . $ node ->getServerRole ()->getValue () . ') @ ' . $ node ->getUrl ()->getValue (),
265
280
iterator_to_array ($ nodes )
266
- ))
281
+ )),
282
+ $ this ->getDatabaseName (),
283
+ $ this ->getCollectionName (),
267
284
)
268
285
);
286
+
287
+ if (method_exists ($ driverStats , 'setCount ' )) {
288
+ $ driverStats ->setCount (
289
+ $ collectionStats ->getCollections ()[$ this ->getCollectionName ()] ?? $ collectionStats ->getCountOfDocuments ()
290
+ );
291
+ }
292
+
293
+ return $ driverStats ;
269
294
}
270
295
271
296
/**
@@ -275,4 +300,12 @@ protected function getDatabaseName(): string
275
300
{
276
301
return $ this ->getConfig ()->getDatabaseName () ?: static ::RAVENDB_DEFAULT_DB_NAME ;
277
302
}
303
+
304
+ /**
305
+ * @return string
306
+ */
307
+ protected function getCollectionName (): string
308
+ {
309
+ return $ this ->getConfig ()->getCollectionName () ?: static ::RAVENDB_DEFAULT_COLLECTION_NAME ;
310
+ }
278
311
}
0 commit comments