Skip to content

Added database name in the ResultSummary. #591

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ public interface SessionParametersTemplate

/**
* Set the database that the newly created session is going to connect to.
* The given database name cannot be <code>null</code>.
* If the database name is not set, then the default database configured on the server configuration will be connected when the session established.
* If the database name is not set or the database name is set to <code>null</code>,
* then the default database configured in the server configuration will be connected when the session is established.
* For servers that do not support multi-databases, this database value should not be set or could only be set to <code>null</code>.
*
* @param database the database the session going to connect to.
* @return this builder.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.neo4j.driver.internal.retry.RetryLogic;
import org.neo4j.driver.internal.spi.ConnectionProvider;

import static org.neo4j.driver.internal.messaging.request.MultiDatabaseUtil.ABSENT_DB_NAME;

public class SessionFactoryImpl implements SessionFactory
{
private final ConnectionProvider connectionProvider;
Expand All @@ -47,7 +49,7 @@ public class SessionFactoryImpl implements SessionFactory
public NetworkSession newInstance( SessionParameters parameters )
{
BookmarksHolder bookmarksHolder = new DefaultBookmarksHolder( Bookmarks.from( parameters.bookmarks() ) );
return createSession( connectionProvider, retryLogic, parameters.database(), parameters.defaultAccessMode(), bookmarksHolder, logging );
return createSession( connectionProvider, retryLogic, parameters.database().orElse( ABSENT_DB_NAME ), parameters.defaultAccessMode(), bookmarksHolder, logging );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import org.neo4j.driver.AccessMode;
import org.neo4j.driver.SessionParametersTemplate;
Expand Down Expand Up @@ -87,11 +88,11 @@ public AccessMode defaultAccessMode()

/**
* The database where the session is going to connect to.
* @return the database name where the session is going to connect to.
* @return the nullable database name where the session is going to connect to.
*/
public String database()
public Optional<String> database()
{
return database;
return Optional.ofNullable( database );
}

@Override
Expand Down Expand Up @@ -125,7 +126,7 @@ public static class Template implements SessionParametersTemplate
{
private List<String> bookmarks = null;
private AccessMode defaultAccessMode = AccessMode.WRITE;
private String database = ABSENT_DB_NAME;
private String database = null;

private Template()
{
Expand Down Expand Up @@ -162,7 +163,11 @@ public Template withDefaultAccessMode( AccessMode mode )
@Override
public Template withDatabase( String database )
{
Objects.requireNonNull( database, "Database cannot be null." );
if ( ABSENT_DB_NAME.equals( database ) )
{
// Disallow users to use bolt internal value directly. To users, this is totally an illegal database name.
throw new IllegalArgumentException( String.format( "Illegal database name '%s'.", database ) );
}
this.database = database;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2002-2019 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.neo4j.driver.internal.summary;

import java.util.Objects;

import org.neo4j.driver.summary.DatabaseInfo;

public class InternalDatabaseInfo implements DatabaseInfo
{
public static DatabaseInfo DEFAULT_DATABASE_INFO = new InternalDatabaseInfo( null );

private final String name;

public InternalDatabaseInfo( String name )
{
this.name = name;
}

@Override
public String name()
{
return this.name;
}

@Override
public boolean equals( Object o )
{
if ( this == o )
{
return true;
}
if ( o == null || getClass() != o.getClass() )
{
return false;
}
InternalDatabaseInfo that = (InternalDatabaseInfo) o;
return Objects.equals( name, that.name );
}

@Override
public int hashCode()
{
return Objects.hash( name );
}

@Override
public String toString()
{
return "InternalDatabaseInfo{" + "name='" + name + '\'' + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.concurrent.TimeUnit;

import org.neo4j.driver.Statement;
import org.neo4j.driver.summary.DatabaseInfo;
import org.neo4j.driver.summary.Notification;
import org.neo4j.driver.summary.Plan;
import org.neo4j.driver.summary.ProfiledPlan;
Expand All @@ -43,13 +44,14 @@ public class InternalResultSummary implements ResultSummary
private final List<Notification> notifications;
private final long resultAvailableAfter;
private final long resultConsumedAfter;
private final DatabaseInfo databaseInfo;

public InternalResultSummary( Statement statement, ServerInfo serverInfo, StatementType statementType,
SummaryCounters counters, Plan plan, ProfiledPlan profile, List<Notification> notifications,
long resultAvailableAfter, long resultConsumedAfter )
public InternalResultSummary( Statement statement, ServerInfo serverInfo, DatabaseInfo databaseInfo, StatementType statementType,
SummaryCounters counters, Plan plan, ProfiledPlan profile, List<Notification> notifications, long resultAvailableAfter, long resultConsumedAfter )
{
this.statement = statement;
this.serverInfo = serverInfo;
this.databaseInfo = databaseInfo;
this.statementType = statementType;
this.counters = counters;
this.plan = resolvePlan( plan, profile );
Expand Down Expand Up @@ -127,6 +129,12 @@ public ServerInfo server()
return serverInfo;
}

@Override
public DatabaseInfo database()
{
return databaseInfo;
}

@Override
public boolean equals( Object o )
{
Expand Down Expand Up @@ -163,6 +171,7 @@ public String toString()
return "InternalResultSummary{" +
"statement=" + statement +
", serverInfo=" + serverInfo +
", databaseInfo=" + databaseInfo +
", statementType=" + statementType +
", counters=" + counters +
", plan=" + plan +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package org.neo4j.driver.internal.summary;

import java.util.Objects;

import org.neo4j.driver.internal.BoltServerAddress;
import org.neo4j.driver.internal.util.ServerVersion;
import org.neo4j.driver.summary.ServerInfo;
Expand All @@ -44,4 +46,31 @@ public String version()
{
return version;
}

@Override
public boolean equals( Object o )
{
if ( this == o )
{
return true;
}
if ( o == null || getClass() != o.getClass() )
{
return false;
}
InternalServerInfo that = (InternalServerInfo) o;
return Objects.equals( address, that.address ) && Objects.equals( version, that.version );
}

@Override
public int hashCode()
{
return Objects.hash( address, version );
}

@Override
public String toString()
{
return "InternalServerInfo{" + "address='" + address + '\'' + ", version='" + version + '\'' + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,19 @@
import java.util.List;
import java.util.Map;

import org.neo4j.driver.Statement;
import org.neo4j.driver.Value;
import org.neo4j.driver.exceptions.UntrustedServerException;
import org.neo4j.driver.internal.Bookmarks;
import org.neo4j.driver.internal.spi.Connection;
import org.neo4j.driver.internal.summary.InternalDatabaseInfo;
import org.neo4j.driver.internal.summary.InternalNotification;
import org.neo4j.driver.internal.summary.InternalPlan;
import org.neo4j.driver.internal.summary.InternalProfiledPlan;
import org.neo4j.driver.internal.summary.InternalResultSummary;
import org.neo4j.driver.internal.summary.InternalServerInfo;
import org.neo4j.driver.internal.summary.InternalSummaryCounters;
import org.neo4j.driver.Statement;
import org.neo4j.driver.Value;
import org.neo4j.driver.exceptions.UntrustedServerException;
import org.neo4j.driver.summary.DatabaseInfo;
import org.neo4j.driver.summary.Notification;
import org.neo4j.driver.summary.Plan;
import org.neo4j.driver.summary.ProfiledPlan;
Expand All @@ -42,6 +44,7 @@
import org.neo4j.driver.summary.StatementType;

import static java.util.Collections.emptyList;
import static org.neo4j.driver.internal.summary.InternalDatabaseInfo.DEFAULT_DATABASE_INFO;
import static org.neo4j.driver.internal.types.InternalTypeSystem.TYPE_SYSTEM;

public class MetadataExtractor
Expand Down Expand Up @@ -99,12 +102,26 @@ public long extractResultAvailableAfter( Map<String,Value> metadata )
public ResultSummary extractSummary( Statement statement, Connection connection, long resultAvailableAfter, Map<String,Value> metadata )
{
ServerInfo serverInfo = new InternalServerInfo( connection.serverAddress(), connection.serverVersion() );
return new InternalResultSummary( statement, serverInfo, extractStatementType( metadata ),
extractCounters( metadata ), extractPlan( metadata ), extractProfiledPlan( metadata ),
extractNotifications( metadata ), resultAvailableAfter, extractResultConsumedAfter( metadata, resultConsumedAfterMetadataKey ) );
DatabaseInfo dbInfo = extractDatabaseInfo( metadata );
return new InternalResultSummary( statement, serverInfo, dbInfo, extractStatementType( metadata ), extractCounters( metadata ), extractPlan( metadata ),
extractProfiledPlan( metadata ), extractNotifications( metadata ), resultAvailableAfter,
extractResultConsumedAfter( metadata, resultConsumedAfterMetadataKey ) );
}

public static DatabaseInfo extractDatabaseInfo( Map<String,Value> metadata )
{
Value dbValue = metadata.get( "db" );
if ( dbValue == null || dbValue.isNull() )
{
return DEFAULT_DATABASE_INFO;
}
else
{
return new InternalDatabaseInfo( dbValue.asString() );
}
}

public Bookmarks extractBookmarks( Map<String,Value> metadata )
public static Bookmarks extractBookmarks( Map<String,Value> metadata )
{
Value bookmarkValue = metadata.get( "bookmark" );
if ( bookmarkValue != null && !bookmarkValue.isNull() && bookmarkValue.hasType( TYPE_SYSTEM.STRING() ) )
Expand Down
32 changes: 32 additions & 0 deletions driver/src/main/java/org/neo4j/driver/summary/DatabaseInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2002-2019 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.neo4j.driver.summary;

/**
* Provides basic information about where a {@link ResultSummary} is obtained from.
*/
public interface DatabaseInfo
{
/**
* The name of the database where a {@link ResultSummary} is obtained from.
* Default to {@code null} if servers does not support multi-databases.
* @return the name of the database where a {@link ResultSummary} is obtained from
*/
String name();
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,13 @@ public interface ResultSummary

/**
* The basic information of the server where the result is obtained from
* @return basic information of the server where the result is obtain from
* @return basic information of the server where the result is obtained from
*/
ServerInfo server();

/**
* The basic information of the database where the result is obtained from
* @return the basic information of the database where the result is obtained from
*/
DatabaseInfo database();
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.neo4j.driver.internal.SessionParameters.empty;
import static org.neo4j.driver.internal.SessionParameters.template;
import static org.neo4j.driver.internal.messaging.request.MultiDatabaseUtil.ABSENT_DB_NAME;
Expand All @@ -47,7 +49,7 @@ void shouldReturnDefaultValues() throws Throwable
SessionParameters parameters = empty();

Assert.assertEquals( AccessMode.WRITE, parameters.defaultAccessMode() );
assertEquals( ABSENT_DB_NAME, parameters.database() );
assertFalse( parameters.database().isPresent() );
assertNull( parameters.bookmarks() );
}

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

@ParameterizedTest
@ValueSource( strings = {"", "foo", "data", ABSENT_DB_NAME} )
@ValueSource( strings = {"foo", "data", "my awesome database", " "} )
void shouldChangeDatabaseName( String databaseName )
{
SessionParameters parameters = template().withDatabase( databaseName ).build();
assertEquals( databaseName, parameters.database() );
assertTrue( parameters.database().isPresent() );
assertEquals( databaseName, parameters.database().get() );
}

@Test
void shouldForbiddenNullDatabaseName() throws Throwable
void shouldAllowNullDatabaseName() throws Throwable
{
NullPointerException error = assertThrows( NullPointerException.class, () -> template().withDatabase( null ).build());
assertThat( error.getMessage(), equalTo( "Database cannot be null." ) );
SessionParameters parameters = template().withDatabase( null ).build();
assertFalse( parameters.database().isPresent() );
assertEquals( "", parameters.database().orElse( ABSENT_DB_NAME ) );
}

@ParameterizedTest
@ValueSource( strings = {"", ABSENT_DB_NAME} )
void shouldForbiddenEmptyStringDatabaseName( String databaseName ) throws Throwable
{
IllegalArgumentException error = assertThrows( IllegalArgumentException.class, () -> template().withDatabase( databaseName ).build());
assertThat( error.getMessage(), equalTo( "Illegal database name ''." ) );
}

@Test
Expand Down
Loading