Ejemplo n.º 1
0
  @Test
  public void should_check_for_common_operation_on_found_clustered_entity_by_iterator()
      throws Exception {
    long partitionKey = RandomUtils.nextLong(0, Long.MAX_VALUE);
    insertClusteredValues(partitionKey, 1, "name1", 1);

    Iterator<ClusteredEntity> iter =
        manager
            .sliceQuery(ClusteredEntity.class)
            .forIteration()
            .withPartitionComponents(partitionKey)
            .iterator();

    iter.hasNext();
    ClusteredEntity clusteredEntity = iter.next();

    // Check for update
    clusteredEntity.setValue("dirty");
    manager.update(clusteredEntity);

    ClusteredEntity check = manager.find(ClusteredEntity.class, clusteredEntity.getId());
    assertThat(check.getValue()).isEqualTo("dirty");

    // Check for refresh
    check.setValue("dirty_again");
    manager.update(check);

    manager.refresh(clusteredEntity);
    assertThat(clusteredEntity.getValue()).isEqualTo("dirty_again");

    // Check for remove
    manager.delete(clusteredEntity);
    assertThat(manager.find(ClusteredEntity.class, clusteredEntity.getId())).isNull();
  }
Ejemplo n.º 2
0
  @Test
  public void should_re_prepare_statements_when_cache_size_exceeded() throws Exception {
    // Given
    CompleteBean bean = builder().id(RandomUtils.nextLong(0, Long.MAX_VALUE)).name("name").buid();

    CompleteBean managed = pm.insert(bean);

    // When
    managed.setAge(10L);
    pm.update(managed);

    managed.setFriends(Arrays.asList("foo", "bar"));
    pm.update(managed);

    managed.setFollowers(Sets.newHashSet("George", "Paul"));
    pm.update(managed);

    managed.setAge(11L);
    pm.update(managed);

    // Then
    CompleteBean found = pm.find(CompleteBean.class, bean.getId());

    assertThat(found.getAge()).isEqualTo(11L);
    assertThat(found.getName()).isEqualTo("name");
    assertThat(found.getFriends()).containsExactly("foo", "bar");
    assertThat(found.getFollowers()).containsOnly("George", "Paul");
  }
Ejemplo n.º 3
0
  @Test
  public void should_iterate_with_custom_params() throws Exception {
    long partitionKey = RandomUtils.nextLong(0, Long.MAX_VALUE);
    insertClusteredValues(partitionKey, 1, "name1", 5);
    insertClusteredValues(partitionKey, 1, "name2", 5);

    Iterator<ClusteredEntity> iter =
        manager
            .sliceQuery(ClusteredEntity.class)
            .forIteration()
            .withPartitionComponents(partitionKey)
            .fromClusterings(1, "name13")
            .toClusterings(1, "name21")
            .iterator(2);

    assertThat(iter.hasNext()).isTrue();
    assertThat(iter.next().getValue()).isEqualTo("value13");

    assertThat(iter.hasNext()).isTrue();
    assertThat(iter.next().getValue()).isEqualTo("value14");

    assertThat(iter.hasNext()).isTrue();
    assertThat(iter.next().getValue()).isEqualTo("value15");

    assertThat(iter.hasNext()).isTrue();
    final ClusteredEntity next = iter.next();
    assertThat(next.getId().getName()).isEqualTo("name21");
    assertThat(next.getValue()).isEqualTo("value11");

    assertThat(iter.hasNext()).isFalse();
  }
Ejemplo n.º 4
0
  @Test
  public void should_iterate_with_partition_keys_and_partition_keys_IN_and_from_clusterings()
      throws Exception {
    // Given
    long partitionKey = RandomUtils.nextLong(0, Long.MAX_VALUE);

    insertCompositeClusteredValues(partitionKey, "bucket1", 1, "abc", 1);
    insertCompositeClusteredValues(partitionKey, "bucket2", 1, "name", 1);
    insertCompositeClusteredValues(partitionKey, "bucket3", 1, "name", 1);

    // When
    final Iterator<CompositeClusteredEntity> iterator =
        manager
            .sliceQuery(CompositeClusteredEntity.class)
            .forIteration()
            .withPartitionComponents(partitionKey)
            .andPartitionComponentsIN("bucket1", "bucket3")
            .fromClusterings(1, "name1")
            .iterator();

    // Then
    assertThat(iterator.hasNext()).isTrue();
    CompositeClusteredEntity next = iterator.next();
    assertThat(next.getId().getBucket()).isEqualTo("bucket3");
    assertThat(next.getId().getName()).isEqualTo("name1");
    assertThat(next.getValue()).isEqualTo("value11");
  }
  @Test
  public void should_dsl_select_slice_async() throws Exception {
    // Given
    final Map<String, Object> values = new HashMap<>();
    final long id = RandomUtils.nextLong(0L, Long.MAX_VALUE);
    values.put("id", id);
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
    dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
    final Date date1 = dateFormat.parse("2015-10-01 00:00:00 GMT");
    final Date date9 = dateFormat.parse("2015-10-09 00:00:00 GMT");
    values.put("date1", "'2015-10-01 00:00:00+0000'");
    values.put("date2", "'2015-10-02 00:00:00+0000'");
    values.put("date3", "'2015-10-03 00:00:00+0000'");
    values.put("date4", "'2015-10-04 00:00:00+0000'");
    values.put("date5", "'2015-10-05 00:00:00+0000'");
    values.put("date6", "'2015-10-06 00:00:00+0000'");
    values.put("date7", "'2015-10-07 00:00:00+0000'");
    values.put("date8", "'2015-10-08 00:00:00+0000'");
    values.put("date9", "'2015-10-09 00:00:00+0000'");
    scriptExecutor.executeScriptTemplate("SimpleEntity/insert_many_rows.cql", values);

    final CountDownLatch latch = new CountDownLatch(1);
    final CassandraLogAsserter logAsserter = new CassandraLogAsserter();
    logAsserter.prepareLogLevel(ASYNC_LOGGER_STRING, "%msg - [%thread]%n");

    // When
    final CompletableFuture<List<SimpleEntity>> future =
        manager
            .dsl()
            .select()
            .consistencyList()
            .simpleSet()
            .simpleMap()
            .value()
            .simpleMap()
            .fromBaseTable()
            .where()
            .id()
            .Eq(id)
            .date()
            .Gte_And_Lt(date1, date9)
            .withResultSetAsyncListener(
                rs -> {
                  LOGGER.info(CALLED);
                  latch.countDown();
                  return rs;
                })
            .withTracing()
            .getListAsync();

    // Then
    latch.await();
    assertThat(future.get()).hasSize(8);
    logAsserter.assertContains("Called - [achilles-default-executor");
  }
  @Test
  public void should_dsl_update_value_async() throws Exception {
    // Given
    final long id = RandomUtils.nextLong(0L, Long.MAX_VALUE);
    final Date date = buildDateKey();
    final AtomicBoolean success = new AtomicBoolean(false);
    scriptExecutor.executeScriptTemplate(
        "SimpleEntity/insert_single_row.cql", ImmutableMap.of("id", id, "table", "simple"));

    final CountDownLatch latch = new CountDownLatch(1);
    final CassandraLogAsserter logAsserter = new CassandraLogAsserter();
    logAsserter.prepareLogLevel(ASYNC_LOGGER_STRING, "%msg - [%thread]%n");

    // When
    manager
        .dsl()
        .update()
        .fromBaseTable()
        .value()
        .Set("new value")
        .where()
        .id()
        .Eq(id)
        .date()
        .Eq(date)
        .if_Value()
        .Eq("0 AM")
        .withLwtResultListener(
            new LWTResultListener() {
              @Override
              public void onSuccess() {
                success.getAndSet(true);
              }

              @Override
              public void onError(LWTResult lwtResult) {}
            })
        .withResultSetAsyncListener(
            rs -> {
              LOGGER.info(CALLED);
              latch.countDown();
              return rs;
            })
        .executeAsync();

    // Then
    latch.await();
    logAsserter.assertContains("Called - [achilles-default-executor");
  }
Ejemplo n.º 7
0
  @Test
  public void should_iterate_with_clustering_IN() throws Exception {
    // Given
    long partitionKey = RandomUtils.nextLong(0, Long.MAX_VALUE);
    insertClusteredValues(partitionKey, 1, "name1", 3);
    insertClusteredValues(partitionKey, 1, "name2", 2);
    insertClusteredValues(partitionKey, 1, "name3", 1);
    insertClusteredValues(partitionKey, 1, "name4", 1);

    // When
    final Iterator<ClusteredEntity> iterator =
        manager
            .sliceQuery(ClusteredEntity.class)
            .forIteration()
            .withPartitionComponents(partitionKey)
            .withClusterings(1)
            .andClusteringsIN("name11", "name12", "name13", "name41")
            .limit(100)
            .iterator(2);

    // Then
    assertThat(iterator.hasNext()).isTrue();
    ClusteredEntity next = iterator.next();
    assertThat(next.getId().getName()).isEqualTo("name11");
    assertThat(next.getValue()).isEqualTo("value11");

    assertThat(iterator.hasNext()).isTrue();
    next = iterator.next();
    assertThat(next.getId().getName()).isEqualTo("name12");
    assertThat(next.getValue()).isEqualTo("value12");

    assertThat(iterator.hasNext()).isTrue();
    next = iterator.next();
    assertThat(next.getId().getName()).isEqualTo("name13");
    assertThat(next.getValue()).isEqualTo("value13");

    assertThat(iterator.hasNext()).isTrue();
    next = iterator.next();
    assertThat(next.getId().getName()).isEqualTo("name41");
    assertThat(next.getValue()).isEqualTo("value11");

    assertThat(iterator.hasNext()).isFalse();
  }
Ejemplo n.º 8
0
  @Test
  public void should_iterate_over_clusterings_components() throws Exception {
    // Given
    long partitionKey = RandomUtils.nextLong(0, Long.MAX_VALUE);
    insertClusteredValues(partitionKey, 1, "name1", 3);
    insertClusteredValues(partitionKey, 2, "name2", 2);
    insertClusteredValues(partitionKey, 3, "name3", 1);
    insertClusteredValues(partitionKey, 4, "name4", 1);

    // When
    final Iterator<ClusteredEntity> iterator =
        manager
            .sliceQuery(ClusteredEntity.class)
            .forIteration()
            .withPartitionComponents(partitionKey)
            .fromClusterings(1)
            .fromInclusiveToExclusiveBounds()
            .limit(6)
            .iterator(2);

    // Then
    assertThat(iterator.hasNext()).isTrue();
    assertThat(iterator.next().getValue()).isEqualTo("value11");

    assertThat(iterator.hasNext()).isTrue();
    assertThat(iterator.next().getValue()).isEqualTo("value12");

    assertThat(iterator.hasNext()).isTrue();
    assertThat(iterator.next().getValue()).isEqualTo("value13");

    assertThat(iterator.hasNext()).isTrue();
    assertThat(iterator.next().getValue()).isEqualTo("value21");

    assertThat(iterator.hasNext()).isTrue();
    assertThat(iterator.next().getValue()).isEqualTo("value22");

    assertThat(iterator.hasNext()).isTrue();
    assertThat(iterator.next().getValue()).isEqualTo("value31");

    assertThat(iterator.hasNext()).isFalse();
  }
  @Test
  public void should_dsl_delete_async() throws Exception {
    // Given
    final long id = RandomUtils.nextLong(0L, Long.MAX_VALUE);
    final Date date = buildDateKey();
    scriptExecutor.executeScriptTemplate(
        "SimpleEntity/insert_single_row.cql", ImmutableMap.of("id", id, "table", "simple"));

    final CountDownLatch latch = new CountDownLatch(1);
    final CassandraLogAsserter logAsserter = new CassandraLogAsserter();
    logAsserter.prepareLogLevel(ASYNC_LOGGER_STRING, "%msg - [%thread]%n");

    // When
    manager
        .dsl()
        .delete()
        .consistencyList()
        .simpleMap()
        .fromBaseTable()
        .where()
        .id()
        .Eq(id)
        .date()
        .Eq(date)
        .withResultSetAsyncListener(
            rs -> {
              LOGGER.info(CALLED);
              latch.countDown();
              return rs;
            })
        .withTracing()
        .executeAsync();

    // Then
    latch.await();
    logAsserter.assertContains("Called - ");
  }