Search in sources :

Example 31 with ResultSet

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");
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) Row(com.datastax.driver.core.Row) LocalDateTime(java.time.LocalDateTime) DateTime(org.joda.time.DateTime) FutureAction(stream.flarebot.flarebot.scheduler.FutureAction)

Example 32 with ResultSet

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();
    });
}
Also used : RunnableWrapper(stream.flarebot.flarebot.util.objects.RunnableWrapper) ResultSet(com.datastax.driver.core.ResultSet)

Example 33 with ResultSet

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());
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) Row(com.datastax.driver.core.Row) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 34 with ResultSet

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());
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) Row(com.datastax.driver.core.Row) Test(org.junit.Test)

Example 35 with ResultSet

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));
}
Also used : ArrayList(java.util.ArrayList) ResultSet(com.datastax.driver.core.ResultSet) Row(com.datastax.driver.core.Row) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

ResultSet (com.datastax.driver.core.ResultSet)95 Row (com.datastax.driver.core.Row)61 Test (org.junit.Test)43 ArrayList (java.util.ArrayList)17 BoundStatement (com.datastax.driver.core.BoundStatement)13 Session (com.datastax.driver.core.Session)12 Cluster (com.datastax.driver.core.Cluster)10 Statement (com.datastax.driver.core.Statement)8 List (java.util.List)8 PreparedStatement (com.datastax.driver.core.PreparedStatement)7 ByteBuffer (java.nio.ByteBuffer)5 StaticBuffer (org.janusgraph.diskstorage.StaticBuffer)5 BatchStatement (com.datastax.driver.core.BatchStatement)4 ResultSetFuture (com.datastax.driver.core.ResultSetFuture)4 Select (com.datastax.driver.core.querybuilder.Select)4 Tuple (io.vavr.Tuple)4 IOException (java.io.IOException)4 HashSet (java.util.HashSet)4 TypeHint (org.apache.flink.api.common.typeinfo.TypeHint)4 Entry (org.janusgraph.diskstorage.Entry)4