Search in sources :

Example 6 with Direction

use of nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.Direction in project timbuctoo by HuygensING.

the class BdbSchemaStore method onChangedSubject.

@Override
public void onChangedSubject(String subject, ChangeFetcher changeFetcher) {
    // Step 1: Get the types that where added, unchanged, removed
    List<Type> addedTypes = new ArrayList<>();
    List<Type> removedTypes = new ArrayList<>();
    List<Type> unchangedTypes = new ArrayList<>();
    try (Stream<CursorQuad> subjTypes = changeFetcher.getPredicates(subject, RDF_TYPE, OUT, true, true, true)) {
        subjTypes.forEach(type -> {
            boolean hadTypesBefore = false;
            final Type typeOfSubject = types.computeIfAbsent(type.getObject(), TYPE_MAKER);
            if (type.getChangeType() == ChangeType.ASSERTED) {
                typeOfSubject.registerSubject(1);
                addedTypes.add(typeOfSubject);
            } else if (type.getChangeType() == ChangeType.RETRACTED) {
                hadTypesBefore = true;
                typeOfSubject.registerSubject(-1);
                removedTypes.add(typeOfSubject);
            } else if (type.getChangeType() == ChangeType.UNCHANGED) {
                hadTypesBefore = true;
                unchangedTypes.add(typeOfSubject);
            }
            if (!hadTypesBefore) {
                try (Stream<CursorQuad> predicates = changeFetcher.getPredicates(subject, true, true, false)) {
                    boolean subjectIsNew = !predicates.findAny().isPresent();
                    if (!subjectIsNew) {
                        final Type unknown = types.computeIfAbsent(UNKNOWN, TYPE_MAKER);
                        removedTypes.add(unknown);
                        unknown.registerSubject(-1);
                    }
                }
            }
        });
    }
    if (addedTypes.isEmpty() && unchangedTypes.isEmpty()) {
        // subject currently has no types
        if (removedTypes.isEmpty()) {
            // subject had no types either
            try (Stream<CursorQuad> predicates = changeFetcher.getPredicates(subject, true, true, false)) {
                boolean subjectIsNew = !predicates.findAny().isPresent();
                if (subjectIsNew) {
                    final Type unknown = types.computeIfAbsent(UNKNOWN, TYPE_MAKER);
                    addedTypes.add(unknown);
                    unknown.registerSubject(1);
                } else {
                    unchangedTypes.add(types.computeIfAbsent(UNKNOWN, TYPE_MAKER));
                }
            }
        } else {
            // subject has become unknown
            final Type unknown = types.computeIfAbsent(UNKNOWN, TYPE_MAKER);
            addedTypes.add(unknown);
            unknown.registerSubject(1);
        }
    }
    // it it was asserted -> add it to the unchanged types and to the added types
    try (Stream<CursorQuad> predicates = changeFetcher.getPredicates(subject, true, true, true)) {
        String prevPred = "";
        Direction[] prevDir = new Direction[] { null };
        int retractedCount = 0;
        int assertedCount = 0;
        int unchangedCount = 0;
        // updatePredicateOccurrence)
        for (CursorQuad quad : (Iterable<CursorQuad>) predicates::iterator) {
            boolean predicateSameAsPrev = prevPred.equals(quad.getPredicate()) && prevDir[0] == quad.getDirection();
            if (!predicateSameAsPrev) {
                updatePredicateOccurrence(addedTypes, removedTypes, unchangedTypes, retractedCount, unchangedCount, assertedCount, prevPred, prevDir[0]);
                prevPred = quad.getPredicate();
                prevDir[0] = quad.getDirection();
                retractedCount = 0;
                assertedCount = 0;
                unchangedCount = 0;
            }
            if (quad.getChangeType() == ChangeType.RETRACTED) {
                retractedCount++;
            } else if (quad.getChangeType() == ChangeType.UNCHANGED) {
                unchangedCount++;
            } else if (quad.getChangeType() == ChangeType.ASSERTED) {
                assertedCount++;
            }
            if (quad.getDirection() != Direction.IN) {
                if (quad.getChangeType() == ChangeType.RETRACTED) {
                    for (Type type : unchangedTypes) {
                        updatePredicateType(type, quad, false, changeFetcher);
                    }
                    for (Type type : removedTypes) {
                        updatePredicateType(type, quad, false, changeFetcher);
                    }
                } else if (quad.getChangeType() == ChangeType.UNCHANGED) {
                    for (Type type : removedTypes) {
                        updatePredicateType(type, quad, false, changeFetcher);
                    }
                    for (Type type : addedTypes) {
                        updatePredicateType(type, quad, true, changeFetcher);
                    }
                } else if (quad.getChangeType() == ChangeType.ASSERTED) {
                    for (Type type : unchangedTypes) {
                        updatePredicateType(type, quad, true, changeFetcher);
                    }
                    for (Type type : addedTypes) {
                        updatePredicateType(type, quad, true, changeFetcher);
                    }
                }
            }
        }
        updatePredicateOccurrence(addedTypes, removedTypes, unchangedTypes, retractedCount, unchangedCount, assertedCount, prevPred, prevDir[0]);
    }
}
Also used : Type(nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Type) ChangeType(nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.ChangeType) CursorQuad(nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.CursorQuad) ArrayList(java.util.ArrayList) Direction(nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.Direction)

Example 7 with Direction

use of nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.Direction in project timbuctoo by HuygensING.

the class PredicateMutationRdfPatcher method sendQuads.

@Override
public void sendQuads(RdfPatchSerializer saver, Consumer<String> importStatusConsumer, DataSet dataSet) throws LogStorageFailedException {
    final QuadStore quadStore = dataSet.getQuadStore();
    final Map<String, String> foundSubjects = new HashMap<>();
    for (Map.Entry<UUID, PredicateMutation.SubjectFinder> entry : mutation.getSubjectFinders().entrySet()) {
        foundSubjects.put(entry.getKey().toString(), entry.getValue().getSubject(quadStore));
    }
    for (CursorQuad newValue : mutation.getFullRetractions()) {
        final String subject = foundSubjects.getOrDefault(newValue.getSubject(), newValue.getSubject());
        final String predicate = newValue.getPredicate();
        final Direction direction = newValue.getDirection();
        try (Stream<CursorQuad> quads = quadStore.getQuads(subject, predicate, direction, "")) {
            for (CursorQuad oldValue : (Iterable<CursorQuad>) quads::iterator) {
                saver.delQuad(oldValue.getSubject(), oldValue.getPredicate(), oldValue.getObject(), oldValue.getValuetype().orElse(null), oldValue.getLanguage().orElse(null), null);
            }
        }
    }
    for (CursorQuad oldValue : mutation.getRetractions()) {
        saver.delQuad(foundSubjects.getOrDefault(oldValue.getSubject(), oldValue.getSubject()), oldValue.getPredicate(), foundSubjects.getOrDefault(oldValue.getObject(), oldValue.getObject()), oldValue.getValuetype().orElse(null), oldValue.getLanguage().orElse(null), null);
    }
    for (CursorQuad newValue : mutation.getAdditions()) {
        if (newValue.getObject() != null) {
            saver.onQuad(foundSubjects.getOrDefault(newValue.getSubject(), newValue.getSubject()), newValue.getPredicate(), foundSubjects.getOrDefault(newValue.getObject(), newValue.getObject()), newValue.getValuetype().orElse(null), newValue.getLanguage().orElse(null), null);
        }
    }
}
Also used : QuadStore(nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.QuadStore) HashMap(java.util.HashMap) Direction(nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.Direction) CursorQuad(nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.CursorQuad) UUID(java.util.UUID) HashMap(java.util.HashMap) Map(java.util.Map)

Example 8 with Direction

use of nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.Direction in project timbuctoo by HuygensING.

the class StoreUpdater method deleteQuad.

private void deleteQuad(String subject, String predicate, Direction direction, String object, String valueType, String language) throws RdfProcessingFailedException {
    try {
        final boolean wasChanged = tripleStore.deleteQuad(subject, predicate, direction, object, valueType, language);
        if (wasChanged && currentversion >= 0) {
            truePatchStore.put(subject, currentversion, predicate, direction, false, object, valueType, language);
            updatedPerPatchStore.put(currentversion, subject);
        }
    } catch (DatabaseWriteException e) {
        throw new RdfProcessingFailedException(e);
    }
}
Also used : DatabaseWriteException(nl.knaw.huygens.timbuctoo.v5.berkeleydb.exceptions.DatabaseWriteException) RdfProcessingFailedException(nl.knaw.huygens.timbuctoo.v5.dataset.exceptions.RdfProcessingFailedException)

Example 9 with Direction

use of nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.Direction in project timbuctoo by HuygensING.

the class StoreUpdater method putQuad.

private void putQuad(String subject, String predicate, Direction direction, String object, String valueType, String language) throws RdfProcessingFailedException {
    try {
        final boolean wasChanged = tripleStore.putQuad(subject, predicate, direction, object, valueType, language);
        if (wasChanged && currentversion >= 0) {
            truePatchStore.put(subject, currentversion, predicate, direction, true, object, valueType, language);
            updatedPerPatchStore.put(currentversion, subject);
        }
    } catch (DatabaseWriteException e) {
        throw new RdfProcessingFailedException(e);
    }
}
Also used : DatabaseWriteException(nl.knaw.huygens.timbuctoo.v5.berkeleydb.exceptions.DatabaseWriteException) RdfProcessingFailedException(nl.knaw.huygens.timbuctoo.v5.dataset.exceptions.RdfProcessingFailedException)

Example 10 with Direction

use of nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.Direction in project timbuctoo by HuygensING.

the class SerializerExecutionStrategy method execute.

@Override
public CompletableFuture<ExecutionResult> execute(ExecutionContext executionContext, ExecutionStrategyParameters parameters) throws NonNullableFieldWasNullException {
    Map<String, java.util.List<Field>> fields = parameters.fields();
    GraphQLObjectType parentType = parameters.typeInfo().castType(GraphQLObjectType.class);
    return super.execute(executionContext, parameters).thenApply(sourceResult -> {
        Map<String, Object> data = sourceResult.getData();
        if (parameters.source() instanceof TypedValue) {
            String value = ((TypedValue) parameters.source()).getValue();
            String typename = ((TypedValue) parameters.source()).getType();
            Value result;
            if (value == null) {
                result = null;
            } else if (data.containsKey("__typename")) {
                result = Value.create(value, typename, (String) data.get("__typename"));
            } else {
                result = Value.create(value, typename);
            }
            return new ExecutionResultImpl(result, sourceResult.getErrors(), sourceResult.getExtensions());
        } else if (parameters.source() instanceof SubjectReference) {
            final String uri = ((SubjectReference) parameters.source()).getSubjectUri();
            final Set<String> types = ((SubjectReference) parameters.source()).getTypes();
            final String graphqlType = getDirectiveArgument(parentType, "rdfType", "uri").orElse(null);
            String type;
            if (graphqlType != null && types.contains(graphqlType)) {
                type = graphqlType;
            } else {
                Optional<String> firstType = types.stream().sorted().findFirst();
                if (firstType.isPresent()) {
                    type = firstType.get();
                } else {
                    LOG.error("No type present on " + uri + ". Expected at least TIM_UNKNOWN");
                    type = RdfConstants.UNKNOWN;
                }
            }
            LinkedHashMap<PredicateInfo, Serializable> copy = new LinkedHashMap<>();
            for (Map.Entry<String, Object> entry : data.entrySet()) {
                final String graphqlFieldName = entry.getKey();
                final GraphQLFieldDefinition fieldDesc = parentType.getFieldDefinition(entry.getKey());
                Optional<String> predicateUri = getDirectiveArgument(fieldDesc, "rdf", "predicate");
                Optional<Direction> direction = getDirectiveArgument(fieldDesc, "rdf", "direction").map(Direction::valueOf);
                final PredicateInfo predicateInfo;
                predicateInfo = predicateUri.map(predUri -> PredicateInfo.predicateInfo(graphqlFieldName, predUri, direction.orElse(Direction.OUT))).orElseGet(() -> PredicateInfo.predicateInfo(graphqlFieldName, null, Direction.OUT));
                if (entry.getValue() == null || entry.getValue() instanceof Serializable) {
                    copy.put(predicateInfo, (Serializable) entry.getValue());
                } else {
                    copy.put(predicateInfo, Value.fromRawJavaType(entry.getValue()));
                }
            }
            return new ExecutionResultImpl(Entity.entity(uri, type, copy), sourceResult.getErrors(), sourceResult.getExtensions());
        } else if (parameters.source() instanceof PaginatedList) {
            PaginatedList<? extends DatabaseResult> source = (PaginatedList) parameters.source();
            return new ExecutionResultImpl(serializableList(source.getPrevCursor().orElse(null), source.getNextCursor().orElse(null), ((GraphqlIntrospectionList) data.get("items")).getItems()), sourceResult.getErrors(), sourceResult.getExtensions());
        } else if (executionContext.getGraphQLSchema().getQueryType() == parentType) {
            return new ExecutionResultImpl(QueryContainer.queryContainer(sourceResult.getData()), sourceResult.getErrors(), sourceResult.getExtensions());
        } else {
            LinkedHashMap<String, Serializable> copy = new LinkedHashMap<>();
            for (Map.Entry<String, Object> entry : data.entrySet()) {
                if (entry.getValue() == null || entry.getValue() instanceof Serializable) {
                    copy.put(entry.getKey(), (Serializable) entry.getValue());
                } else {
                    copy.put(entry.getKey(), GraphqlIntrospectionValue.fromRawJavaType(entry.getValue()));
                }
            }
            return new ExecutionResultImpl(graphqlIntrospectionObject(copy), sourceResult.getErrors(), sourceResult.getExtensions());
        }
    });
}
Also used : TypedValue(nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.TypedValue) SerializableList.serializableList(nl.knaw.huygens.timbuctoo.v5.serializable.dto.SerializableList.serializableList) GraphqlIntrospectionValue(nl.knaw.huygens.timbuctoo.v5.serializable.dto.GraphqlIntrospectionValue) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) CompletableFuture(java.util.concurrent.CompletableFuture) ExecutionContext(graphql.execution.ExecutionContext) GraphqlIntrospectionObject.graphqlIntrospectionObject(nl.knaw.huygens.timbuctoo.v5.serializable.dto.GraphqlIntrospectionObject.graphqlIntrospectionObject) SubjectReference(nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.SubjectReference) ExecutionStrategyParameters(graphql.execution.ExecutionStrategyParameters) Value(nl.knaw.huygens.timbuctoo.v5.serializable.dto.Value) ExecutionResult(graphql.ExecutionResult) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) AsyncExecutionStrategy(graphql.execution.AsyncExecutionStrategy) ExecutionResultImpl(graphql.ExecutionResultImpl) QueryContainer(nl.knaw.huygens.timbuctoo.v5.serializable.dto.QueryContainer) GraphQLObjectType(graphql.schema.GraphQLObjectType) GraphqlIntrospectionList(nl.knaw.huygens.timbuctoo.v5.serializable.dto.GraphqlIntrospectionList) Logger(org.slf4j.Logger) NonNullableFieldWasNullException(graphql.execution.NonNullableFieldWasNullException) RdfConstants(nl.knaw.huygens.timbuctoo.v5.util.RdfConstants) Set(java.util.Set) Serializable(nl.knaw.huygens.timbuctoo.v5.serializable.dto.Serializable) Field(graphql.language.Field) DatabaseResult(nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.DatabaseResult) Collectors(java.util.stream.Collectors) List(java.util.List) PaginatedList(nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.PaginatedList) StringValue(graphql.language.StringValue) PredicateInfo(nl.knaw.huygens.timbuctoo.v5.serializable.dto.PredicateInfo) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) Optional(java.util.Optional) Direction(nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.Direction) GraphqlIntrospectionList.graphqlIntrospectionList(nl.knaw.huygens.timbuctoo.v5.serializable.dto.GraphqlIntrospectionList.graphqlIntrospectionList) Entity(nl.knaw.huygens.timbuctoo.v5.serializable.dto.Entity) Serializable(nl.knaw.huygens.timbuctoo.v5.serializable.dto.Serializable) Set(java.util.Set) Optional(java.util.Optional) GraphqlIntrospectionList(nl.knaw.huygens.timbuctoo.v5.serializable.dto.GraphqlIntrospectionList) SubjectReference(nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.SubjectReference) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) LinkedHashMap(java.util.LinkedHashMap) ExecutionResultImpl(graphql.ExecutionResultImpl) GraphQLObjectType(graphql.schema.GraphQLObjectType) TypedValue(nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.TypedValue) GraphqlIntrospectionValue(nl.knaw.huygens.timbuctoo.v5.serializable.dto.GraphqlIntrospectionValue) Value(nl.knaw.huygens.timbuctoo.v5.serializable.dto.Value) StringValue(graphql.language.StringValue) SerializableList.serializableList(nl.knaw.huygens.timbuctoo.v5.serializable.dto.SerializableList.serializableList) GraphqlIntrospectionList(nl.knaw.huygens.timbuctoo.v5.serializable.dto.GraphqlIntrospectionList) List(java.util.List) PaginatedList(nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.PaginatedList) GraphqlIntrospectionList.graphqlIntrospectionList(nl.knaw.huygens.timbuctoo.v5.serializable.dto.GraphqlIntrospectionList.graphqlIntrospectionList) GraphqlIntrospectionObject.graphqlIntrospectionObject(nl.knaw.huygens.timbuctoo.v5.serializable.dto.GraphqlIntrospectionObject.graphqlIntrospectionObject) PredicateInfo(nl.knaw.huygens.timbuctoo.v5.serializable.dto.PredicateInfo) PaginatedList(nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.PaginatedList) TypedValue(nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.TypedValue)

Aggregations

Direction (nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.Direction)5 Type (nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Type)4 ChangeType (nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.ChangeType)3 StringValue (graphql.language.StringValue)2 Map (java.util.Map)2 DatabaseWriteException (nl.knaw.huygens.timbuctoo.v5.berkeleydb.exceptions.DatabaseWriteException)2 RdfProcessingFailedException (nl.knaw.huygens.timbuctoo.v5.dataset.exceptions.RdfProcessingFailedException)2 CursorQuad (nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.CursorQuad)2 Predicate (nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Predicate)2 DatabaseResult (nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.DatabaseResult)2 ExecutionResult (graphql.ExecutionResult)1 ExecutionResultImpl (graphql.ExecutionResultImpl)1 AsyncExecutionStrategy (graphql.execution.AsyncExecutionStrategy)1 ExecutionContext (graphql.execution.ExecutionContext)1 ExecutionStrategyParameters (graphql.execution.ExecutionStrategyParameters)1 NonNullableFieldWasNullException (graphql.execution.NonNullableFieldWasNullException)1 BooleanValue (graphql.language.BooleanValue)1 Directive (graphql.language.Directive)1 Field (graphql.language.Field)1 GraphQLFieldDefinition (graphql.schema.GraphQLFieldDefinition)1