Search in sources :

Example 1 with RyaDetailsMutator

use of org.apache.rya.api.instance.RyaDetailsUpdater.RyaDetailsMutator in project incubator-rya by apache.

the class AccumuloCreatePCJ method createPCJ.

@Override
public String createPCJ(final String instanceName, final String sparql, Set<ExportStrategy> strategies) throws InstanceDoesNotExistException, RyaClientException {
    requireNonNull(instanceName);
    requireNonNull(sparql);
    final Optional<RyaDetails> ryaDetailsHolder = getInstanceDetails.getDetails(instanceName);
    final boolean ryaInstanceExists = ryaDetailsHolder.isPresent();
    if (!ryaInstanceExists) {
        throw new InstanceDoesNotExistException(String.format("The '%s' instance of Rya does not exist.", instanceName));
    }
    final PCJIndexDetails pcjIndexDetails = ryaDetailsHolder.get().getPCJIndexDetails();
    final boolean pcjIndexingEnabeld = pcjIndexDetails.isEnabled();
    if (!pcjIndexingEnabeld) {
        throw new RyaClientException(String.format("The '%s' instance of Rya does not have PCJ Indexing enabled.", instanceName));
    }
    // Create the PCJ table that will receive the index results.
    final String pcjId;
    try (final PrecomputedJoinStorage pcjStorage = new AccumuloPcjStorage(getConnector(), instanceName)) {
        pcjId = pcjStorage.createPcj(sparql);
        // If a Fluo application is being used, task it with updating the PCJ.
        final Optional<FluoDetails> fluoDetailsHolder = pcjIndexDetails.getFluoDetails();
        if (fluoDetailsHolder.isPresent()) {
            final String fluoAppName = fluoDetailsHolder.get().getUpdateAppName();
            try {
                updateFluoApp(instanceName, fluoAppName, pcjId, sparql, strategies);
            } catch (RepositoryException | MalformedQueryException | SailException | QueryEvaluationException | PcjException | RyaDAOException e) {
                throw new RyaClientException("Problem while initializing the Fluo application with the new PCJ.", e);
            } catch (UnsupportedQueryException e) {
                throw new RyaClientException("The new PCJ could not be initialized because it either contains an unsupported query node " + "or an invalid ExportStrategy for the given QueryType.  Projection queries can be exported to either Rya or Kafka," + "unless they contain an aggregation, in which case they can only be exported to Kafka.  Construct queries can be exported" + "to Rya and Kafka, and Periodic queries can only be exported to Rya.");
            }
            // Update the Rya Details to indicate the PCJ is being updated incrementally.
            final RyaDetailsRepository detailsRepo = new AccumuloRyaInstanceDetailsRepository(getConnector(), instanceName);
            try {
                new RyaDetailsUpdater(detailsRepo).update(new RyaDetailsMutator() {

                    @Override
                    public RyaDetails mutate(final RyaDetails originalDetails) throws CouldNotApplyMutationException {
                        // Update the original PCJ Details to indicate they are incrementally updated.
                        final PCJDetails originalPCJDetails = originalDetails.getPCJIndexDetails().getPCJDetails().get(pcjId);
                        final PCJDetails.Builder mutatedPCJDetails = PCJDetails.builder(originalPCJDetails).setUpdateStrategy(PCJUpdateStrategy.INCREMENTAL);
                        // Replace the old PCJ Details with the updated ones.
                        final RyaDetails.Builder builder = RyaDetails.builder(originalDetails);
                        builder.getPCJIndexDetails().addPCJDetails(mutatedPCJDetails);
                        return builder.build();
                    }
                });
            } catch (RyaDetailsRepositoryException | CouldNotApplyMutationException e) {
                throw new RyaClientException("Problem while updating the Rya instance's Details to indicate the PCJ is being incrementally updated.", e);
            }
        }
        // Return the ID that was assigned to the PCJ.
        return pcjId;
    } catch (final PCJStorageException e) {
        throw new RyaClientException("Problem while initializing the PCJ table.", e);
    }
}
Also used : AccumuloPcjStorage(org.apache.rya.indexing.pcj.storage.accumulo.AccumuloPcjStorage) RyaDetailsUpdater(org.apache.rya.api.instance.RyaDetailsUpdater) PcjException(org.apache.rya.indexing.pcj.storage.PcjException) UnsupportedQueryException(org.apache.rya.indexing.pcj.fluo.app.query.UnsupportedQueryException) RyaDetails(org.apache.rya.api.instance.RyaDetails) AccumuloRyaInstanceDetailsRepository(org.apache.rya.accumulo.instance.AccumuloRyaInstanceDetailsRepository) PCJDetails(org.apache.rya.api.instance.RyaDetails.PCJIndexDetails.PCJDetails) PrecomputedJoinStorage(org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage) MalformedQueryException(org.openrdf.query.MalformedQueryException) RyaDetailsRepositoryException(org.apache.rya.api.instance.RyaDetailsRepository.RyaDetailsRepositoryException) FluoDetails(org.apache.rya.api.instance.RyaDetails.PCJIndexDetails.FluoDetails) PCJIndexDetails(org.apache.rya.api.instance.RyaDetails.PCJIndexDetails) RyaClientException(org.apache.rya.api.client.RyaClientException) RyaDetailsMutator(org.apache.rya.api.instance.RyaDetailsUpdater.RyaDetailsMutator) CouldNotApplyMutationException(org.apache.rya.api.instance.RyaDetailsUpdater.RyaDetailsMutator.CouldNotApplyMutationException) RepositoryException(org.openrdf.repository.RepositoryException) RyaDetailsRepositoryException(org.apache.rya.api.instance.RyaDetailsRepository.RyaDetailsRepositoryException) InstanceDoesNotExistException(org.apache.rya.api.client.InstanceDoesNotExistException) SailException(org.openrdf.sail.SailException) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) RyaDAOException(org.apache.rya.api.persist.RyaDAOException) RyaDetailsRepository(org.apache.rya.api.instance.RyaDetailsRepository) PCJStorageException(org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage.PCJStorageException)

Example 2 with RyaDetailsMutator

use of org.apache.rya.api.instance.RyaDetailsUpdater.RyaDetailsMutator in project incubator-rya by apache.

the class MongoBatchUpdatePCJ method updatePCJMetadata.

private void updatePCJMetadata(final String ryaInstanceName, final String pcjId, final MongoClient client) throws RyaClientException {
    // Update the PCJ's metadata to indicate it was just batch updated.
    try {
        final RyaDetailsRepository detailsRepo = new MongoRyaInstanceDetailsRepository(client, ryaInstanceName);
        new RyaDetailsUpdater(detailsRepo).update(new RyaDetailsMutator() {

            @Override
            public RyaDetails mutate(final RyaDetails originalDetails) throws CouldNotApplyMutationException {
                // Update the original PCJ Details to indicate they were batch updated.
                final PCJDetails originalPCJDetails = originalDetails.getPCJIndexDetails().getPCJDetails().get(pcjId);
                final PCJDetails.Builder mutatedPCJDetails = PCJDetails.builder(originalPCJDetails).setUpdateStrategy(PCJUpdateStrategy.BATCH).setLastUpdateTime(new Date());
                // Replace the old PCJ Details with the updated ones.
                final RyaDetails.Builder builder = RyaDetails.builder(originalDetails);
                builder.getPCJIndexDetails().addPCJDetails(mutatedPCJDetails);
                return builder.build();
            }
        });
    } catch (final RyaDetailsRepositoryException | CouldNotApplyMutationException e) {
        throw new RyaClientException("Could not update the PCJ's metadata.", e);
    }
}
Also used : RyaClientException(org.apache.rya.api.client.RyaClientException) RyaDetailsUpdater(org.apache.rya.api.instance.RyaDetailsUpdater) RyaDetailsMutator(org.apache.rya.api.instance.RyaDetailsUpdater.RyaDetailsMutator) CouldNotApplyMutationException(org.apache.rya.api.instance.RyaDetailsUpdater.RyaDetailsMutator.CouldNotApplyMutationException) MongoRyaInstanceDetailsRepository(org.apache.rya.mongodb.instance.MongoRyaInstanceDetailsRepository) RyaDetails(org.apache.rya.api.instance.RyaDetails) Date(java.util.Date) PCJDetails(org.apache.rya.api.instance.RyaDetails.PCJIndexDetails.PCJDetails) RyaDetailsRepository(org.apache.rya.api.instance.RyaDetailsRepository) RyaDetailsRepositoryException(org.apache.rya.api.instance.RyaDetailsRepository.RyaDetailsRepositoryException)

Example 3 with RyaDetailsMutator

use of org.apache.rya.api.instance.RyaDetailsUpdater.RyaDetailsMutator in project incubator-rya by apache.

the class AccumuloBatchUpdatePCJ method updatePCJMetadata.

private void updatePCJMetadata(final String ryaInstanceName, final String pcjId) throws RyaClientException {
    // Update the PCJ's metadata to indicate it was just batch updated.
    try {
        final RyaDetailsRepository detailsRepo = new AccumuloRyaInstanceDetailsRepository(super.getConnector(), ryaInstanceName);
        new RyaDetailsUpdater(detailsRepo).update(new RyaDetailsMutator() {

            @Override
            public RyaDetails mutate(final RyaDetails originalDetails) throws CouldNotApplyMutationException {
                // Update the original PCJ Details to indicate they were batch updated.
                final PCJDetails originalPCJDetails = originalDetails.getPCJIndexDetails().getPCJDetails().get(pcjId);
                final PCJDetails.Builder mutatedPCJDetails = PCJDetails.builder(originalPCJDetails).setUpdateStrategy(PCJUpdateStrategy.BATCH).setLastUpdateTime(new Date());
                // Replace the old PCJ Details with the updated ones.
                final RyaDetails.Builder builder = RyaDetails.builder(originalDetails);
                builder.getPCJIndexDetails().addPCJDetails(mutatedPCJDetails);
                return builder.build();
            }
        });
    } catch (final RyaDetailsRepositoryException | CouldNotApplyMutationException e) {
        throw new RyaClientException("Could not update the PCJ's metadata.", e);
    }
}
Also used : RyaClientException(org.apache.rya.api.client.RyaClientException) RyaDetailsUpdater(org.apache.rya.api.instance.RyaDetailsUpdater) RyaDetailsMutator(org.apache.rya.api.instance.RyaDetailsUpdater.RyaDetailsMutator) CouldNotApplyMutationException(org.apache.rya.api.instance.RyaDetailsUpdater.RyaDetailsMutator.CouldNotApplyMutationException) RyaDetails(org.apache.rya.api.instance.RyaDetails) AccumuloRyaInstanceDetailsRepository(org.apache.rya.accumulo.instance.AccumuloRyaInstanceDetailsRepository) Date(java.util.Date) PCJDetails(org.apache.rya.api.instance.RyaDetails.PCJIndexDetails.PCJDetails) RyaDetailsRepository(org.apache.rya.api.instance.RyaDetailsRepository) RyaDetailsRepositoryException(org.apache.rya.api.instance.RyaDetailsRepository.RyaDetailsRepositoryException)

Example 4 with RyaDetailsMutator

use of org.apache.rya.api.instance.RyaDetailsUpdater.RyaDetailsMutator in project incubator-rya by apache.

the class RyaDetailsUpdaterTest method update_concurrentUpdateEncountered.

@Test
public void update_concurrentUpdateEncountered() throws NotInitializedException, RyaDetailsRepositoryException, CouldNotApplyMutationException {
    // Setup initial details and mock a repository that returns them.
    final RyaDetails originalDetails = RyaDetails.builder().setRyaInstanceName("instanceName").setRyaVersion("0.0.0.0").setFreeTextDetails(new FreeTextIndexDetails(true)).setEntityCentricIndexDetails(new EntityCentricIndexDetails(true)).setTemporalIndexDetails(new TemporalIndexDetails(true)).setPCJIndexDetails(PCJIndexDetails.builder().setEnabled(true)).setJoinSelectivityDetails(new JoinSelectivityDetails(Optional.<Date>absent())).setProspectorDetails(new ProspectorDetails(Optional.<Date>absent())).build();
    final RyaDetails otherUsersUpdate = RyaDetails.builder(originalDetails).setRyaVersion("1.1.1.1").build();
    // The first time get detail is called, we get the original state for the details.
    // When the mutator tries to update using those, it throws an exception.
    // The second iteration, the other user's state is updated.
    // When the mutator tries to update again, it succeeds.
    final RyaDetailsRepository detailsRepo = mock(RyaDetailsRepository.class);
    when(detailsRepo.getRyaInstanceDetails()).thenReturn(originalDetails).thenReturn(otherUsersUpdate);
    doThrow(ConcurrentUpdateException.class).when(detailsRepo).update(eq(originalDetails), any(RyaDetails.class));
    // Run the test.
    new RyaDetailsUpdater(detailsRepo).update(new RyaDetailsMutator() {

        @Override
        public RyaDetails mutate(final RyaDetails old) {
            return RyaDetails.builder(old).setTemporalIndexDetails(new TemporalIndexDetails(false)).build();
        }
    });
    // Verify the intended mutation eventually gets committed.
    final RyaDetails finalDetails = RyaDetails.builder(originalDetails).setRyaVersion("1.1.1.1").setTemporalIndexDetails(new TemporalIndexDetails(false)).build();
    verify(detailsRepo, times(1)).update(eq(otherUsersUpdate), eq(finalDetails));
}
Also used : ProspectorDetails(org.apache.rya.api.instance.RyaDetails.ProspectorDetails) EntityCentricIndexDetails(org.apache.rya.api.instance.RyaDetails.EntityCentricIndexDetails) TemporalIndexDetails(org.apache.rya.api.instance.RyaDetails.TemporalIndexDetails) RyaDetailsMutator(org.apache.rya.api.instance.RyaDetailsUpdater.RyaDetailsMutator) FreeTextIndexDetails(org.apache.rya.api.instance.RyaDetails.FreeTextIndexDetails) JoinSelectivityDetails(org.apache.rya.api.instance.RyaDetails.JoinSelectivityDetails) Date(java.util.Date) Test(org.junit.Test)

Example 5 with RyaDetailsMutator

use of org.apache.rya.api.instance.RyaDetailsUpdater.RyaDetailsMutator in project incubator-rya by apache.

the class RyaDetailsUpdaterTest method update.

@Test
public void update() throws RyaDetailsRepositoryException, CouldNotApplyMutationException {
    // Setup initial details and mock a repository that returns them.
    final RyaDetails originalDetails = RyaDetails.builder().setRyaInstanceName("instanceName").setRyaVersion("0.0.0.0").setFreeTextDetails(new FreeTextIndexDetails(true)).setEntityCentricIndexDetails(new EntityCentricIndexDetails(true)).setTemporalIndexDetails(new TemporalIndexDetails(true)).setPCJIndexDetails(PCJIndexDetails.builder().setEnabled(true)).setJoinSelectivityDetails(new JoinSelectivityDetails(Optional.<Date>absent())).setProspectorDetails(new ProspectorDetails(Optional.<Date>absent())).build();
    final RyaDetailsRepository detailsRepo = mock(RyaDetailsRepository.class);
    when(detailsRepo.getRyaInstanceDetails()).thenReturn(originalDetails);
    // Use an updater to change the Rya version number.
    new RyaDetailsUpdater(detailsRepo).update(new RyaDetailsMutator() {

        @Override
        public RyaDetails mutate(final RyaDetails old) {
            return RyaDetails.builder(old).setRyaVersion("1.1.1.1").build();
        }
    });
    // Verify the repository was asked to update the details.
    final RyaDetails mutatedDetails = RyaDetails.builder(originalDetails).setRyaVersion("1.1.1.1").build();
    verify(detailsRepo, times(1)).update(eq(originalDetails), eq(mutatedDetails));
}
Also used : ProspectorDetails(org.apache.rya.api.instance.RyaDetails.ProspectorDetails) EntityCentricIndexDetails(org.apache.rya.api.instance.RyaDetails.EntityCentricIndexDetails) TemporalIndexDetails(org.apache.rya.api.instance.RyaDetails.TemporalIndexDetails) RyaDetailsMutator(org.apache.rya.api.instance.RyaDetailsUpdater.RyaDetailsMutator) FreeTextIndexDetails(org.apache.rya.api.instance.RyaDetails.FreeTextIndexDetails) JoinSelectivityDetails(org.apache.rya.api.instance.RyaDetails.JoinSelectivityDetails) Date(java.util.Date) Test(org.junit.Test)

Aggregations

RyaDetailsMutator (org.apache.rya.api.instance.RyaDetailsUpdater.RyaDetailsMutator)5 Date (java.util.Date)4 RyaClientException (org.apache.rya.api.client.RyaClientException)3 RyaDetails (org.apache.rya.api.instance.RyaDetails)3 PCJDetails (org.apache.rya.api.instance.RyaDetails.PCJIndexDetails.PCJDetails)3 RyaDetailsRepository (org.apache.rya.api.instance.RyaDetailsRepository)3 RyaDetailsRepositoryException (org.apache.rya.api.instance.RyaDetailsRepository.RyaDetailsRepositoryException)3 RyaDetailsUpdater (org.apache.rya.api.instance.RyaDetailsUpdater)3 CouldNotApplyMutationException (org.apache.rya.api.instance.RyaDetailsUpdater.RyaDetailsMutator.CouldNotApplyMutationException)3 AccumuloRyaInstanceDetailsRepository (org.apache.rya.accumulo.instance.AccumuloRyaInstanceDetailsRepository)2 EntityCentricIndexDetails (org.apache.rya.api.instance.RyaDetails.EntityCentricIndexDetails)2 FreeTextIndexDetails (org.apache.rya.api.instance.RyaDetails.FreeTextIndexDetails)2 JoinSelectivityDetails (org.apache.rya.api.instance.RyaDetails.JoinSelectivityDetails)2 ProspectorDetails (org.apache.rya.api.instance.RyaDetails.ProspectorDetails)2 TemporalIndexDetails (org.apache.rya.api.instance.RyaDetails.TemporalIndexDetails)2 Test (org.junit.Test)2 InstanceDoesNotExistException (org.apache.rya.api.client.InstanceDoesNotExistException)1 PCJIndexDetails (org.apache.rya.api.instance.RyaDetails.PCJIndexDetails)1 FluoDetails (org.apache.rya.api.instance.RyaDetails.PCJIndexDetails.FluoDetails)1 RyaDAOException (org.apache.rya.api.persist.RyaDAOException)1