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 2 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
@@ -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 @@ -25,6 +25,7 @@

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;
Expand All @@ -34,6 +35,7 @@
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 Bookmarks extractBookmarks( Map<String,Value> metadata )
public static DatabaseInfo extractDatabaseInfo( Map<String,Value> metadata )
{
Value dbValue = metadata.get( "db" );
if ( dbValue == null || dbValue.isNull() || !dbValue.hasType( TYPE_SYSTEM.STRING() ) )
{
return DEFAULT_DATABASE_INFO;
}
else
{
return new InternalDatabaseInfo( dbValue.asString() );
}
}

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 @@ -59,6 +59,7 @@
import static org.mockito.Mockito.when;
import static org.neo4j.driver.Values.value;
import static org.neo4j.driver.Values.values;
import static org.neo4j.driver.internal.summary.InternalDatabaseInfo.DEFAULT_DATABASE_INFO;
import static org.neo4j.driver.internal.util.Futures.completedWithNull;
import static org.neo4j.driver.internal.util.Futures.failedFuture;
import static org.neo4j.driver.util.TestUtil.await;
Expand All @@ -85,9 +86,8 @@ void shouldReturnSummary()
PullAllResponseHandler pullAllHandler = mock( PullAllResponseHandler.class );

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

AsyncStatementResultCursor cursor = newCursor( pullAllHandler );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.neo4j.driver.Value;
import org.neo4j.driver.Values;
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 @@ -49,6 +50,7 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.neo4j.driver.internal.summary.InternalSummaryCounters.EMPTY_STATS;
import static org.neo4j.driver.internal.util.MetadataExtractor.extractDatabaseInfo;
import static org.neo4j.driver.internal.util.MetadataExtractor.extractNeo4jServerVersion;
import static org.neo4j.driver.Values.parameters;
import static org.neo4j.driver.Values.value;
Expand Down Expand Up @@ -390,6 +392,33 @@ void shouldExtractServerVersion()
assertEquals( ServerVersion.v3_5_0, version );
}


@Test
void shouldExtractDatabase()
{
// Given
Map<String,Value> metadata = singletonMap( "db", value( "MyAwesomeDatabase" ) );

// When
DatabaseInfo db = extractDatabaseInfo( metadata );

// Then
assertEquals( "MyAwesomeDatabase", db.name() );
}

@Test
void shouldDefaultToNullDatabaseName()
{
// Given
Map<String,Value> metadata = singletonMap( "no_db", value( "no_db" ) );

// When
DatabaseInfo db = extractDatabaseInfo( metadata );

// Then
assertNull( db.name() );
}

@Test
void shouldFailToExtractServerVersionWhenMetadataDoesNotContainIt()
{
Expand Down