Search in sources :

Example 11 with QuadStore

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

the class DataSet method dataSet.

public static DataSet dataSet(DataSetMetaData metadata, ExecutorService executorService, String rdfPrefix, BdbEnvironmentCreator dataStoreFactory, Runnable onUpdated, DataSetStorage dataSetStorage) throws IOException, DataStoreCreationException {
    String userId = metadata.getOwnerId();
    String dataSetId = metadata.getDataSetId();
    File descriptionFile = dataSetStorage.getResourceSyncDescriptionFile();
    FileStorage fileStorage = dataSetStorage.getFileStorage();
    ImportManager importManager = new ImportManager(dataSetStorage.getLogList(), fileStorage, fileStorage, dataSetStorage.getLogStorage(), executorService, dataSetStorage.getRdfIo(), onUpdated);
    try {
        importManager.subscribeToRdf(new RdfDescriptionSaver(descriptionFile, metadata.getBaseUri(), importManager.getImportStatus()));
    } catch (ParserConfigurationException | SAXException e) {
        LOG.error("Could not construct import manager of data set", e);
    }
    final TupleBinding<String> stringBinding = TupleBinding.getPrimitiveBinding(String.class);
    try {
        StringStringIsCleanHandler stringStringIsCleanHandler = new StringStringIsCleanHandler();
        BdbTripleStore quadStore = new BdbTripleStore(dataStoreFactory.getDatabase(userId, dataSetId, "rdfData", true, stringBinding, stringBinding, stringStringIsCleanHandler));
        final BdbTypeNameStore typeNameStore = new BdbTypeNameStore(new BdbBackedData(dataStoreFactory.getDatabase(userId, dataSetId, "typenames", false, stringBinding, stringBinding, stringStringIsCleanHandler)), rdfPrefix);
        final BdbSchemaStore schema = new BdbSchemaStore(new BdbBackedData(dataStoreFactory.getDatabase(userId, dataSetId, "schema", false, stringBinding, stringBinding, stringStringIsCleanHandler)), importManager.getImportStatus());
        final BdbTruePatchStore truePatchStore = new BdbTruePatchStore(dataStoreFactory.getDatabase(userId, dataSetId, "truePatch", true, stringBinding, stringBinding, stringStringIsCleanHandler));
        final TupleBinding<Integer> integerBinding = TupleBinding.getPrimitiveBinding(Integer.class);
        final UpdatedPerPatchStore updatedPerPatchStore = new UpdatedPerPatchStore(dataStoreFactory.getDatabase(userId, dataSetId, "updatedPerPatch", true, integerBinding, stringBinding, new IsCleanHandler<Integer, String>() {

            @Override
            public Integer getKey() {
                return Integer.MAX_VALUE;
            }

            @Override
            public String getValue() {
                return "isClean";
            }
        }));
        final BdbRmlDataSourceStore rmlDataSourceStore = new BdbRmlDataSourceStore(dataStoreFactory.getDatabase(userId, dataSetId, "rmlSource", true, stringBinding, stringBinding, stringStringIsCleanHandler), importManager.getImportStatus());
        VersionStore versionStore = new VersionStore(dataStoreFactory.getDatabase(userId, dataSetId, "versions", false, stringBinding, integerBinding, new IsCleanHandler<String, Integer>() {

            @Override
            public String getKey() {
                return "isClean";
            }

            @Override
            public Integer getValue() {
                return Integer.MAX_VALUE;
            }
        }));
        final StoreUpdater storeUpdater = new StoreUpdater(dataStoreFactory, quadStore, typeNameStore, truePatchStore, updatedPerPatchStore, Lists.newArrayList(schema, rmlDataSourceStore), versionStore, importManager.getImportStatus());
        importManager.subscribeToRdf(storeUpdater);
        ImmutableDataSet dataSet = ImmutableDataSet.builder().ownerId(userId).dataSetName(dataSetId).bdbEnvironmentCreator(dataStoreFactory).metadata(metadata).quadStore(quadStore).typeNameStore(typeNameStore).schemaStore(schema).dataSource(new RdfDataSourceFactory(rmlDataSourceStore)).schemaStore(schema).importManager(importManager).dataSetStorage(dataSetStorage).build();
        importManager.init(dataSet);
        if (!quadStore.isClean() || !typeNameStore.isClean() || !schema.isClean() || !truePatchStore.isClean() || !updatedPerPatchStore.isClean() || !rmlDataSourceStore.isClean() || !versionStore.isClean()) {
            LOG.error("Data set '{}__{}' data is corrupted, starting to reimport.", userId, dataSetId);
            quadStore.empty();
            typeNameStore.empty();
            schema.empty();
            truePatchStore.empty();
            updatedPerPatchStore.empty();
            rmlDataSourceStore.empty();
            versionStore.empty();
            importManager.reprocessLogs();
        } else {
            // process unprocessed logs
            importManager.processLogs();
        }
        return dataSet;
    } catch (BdbDbCreationException e) {
        throw new DataStoreCreationException(e.getCause());
    }
}
Also used : ImportManager(nl.knaw.huygens.timbuctoo.v5.dataset.ImportManager) BdbSchemaStore(nl.knaw.huygens.timbuctoo.v5.datastores.implementations.bdb.BdbSchemaStore) BdbTruePatchStore(nl.knaw.huygens.timbuctoo.v5.datastores.implementations.bdb.BdbTruePatchStore) SAXException(org.xml.sax.SAXException) VersionStore(nl.knaw.huygens.timbuctoo.v5.datastores.implementations.bdb.VersionStore) BdbBackedData(nl.knaw.huygens.timbuctoo.v5.datastores.implementations.bdb.BdbBackedData) BdbTypeNameStore(nl.knaw.huygens.timbuctoo.v5.datastores.implementations.bdb.BdbTypeNameStore) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) UpdatedPerPatchStore(nl.knaw.huygens.timbuctoo.v5.datastores.implementations.bdb.UpdatedPerPatchStore) StringStringIsCleanHandler(nl.knaw.huygens.timbuctoo.v5.berkeleydb.isclean.StringStringIsCleanHandler) RdfDescriptionSaver(nl.knaw.huygens.timbuctoo.v5.datastores.implementations.RdfDescriptionSaver) BdbRmlDataSourceStore(nl.knaw.huygens.timbuctoo.v5.datastores.implementations.bdb.BdbRmlDataSourceStore) StoreUpdater(nl.knaw.huygens.timbuctoo.v5.datastores.implementations.bdb.StoreUpdater) IsCleanHandler(nl.knaw.huygens.timbuctoo.v5.berkeleydb.isclean.IsCleanHandler) StringStringIsCleanHandler(nl.knaw.huygens.timbuctoo.v5.berkeleydb.isclean.StringStringIsCleanHandler) BdbDbCreationException(nl.knaw.huygens.timbuctoo.v5.berkeleydb.exceptions.BdbDbCreationException) BdbTripleStore(nl.knaw.huygens.timbuctoo.v5.datastores.implementations.bdb.BdbTripleStore) FileStorage(nl.knaw.huygens.timbuctoo.v5.filestorage.FileStorage) RdfDataSourceFactory(nl.knaw.huygens.timbuctoo.v5.rml.RdfDataSourceFactory) DataStoreCreationException(nl.knaw.huygens.timbuctoo.v5.dataset.exceptions.DataStoreCreationException) File(java.io.File)

Example 12 with QuadStore

use of nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.QuadStore 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 13 with QuadStore

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

the class JsonLdEditEndpoint method submitChanges.

@PUT
public Response submitChanges(String jsonLdImport, @PathParam("user") String ownerId, @PathParam("dataset") String dataSetId, @HeaderParam("authorization") String authHeader) throws LogStorageFailedException {
    Optional<User> user;
    try {
        user = userValidator.getUserFromAccessToken(authHeader);
    } catch (UserValidationException e) {
        user = Optional.empty();
    }
    Optional<DataSet> dataSetOpt = dataSetRepository.getDataSet(user.get(), ownerId, dataSetId);
    if (!dataSetOpt.isPresent()) {
        return Response.status(Response.Status.NOT_FOUND).build();
    }
    final DataSet dataSet = dataSetOpt.get();
    final QuadStore quadStore = dataSet.getQuadStore();
    final ImportManager importManager = dataSet.getImportManager();
    final Response response = checkWriteAccess(dataSet, user, permissionFetcher);
    if (response != null) {
        return response;
    }
    try {
        final Future<ImportStatus> promise = importManager.generateLog(dataSet.getMetadata().getBaseUri(), dataSet.getMetadata().getBaseUri(), fromCurrentState(documentLoader, jsonLdImport, quadStore, TIM_USERS + user.get().getPersistentId(), UUID.randomUUID().toString(), Clock.systemUTC()));
        return handleImportManagerResult(promise);
    } catch (IOException e) {
        return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build();
    } catch (ConcurrentUpdateException e) {
        return Response.status(Response.Status.CONFLICT).entity(e.getMessage()).build();
    }
}
Also used : Response(javax.ws.rs.core.Response) UserValidationException(nl.knaw.huygens.timbuctoo.v5.security.exceptions.UserValidationException) ImportManager(nl.knaw.huygens.timbuctoo.v5.dataset.ImportManager) User(nl.knaw.huygens.timbuctoo.v5.security.dto.User) QuadStore(nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.QuadStore) DataSet(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet) ImportStatus(nl.knaw.huygens.timbuctoo.v5.dataset.ImportStatus) IOException(java.io.IOException) ConcurrentUpdateException(nl.knaw.huygens.timbuctoo.v5.jsonldimport.ConcurrentUpdateException) PUT(javax.ws.rs.PUT)

Example 14 with QuadStore

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

the class SummaryPropDataRetrieverTest method createSummaryPropertyWalksTheUserConfiguredPathFirst.

@Test
public void createSummaryPropertyWalksTheUserConfiguredPathFirst() throws JsonProcessingException {
    List<SummaryProp> defaultProperties = Lists.newArrayList(summaryPropertyWithPath("http://example.org/path"));
    SummaryPropDataRetriever instance = new SummaryPropDataRetriever("http://example.org/userConfigured", defaultProperties);
    SummaryProp summaryProp = summaryPropertyWithPath("http://example.org/userPath");
    CursorQuad collectionQuad = quadWithObject("http://example.org/collection", Optional.empty());
    CursorQuad userConfiguredSummaryProp = quadWithObject(OBJECT_MAPPER.writeValueAsString(summaryProp), Optional.empty());
    QuadStore quadStore = mock(QuadStore.class);
    given(quadStore.getQuads("http://example.org/source", RdfConstants.RDF_TYPE, Direction.OUT, "")).willReturn(Stream.of(collectionQuad));
    given(quadStore.getQuads("http://example.org/collection", "http://example.org/userConfigured", Direction.OUT, "")).willReturn(Stream.of(userConfiguredSummaryProp));
    instance.createSummaryProperty(subjectWithUri("http://example.org/source"), dataSetWithQuadStore(quadStore));
    InOrder inOrder = inOrder(quadStore);
    // walk the path of the user configured SummaryProp
    inOrder.verify(quadStore).getQuads("http://example.org/source", "http://example.org/userPath", Direction.OUT, "");
    // walk the path of the default SummaryProp
    inOrder.verify(quadStore).getQuads("http://example.org/source", "http://example.org/path", 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 15 with QuadStore

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

the class SummaryPropDataRetrieverTest method createSummaryPropertyStopsWhenTheFirstPartOfThePathCannotBeFound.

@Test
public void createSummaryPropertyStopsWhenTheFirstPartOfThePathCannotBeFound() {
    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);
    given(quadStore.getQuads("http://example.org/source", "http://example.org/path", Direction.OUT, "")).willReturn(Stream.empty());
    instance.createSummaryProperty(subjectWithUri("http://example.org/source"), dataSetWithQuadStore(quadStore));
    verify(quadStore).getQuads("http://example.org/source", "http://example.org/path", Direction.OUT, "");
    verify(quadStore, never()).getQuads(anyString(), eq("http://example.org/path2"), eq(Direction.OUT), eq(""));
}
Also used : QuadStore(nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.QuadStore) SummaryProp(nl.knaw.huygens.timbuctoo.v5.graphql.defaultconfiguration.SummaryProp) Test(org.junit.Test)

Aggregations

QuadStore (nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.QuadStore)16 CursorQuad (nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.CursorQuad)11 Test (org.junit.Test)11 SummaryProp (nl.knaw.huygens.timbuctoo.v5.graphql.defaultconfiguration.SummaryProp)8 DataSet (nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet)5 TypedValue (nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.TypedValue)5 IOException (java.io.IOException)4 List (java.util.List)4 Direction (nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.Direction)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 DocumentLoader (com.github.jsonldjava.core.DocumentLoader)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Optional (java.util.Optional)3 Stream (java.util.stream.Stream)3 SubjectReference (nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.SubjectReference)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2