Example #1
0
  @Test
  public void should_initialize_lazy_properties() throws Exception {
    Tweet tweet = new Tweet();
    tweet.setId(UUIDGen.getTimeUUID());
    tweet.setContent("welcome");

    CompleteBean entity =
        CompleteBeanTestBuilder.builder()
            .randomId()
            .name("name")
            .label("label")
            .age(45L)
            .addFriends("foo", "bar")
            .welcomeTweet(tweet)
            .version(CounterBuilder.incr(11L))
            .buid();

    manager.insert(entity);

    CompleteBean foundEntity = manager.find(CompleteBean.class, entity.getId());

    CompleteBean rawEntity = manager.initAndRemoveProxy(foundEntity);

    assertThat(rawEntity.getName()).isEqualTo("name");
    assertThat(rawEntity.getLabel()).isEqualTo("label");
    assertThat(rawEntity.getAge()).isEqualTo(45L);
    assertThat(rawEntity.getFriends()).containsExactly("foo", "bar");
    assertThat(rawEntity.getWelcomeTweet().getContent()).isEqualTo("welcome");
    assertThat(rawEntity.getVersion()).isInstanceOf(InternalCounterImpl.class);
    assertThat(rawEntity.getVersion().get()).isEqualTo(11L);
  }
  @Test
  public void should_query_with_default_params() throws Exception {
    long partitionKey = RandomUtils.nextLong();
    List<ClusteredEntityWithReverseClustering> entities =
        manager
            .sliceQuery(ClusteredEntityWithReverseClustering.class)
            .partitionComponents(partitionKey)
            .getFirst(5);
    assertThat(entities).isEmpty();

    insertValues(partitionKey, 5);

    entities =
        manager
            .sliceQuery(ClusteredEntityWithReverseClustering.class)
            .partitionComponents(partitionKey)
            .fromClusterings(4)
            .toClusterings(2)
            .get();

    assertThat(entities).hasSize(3);

    assertThat(entities.get(0).getValue()).isEqualTo("value4");
    assertThat(entities.get(0).getId().getId()).isEqualTo(partitionKey);
    assertThat(entities.get(0).getId().getCount()).isEqualTo(4);
    assertThat(entities.get(0).getId().getName()).isEqualTo("name4");
    assertThat(entities.get(1).getValue()).isEqualTo("value3");
    assertThat(entities.get(1).getId().getId()).isEqualTo(partitionKey);
    assertThat(entities.get(1).getId().getCount()).isEqualTo(3);
    assertThat(entities.get(1).getId().getName()).isEqualTo("name3");
    assertThat(entities.get(2).getValue()).isEqualTo("value2");
    assertThat(entities.get(2).getId().getId()).isEqualTo(partitionKey);
    assertThat(entities.get(2).getId().getCount()).isEqualTo(2);
    assertThat(entities.get(2).getId().getName()).isEqualTo("name2");

    entities =
        manager
            .sliceQuery(ClusteredEntityWithReverseClustering.class)
            .fromEmbeddedId(new ClusteredKey(partitionKey, 4, null))
            .toEmbeddedId(new ClusteredKey(partitionKey, 2, null))
            .get();

    assertThat(entities).hasSize(3);

    assertThat(entities.get(0).getValue()).isEqualTo("value4");
    assertThat(entities.get(0).getId().getId()).isEqualTo(partitionKey);
    assertThat(entities.get(0).getId().getCount()).isEqualTo(4);
    assertThat(entities.get(0).getId().getName()).isEqualTo("name4");
    assertThat(entities.get(1).getValue()).isEqualTo("value3");
    assertThat(entities.get(1).getId().getId()).isEqualTo(partitionKey);
    assertThat(entities.get(1).getId().getCount()).isEqualTo(3);
    assertThat(entities.get(1).getId().getName()).isEqualTo("name3");
    assertThat(entities.get(2).getValue()).isEqualTo("value2");
    assertThat(entities.get(2).getId().getId()).isEqualTo(partitionKey);
    assertThat(entities.get(2).getId().getCount()).isEqualTo(2);
    assertThat(entities.get(2).getId().getName()).isEqualTo("name2");
  }
  @Test
  public void should_query_with_reverse_ordering() throws Exception {
    long partitionKey = RandomUtils.nextLong();
    insertValues(partitionKey, 5);

    List<ClusteredEntityWithReverseClustering> entities =
        manager
            .sliceQuery(ClusteredEntityWithReverseClustering.class)
            .partitionComponents(partitionKey)
            .fromClusterings(2)
            .toClusterings(4)
            .ordering(DESCENDING)
            .get();

    assertThat(entities).hasSize(3);

    assertThat(entities.get(0).getValue()).isEqualTo("value2");
    assertThat(entities.get(0).getId().getId()).isEqualTo(partitionKey);
    assertThat(entities.get(0).getId().getCount()).isEqualTo(2);
    assertThat(entities.get(0).getId().getName()).isEqualTo("name2");
    assertThat(entities.get(1).getValue()).isEqualTo("value3");
    assertThat(entities.get(1).getId().getId()).isEqualTo(partitionKey);
    assertThat(entities.get(1).getId().getCount()).isEqualTo(3);
    assertThat(entities.get(1).getId().getName()).isEqualTo("name3");
    assertThat(entities.get(2).getValue()).isEqualTo("value4");
    assertThat(entities.get(2).getId().getId()).isEqualTo(partitionKey);
    assertThat(entities.get(2).getId().getCount()).isEqualTo(4);
    assertThat(entities.get(2).getId().getName()).isEqualTo("name4");
  }
 private void insertClusteredEntity(
     Long partitionKey, int count, String name, String clusteredValue) {
   ClusteredKey embeddedId = new ClusteredKey(partitionKey, count, name);
   ClusteredEntityWithReverseClustering entity =
       new ClusteredEntityWithReverseClustering(embeddedId, clusteredValue);
   manager.persist(entity);
 }
 private void insertClusteredEntity(
     Long partitionKey, int count, String name, String clusteredValue) {
   ClusteredEntity.CompoundPK compoundPK =
       new ClusteredEntity.CompoundPK(partitionKey, count, name);
   ClusteredEntity entity = new ClusteredEntity(compoundPK, clusteredValue);
   manager.insert(entity);
 }
  @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_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();
  }
  @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();
  }
Example #9
0
  @Test
  public void should_initialize_counter_value() throws Exception {
    CompleteBean entity = CompleteBeanTestBuilder.builder().randomId().name("name").buid();

    entity = manager.insert(entity);

    entity.getVersion().incr(2L);

    manager.update(entity);

    RegularStatement statement = select().from("CompleteBean").where(eq("id", bindMarker()));

    CompleteBean foundEntity =
        manager.typedQuery(CompleteBean.class, statement, entity.getId()).getFirst();

    CompleteBean rawEntity = manager.initAndRemoveProxy(foundEntity);

    assertThat(rawEntity.getVersion()).isInstanceOf(InternalCounterImpl.class);
    assertThat(rawEntity.getVersion().get()).isEqualTo(2L);
  }
Example #10
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");
  }
  @Bean(destroyMethod = "shutDown")
  public PersistenceManager getPersistenceManager() {

    final List<String> activeProfiles = Arrays.asList(env.getActiveProfiles());
    boolean isProduction = activeProfiles.contains(Profiles.SPRING_PROFILE_PRODUCTION);
    PersistenceManagerFactory pmFactory =
        PersistenceManagerFactoryBuilder.builder(cluster)
            .withEntityPackages(UserEntity.class.getPackage().getName())
            .withDefaultReadConsistency(ConsistencyLevel.ONE)
            .withDefaultWriteConsistency(ConsistencyLevel.ONE)
            .withKeyspaceName(Schema.KEYSPACE)
            .withExecutorServiceMinThreadCount(5)
            .withExecutorServiceMaxThreadCount(10)
            .forceTableCreation(isProduction ? false : true)
            .build();

    final PersistenceManager pm = pmFactory.createPersistenceManager();

    pm.insert(
        UserEntity.fromModel(KILLRCHAT_USER),
        OptionsBuilder.ifNotExists()
            .lwtResultListener(
                new LWTResultListener() {
                  @Override
                  public void onSuccess() {
                    logger.info("Create new administration 'killrchat' account");
                  }

                  @Override
                  public void onError(LWTResult lwtResult) {
                    logger.debug("Administration 'killrchat' account already exists");
                  }
                }));

    return pm;
  }
  @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();
  }
  @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();
  }
 private void insertCompositeClusteredEntity(
     long id, String bucket, int count, String name, String clusteredValue) {
   CompoundPK compoundPK = new CompoundPK(id, bucket, count, name);
   CompositeClusteredEntity entity = new CompositeClusteredEntity(compoundPK, clusteredValue);
   manager.insert(entity);
 }