Skip to content

Commit

Permalink
Can now configure a database name. #7.
Browse files Browse the repository at this point in the history
  • Loading branch information
essiembre committed Jun 18, 2021
1 parent 7daf9b0 commit 015916e
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 11 deletions.
7 changes: 7 additions & 0 deletions CHANGES.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
</properties>
<body>

<release version="2.0.0-SNAPSHOT" date="2021-??-??"
description="Changes since last milestone">
<action dev="essiembre" type="add" issue="7">
Can now configure a database name.
</action>
</release>

<release version="2.0.0-M1" date="2021-03-01" description="Major release">
<action dev="essiembre" type="update">
Major refactor to support Norconex Committer Core 3.x. Adds ability
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
<parent>
<groupId>com.norconex.commons</groupId>
<artifactId>norconex-commons-maven-parent</artifactId>
<version>1.0.0-M1</version>
<version>1.0.0-SNAPSHOT</version>
</parent>
<groupId>com.norconex.collectors</groupId>
<artifactId>norconex-committer-neo4j</artifactId>
<version>2.0.0-M1</version>
<version>2.0.0-SNAPSHOT</version>
<name>Norconex Committer Neo4j</name>
<description>Neo4j implementation of Norconex Committer.</description>

Expand Down
21 changes: 15 additions & 6 deletions src/main/java/com/norconex/committer/neo4j/Neo4jClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import org.neo4j.driver.Driver;
import org.neo4j.driver.GraphDatabase;
import org.neo4j.driver.Session;
import org.neo4j.driver.SessionConfig;
import org.neo4j.driver.SessionConfig.Builder;
import org.neo4j.driver.internal.value.NullValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -55,12 +57,14 @@ class Neo4jClient {

private final Neo4jCommitterConfig config;

private Driver neo4jDriver;
private final Driver neo4jDriver;
private final SessionConfig sessionConfig;

public Neo4jClient(Neo4jCommitterConfig config) {
this.config = Objects.requireNonNull(
config, "'config' must not be null.");
this.neo4jDriver = createNeo4jDriver();
this.sessionConfig = createNeo4jSessionConfig();
}

private Driver createNeo4jDriver() {
Expand All @@ -78,6 +82,13 @@ private Driver createNeo4jDriver() {
LOG.info("Neo4j Driver loaded.");
return driver;
}
private SessionConfig createNeo4jSessionConfig() {
Builder b = SessionConfig.builder();
if (StringUtils.isNotBlank(this.config.getDatabase())) {
b.withDatabase(config.getDatabase());
}
return b.build();
}

public void post(Iterator<ICommitterRequest> it) throws CommitterException {
while (it.hasNext()) {
Expand All @@ -98,9 +109,7 @@ public void post(Iterator<ICommitterRequest> it) throws CommitterException {
}

public void close() {
if (neo4jDriver != null) {
neo4jDriver.close();
}
neo4jDriver.close();
LOG.info("Neo4j driver closed.");
}

Expand All @@ -113,7 +122,7 @@ private void postUpsert(UpsertRequest req) throws IOException {
meta.set(config.getNodeContentProperty(), IOUtils.toString(
req.getContent(), StandardCharsets.UTF_8));
}
try (Session session = neo4jDriver.session()) {
try (Session session = neo4jDriver.session(sessionConfig)) {
session.writeTransaction(tx -> {
tx.run(config.getUpsertCypher(), toObjectMap(meta));
return null;
Expand All @@ -125,7 +134,7 @@ private void postDelete(DeleteRequest req) {
Properties meta = req.getMetadata();
Optional.ofNullable(trimToNull(config.getNodeIdProperty())).ifPresent(
fld -> meta.set(fld, req.getReference()));
try (Session session = neo4jDriver.session()) {
try (Session session = neo4jDriver.session(sessionConfig)) {
session.writeTransaction(tx -> {
tx.run(config.getDeleteCypher(), toObjectMap(meta));
return null;
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/com/norconex/committer/neo4j/Neo4jCommitter.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@
* </deleteCypher>
*
* <!-- Optional settings --->
* <database>
* (A database name when using one other than the default one.)
* </database>
* <credentials>
* {@nx.include [email protected]}
* </credentials>
Expand All @@ -81,17 +84,17 @@
* (Optional property name where to store the document reference
* in Neo4j graph entries. Use it as a cypher parameter to uniquely
* identify your graph entries in your configured "upsertCypher" and
* "deleteCypher" queries. Default is "id").
* "deleteCypher" queries. Default is "id".)
* </nodeIdProperty>
* <nodeContentProperty>
* (Optional property name where to store the document content
* in Neo4j graph entries. Use it as a cypher parameter
* in your configured "upsertCypher" query. Default is "content").
* in your configured "upsertCypher" query. Default is "content".)
* </nodeContentProperty>
* <optionalParameters>
* (Comma-separated list of parameter names that can be missing when
* creating the query. They will be set to {@link NullValue}) to avoid
* client exception for missing parameters.
* client exception for missing parameters.)
* <optionalParameters>
*
* {@nx.include com.norconex.committer.core3.batch.AbstractBatchCommitter#options}
Expand Down Expand Up @@ -131,6 +134,7 @@
* @author Sylvain Roussy
* @author Pascal Essiembre
*/
@SuppressWarnings("javadoc")
public class Neo4jCommitter extends AbstractBatchCommitter {

private final Neo4jCommitterConfig config;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<xs:extension base="AbstractBatchCommitter">
<xs:all>
<xs:element name="uri" type="xs:string" minOccurs="1" maxOccurs="1" />
<xs:element name="database" type="xs:string" minOccurs="0" maxOccurs="1" />
<xs:element name="upsertCypher" type="xs:string" minOccurs="1" maxOccurs="1" />
<xs:element name="deleteCypher" type="xs:string" minOccurs="1" maxOccurs="1" />
<xs:element ref="credentials" minOccurs="0" maxOccurs="1"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class Neo4jCommitterConfig implements Serializable {
public static final String DEFAULT_NEO4J_CONTENT_PROPERTY = "content";

private String uri;
private String database;
private final Credentials credentials = new Credentials();
private String multiValuesJoiner;
private String nodeIdProperty = DEFAULT_NEO4J_ID_PROPERTY;
Expand All @@ -53,6 +54,13 @@ public class Neo4jCommitterConfig implements Serializable {
private String deleteCypher;
private final Set<String> optionalParameters = new HashSet<>();

public String getDatabase() {
return database;
}
public void setDatabase(String database) {
this.database = database;
}

public Credentials getCredentials() {
return credentials;
}
Expand Down Expand Up @@ -114,6 +122,7 @@ public void addOptionalParameter(String optionalParameter) {

void saveToXML(XML xml) {
xml.addElement("uri", getUri());
xml.addElement("database", getDatabase());
credentials.saveToXML(xml.addElement("credentials"));
xml.addElement("multiValuesJoiner", getMultiValuesJoiner());
xml.addElement("nodeIdProperty", getNodeIdProperty());
Expand All @@ -125,6 +134,7 @@ void saveToXML(XML xml) {
}
void loadFromXML(XML xml) {
setUri(xml.getString("uri", getUri()));
setDatabase(xml.getString("database", getDatabase()));
xml.ifXML("credentials", x -> x.populate(credentials));
setMultiValuesJoiner(xml.getString(
"multiValuesJoiner", getMultiValuesJoiner()));
Expand Down

0 comments on commit 015916e

Please sign in to comment.