use of org.apache.rya.streams.api.entity.StreamsQuery in project incubator-rya by apache.
the class ListQueriesCommand method execute.
@Override
public void execute(final String[] args) throws ArgumentsException, ExecutionException {
requireNonNull(args);
// Parse the command line arguments.
final KafkaParameters params = new KafkaParameters();
try {
new JCommander(params, args);
} catch (final ParameterException e) {
throw new ArgumentsException("Could not list the queries because of invalid command line parameters.", e);
}
// Create the Kafka backed QueryChangeLog.
final String bootstrapServers = params.kafkaIP + ":" + params.kafkaPort;
final String topic = KafkaTopics.queryChangeLogTopic(params.ryaInstance);
final QueryChangeLog queryChangeLog = KafkaQueryChangeLogFactory.make(bootstrapServers, topic);
// The ListQueries command doesn't use the scheduled service feature.
final Scheduler scheduler = Scheduler.newFixedRateSchedule(0L, 5, TimeUnit.SECONDS);
final QueryRepository queryRepo = new InMemoryQueryRepository(queryChangeLog, scheduler);
// Execute the list queries command.
try {
final ListQueries listQueries = new DefaultListQueries(queryRepo);
try {
final Set<StreamsQuery> queries = listQueries.all();
System.out.println(formatQueries(queries));
} catch (final RyaStreamsException e) {
System.err.println("Unable to retrieve the queries.");
e.printStackTrace();
System.exit(1);
}
} catch (final Exception e) {
System.err.println("Problem encountered while closing the QueryRepository.");
e.printStackTrace();
System.exit(1);
}
}
use of org.apache.rya.streams.api.entity.StreamsQuery in project incubator-rya by apache.
the class DeleteQueryCommandIT method longParams.
@Test
public void longParams() throws Exception {
// Add a few queries to Rya Streams.
queryRepo.add("query1", true, true);
final UUID query2Id = queryRepo.add("query2", false, true).getQueryId();
queryRepo.add("query3", true, false);
// Show that all three of the queries were added.
Set<StreamsQuery> queries = queryRepo.list();
assertEquals(3, queries.size());
// Delete query 2 using the delete query command.
final String[] deleteArgs = new String[] { "--ryaInstance", "" + ryaInstance, "--kafkaHostname", kafka.getKafkaHostname(), "--kafkaPort", kafka.getKafkaPort(), "--queryID", query2Id.toString() };
final DeleteQueryCommand deleteCommand = new DeleteQueryCommand();
deleteCommand.execute(deleteArgs);
// Show query2 was deleted.
queries = queryRepo.list();
assertEquals(2, queries.size());
for (final StreamsQuery query : queries) {
assertNotEquals(query2Id, query.getQueryId());
}
}
use of org.apache.rya.streams.api.entity.StreamsQuery in project incubator-rya by apache.
the class InMemoryQueryRepository method updateCache.
/**
* Updates the {@link #queriesCache} to reflect the latest position within the {@link #changeLog}.
*/
private void updateCache() {
log.trace("updateCache() - Enter");
CloseableIteration<ChangeLogEntry<QueryChange>, QueryChangeLogException> it = null;
try {
// Iterate over everything since the last position that was handled within the change log.
log.debug("Starting cache position:" + cachePosition);
if (cachePosition.isPresent()) {
it = changeLog.readFromPosition(cachePosition.get() + 1);
} else {
it = changeLog.readFromStart();
}
// Apply each change to the cache.
while (it.hasNext()) {
final ChangeLogEntry<QueryChange> entry = it.next();
final QueryChange change = entry.getEntry();
final UUID queryId = change.getQueryId();
log.debug("Updating the cache to reflect:\n" + change);
switch(change.getChangeType()) {
case CREATE:
final StreamsQuery query = new StreamsQuery(queryId, change.getSparql().get(), change.getIsActive().get(), change.getIsInsert().get());
queriesCache.put(queryId, query);
break;
case UPDATE:
if (queriesCache.containsKey(queryId)) {
final StreamsQuery old = queriesCache.get(queryId);
final StreamsQuery updated = new StreamsQuery(old.getQueryId(), old.getSparql(), change.getIsActive().get(), old.isInsert());
queriesCache.put(queryId, updated);
}
break;
case DELETE:
queriesCache.remove(queryId);
break;
}
log.debug("Notifying listeners with the updated state.");
final Optional<StreamsQuery> newQueryState = Optional.ofNullable(queriesCache.get(queryId));
listeners.forEach(listener -> listener.notify(entry, newQueryState));
cachePosition = Optional.of(entry.getPosition());
log.debug("New chache position: " + cachePosition);
}
} catch (final QueryChangeLogException e) {
// Rethrow the exception because the object the supplier tried to create could not be created.
throw new RuntimeException("Could not update the cache of " + InMemoryQueryRepository.class.getName(), e);
} finally {
// Try to close the iteration if it was opened.
try {
if (it != null) {
it.close();
}
} catch (final QueryChangeLogException e) {
log.error("Could not close the " + CloseableIteration.class.getName(), e);
}
log.trace("updateCache() - Exit");
}
}
use of org.apache.rya.streams.api.entity.StreamsQuery in project incubator-rya by apache.
the class InMemoryQueryRepositoryTest method updateListenerNotify_multiClient.
@Test
public void updateListenerNotify_multiClient() throws Exception {
// Setup a totally in memory QueryRepository.
final QueryChangeLog changeLog = new InMemoryQueryChangeLog();
final QueryRepository queries = new InMemoryQueryRepository(changeLog, SCHEDULE);
final QueryRepository queries2 = new InMemoryQueryRepository(changeLog, SCHEDULE);
try {
queries.startAndWait();
queries2.startAndWait();
// show listener on repo that query was added to is being notified of the new query.
final CountDownLatch repo1Latch = new CountDownLatch(1);
queries.subscribe((queryChangeEvent, newQueryState) -> {
final ChangeLogEntry<QueryChange> expected = new ChangeLogEntry<>(0L, QueryChange.create(queryChangeEvent.getEntry().getQueryId(), "query 2", true, false));
final Optional<StreamsQuery> expectedQueryState = Optional.of(new StreamsQuery(queryChangeEvent.getEntry().getQueryId(), "query 2", true, false));
assertEquals(expected, queryChangeEvent);
assertEquals(expectedQueryState, newQueryState);
repo1Latch.countDown();
});
// show listener not on the repo that query was added to is being notified as well.
final CountDownLatch repo2Latch = new CountDownLatch(1);
queries2.subscribe((queryChangeEvent, newQueryState) -> {
final ChangeLogEntry<QueryChange> expected = new ChangeLogEntry<>(0L, QueryChange.create(queryChangeEvent.getEntry().getQueryId(), "query 2", true, false));
final Optional<StreamsQuery> expectedQueryState = Optional.of(new StreamsQuery(queryChangeEvent.getEntry().getQueryId(), "query 2", true, false));
assertEquals(expected, queryChangeEvent);
assertEquals(expectedQueryState, newQueryState);
repo2Latch.countDown();
});
queries.add("query 2", true, false);
assertTrue(repo1Latch.await(5, TimeUnit.SECONDS));
assertTrue(repo2Latch.await(5, TimeUnit.SECONDS));
} catch (final InterruptedException e) {
} finally {
queries.stop();
queries2.stop();
}
}
use of org.apache.rya.streams.api.entity.StreamsQuery in project incubator-rya by apache.
the class InMemoryQueryRepositoryTest method update.
@Test
public void update() throws Exception {
// Setup a totally in memory QueryRepository.
final QueryRepository queries = new InMemoryQueryRepository(new InMemoryQueryChangeLog(), SCHEDULE);
// Add a query to it.
final StreamsQuery query = queries.add("query 1", true, false);
// Change the isActive state of that query.
queries.updateIsActive(query.getQueryId(), false);
// Show the fetched query matches the expected one.
final Optional<StreamsQuery> fetched = queries.get(query.getQueryId());
final StreamsQuery expected = new StreamsQuery(query.getQueryId(), query.getSparql(), false, false);
assertEquals(expected, fetched.get());
}
Aggregations