Skip to content

Commit

Permalink
Implement support for all node indices
Browse files Browse the repository at this point in the history
  • Loading branch information
fbiville committed Jun 4, 2024
1 parent e7322eb commit 1267a59
Show file tree
Hide file tree
Showing 11 changed files with 1,742 additions and 378 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@
import liquibase.ext.neo4j.database.Neo4jDatabase;
import liquibase.ext.neo4j.structure.EntityType;
import liquibase.ext.neo4j.structure.Label;
import liquibase.ext.neo4j.structure.NodeBTreeIndex;
import liquibase.ext.neo4j.structure.NodeFullTextIndex;
import liquibase.ext.neo4j.structure.NodeIndex;
import liquibase.ext.neo4j.structure.NodePointIndex;
import liquibase.ext.neo4j.structure.NodeRangeIndex;
import liquibase.ext.neo4j.structure.NodeTextIndex;
import liquibase.logging.Logger;
import liquibase.snapshot.DatabaseSnapshot;
import liquibase.snapshot.InvalidExampleException;
Expand All @@ -21,13 +16,9 @@
import liquibase.statement.core.RawParameterizedSqlStatement;
import liquibase.structure.DatabaseObject;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class NodeIndexSnapshotGeneratorNeo4j implements SnapshotGenerator {

Expand All @@ -36,12 +27,12 @@ public int getPriority(Class<? extends DatabaseObject> objectType, Database data
if (!(database instanceof Neo4jDatabase)) {
return PRIORITY_NONE;
}
if (supportedIndexTypes().contains(objectType)) {
return PRIORITY_DEFAULT;
}
if (Label.class.isAssignableFrom(objectType)) {
return PRIORITY_ADDITIONAL;
}
if (NodeIndex.class.isAssignableFrom(objectType)) {
return PRIORITY_DEFAULT;
}
return PRIORITY_NONE;
}

Expand All @@ -51,7 +42,10 @@ public <T extends DatabaseObject> T snapshot(T example, DatabaseSnapshot snapsho
if (!(database instanceof Neo4jDatabase)) {
return chain.snapshot(example, snapshot);
}
if (isAnIndex(example)) {
if (!snapshot.getSnapshotControl().shouldInclude(NodeIndex.class)) {
return chain.snapshot(example, snapshot);
}
if (example instanceof NodeIndex) {
return example;
}
if (!(example instanceof Label)) {
Expand All @@ -66,7 +60,8 @@ public <T extends DatabaseObject> T snapshot(T example, DatabaseSnapshot snapsho
}

Label label = (Label) example;
retrieveIndices(neo4j, label).forEach(label::addIndex);
List<NodeIndex> indices = retrieveIndices(neo4j, label);
indices.forEach(label::addIndex);
return example;
}

Expand All @@ -92,76 +87,23 @@ private static List<NodeIndex> retrieveIndices(Neo4jDatabase neo4j, Label label)
label.getName()
))
.stream()
.flatMap(row -> mapIndex(label, row))
.map(row -> mapIndex(label, row))
.collect(Collectors.toList());
} catch (LiquibaseException e) {
throw new DatabaseException("Could not retrieve BTREE node indexes", e);
}
}

@SuppressWarnings("unchecked")
private static Stream<NodeIndex> mapIndex(Label label, Map<String, ?> row) {
String type = ((String) row.get("type")).toLowerCase(Locale.ROOT);
switch (type) {
case "btree":
return Stream.of(new NodeBTreeIndex(
label.getCatalog(),
(String) row.get("name"),
((List<String>) row.get("labels")).get(0),
(List<String>) row.get("properties"),
(String) row.get("indexProvider"),
getIndexConfig(row)));
case "fulltext":
return Stream.of(new NodeFullTextIndex(
label.getCatalog(),
(String) row.get("name"),
((List<String>) row.get("labels")),
(List<String>) row.get("properties"),
(String) row.get("indexProvider"),
getIndexConfig(row)));
case "point":
return Stream.of(new NodePointIndex(
label.getCatalog(),
(String) row.get("name"),
((List<String>) row.get("labels")).get(0),
((List<String>) row.get("properties")).get(0),
(String) row.get("indexProvider"),
getIndexConfig(row)));
case "range":
return Stream.of(new NodeRangeIndex(
label.getCatalog(),
(String) row.get("name"),
((List<String>) row.get("labels")).get(0),
(List<String>) row.get("properties"),
(String) row.get("indexProvider"),
getIndexConfig(row)));
case "text":
return Stream.of(new NodeTextIndex(
label.getCatalog(),
(String) row.get("name"),
((List<String>) row.get("labels")).get(0),
((List<String>) row.get("properties")).get(0),
(String) row.get("indexProvider"),
getIndexConfig(row)));
default:
return Stream.empty();
}
}

private static <T extends DatabaseObject> boolean isAnIndex(T example) {
return supportedIndexTypes()
.stream()
.anyMatch(type -> type.isAssignableFrom(example.getClass()));
}

private static Set<Class<?>> supportedIndexTypes() {
return Scope.getCurrentScope()
.getServiceLocator()
.findInstances(DatabaseObject.class)
.stream()
.filter(object -> object instanceof NodeIndex)
.map(Object::getClass)
.collect(Collectors.toCollection(LinkedHashSet::new));
private static NodeIndex mapIndex(Label label, Map<String, ?> row) {
return new NodeIndex(
label,
(String) row.get("type"),
(String) row.get("name"),
((List<String>) row.get("labels")),
(List<String>) row.get("properties"),
(String) row.get("indexProvider"),
getIndexConfig(row));
}

@SuppressWarnings("unchecked")
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/liquibase/ext/neo4j/structure/Label.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,21 @@
import liquibase.structure.core.Catalog;
import liquibase.structure.core.Schema;

import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

public class Label extends AbstractDatabaseObject implements CatalogLevelObject {

private Catalog catalog;
private String database;
private String value;

public Label() {
}

public Label(Catalog catalog, String value) {
this.catalog = catalog;
this.database = catalog.getName();
this.value = value;
this.setAttribute("value", value);
}

@Override
Expand All @@ -33,12 +30,12 @@ public DatabaseObject[] getContainingObjects() {

@Override
public String getName() {
return value;
return this.getAttribute("value", String.class);
}

@Override
public DatabaseObject setName(String name) {
this.value = name;
this.setAttribute("value", name);
return this;
}

Expand Down
54 changes: 0 additions & 54 deletions src/main/java/liquibase/ext/neo4j/structure/NodeBTreeIndex.java

This file was deleted.

54 changes: 0 additions & 54 deletions src/main/java/liquibase/ext/neo4j/structure/NodeFullTextIndex.java

This file was deleted.

51 changes: 50 additions & 1 deletion src/main/java/liquibase/ext/neo4j/structure/NodeIndex.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,55 @@
package liquibase.ext.neo4j.structure;

import liquibase.structure.AbstractDatabaseObject;
import liquibase.structure.CatalogLevelObject;
import liquibase.structure.DatabaseObject;
import liquibase.structure.core.Catalog;
import liquibase.structure.core.Schema;

public interface NodeIndex extends DatabaseObject {
import java.util.List;
import java.util.Map;

public class NodeIndex extends AbstractDatabaseObject implements CatalogLevelObject {

private Label label;
private String name;

public NodeIndex() {
}

public NodeIndex(Label label, String type, String name, List<String> labels, List<String> properties, String indexProvider, Map<String, Object> indexConfig) {
this.label = label;
this.name = name;
this.setAttribute("type", type);
this.setAttribute("labels", labels);
this.setAttribute("properties", properties);
this.setAttribute("indexProvider", indexProvider);
this.setAttribute("indexConfig", indexConfig);
}

@Override
public DatabaseObject[] getContainingObjects() {
return null;
}

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

@Override
public String getName() {
return name;
}

@Override
public Catalog getCatalog() {
return label.getCatalog();
}

@Override
public Schema getSchema() {
return null;
}
}
Loading

0 comments on commit 1267a59

Please sign in to comment.