Skip to content

Commit

Permalink
introducing parameter object for common request scoped things
Browse files Browse the repository at this point in the history
  • Loading branch information
lassewesth committed Nov 15, 2023
1 parent 1f18c44 commit 909feda
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.gds.algorithms;

import org.neo4j.gds.api.DatabaseId;
import org.neo4j.gds.api.User;
import org.neo4j.gds.termination.TerminationFlag;

/**
* This is a handy class for transporting similar dependencies through layers.
* And especially useful when that list grows or shrinks - less sites to edit innit
*/
public final class RequestScopedDependencies {
private final DatabaseId databaseId;
private final TerminationFlag terminationFlag;
private final User user;

/**
* Over-doing it with a private constructor?
* <p>
* I just really like the <code>RequestScopedDependencies.builder().build()</code> form
*/
private RequestScopedDependencies(
DatabaseId databaseId,
TerminationFlag terminationFlag,
User user
) {
this.databaseId = databaseId;
this.terminationFlag = terminationFlag;
this.user = user;
}

public static RequestScopedDependenciesBuilder builder() {
return new RequestScopedDependenciesBuilder();
}

public DatabaseId getDatabaseId() {
return databaseId;
}

public TerminationFlag getTerminationFlag() {
return terminationFlag;
}

public User getUser() {
return user;
}

public static class RequestScopedDependenciesBuilder {
private DatabaseId databaseId = DatabaseId.DEFAULT;
private User user = User.DEFAULT;
private TerminationFlag terminationFlag = TerminationFlag.DEFAULT;

public RequestScopedDependenciesBuilder with(DatabaseId databaseId) {
this.databaseId = databaseId;
return this;
}

public RequestScopedDependenciesBuilder with(TerminationFlag terminationFlag) {
this.terminationFlag = terminationFlag;
return this;
}

public RequestScopedDependenciesBuilder with(User user) {
this.user = user;
return this;
}

public RequestScopedDependencies build() {
return new RequestScopedDependencies(databaseId, terminationFlag, user);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.neo4j.gds.algorithms.AlgorithmComputationResult;
import org.neo4j.gds.algorithms.AlgorithmMemoryEstimation;
import org.neo4j.gds.algorithms.AlgorithmMemoryValidationService;
import org.neo4j.gds.algorithms.RequestScopedDependencies;
import org.neo4j.gds.api.DatabaseId;
import org.neo4j.gds.api.GraphName;
import org.neo4j.gds.api.User;
Expand Down Expand Up @@ -63,6 +64,10 @@ public AlgorithmRunner(
this.log = log;
}

/**
* @deprecated Strangle this one, use the other one
*/
@Deprecated
public <A extends Algorithm<R>, R, C extends AlgoBaseConfig> AlgorithmComputationResult<R> run(
String graphName,
C config,
Expand All @@ -71,6 +76,28 @@ public <A extends Algorithm<R>, R, C extends AlgoBaseConfig> AlgorithmComputatio
User user,
DatabaseId databaseId,
TerminationFlag terminationFlag
) {
var requestScopedDependencies = RequestScopedDependencies.builder()
.with(databaseId)
.with(terminationFlag)
.with(user)
.build();

return run(
requestScopedDependencies,
graphName,
config,
relationshipProperty,
algorithmFactory
);
}

public <A extends Algorithm<R>, R, C extends AlgoBaseConfig> AlgorithmComputationResult<R> run(
RequestScopedDependencies requestScopedDependencies,
String graphName,
C config,
Optional<String> relationshipProperty,
GraphAlgorithmFactory<A, C> algorithmFactory
) {
// TODO: Is this the best place to check for preconditions???
PreconditionsProvider.preconditions().check();
Expand All @@ -80,8 +107,8 @@ public <A extends Algorithm<R>, R, C extends AlgoBaseConfig> AlgorithmComputatio
GraphName.parse(graphName),
config,
relationshipProperty,
user,
databaseId
requestScopedDependencies.getUser(),
requestScopedDependencies.getDatabaseId()
);

var graph = graphWithGraphStore.getLeft();
Expand Down Expand Up @@ -115,7 +142,7 @@ public <A extends Algorithm<R>, R, C extends AlgoBaseConfig> AlgorithmComputatio
);

// this really belongs in the factory build thing
algorithm.setTerminationFlag(terminationFlag);
algorithm.setTerminationFlag(requestScopedDependencies.getTerminationFlag());

// run the algorithm
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.gds.algorithms.someotherpackage;

import org.junit.jupiter.api.Test;
import org.neo4j.gds.algorithms.RequestScopedDependencies;
import org.neo4j.gds.api.DatabaseId;
import org.neo4j.gds.api.User;
import org.neo4j.gds.termination.TerminationFlag;

import static org.assertj.core.api.Assertions.assertThat;

class RequestScopedDependenciesTest {
@Test
void shouldBeSafeAndConvenient() {
var rsd = RequestScopedDependencies.builder().build();

assertThat(rsd.getDatabaseId()).isEqualTo(DatabaseId.DEFAULT);
assertThat(rsd.getUser()).isEqualTo(User.DEFAULT);
assertThat(rsd.getTerminationFlag()).isEqualTo(TerminationFlag.DEFAULT);
}

@Test
void shouldBuildBespokeProducts() {
var rsd = RequestScopedDependencies.builder()
.with(DatabaseId.of("IMDB"))
.with(TerminationFlag.STOP_RUNNING)
.with(new User("Colin Needham", true))
.build();

assertThat(rsd.getDatabaseId()).isEqualTo(DatabaseId.of("IMDB"));
assertThat(rsd.getTerminationFlag()).isEqualTo(TerminationFlag.STOP_RUNNING);
assertThat(rsd.getUser()).isEqualTo(new User("Colin Needham", true));
}
}
3 changes: 2 additions & 1 deletion core/src/main/java/org/neo4j/gds/api/DatabaseId.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
import static org.neo4j.gds.utils.StringFormatting.toLowerCaseWithLocale;

public final class DatabaseId {

// defaults to something that you will never find in the wild
public static final DatabaseId DEFAULT = DatabaseId.of("non-existent/" + UUID.randomUUID());
public static final DatabaseId EMPTY = new DatabaseId("");

public static DatabaseId of(String databaseName) {
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/java/org/neo4j/gds/api/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@
import org.apache.commons.lang3.builder.HashCodeBuilder;

import java.util.Optional;
import java.util.UUID;

public class User {
// defaults to something that you will never find in the wild
public static final User DEFAULT = new User("anonymous/" + UUID.randomUUID(), false);

private final String username;
private final boolean isAdmin;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
public interface TerminationFlag {

TerminationFlag RUNNING_TRUE = () -> true;
TerminationFlag DEFAULT = () -> true;
TerminationFlag STOP_RUNNING = () -> false;

int RUN_CHECK_NODE_COUNT = 10_000;

Expand Down

0 comments on commit 909feda

Please sign in to comment.