org.neo4j.driver.internal.messaging.request.TransactionMetadataBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of neo4j-java-driver Show documentation
Show all versions of neo4j-java-driver Show documentation
Access to the Neo4j graph database through Java
/*
* Copyright (c) "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.messaging.request;
import static java.util.Collections.emptyMap;
import static org.neo4j.driver.Values.value;
import static org.neo4j.driver.internal.DatabaseNameUtil.defaultDatabase;
import java.time.Duration;
import java.util.Map;
import org.neo4j.driver.AccessMode;
import org.neo4j.driver.Bookmark;
import org.neo4j.driver.Logger;
import org.neo4j.driver.Logging;
import org.neo4j.driver.Value;
import org.neo4j.driver.internal.DatabaseName;
import org.neo4j.driver.internal.util.Iterables;
public class TransactionMetadataBuilder {
private static final String BOOKMARKS_METADATA_KEY = "bookmarks";
private static final String DATABASE_NAME_KEY = "db";
private static final String TX_TIMEOUT_METADATA_KEY = "tx_timeout";
private static final String TX_METADATA_METADATA_KEY = "tx_metadata";
private static final String MODE_KEY = "mode";
private static final String MODE_READ_VALUE = "r";
private static final String IMPERSONATED_USER_KEY = "imp_user";
public static Map buildMetadata(
Duration txTimeout,
Map txMetadata,
AccessMode mode,
Bookmark bookmark,
String impersonatedUser,
Logging logging) {
return buildMetadata(txTimeout, txMetadata, defaultDatabase(), mode, bookmark, impersonatedUser, logging);
}
public static Map buildMetadata(
Duration txTimeout,
Map txMetadata,
DatabaseName databaseName,
AccessMode mode,
Bookmark bookmark,
String impersonatedUser,
Logging logging) {
boolean bookmarksPresent = bookmark != null && !bookmark.isEmpty();
boolean txTimeoutPresent = txTimeout != null;
boolean txMetadataPresent = txMetadata != null && !txMetadata.isEmpty();
boolean accessModePresent = mode == AccessMode.READ;
boolean databaseNamePresent = databaseName.databaseName().isPresent();
boolean impersonatedUserPresent = impersonatedUser != null;
if (!bookmarksPresent
&& !txTimeoutPresent
&& !txMetadataPresent
&& !accessModePresent
&& !databaseNamePresent
&& !impersonatedUserPresent) {
return emptyMap();
}
Map result = Iterables.newHashMapWithSize(5);
if (bookmarksPresent) {
result.put(BOOKMARKS_METADATA_KEY, value(bookmark.values()));
}
if (txTimeoutPresent) {
long millis = txTimeout.toMillis();
if (txTimeout.getNano() % 1_000_000 > 0) {
Logger log = logging.getLog(TransactionMetadataBuilder.class);
millis++;
log.info(
"The transaction timeout has been rounded up to next millisecond value since the config had a fractional millisecond value");
}
result.put(TX_TIMEOUT_METADATA_KEY, value(millis));
}
if (txMetadataPresent) {
result.put(TX_METADATA_METADATA_KEY, value(txMetadata));
}
if (accessModePresent) {
result.put(MODE_KEY, value(MODE_READ_VALUE));
}
if (impersonatedUserPresent) {
result.put(IMPERSONATED_USER_KEY, value(impersonatedUser));
}
databaseName.databaseName().ifPresent(name -> result.put(DATABASE_NAME_KEY, value(name)));
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy