Skip to content

Commit d5a4554

Browse files
authored
Merge pull request #591 from zhenlineo/2.0-database-name-in-summary
Added database name in the ResultSummary.
2 parents 1691d6e + 80203b3 commit d5a4554

File tree

12 files changed

+260
-34
lines changed

12 files changed

+260
-34
lines changed

driver/src/main/java/org/neo4j/driver/SessionParametersTemplate.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ public interface SessionParametersTemplate
5858

5959
/**
6060
* Set the database that the newly created session is going to connect to.
61-
* The given database name cannot be <code>null</code>.
62-
* If the database name is not set, then the default database configured on the server configuration will be connected when the session established.
61+
* If the database name is not set or the database name is set to <code>null</code>,
62+
* then the default database configured in the server configuration will be connected when the session is established.
63+
* For servers that do not support multi-databases, this database value should not be set or could only be set to <code>null</code>.
6364
*
6465
* @param database the database the session going to connect to.
6566
* @return this builder.

driver/src/main/java/org/neo4j/driver/internal/SessionFactoryImpl.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import org.neo4j.driver.internal.retry.RetryLogic;
2929
import org.neo4j.driver.internal.spi.ConnectionProvider;
3030

31+
import static org.neo4j.driver.internal.messaging.request.MultiDatabaseUtil.ABSENT_DB_NAME;
32+
3133
public class SessionFactoryImpl implements SessionFactory
3234
{
3335
private final ConnectionProvider connectionProvider;
@@ -47,7 +49,7 @@ public class SessionFactoryImpl implements SessionFactory
4749
public NetworkSession newInstance( SessionParameters parameters )
4850
{
4951
BookmarksHolder bookmarksHolder = new DefaultBookmarksHolder( Bookmarks.from( parameters.bookmarks() ) );
50-
return createSession( connectionProvider, retryLogic, parameters.database(), parameters.defaultAccessMode(), bookmarksHolder, logging );
52+
return createSession( connectionProvider, retryLogic, parameters.database().orElse( ABSENT_DB_NAME ), parameters.defaultAccessMode(), bookmarksHolder, logging );
5153
}
5254

5355
@Override

driver/src/main/java/org/neo4j/driver/internal/SessionParameters.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Arrays;
2222
import java.util.List;
2323
import java.util.Objects;
24+
import java.util.Optional;
2425

2526
import org.neo4j.driver.AccessMode;
2627
import org.neo4j.driver.SessionParametersTemplate;
@@ -87,11 +88,11 @@ public AccessMode defaultAccessMode()
8788

8889
/**
8990
* The database where the session is going to connect to.
90-
* @return the database name where the session is going to connect to.
91+
* @return the nullable database name where the session is going to connect to.
9192
*/
92-
public String database()
93+
public Optional<String> database()
9394
{
94-
return database;
95+
return Optional.ofNullable( database );
9596
}
9697

9798
@Override
@@ -125,7 +126,7 @@ public static class Template implements SessionParametersTemplate
125126
{
126127
private List<String> bookmarks = null;
127128
private AccessMode defaultAccessMode = AccessMode.WRITE;
128-
private String database = ABSENT_DB_NAME;
129+
private String database = null;
129130

130131
private Template()
131132
{
@@ -162,7 +163,11 @@ public Template withDefaultAccessMode( AccessMode mode )
162163
@Override
163164
public Template withDatabase( String database )
164165
{
165-
Objects.requireNonNull( database, "Database cannot be null." );
166+
if ( ABSENT_DB_NAME.equals( database ) )
167+
{
168+
// Disallow users to use bolt internal value directly. To users, this is totally an illegal database name.
169+
throw new IllegalArgumentException( String.format( "Illegal database name '%s'.", database ) );
170+
}
166171
this.database = database;
167172
return this;
168173
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 java.util.Objects;
22+
23+
import org.neo4j.driver.summary.DatabaseInfo;
24+
25+
public class InternalDatabaseInfo implements DatabaseInfo
26+
{
27+
public static DatabaseInfo DEFAULT_DATABASE_INFO = new InternalDatabaseInfo( null );
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+
42+
@Override
43+
public boolean equals( Object o )
44+
{
45+
if ( this == o )
46+
{
47+
return true;
48+
}
49+
if ( o == null || getClass() != o.getClass() )
50+
{
51+
return false;
52+
}
53+
InternalDatabaseInfo that = (InternalDatabaseInfo) o;
54+
return Objects.equals( name, that.name );
55+
}
56+
57+
@Override
58+
public int hashCode()
59+
{
60+
return Objects.hash( name );
61+
}
62+
63+
@Override
64+
public String toString()
65+
{
66+
return "InternalDatabaseInfo{" + "name='" + name + '\'' + '}';
67+
}
68+
}

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

Lines changed: 12 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
{
@@ -163,6 +171,7 @@ public String toString()
163171
return "InternalResultSummary{" +
164172
"statement=" + statement +
165173
", serverInfo=" + serverInfo +
174+
", databaseInfo=" + databaseInfo +
166175
", statementType=" + statementType +
167176
", counters=" + counters +
168177
", plan=" + plan +

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*/
1919
package org.neo4j.driver.internal.summary;
2020

21+
import java.util.Objects;
22+
2123
import org.neo4j.driver.internal.BoltServerAddress;
2224
import org.neo4j.driver.internal.util.ServerVersion;
2325
import org.neo4j.driver.summary.ServerInfo;
@@ -44,4 +46,31 @@ public String version()
4446
{
4547
return version;
4648
}
49+
50+
@Override
51+
public boolean equals( Object o )
52+
{
53+
if ( this == o )
54+
{
55+
return true;
56+
}
57+
if ( o == null || getClass() != o.getClass() )
58+
{
59+
return false;
60+
}
61+
InternalServerInfo that = (InternalServerInfo) o;
62+
return Objects.equals( address, that.address ) && Objects.equals( version, that.version );
63+
}
64+
65+
@Override
66+
public int hashCode()
67+
{
68+
return Objects.hash( address, version );
69+
}
70+
71+
@Override
72+
public String toString()
73+
{
74+
return "InternalServerInfo{" + "address='" + address + '\'' + ", version='" + version + '\'' + '}';
75+
}
4776
}

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,19 @@
2323
import java.util.List;
2424
import java.util.Map;
2525

26+
import org.neo4j.driver.Statement;
27+
import org.neo4j.driver.Value;
28+
import org.neo4j.driver.exceptions.UntrustedServerException;
2629
import org.neo4j.driver.internal.Bookmarks;
2730
import org.neo4j.driver.internal.spi.Connection;
31+
import org.neo4j.driver.internal.summary.InternalDatabaseInfo;
2832
import org.neo4j.driver.internal.summary.InternalNotification;
2933
import org.neo4j.driver.internal.summary.InternalPlan;
3034
import org.neo4j.driver.internal.summary.InternalProfiledPlan;
3135
import org.neo4j.driver.internal.summary.InternalResultSummary;
3236
import org.neo4j.driver.internal.summary.InternalServerInfo;
3337
import org.neo4j.driver.internal.summary.InternalSummaryCounters;
34-
import org.neo4j.driver.Statement;
35-
import org.neo4j.driver.Value;
36-
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 ) );
109+
}
110+
111+
public static DatabaseInfo extractDatabaseInfo( Map<String,Value> metadata )
112+
{
113+
Value dbValue = metadata.get( "db" );
114+
if ( dbValue == null || dbValue.isNull() )
115+
{
116+
return DEFAULT_DATABASE_INFO;
117+
}
118+
else
119+
{
120+
return new InternalDatabaseInfo( dbValue.asString() );
121+
}
105122
}
106123

107-
public Bookmarks extractBookmarks( Map<String,Value> metadata )
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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
* Default to {@code null} if servers does not support multi-databases.
29+
* @return the name of the database where a {@link ResultSummary} is obtained from
30+
*/
31+
String name();
32+
}

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/SessionParametersTest.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
import static org.junit.Assert.assertEquals;
3535
import static org.junit.Assert.assertNull;
3636
import static org.junit.Assert.assertThat;
37+
import static org.junit.jupiter.api.Assertions.assertFalse;
3738
import static org.junit.jupiter.api.Assertions.assertThrows;
39+
import static org.junit.jupiter.api.Assertions.assertTrue;
3840
import static org.neo4j.driver.internal.SessionParameters.empty;
3941
import static org.neo4j.driver.internal.SessionParameters.template;
4042
import static org.neo4j.driver.internal.messaging.request.MultiDatabaseUtil.ABSENT_DB_NAME;
@@ -47,7 +49,7 @@ void shouldReturnDefaultValues() throws Throwable
4749
SessionParameters parameters = empty();
4850

4951
Assert.assertEquals( AccessMode.WRITE, parameters.defaultAccessMode() );
50-
assertEquals( ABSENT_DB_NAME, parameters.database() );
52+
assertFalse( parameters.database().isPresent() );
5153
assertNull( parameters.bookmarks() );
5254
}
5355

@@ -60,18 +62,28 @@ void shouldChangeAccessMode( AccessMode mode ) throws Throwable
6062
}
6163

6264
@ParameterizedTest
63-
@ValueSource( strings = {"", "foo", "data", ABSENT_DB_NAME} )
65+
@ValueSource( strings = {"foo", "data", "my awesome database", " "} )
6466
void shouldChangeDatabaseName( String databaseName )
6567
{
6668
SessionParameters parameters = template().withDatabase( databaseName ).build();
67-
assertEquals( databaseName, parameters.database() );
69+
assertTrue( parameters.database().isPresent() );
70+
assertEquals( databaseName, parameters.database().get() );
6871
}
6972

7073
@Test
71-
void shouldForbiddenNullDatabaseName() throws Throwable
74+
void shouldAllowNullDatabaseName() throws Throwable
7275
{
73-
NullPointerException error = assertThrows( NullPointerException.class, () -> template().withDatabase( null ).build());
74-
assertThat( error.getMessage(), equalTo( "Database cannot be null." ) );
76+
SessionParameters parameters = template().withDatabase( null ).build();
77+
assertFalse( parameters.database().isPresent() );
78+
assertEquals( "", parameters.database().orElse( ABSENT_DB_NAME ) );
79+
}
80+
81+
@ParameterizedTest
82+
@ValueSource( strings = {"", ABSENT_DB_NAME} )
83+
void shouldForbiddenEmptyStringDatabaseName( String databaseName ) throws Throwable
84+
{
85+
IllegalArgumentException error = assertThrows( IllegalArgumentException.class, () -> template().withDatabase( databaseName ).build());
86+
assertThat( error.getMessage(), equalTo( "Illegal database name ''." ) );
7587
}
7688

7789
@Test

0 commit comments

Comments
 (0)