Skip to content

Commit

Permalink
Add a task to fix the default graph in existing datasets
Browse files Browse the repository at this point in the history
  • Loading branch information
kerim1 committed Jun 14, 2022
1 parent 1531516 commit bb7b4f8
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
import nl.knaw.huygens.timbuctoo.server.tasks.CompressFilesTask;
import nl.knaw.huygens.timbuctoo.server.tasks.DatabaseValidationTask;
import nl.knaw.huygens.timbuctoo.server.tasks.DbLogCreatorTask;
import nl.knaw.huygens.timbuctoo.server.tasks.MoveDefaultGraphsTask;
import nl.knaw.huygens.timbuctoo.server.tasks.MoveEdgesTask;
import nl.knaw.huygens.timbuctoo.server.tasks.ReimportDatasetsTask;
import nl.knaw.huygens.timbuctoo.server.tasks.UserCreationTask;
Expand Down Expand Up @@ -414,6 +415,7 @@ public void run(TimbuctooConfiguration configuration, Environment environment) t

environment.admin().addTask(new StopBdbDataStore(configuration.getDatabases()));
environment.admin().addTask(new BackupTask(dataSetRepository));
environment.admin().addTask(new MoveDefaultGraphsTask(dataSetRepository));
environment.admin().addTask(new AddTypeToNeo4JVertexTask(transactionEnforcer, crudServiceFactory));
environment.admin().addTask(new MoveEdgesTask(transactionEnforcer, crudServiceFactory));
environment.admin().addTask(new AddPidsToWomenWritersEntities(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package nl.knaw.huygens.timbuctoo.server.tasks;

import io.dropwizard.servlets.tasks.Task;
import nl.knaw.huygens.timbuctoo.v5.dataset.DataSetRepository;
import nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet;
import nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.CursorQuad;
import nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.Direction;
import nl.knaw.huygens.timbuctoo.v5.datastores.rssource.DataSetQuadGenerator;
import nl.knaw.huygens.timbuctoo.v5.graphql.mutations.PredicateMutationRdfPatcher;
import nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.PredicateMutation;
import nl.knaw.huygens.timbuctoo.v5.rdfio.implementations.BasicRdfPatchSerializer;
import nl.knaw.huygens.timbuctoo.v5.util.Graph;
import nl.knaw.huygens.timbuctoo.v5.util.RdfConstants;

import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;

import static nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.PredicateMutation.replace;

public class MoveDefaultGraphsTask extends Task {
private static final DataSetQuadGenerator DATASET_QUAD_GENERATOR = new DataSetQuadGenerator();

private final DataSetRepository dataSetRepository;

public MoveDefaultGraphsTask(DataSetRepository dataSetRepository) {
super("moveDefaultGraphs");
this.dataSetRepository = dataSetRepository;
}

@Override
public void execute(Map<String, List<String>> parameters, PrintWriter output) throws Exception {
boolean hasMutations;
PredicateMutation predicateMutation;

for (DataSet dataSet : dataSetRepository.getDataSets()) {
hasMutations = false;
predicateMutation = new PredicateMutation();

String baseUri = dataSet.getMetadata().getBaseUri().endsWith("/") ?
dataSet.getMetadata().getBaseUri() :
dataSet.getMetadata().getBaseUri() + "/";

Optional<Graph> graphOne = Optional.of(new Graph(baseUri));
Optional<Graph> graphTwo = Optional.of(new Graph(baseUri.substring(0, baseUri.length() - 1)));
try (Stream<CursorQuad> quads = dataSet.getQuadStore().getAllQuads()
.filter(quad -> quad.getDirection() == Direction.OUT)
.filter(quad -> quad.inGraph(graphOne) || quad.inGraph(graphTwo))) {
for (CursorQuad quad : (Iterable<CursorQuad>) quads::iterator) {
if (!hasMutations) {
hasMutations = true;
output.println(dataSet.getMetadata().getCombinedId());
output.println();
output.flush();
}

Optional<String> dataType = quad.getValuetype();
Optional<String> language = quad.getLanguage();
if (dataType == null || !dataType.isPresent()) {
output.println(DATASET_QUAD_GENERATOR.onRelation(
quad.getSubject(),
quad.getPredicate(),
quad.getObject(),
quad.getGraph().orElse(null)
));
} else if (language != null && language.isPresent() && dataType.get().equals(RdfConstants.LANGSTRING)) {
output.println(DATASET_QUAD_GENERATOR.onLanguageTaggedString(
quad.getSubject(),
quad.getPredicate(),
quad.getObject(),
language.get(),
quad.getGraph().orElse(null)
));
} else {
output.println(DATASET_QUAD_GENERATOR.onValue(
quad.getSubject(),
quad.getPredicate(),
quad.getObject(),
dataType.get(),
quad.getGraph().orElse(null)
));
}

predicateMutation.entity(baseUri, replace(quad.getPredicate(), new PredicateMutation.Value(
quad.getObject(),
quad.getValuetype().orElse(null),
quad.getLanguage().orElse(null),
null
)));
}

if (hasMutations) {
output.println();
output.flush();

PredicateMutationRdfPatcher rdfPatcher = new PredicateMutationRdfPatcher(predicateMutation);
rdfPatcher.sendQuads(new BasicRdfPatchSerializer(output), null, dataSet);
output.println();
output.flush();

if (!parameters.containsKey("dry-run")) {
dataSet.getImportManager().generateLog(baseUri, null, rdfPatcher).get();
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void sendQuads(RdfPatchSerializer saver, Consumer<String> importStatusCon
oldValue.getObject(),
oldValue.getValuetype().orElse(null),
oldValue.getLanguage().orElse(null),
null
oldValue.getGraph().orElse(null)
);
}
}
Expand All @@ -65,7 +65,7 @@ public void sendQuads(RdfPatchSerializer saver, Consumer<String> importStatusCon
foundSubjects.getOrDefault(oldValue.getObject(), oldValue.getObject()),
oldValue.getValuetype().orElse(null),
oldValue.getLanguage().orElse(null),
null
oldValue.getGraph().orElse(null)
);
}

Expand All @@ -77,7 +77,7 @@ public void sendQuads(RdfPatchSerializer saver, Consumer<String> importStatusCon
foundSubjects.getOrDefault(newValue.getObject(), newValue.getObject()),
newValue.getValuetype().orElse(null),
newValue.getLanguage().orElse(null),
null
newValue.getGraph().orElse(null)
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public BasicRdfPatchSerializer(OutputStream output) {
this.printer = printWriter::write;
}

public BasicRdfPatchSerializer(PrintWriter printWriter) {
this.printWriter = printWriter;
this.printer = printWriter::write;
}

protected BasicRdfPatchSerializer(Consumer<String> printWriter) {
this.printer = printWriter;
this.printWriter = null;
Expand Down
15 changes: 5 additions & 10 deletions timbuctoo-instancev4/src/main/resources/banner.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@


* ) ) )
` ) /( ( ) ( /( ( ( /(
( )(_)))\ ( )\()) ))\ ( )\()) ( (
(_(_())((_) )\ '((_)\ /((_) )\ (_))/ )\ )\
|_ _| (_) _((_)) | |(_)(_))( ((_)| |_ ((_) ((_)
| | | || ' \()| '_ \| || |/ _| | _|/ _ \/ _ \
|_| |_||_|_|_| |_.__/ \_,_|\__| \__|\___/\___/ v6

_____ _ _ _
/__ (_)_ __ ___ | |__ _ _ ___| |_ ___ ___
/ /\/ | '_ ` _ \| '_ \| | | |/ __| __/ _ \ / _ \
/ / | | | | | | | |_) | |_| | (__| || (_) | (_) |
\/ |_|_| |_| |_|_.__/ \__,_|\___|\__\___/ \___/ v7

0 comments on commit bb7b4f8

Please sign in to comment.