use of com.datastax.driver.core.ResultSet in project FlareBot by FlareBot.
the class FlareBot method loadFutureTasks.
private void loadFutureTasks() {
if (FlareBot.testBot)
return;
final int[] loaded = { 0 };
CassandraController.runTask(session -> {
ResultSet set = session.execute("SELECT * FROM flarebot.future_tasks");
Row row;
while ((row = set.one()) != null) {
FutureAction fa = new FutureAction(row.getLong("guild_id"), row.getLong("channel_id"), row.getLong("responsible"), row.getLong("target"), row.getString("content"), new DateTime(row.getTimestamp("expires_at")), new DateTime(row.getTimestamp("created_at")), FutureAction.Action.valueOf(row.getString("action").toUpperCase()));
try {
if (new DateTime().isAfter(fa.getExpires()))
fa.execute();
else {
fa.queue();
loaded[0]++;
}
} catch (NullPointerException e) {
LOGGER.error("Failed to execute/queue future task" + "\nAction: " + fa.getAction() + "\nResponsible: " + fa.getResponsible() + "\nTarget: " + fa.getTarget() + "\nContent: " + fa.getContent(), e);
}
}
});
LOGGER.info("Loaded " + loaded[0] + " future tasks");
}
use of com.datastax.driver.core.ResultSet in project FlareBot by FlareBot.
the class FlareBotManager method savePlaylist.
public void savePlaylist(Command command, TextChannel channel, String ownerId, boolean overwriteAllowed, String name, List<String> songs) {
CassandraController.runTask(session -> {
if (savePlaylistStatement == null)
savePlaylistStatement = session.prepare("SELECT * FROM flarebot.playlist " + "WHERE playlist_name = ? AND guild_id = ?");
ResultSet set = session.execute(savePlaylistStatement.bind().setString(0, name).setString(1, channel.getGuild().getId()));
if (set.one() != null) {
if (ConfirmUtil.checkExists(ownerId, command.getClass())) {
MessageUtils.sendWarningMessage("Overwriting playlist!", channel);
} else if (!overwriteAllowed) {
MessageUtils.sendErrorMessage("That name is already taken! You need the `flarebot.queue.save.overwrite` permission to overwrite", channel);
return;
} else {
MessageUtils.sendErrorMessage("That name is already taken! Do this again within 1 minute to overwrite!", channel);
ConfirmUtil.pushAction(ownerId, new RunnableWrapper(Runnables.doNothing(), command.getClass()));
return;
}
}
if (insertPlaylistStatement == null)
insertPlaylistStatement = session.prepare("INSERT INTO flarebot.playlist" + " (playlist_name, guild_id, owner, songs, scope, times_played) VALUES (?, ?, ?, ?, ?, ?)");
session.execute(insertPlaylistStatement.bind().setString(0, name).setString(1, channel.getGuild().getId()).setString(2, ownerId).setList(3, songs).setString(4, "local").setInt(5, 0));
channel.sendMessage(MessageUtils.getEmbed(Getters.getUserById(ownerId)).setDescription("Successfully saved the playlist '" + MessageUtils.escapeMarkdown(name) + "'").build()).queue();
});
}
use of com.datastax.driver.core.ResultSet in project apex-malhar by apache.
the class AbstractUpsertOutputOperatorCodecsTest method testForCollectionRemoval.
@Test
public void testForCollectionRemoval() throws Exception {
User aUser = new User();
String userId = "user" + System.currentTimeMillis();
aUser.setUserid(userId);
FullName fullName = new FullName("first12" + System.currentTimeMillis(), "last12" + System.currentTimeMillis());
aUser.setUsername(fullName);
Set<String> emails = new HashSet<>();
emails.add(new String("1"));
emails.add(new String("2"));
aUser.setEmails(emails);
UpsertExecutionContext<User> originalEntry = new UpsertExecutionContext<>();
originalEntry.setPayload(aUser);
UpsertExecutionContext<User> subsequentUpdateForEmails = new UpsertExecutionContext<>();
subsequentUpdateForEmails.setCollectionMutationStyle(UpsertExecutionContext.CollectionMutationStyle.REMOVE_FROM_EXISTING_COLLECTION);
subsequentUpdateForEmails.setNullHandlingMutationStyle(UpsertExecutionContext.NullHandlingMutationStyle.IGNORE_NULL_COLUMNS);
User oldUser = new User();
oldUser.setUserid(userId);
Set<String> updatedEmails = new HashSet<>();
updatedEmails.add(new String("1"));
oldUser.setEmails(updatedEmails);
subsequentUpdateForEmails.setPayload(oldUser);
userUpsertOperator.beginWindow(3);
userUpsertOperator.input.process(originalEntry);
userUpsertOperator.input.process(subsequentUpdateForEmails);
userUpsertOperator.endWindow();
ResultSet results = userUpsertOperator.session.execute("SELECT * FROM unittests.users WHERE userid = '" + userId + "'");
List<Row> rows = results.all();
Row userRow = rows.get(0);
Set<String> existingEmailsEntry = userRow.getSet("emails", String.class);
assertEquals(1, existingEmailsEntry.size());
assertEquals("" + 2, "" + existingEmailsEntry.iterator().next());
}
use of com.datastax.driver.core.ResultSet in project apex-malhar by apache.
the class AbstractUpsertOutputOperatorCodecsTest method testForSingleRowInsertWithOverridingConsistency.
@Test
public void testForSingleRowInsertWithOverridingConsistency() throws Exception {
User aUser = new User();
aUser.setUserid("userWithConsistency" + System.currentTimeMillis());
FullName fullName = new FullName("first" + System.currentTimeMillis(), "last" + System.currentTimeMillis());
aUser.setUsername(fullName);
Address address = new Address("city21", "Street31", 12, null);
aUser.setCurrentaddress(address);
UpsertExecutionContext<User> anUpdate = new UpsertExecutionContext<>();
anUpdate.setOverridingConsistencyLevel(ConsistencyLevel.LOCAL_SERIAL);
anUpdate.setPayload(aUser);
userUpsertOperator.beginWindow(8);
userUpsertOperator.input.process(anUpdate);
userUpsertOperator.endWindow();
ResultSet results = userUpsertOperator.session.execute("SELECT * FROM unittests.users WHERE userid = '" + aUser.getUserid() + "'");
List<Row> rows = results.all();
assertEquals(rows.size(), 1);
assertTrue(results.isExhausted());
}
use of com.datastax.driver.core.ResultSet in project apex-malhar by apache.
the class AbstractUpsertOutputOperatorCodecsTest method testForListAppendAndIfExists.
@Test
public void testForListAppendAndIfExists() throws Exception {
User aUser = new User();
String userId = "user" + System.currentTimeMillis();
aUser.setUserid(userId);
FullName fullName = new FullName("first" + System.currentTimeMillis(), "last" + System.currentTimeMillis());
aUser.setUsername(fullName);
Address address = new Address("street1", "city1", 13, null);
aUser.setCurrentaddress(address);
Set<String> emails = new HashSet<>();
emails.add(new String("1"));
emails.add(new String("2"));
aUser.setEmails(emails);
List<Integer> topScores = new ArrayList<>();
topScores.add(1);
topScores.add(2);
aUser.setTopScores(topScores);
UpsertExecutionContext<User> originalEntry = new UpsertExecutionContext<>();
originalEntry.setPayload(aUser);
UpsertExecutionContext<User> subsequentUpdateForTopScores = new UpsertExecutionContext<>();
subsequentUpdateForTopScores.setListPlacementStyle(UpsertExecutionContext.ListPlacementStyle.APPEND_TO_EXISTING_LIST);
subsequentUpdateForTopScores.setCollectionMutationStyle(UpsertExecutionContext.CollectionMutationStyle.ADD_TO_EXISTING_COLLECTION);
subsequentUpdateForTopScores.setNullHandlingMutationStyle(UpsertExecutionContext.NullHandlingMutationStyle.IGNORE_NULL_COLUMNS);
subsequentUpdateForTopScores.setUpdateOnlyIfPrimaryKeyExists(true);
User oldUser = new User();
oldUser.setUserid(userId + System.currentTimeMillis());
List<Integer> topScoresAppended = new ArrayList<>();
topScoresAppended.add(3);
oldUser.setTopScores(topScoresAppended);
subsequentUpdateForTopScores.setPayload(oldUser);
userUpsertOperator.beginWindow(5);
userUpsertOperator.input.process(originalEntry);
userUpsertOperator.input.process(subsequentUpdateForTopScores);
userUpsertOperator.endWindow();
ResultSet results = userUpsertOperator.session.execute("SELECT * FROM unittests.users WHERE userid = '" + userId + "'");
List<Row> rows = results.all();
Row userRow = rows.get(0);
List<Integer> topScoresEntry = userRow.getList("top_scores", Integer.class);
assertEquals(2, topScoresEntry.size());
assertEquals("" + 2, "" + topScoresEntry.get(1));
}
Aggregations