2727use Phpfastcache \Exceptions \PhpfastcacheInvalidArgumentException ;
2828use Phpfastcache \Exceptions \PhpfastcacheUnsupportedMethodException ;
2929use RavenDB \Documents \DocumentStore ;
30+ use RavenDB \Documents \Operations \CollectionStatistics ;
3031use RavenDB \Documents \Operations \DeleteByQueryOperation ;
32+ use RavenDB \Documents \Operations \DetailedDatabaseStatistics ;
33+ use RavenDB \Documents \Operations \GetCollectionStatisticsOperation ;
34+ use RavenDB \Documents \Operations \GetDetailedStatisticsOperation ;
3135use RavenDB \Documents \Queries \IndexQuery ;
3236use RavenDB \Documents \Session \DocumentSession ;
33- use RavenDB \Documents \Session \QueryStatistics ;
3437use RavenDB \Exceptions \RavenException ;
3538use RavenDB \Http \ServerNode ;
3639use RavenDB \ServerWide \Operations \BuildNumber ;
40+ use RavenDB \ServerWide \Operations \Configuration \GetDatabaseSettingsOperation ;
3741use RavenDB \ServerWide \Operations \GetBuildNumberOperation ;
42+ use RavenDB \Type \Duration ;
3843
3944/**
4045 * Class Driver
4146 * @property DocumentSession $instance Instance of driver service
4247 * @method Config getConfig()
48+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
4349 */
4450class Driver implements AggregatablePoolInterface
4551{
@@ -85,10 +91,15 @@ protected function driverConnect(): bool
8591 if ($ authOptions ) {
8692 $ this ->documentStorage ->setAuthOptions ($ authOptions );
8793 }
88- $ this ->documentStorage ->getConventions ()->setFindCollectionName (fn () => $ this ->getConfig ()-> getCollectionName ());
94+ $ this ->documentStorage ->getConventions ()->setFindCollectionName (fn () => $ this ->getCollectionName ());
8995 $ this ->documentStorage ->getConventions ()->setFindIdentityProperty (static fn () => 'key ' );
96+ $ this ->documentStorage ->getConventions ()->setRequestTimeout (Duration::ofSeconds (1 ));
9097 $ this ->documentStorage ->initialize ();
98+ $ this ->documentStorage ->getRequestExecutor ()->setDefaultTimeout (Duration::ofSeconds (1 ));
9199 $ 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+ }
92103 } catch (RavenException $ e ) {
93104 throw new PhpfastcacheDriverConnectException ('Unable to connect to Raven server: ' . $ e ->getMessage ());
94105 }
@@ -234,7 +245,7 @@ protected function driverDeleteMultiple(array $keys): bool
234245 protected function driverClear (): bool
235246 {
236247 $ 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 ())))
238249 );
239250
240251 $ this ->instance ->clear ();
@@ -249,12 +260,16 @@ public function getStats(): DriverStatistic
249260 $ nodes = $ this ->instance ->getRequestExecutor ()->getTopology ()->getNodes ();
250261 /** @var BuildNumber|null $buildNumber */
251262 $ 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 ());
252267
253- return (new DriverStatistic ())
254- ->setRawData ([ ' build ' => $ buildNumber , 'nodes ' => $ nodes ] )
268+ $ driverStats = (new DriverStatistic ())
269+ ->setRawData (compact ( ' nodes ' , ' buildNumber ' , 'collectionStats ' , ' databaseStats ' ) )
255270 ->setInfo (
256271 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". ' ,
258273 $ buildNumber ?->getFullVersion() ?? 'Unknown version ' ,
259274 $ buildNumber ?->getCommitHash() ?? '******** ' ,
260275 InstalledVersions::getPrettyVersion ('ravendb/ravendb-php-client ' ),
@@ -263,9 +278,19 @@ public function getStats(): DriverStatistic
263278 implode (', ' , array_map (
264279 fn (ServerNode $ node ) => 'Node # ' . $ node ->getClusterTag () . ' ( ' . $ node ->getServerRole ()->getValue () . ') @ ' . $ node ->getUrl ()->getValue (),
265280 iterator_to_array ($ nodes )
266- ))
281+ )),
282+ $ this ->getDatabaseName (),
283+ $ this ->getCollectionName (),
267284 )
268285 );
286+
287+ if (method_exists ($ driverStats , 'setCount ' )) {
288+ $ driverStats ->setCount (
289+ $ collectionStats ->getCollections ()[$ this ->getCollectionName ()] ?? $ collectionStats ->getCountOfDocuments ()
290+ );
291+ }
292+
293+ return $ driverStats ;
269294 }
270295
271296 /**
@@ -275,4 +300,12 @@ protected function getDatabaseName(): string
275300 {
276301 return $ this ->getConfig ()->getDatabaseName () ?: static ::RAVENDB_DEFAULT_DB_NAME ;
277302 }
303+
304+ /**
305+ * @return string
306+ */
307+ protected function getCollectionName (): string
308+ {
309+ return $ this ->getConfig ()->getCollectionName () ?: static ::RAVENDB_DEFAULT_COLLECTION_NAME ;
310+ }
278311}
0 commit comments