Skip to content

Adding schema constraints while creating JanusGraph schema #21

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 9 commits into from
Apr 27, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
import static org.carlspring.strongbox.db.schema.Vertices.ARTIFACT_ID_GROUP;
import static org.carlspring.strongbox.db.schema.Vertices.ARTIFACT_TAG;
import static org.carlspring.strongbox.db.schema.Vertices.GENERIC_ARTIFACT_COORDINATES;
import static org.carlspring.strongbox.db.schema.Vertices.RAW_ARTIFACT_COORDINATES;
import static org.carlspring.strongbox.db.schema.Vertices.MAVEN_ARTIFACT_COORDINATES;
import static org.carlspring.strongbox.db.schema.Vertices.NPM_ARTIFACT_COORDINATES;
import static org.carlspring.strongbox.db.schema.Vertices.NUGET_ARTIFACT_COORDINATES;
import static org.carlspring.strongbox.db.schema.Vertices.PYPI_ARTIFACT_COORDINATES;
import static org.carlspring.strongbox.db.schema.Vertices.RAW_ARTIFACT_COORDINATES;
import static org.carlspring.strongbox.db.schema.Vertices.REMOTE_ARTIFACT;
import static org.carlspring.strongbox.db.schema.Vertices.USER;
import static org.janusgraph.core.Multiplicity.MANY2ONE;
Expand All @@ -35,6 +35,7 @@
import org.janusgraph.core.JanusGraph;
import org.janusgraph.core.Multiplicity;
import org.janusgraph.core.PropertyKey;
import org.janusgraph.core.VertexLabel;
import org.janusgraph.core.schema.ConsistencyModifier;
import org.janusgraph.core.schema.EdgeLabelMaker;
import org.janusgraph.core.schema.JanusGraphIndex;
Expand Down Expand Up @@ -235,7 +236,182 @@ private void applySchemaChanges(JanusGraphManagement jgm)
makeEdgeLabelIfDoesNotExist(jgm, REMOTE_ARTIFACT_INHERIT_ARTIFACT, ONE2ONE);
makeEdgeLabelIfDoesNotExist(jgm, ARTIFACT_COORDINATES_INHERIT_GENERIC_ARTIFACT_COORDINATES, ONE2ONE);
makeEdgeLabelIfDoesNotExist(jgm, ARTIFACT_GROUP_HAS_ARTIFACTS, ONE2MANY);


// Add property constraints
applyPropertyConstraints(jgm);

// Add connection constraints
applyConnectionConstraints(jgm);
}

private void applyConnectionConstraints(JanusGraphManagement jgm)
{
jgm.addConnection(jgm.getEdgeLabel(ARTIFACT_HAS_ARTIFACT_COORDINATES),
jgm.getVertexLabel(ARTIFACT),
jgm.getVertexLabel(GENERIC_ARTIFACT_COORDINATES));

jgm.addConnection(jgm.getEdgeLabel(ARTIFACT_HAS_TAGS),
jgm.getVertexLabel(ARTIFACT),
jgm.getVertexLabel(ARTIFACT_TAG));

jgm.addConnection(jgm.getEdgeLabel(ARTIFACT_GROUP_HAS_ARTIFACTS),
jgm.getVertexLabel(ARTIFACT_ID_GROUP),
jgm.getVertexLabel(ARTIFACT));

jgm.addConnection(jgm.getEdgeLabel(REMOTE_ARTIFACT_INHERIT_ARTIFACT),
jgm.getVertexLabel(REMOTE_ARTIFACT),
jgm.getVertexLabel(ARTIFACT));

jgm.addConnection(jgm.getEdgeLabel(ARTIFACT_COORDINATES_INHERIT_GENERIC_ARTIFACT_COORDINATES),
jgm.getVertexLabel(RAW_ARTIFACT_COORDINATES),
jgm.getVertexLabel(GENERIC_ARTIFACT_COORDINATES));

jgm.addConnection(jgm.getEdgeLabel(ARTIFACT_COORDINATES_INHERIT_GENERIC_ARTIFACT_COORDINATES),
jgm.getVertexLabel(NUGET_ARTIFACT_COORDINATES),
jgm.getVertexLabel(GENERIC_ARTIFACT_COORDINATES));

jgm.addConnection(jgm.getEdgeLabel(ARTIFACT_COORDINATES_INHERIT_GENERIC_ARTIFACT_COORDINATES),
jgm.getVertexLabel(NPM_ARTIFACT_COORDINATES),
jgm.getVertexLabel(GENERIC_ARTIFACT_COORDINATES));

jgm.addConnection(jgm.getEdgeLabel(ARTIFACT_COORDINATES_INHERIT_GENERIC_ARTIFACT_COORDINATES),
jgm.getVertexLabel(MAVEN_ARTIFACT_COORDINATES),
jgm.getVertexLabel(GENERIC_ARTIFACT_COORDINATES));

jgm.addConnection(jgm.getEdgeLabel(ARTIFACT_COORDINATES_INHERIT_GENERIC_ARTIFACT_COORDINATES),
jgm.getVertexLabel(PYPI_ARTIFACT_COORDINATES),
jgm.getVertexLabel(GENERIC_ARTIFACT_COORDINATES));

}

private void applyPropertyConstraints(JanusGraphManagement jgm)
{
// Vertex Property Constraints
addVertexPropertyConstraints(jgm,
ARTIFACT,
"uuid",
"storageId",
"repositoryId",
"created",
"lastUpdated",
"lastUsed",
"sizeInBytes",
"downloadCount",
"filenames",
"checksums");

addVertexPropertyConstraints(jgm,
REMOTE_ARTIFACT,
"uuid",
"cached",
"created");

addVertexPropertyConstraints(jgm,
GENERIC_ARTIFACT_COORDINATES,
"uuid",
"version",
"coordinates.id",
"coordinates.extension",
"coordinates.name",
"coordinates.path",
"coordinates.scope",
"coordinates.groupId",
"coordinates.artifactId",
"coordinates.classifier",
"coordinates.distribution",
"coordinates.build",
"coordinates.abi",
"coordinates.platform",
"coordinates.packaging",
"coordinates.languageImplementationVersion",
"created");

addVertexPropertyConstraints(jgm,
RAW_ARTIFACT_COORDINATES,
"uuid",
"version",
"coordinates.extension",
"coordinates.name",
"coordinates.path",
"created");

addVertexPropertyConstraints(jgm,
MAVEN_ARTIFACT_COORDINATES,
"uuid",
"version",
"coordinates.extension",
"coordinates.name",
"coordinates.groupId",
"coordinates.artifactId",
"coordinates.classifier",
"created");

addVertexPropertyConstraints(jgm,
NPM_ARTIFACT_COORDINATES,
"uuid",
"version",
"coordinates.extension",
"coordinates.name",
"coordinates.scope",
"created");

addVertexPropertyConstraints(jgm,
NUGET_ARTIFACT_COORDINATES,
"uuid",
"version",
"coordinates.extension",
"coordinates.name",
"coordinates.id",
"created");

addVertexPropertyConstraints(jgm,
PYPI_ARTIFACT_COORDINATES,
"uuid",
"version",
"coordinates.extension",
"coordinates.name",
"coordinates.build",
"coordinates.abi",
"coordinates.platform",
"coordinates.packaging",
"coordinates.distribution",
"coordinates.languageImplementationVersion",
"created");

addVertexPropertyConstraints(jgm,
ARTIFACT_TAG,
"uuid",
"created");

addVertexPropertyConstraints(jgm,
ARTIFACT_ID_GROUP,
"uuid",
"storageId",
"repositoryId",
"name",
"created");

addVertexPropertyConstraints(jgm,
USER,
"uuid",
"password",
"enabled",
"roles",
"securityTokenKey",
"sourceId",
"created",
"lastUpdated");
}

private void addVertexPropertyConstraints(JanusGraphManagement jgm,
String vertex,
String... propertykeys)
{
VertexLabel vertexLabel = jgm.getVertexLabel(vertex);
for (String propertyKey : propertykeys)
{
jgm.addProperties(vertexLabel, jgm.getPropertyKey(propertyKey));
}
}

private void createProperties(JanusGraphManagement jgm)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@ public interface Vertices
String REMOTE_ARTIFACT = "RemoteArtifact";
String GENERIC_ARTIFACT_COORDINATES = "GenericArtifactCoordinates";
String ARTIFACT_COORDINATES = "ArtifactCoordinates";

String RAW_ARTIFACT_COORDINATES = "RawArtifactCoordinates";
String MAVEN_ARTIFACT_COORDINATES = "MavenArtifactCoordinates";
String NPM_ARTIFACT_COORDINATES = "NpmArtifactCoordinates";
String NUGET_ARTIFACT_COORDINATES = "NugetArtifactCoordinates";
String PYPI_ARTIFACT_COORDINATES = "PypiArtifactCoordinates";
String RPM_ARTIFACT_COORDINATES = "RpmArtifactCoordinates";

String ARTIFACT_TAG = "ArtifactTag";
String ARTIFACT_ID_GROUP = "ArtifactIdGroup";

String USER = "User";
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,11 @@ private JanusGraph provideJanusGraphInstance()
.set("storage.password",
janusGraphProperties.getStoragePassword())
.set("storage.cql.keyspace", "strongbox")
.set("storage.cql.only-use-local-consistency-for-system-operations", true)
.set("storage.cql.only-use-local-consistency-for-system-operations",
true)
.set("tx.log-tx", true)
.set("schema.default", "none")
.set("schema.constraints", true)
.open();

try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ private JanusGraph provideJanusGraphInstance()

JanusGraph janusGraphLocal = JanusGraphFactory.build()
.set("storage.backend", "inmemory")
.set("schema.default", "none")
.set("schema.constraints", true)
.open();

try
Expand Down