Search in sources :

Example 11 with CursorQuad

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

the class SummaryPropDataRetrieverTest method retrieveDefaultPropertyUsesTheFirstQuadFound.

@Test
public void retrieveDefaultPropertyUsesTheFirstQuadFound() {
    List<SummaryProp> defaultProperties = Lists.newArrayList(summaryPropertyWithPath("http://example.org/path", "http://example.org/path2"));
    SummaryPropDataRetriever instance = new SummaryPropDataRetriever("http://example.org/userConfigured", defaultProperties);
    QuadStore quadStore = mock(QuadStore.class);
    CursorQuad foundQuad1 = quadWithObject("http://example.org/objectFound1", Optional.empty());
    CursorQuad foundQuad2 = quadWithObject("http://example.org/objectFound2", Optional.empty());
    given(quadStore.getQuads("http://example.org/source", "http://example.org/path", Direction.OUT, "")).willReturn(Stream.of(foundQuad1, foundQuad2));
    CursorQuad quadWithObjectUriOfPath2 = quadWithObject("http://example.org/objectOfPath2", Optional.empty());
    given(quadStore.getQuads("http://example.org/objectFound1", "http://example.org/path2", Direction.OUT, "")).willReturn(Stream.of(quadWithObjectUriOfPath2));
    instance.createSummaryProperty(subjectWithUri("http://example.org/source"), dataSetWithQuadStore(quadStore));
    verify(quadStore).getQuads("http://example.org/source", "http://example.org/path", Direction.OUT, "");
    verify(quadStore).getQuads("http://example.org/objectFound1", "http://example.org/path2", Direction.OUT, "");
    verify(quadStore, never()).getQuads("http://example.org/objectFound2", "http://example.org/path2", Direction.OUT, "");
}
Also used : QuadStore(nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.QuadStore) CursorQuad(nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.CursorQuad) SummaryProp(nl.knaw.huygens.timbuctoo.v5.graphql.defaultconfiguration.SummaryProp) Test(org.junit.Test)

Example 12 with CursorQuad

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

the class SummaryPropDataRetrieverTest method createSummaryPropertyWalksTheWholePathOfTheConfiguredDefaultProperty.

@Test
public void createSummaryPropertyWalksTheWholePathOfTheConfiguredDefaultProperty() {
    List<SummaryProp> defaultProperties = Lists.newArrayList(summaryPropertyWithPath("http://example.org/path", "http://example.org/path2"));
    SummaryPropDataRetriever instance = new SummaryPropDataRetriever("http://example.org/userConfigured", defaultProperties);
    CursorQuad foundQuad = quadWithObject("http://example.org/objectFound", Optional.empty());
    QuadStore quadStore = mock(QuadStore.class);
    given(quadStore.getQuads("http://example.org/source", "http://example.org/path", Direction.OUT, "")).willReturn(Stream.of(foundQuad));
    instance.createSummaryProperty(subjectWithUri("http://example.org/source"), dataSetWithQuadStore(quadStore));
    InOrder inOrder = inOrder(quadStore);
    inOrder.verify(quadStore).getQuads("http://example.org/source", "http://example.org/path", Direction.OUT, "");
    inOrder.verify(quadStore).getQuads("http://example.org/objectFound", "http://example.org/path2", Direction.OUT, "");
}
Also used : QuadStore(nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.QuadStore) InOrder(org.mockito.InOrder) CursorQuad(nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.CursorQuad) SummaryProp(nl.knaw.huygens.timbuctoo.v5.graphql.defaultconfiguration.SummaryProp) Test(org.junit.Test)

Example 13 with CursorQuad

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

the class BdbSchemaStore method updatePredicateType.

public void updatePredicateType(Type type, CursorQuad quad, boolean inc, ChangeFetcher changeFetcher) {
    final Predicate predicate = type.getOrCreatePredicate(quad.getPredicate(), quad.getDirection());
    if (quad.getValuetype().isPresent()) {
        predicate.incValueType(quad.getValuetype().get(), inc ? 1 : -1);
    } else {
        try (Stream<CursorQuad> typeQs = changeFetcher.getPredicates(quad.getObject(), RDF_TYPE, OUT, !inc, true, inc)) {
            boolean[] hadType = new boolean[] { false };
            typeQs.forEach(typeQ -> {
                hadType[0] = true;
                predicate.incReferenceType(typeQ.getObject(), inc ? 1 : -1);
            });
            if (!hadType[0]) {
                predicate.incReferenceType(UNKNOWN, inc ? 1 : -1);
            }
        }
    }
}
Also used : CursorQuad(nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.CursorQuad) Predicate(nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Predicate)

Example 14 with CursorQuad

use of nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.CursorQuad 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 15 with CursorQuad

use of nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.CursorQuad 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)

Aggregations

CursorQuad (nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.CursorQuad)19 QuadStore (nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.QuadStore)10 SummaryProp (nl.knaw.huygens.timbuctoo.v5.graphql.defaultconfiguration.SummaryProp)7 Test (org.junit.Test)7 Direction (nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.Direction)6 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 List (java.util.List)4 TypedValue (nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.TypedValue)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 IOException (java.io.IOException)3 Map (java.util.Map)3 Optional (java.util.Optional)3 Stream (java.util.stream.Stream)3 DataSet (nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet)3 Type (nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Type)3 SubjectReference (nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.SubjectReference)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3