Skip to content

Commit cfff31f

Browse files
author
Zhen Li
committed
Added database name in the ResultSummary.
1 parent 1d00a53 commit cfff31f

File tree

7 files changed

+143
-11
lines changed

7 files changed

+143
-11
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (c) 2002-2019 "Neo4j,"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.neo4j.driver.internal.summary;
20+
21+
import org.neo4j.driver.summary.DatabaseInfo;
22+
23+
import static org.neo4j.driver.internal.messaging.request.MultiDatabaseUtil.ABSENT_DB_NAME;
24+
25+
public class InternalDatabaseInfo implements DatabaseInfo
26+
{
27+
public static DatabaseInfo DEFAULT_DATABASE_INFO = new InternalDatabaseInfo( ABSENT_DB_NAME );
28+
29+
private final String name;
30+
31+
public InternalDatabaseInfo( String name )
32+
{
33+
this.name = name;
34+
}
35+
36+
@Override
37+
public String name()
38+
{
39+
return this.name;
40+
}
41+
}

driver/src/main/java/org/neo4j/driver/internal/summary/InternalResultSummary.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.concurrent.TimeUnit;
2525

2626
import org.neo4j.driver.Statement;
27+
import org.neo4j.driver.summary.DatabaseInfo;
2728
import org.neo4j.driver.summary.Notification;
2829
import org.neo4j.driver.summary.Plan;
2930
import org.neo4j.driver.summary.ProfiledPlan;
@@ -43,13 +44,14 @@ public class InternalResultSummary implements ResultSummary
4344
private final List<Notification> notifications;
4445
private final long resultAvailableAfter;
4546
private final long resultConsumedAfter;
47+
private final DatabaseInfo databaseInfo;
4648

47-
public InternalResultSummary( Statement statement, ServerInfo serverInfo, StatementType statementType,
48-
SummaryCounters counters, Plan plan, ProfiledPlan profile, List<Notification> notifications,
49-
long resultAvailableAfter, long resultConsumedAfter )
49+
public InternalResultSummary( Statement statement, ServerInfo serverInfo, DatabaseInfo databaseInfo, StatementType statementType,
50+
SummaryCounters counters, Plan plan, ProfiledPlan profile, List<Notification> notifications, long resultAvailableAfter, long resultConsumedAfter )
5051
{
5152
this.statement = statement;
5253
this.serverInfo = serverInfo;
54+
this.databaseInfo = databaseInfo;
5355
this.statementType = statementType;
5456
this.counters = counters;
5557
this.plan = resolvePlan( plan, profile );
@@ -127,6 +129,12 @@ public ServerInfo server()
127129
return serverInfo;
128130
}
129131

132+
@Override
133+
public DatabaseInfo database()
134+
{
135+
return databaseInfo;
136+
}
137+
130138
@Override
131139
public boolean equals( Object o )
132140
{

driver/src/main/java/org/neo4j/driver/internal/util/MetadataExtractor.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import org.neo4j.driver.internal.Bookmarks;
2727
import org.neo4j.driver.internal.spi.Connection;
28+
import org.neo4j.driver.internal.summary.InternalDatabaseInfo;
2829
import org.neo4j.driver.internal.summary.InternalNotification;
2930
import org.neo4j.driver.internal.summary.InternalPlan;
3031
import org.neo4j.driver.internal.summary.InternalProfiledPlan;
@@ -34,6 +35,7 @@
3435
import org.neo4j.driver.Statement;
3536
import org.neo4j.driver.Value;
3637
import org.neo4j.driver.exceptions.UntrustedServerException;
38+
import org.neo4j.driver.summary.DatabaseInfo;
3739
import org.neo4j.driver.summary.Notification;
3840
import org.neo4j.driver.summary.Plan;
3941
import org.neo4j.driver.summary.ProfiledPlan;
@@ -42,6 +44,7 @@
4244
import org.neo4j.driver.summary.StatementType;
4345

4446
import static java.util.Collections.emptyList;
47+
import static org.neo4j.driver.internal.summary.InternalDatabaseInfo.DEFAULT_DATABASE_INFO;
4548
import static org.neo4j.driver.internal.types.InternalTypeSystem.TYPE_SYSTEM;
4649

4750
public class MetadataExtractor
@@ -99,12 +102,26 @@ public long extractResultAvailableAfter( Map<String,Value> metadata )
99102
public ResultSummary extractSummary( Statement statement, Connection connection, long resultAvailableAfter, Map<String,Value> metadata )
100103
{
101104
ServerInfo serverInfo = new InternalServerInfo( connection.serverAddress(), connection.serverVersion() );
102-
return new InternalResultSummary( statement, serverInfo, extractStatementType( metadata ),
103-
extractCounters( metadata ), extractPlan( metadata ), extractProfiledPlan( metadata ),
104-
extractNotifications( metadata ), resultAvailableAfter, extractResultConsumedAfter( metadata, resultConsumedAfterMetadataKey ) );
105+
DatabaseInfo dbInfo = extractDatabaseInfo( metadata );
106+
return new InternalResultSummary( statement, serverInfo, dbInfo, extractStatementType( metadata ), extractCounters( metadata ), extractPlan( metadata ),
107+
extractProfiledPlan( metadata ), extractNotifications( metadata ), resultAvailableAfter,
108+
extractResultConsumedAfter( metadata, resultConsumedAfterMetadataKey ) );
105109
}
106110

107-
public Bookmarks extractBookmarks( Map<String,Value> metadata )
111+
public static DatabaseInfo extractDatabaseInfo( Map<String,Value> metadata )
112+
{
113+
Value dbValue = metadata.get( "db" );
114+
if ( dbValue == null || dbValue.isNull() || !dbValue.hasType( TYPE_SYSTEM.STRING() ) )
115+
{
116+
return DEFAULT_DATABASE_INFO;
117+
}
118+
else
119+
{
120+
return new InternalDatabaseInfo( dbValue.asString() );
121+
}
122+
}
123+
124+
public static Bookmarks extractBookmarks( Map<String,Value> metadata )
108125
{
109126
Value bookmarkValue = metadata.get( "bookmark" );
110127
if ( bookmarkValue != null && !bookmarkValue.isNull() && bookmarkValue.hasType( TYPE_SYSTEM.STRING() ) )
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) 2002-2019 "Neo4j,"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.neo4j.driver.summary;
20+
21+
/**
22+
* Provides basic information about where a {@link ResultSummary} is obtained from.
23+
*/
24+
public interface DatabaseInfo
25+
{
26+
/**
27+
* The name of the database where a {@link ResultSummary} is obtained from.
28+
* @return the name of the database where a {@link ResultSummary} is obtained from
29+
*/
30+
String name();
31+
}

driver/src/main/java/org/neo4j/driver/summary/ResultSummary.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,13 @@ public interface ResultSummary
110110

111111
/**
112112
* The basic information of the server where the result is obtained from
113-
* @return basic information of the server where the result is obtain from
113+
* @return basic information of the server where the result is obtained from
114114
*/
115115
ServerInfo server();
116+
117+
/**
118+
* The basic information of the database where the result is obtained from
119+
* @return the basic information of the database where the result is obtained from
120+
*/
121+
DatabaseInfo database();
116122
}

driver/src/test/java/org/neo4j/driver/internal/async/AsyncStatementResultCursorTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import static org.mockito.Mockito.when;
6060
import static org.neo4j.driver.Values.value;
6161
import static org.neo4j.driver.Values.values;
62+
import static org.neo4j.driver.internal.summary.InternalDatabaseInfo.DEFAULT_DATABASE_INFO;
6263
import static org.neo4j.driver.internal.util.Futures.completedWithNull;
6364
import static org.neo4j.driver.internal.util.Futures.failedFuture;
6465
import static org.neo4j.driver.util.TestUtil.await;
@@ -85,9 +86,8 @@ void shouldReturnSummary()
8586
PullAllResponseHandler pullAllHandler = mock( PullAllResponseHandler.class );
8687

8788
ResultSummary summary = new InternalResultSummary( new Statement( "RETURN 42" ),
88-
new InternalServerInfo( BoltServerAddress.LOCAL_DEFAULT, ServerVersion.v3_1_0 ),
89-
StatementType.SCHEMA_WRITE, new InternalSummaryCounters( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ),
90-
null, null, emptyList(), 42, 42 );
89+
new InternalServerInfo( BoltServerAddress.LOCAL_DEFAULT, ServerVersion.v3_1_0 ), DEFAULT_DATABASE_INFO, StatementType.SCHEMA_WRITE,
90+
new InternalSummaryCounters( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ), null, null, emptyList(), 42, 42 );
9191
when( pullAllHandler.summaryAsync() ).thenReturn( completedFuture( summary ) );
9292

9393
AsyncStatementResultCursor cursor = newCursor( pullAllHandler );

driver/src/test/java/org/neo4j/driver/internal/util/MetadataExtractorTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.neo4j.driver.Value;
3333
import org.neo4j.driver.Values;
3434
import org.neo4j.driver.exceptions.UntrustedServerException;
35+
import org.neo4j.driver.summary.DatabaseInfo;
3536
import org.neo4j.driver.summary.Notification;
3637
import org.neo4j.driver.summary.Plan;
3738
import org.neo4j.driver.summary.ProfiledPlan;
@@ -49,6 +50,7 @@
4950
import static org.mockito.Mockito.mock;
5051
import static org.mockito.Mockito.when;
5152
import static org.neo4j.driver.internal.summary.InternalSummaryCounters.EMPTY_STATS;
53+
import static org.neo4j.driver.internal.util.MetadataExtractor.extractDatabaseInfo;
5254
import static org.neo4j.driver.internal.util.MetadataExtractor.extractNeo4jServerVersion;
5355
import static org.neo4j.driver.Values.parameters;
5456
import static org.neo4j.driver.Values.value;
@@ -390,6 +392,33 @@ void shouldExtractServerVersion()
390392
assertEquals( ServerVersion.v3_5_0, version );
391393
}
392394

395+
396+
@Test
397+
void shouldExtractDatabase()
398+
{
399+
// Given
400+
Map<String,Value> metadata = singletonMap( "db", value( "MyAwesomeDatabase" ) );
401+
402+
// When
403+
DatabaseInfo db = extractDatabaseInfo( metadata );
404+
405+
// Then
406+
assertEquals( "MyAwesomeDatabase", db.name() );
407+
}
408+
409+
@Test
410+
void shouldDefaultToEmptyDatabaseName()
411+
{
412+
// Given
413+
Map<String,Value> metadata = singletonMap( "no_db", value( "no_db" ) );
414+
415+
// When
416+
DatabaseInfo db = extractDatabaseInfo( metadata );
417+
418+
// Then
419+
assertEquals( "", db.name() );
420+
}
421+
393422
@Test
394423
void shouldFailToExtractServerVersionWhenMetadataDoesNotContainIt()
395424
{

0 commit comments

Comments
 (0)