public BoundStatementWrapper bindForClusteredCounterIncrementDecrement(
      PersistentStateHolder context,
      PreparedStatement ps,
      PropertyMeta counterMeta,
      Long increment) {

    EntityMeta entityMeta = context.getEntityMeta();
    Object primaryKey = context.getPrimaryKey();

    log.trace(
        "Bind prepared statement {} for clustered counter increment/decrement for {} using primary key {} and value {}",
        ps.getQueryString(),
        entityMeta,
        primaryKey,
        increment);

    ConsistencyLevel consistencyLevel = overrider.getWriteLevel(context);

    List<Object> primaryKeys =
        bindPrimaryKey(
            primaryKey, entityMeta.getIdMeta(), counterMeta.structure().isStaticColumn());
    Object[] keys = addAll(new Object[] {increment}, primaryKeys.toArray());

    BoundStatement bs = ps.bind(keys);

    return new BoundStatementWrapper(
        context.getEntityClass(),
        bs,
        keys,
        getCQLLevel(consistencyLevel),
        NO_LISTENER,
        NO_SERIAL_CONSISTENCY);
  }
  public BoundStatementWrapper bindForClusteredCounterSelect(
      PersistentStateHolder context,
      PreparedStatement ps,
      boolean onlyStaticColumns,
      ConsistencyLevel consistencyLevel) {
    EntityMeta entityMeta = context.getEntityMeta();
    Object primaryKey = context.getPrimaryKey();

    log.trace(
        "Bind prepared statement {} for clustered counter read for {} using primary key {}",
        ps.getQueryString(),
        entityMeta,
        primaryKey);

    List<Object> primaryKeys =
        bindPrimaryKey(primaryKey, entityMeta.getIdMeta(), onlyStaticColumns);
    Object[] boundValues = primaryKeys.toArray();

    BoundStatement bs = ps.bind(boundValues);
    return new BoundStatementWrapper(
        context.getEntityClass(),
        bs,
        boundValues,
        getCQLLevel(consistencyLevel),
        NO_LISTENER,
        NO_SERIAL_CONSISTENCY);
  }
  public <T> T buildProxy(T entity, EntityOperations context, Set<Method> alreadyLoaded) {

    if (entity == null) {
      return null;
    }

    log.debug("Build Cglib proxy for entity {} ", entity);

    Class<?> proxyClass = factory.createProxyClass(entity.getClass(), context.getConfigContext());

    @SuppressWarnings("unchecked")
    T instance = (T) instantiator.instantiate(proxyClass);

    EntityMeta meta = context.getEntityMeta();
    for (PropertyMeta pm : meta.getAllMetasExceptCounters()) {
      Object value = pm.forValues().getValueFromField(entity);
      pm.forValues().setValueToField(instance, value);
    }

    for (PropertyMeta pm : meta.getAllCounterMetas()) {
      pm.forValues().setValueToField(entity, null);
    }

    ((Factory) instance)
        .setCallbacks(new Callback[] {buildInterceptor(context, entity, alreadyLoaded)});
    return instance;
  }
Esempio n. 4
0
  public <T> T load(PersistenceContext context, Class<T> entityClass) {
    log.debug("Loading entity of class {} using PersistenceContext {}", entityClass, context);
    EntityMeta entityMeta = context.getEntityMeta();
    Object primaryKey = context.getPrimaryKey();

    Validator.validateNotNull(entityClass, "Entity class should not be null");
    Validator.validateNotNull(
        primaryKey, "Entity '%s' key should not be null", entityClass.getCanonicalName());
    Validator.validateNotNull(
        entityMeta, "Entity meta for '%s' should not be null", entityClass.getCanonicalName());

    T entity = null;

    if (entityMeta.isClusteredCounter()) {
      entity = counterLoader.loadClusteredCounters(context);
    } else {
      Row row = context.loadEntity();
      if (row != null) {
        entity = entityMeta.instanciate();
        mapper.setNonCounterPropertiesToEntity(row, entityMeta, entity);
      }
    }

    return entity;
  }
 private List<Object> fetchPrimaryKeyValues(
     EntityMeta entityMeta, Object entity, boolean onlyStaticColumns) {
   List<Object> values = new ArrayList<>();
   Object primaryKey = entityMeta.forOperations().getPrimaryKey(entity);
   values.addAll(bindPrimaryKey(primaryKey, entityMeta.getIdMeta(), onlyStaticColumns));
   return values;
 }
Esempio n. 6
0
  public void validateForEntity(EntityMeta entityMeta, TableMetadata tableMetadata) {
    log.debug("Validate existing table {} for {}", tableMetadata.getName(), entityMeta);
    PropertyMeta idMeta = entityMeta.getIdMeta();
    if (entityMeta.isClusteredCounter()) {}

    validateTable(entityMeta, tableMetadata, idMeta);
  }
  private Object[] extractValuesForSimpleCounterBinding(
      EntityMeta entityMeta, PropertyMeta pm, Object primaryKey) {
    PropertyMeta idMeta = entityMeta.getIdMeta();
    String fqcn = entityMeta.getClassName();
    String primaryKeyAsString = idMeta.forTranscoding().forceEncodeToJSONForCounter(primaryKey);
    String cql3ColumnName = pm.getCQL3ColumnName();

    return new Object[] {fqcn, primaryKeyAsString, cql3ColumnName};
  }
 public void remove(EntityOperations context) {
   log.trace("Removing entity using PersistenceContext {}", context);
   EntityMeta entityMeta = context.getEntityMeta();
   if (entityMeta.structure().isClusteredCounter()) {
     context.bindForClusteredCounterRemoval();
   } else {
     context.bindForRemoval(entityMeta.config().getTableName());
     counterPersister.removeRelatedCounters(context);
   }
 }
  public void persist(EntityOperations context) {
    EntityMeta entityMeta = context.getEntityMeta();
    Object entity = context.getEntity();

    log.debug("Persisting transient entity {}", entity);

    if (entityMeta.structure().isClusteredCounter()) {
      counterPersister.persistClusteredCounters(context);
    } else {
      context.pushInsertStatement();
      counterPersister.persistCounters(context, entityMeta.getAllCounterMetas());
    }
  }
Esempio n. 10
0
  @Test
  public void should_not_add_counter_field_if_non_clustered_counter_entity() throws Exception {
    PropertyMeta idMeta = valueClass(Long.class).type(PARTITION_KEY).propertyName("id").build();
    PropertyMeta counterPM = valueClass(Counter.class).type(COUNTER).propertyName("count").build();

    when(meta.getAllMetasExceptId()).thenReturn(asList(counterPM));
    when(meta.getIdMeta()).thenReturn(idMeta);
    when(tableMeta.getColumns()).thenReturn(Arrays.<ColumnMetadata>asList());
    when(meta.structure().isClusteredCounter()).thenReturn(false);

    // When
    updater.updateTableForEntity(session, meta, tableMeta);

    // Then
    verifyZeroInteractions(session);
  }
Esempio n. 11
0
  private void validateTable(
      EntityMeta entityMeta, TableMetadata tableMetadata, PropertyMeta idMeta) {
    if (idMeta.isEmbeddedId()) {
      validatePrimaryKeyComponents(tableMetadata, idMeta, true);
      validatePrimaryKeyComponents(tableMetadata, idMeta, false);
    } else {
      validateColumn(
          tableMetadata,
          idMeta.getPropertyName().toLowerCase(),
          idMeta.getValueClassForTableCreation(),
          idMeta.isIndexed());
    }

    for (PropertyMeta pm : entityMeta.getAllMetasExceptIdAndCounters()) {
      switch (pm.type()) {
        case SIMPLE:
          validateColumn(
              tableMetadata,
              pm.getPropertyName().toLowerCase(),
              pm.getValueClassForTableCreation(),
              pm.isIndexed());
          break;
        case LIST:
        case SET:
        case MAP:
          validateCollectionAndMapColumn(tableMetadata, pm);
          break;
        default:
          break;
      }
    }
  }
Esempio n. 12
0
  public <T> T createEmptyEntity(PersistenceContext context, Class<T> entityClass) {
    log.debug("Loading entity of class {} using PersistenceContext {}", entityClass, context);
    EntityMeta entityMeta = context.getEntityMeta();
    Object primaryKey = context.getPrimaryKey();

    Validator.validateNotNull(entityClass, "Entity class should not be null");
    Validator.validateNotNull(
        primaryKey, "Entity '%s' key should not be null", entityClass.getCanonicalName());
    Validator.validateNotNull(
        entityMeta, "Entity meta for '%s' should not be null", entityClass.getCanonicalName());

    T entity = entityMeta.instanciate();
    entityMeta.getIdMeta().setValueToField(entity, primaryKey);

    return entity;
  }
Esempio n. 13
0
  @Test
  public void should_update_table_with_new_clustered_counter_field() throws Exception {
    // Given
    PropertyMeta idMeta = valueClass(Long.class).type(PARTITION_KEY).cqlColumnName("id").build();
    PropertyMeta counterPM = valueClass(Counter.class).type(COUNTER).cqlColumnName("count").build();

    when(meta.getAllMetasExceptId()).thenReturn(asList(counterPM));
    when(meta.getIdMeta()).thenReturn(idMeta);
    when(tableMeta.getColumns()).thenReturn(Arrays.<ColumnMetadata>asList());
    when(meta.structure().isClusteredCounter()).thenReturn(true);

    // When
    updater.updateTableForEntity(session, meta, tableMeta);

    // Then
    verify(session).execute(stringCaptor.capture());
    assertThat(stringCaptor.getValue()).isEqualTo("\n\tALTER TABLE tableName ADD count counter");
  }
Esempio n. 14
0
 @Before
 public void setUp() throws Exception {
   when(cluster.getMetadata().getKeyspace(keyspaceName)).thenReturn(keyspaceMeta);
   when(keyspaceMeta.getTables()).thenReturn(new ArrayList<TableMetadata>());
   when(keyspaceMeta.getTables()).thenReturn(asList(tableMeta));
   when(tableMeta.getName()).thenReturn("tableName");
   when(meta.config().getQualifiedTableName()).thenReturn("tableName");
   when(meta.config().isSchemaUpdateEnabled()).thenReturn(true);
 }
 private List<Object> fetchCASConditionsValues(
     PersistentStateHolder context, EntityMeta entityMeta) {
   List<Object> values = new ArrayList<>();
   if (context.hasCasConditions()) {
     for (Options.CASCondition CASCondition : context.getCasConditions()) {
       values.add(entityMeta.forTranscoding().encodeCasConditionValue(CASCondition));
     }
   }
   return values;
 }
Esempio n. 16
0
  @Test
  public void should_update_table_with_new_list_field() throws Exception {
    // Given
    PropertyMeta idMeta = valueClass(Long.class).type(PARTITION_KEY).cqlColumnName("id").build();
    PropertyMeta listStringPM =
        valueClass(String.class).type(LIST).cqlColumnName("list_string").build();

    when(meta.getAllMetasExceptId()).thenReturn(asList(listStringPM));
    when(meta.getIdMeta()).thenReturn(idMeta);
    when(tableMeta.getColumns()).thenReturn(Arrays.<ColumnMetadata>asList());

    // When
    updater.updateTableForEntity(session, meta, tableMeta);

    // Then
    verify(session).execute(stringCaptor.capture());
    assertThat(stringCaptor.getValue())
        .isEqualTo("\n\tALTER TABLE tableName ADD list_string list<text>");
  }
Esempio n. 17
0
  @Test
  public void should_update_table_with_new_map_field() throws Exception {
    // Given
    PropertyMeta idMeta = valueClass(Long.class).type(PARTITION_KEY).cqlColumnName("id").build();
    PropertyMeta mapStringPM =
        completeBean(Integer.class, String.class).type(MAP).cqlColumnName("preferences").build();

    when(meta.getAllMetasExceptId()).thenReturn(asList(mapStringPM));
    when(meta.getIdMeta()).thenReturn(idMeta);
    when(tableMeta.getColumns()).thenReturn(Arrays.<ColumnMetadata>asList());

    // When
    updater.updateTableForEntity(session, meta, tableMeta);

    // Then
    verify(session).execute(stringCaptor.capture());
    assertThat(stringCaptor.getValue())
        .isEqualTo("\n\tALTER TABLE tableName ADD preferences map<int, text>");
  }
Esempio n. 18
0
  @Test
  public void should_update_table_with_new_indexed_simple_field() throws Exception {
    // Given
    PropertyMeta idMeta = valueClass(Long.class).type(PARTITION_KEY).cqlColumnName("id").build();
    PropertyMeta longColPM = valueClass(Long.class).type(SIMPLE).cqlColumnName("longcol").build();
    longColPM.setIndexProperties(new IndexProperties("long_index", "longCol"));

    when(meta.getAllMetasExceptId()).thenReturn(asList(longColPM));
    when(meta.getIdMeta()).thenReturn(idMeta);
    when(tableMeta.getColumns()).thenReturn(Arrays.<ColumnMetadata>asList());

    // When
    updater.updateTableForEntity(session, meta, tableMeta);

    // Then
    verify(session, Mockito.times(2)).execute(stringCaptor.capture());
    final List<String> updates = stringCaptor.getAllValues();
    assertThat(updates.get(0)).isEqualTo("\n\tALTER TABLE tableName ADD longcol bigint");
    assertThat(updates.get(1)).isEqualTo("\n\tCREATE INDEX long_index ON tableName(longcol)");
  }
Esempio n. 19
0
  @Test
  public void should_not_update_if_schema_update_disabled() throws Exception {
    // Given
    when(meta.config().isSchemaUpdateEnabled()).thenReturn(false);

    // When
    updater.updateTableForEntity(session, meta, tableMeta);

    // Then
    verifyZeroInteractions(session);
  }
  public BoundStatementWrapper bindForClusteredCounterDelete(
      PersistentStateHolder context, PreparedStatement ps) {
    EntityMeta entityMeta = context.getEntityMeta();
    Object primaryKey = context.getPrimaryKey();

    log.trace(
        "Bind prepared statement {} for simple counter delete for {} using primary key {}",
        ps.getQueryString(),
        entityMeta,
        primaryKey);

    ConsistencyLevel consistencyLevel = overrider.getWriteLevel(context);
    List<Object> primaryKeys = bindPrimaryKey(primaryKey, entityMeta.getIdMeta(), false);
    Object[] boundValues = primaryKeys.toArray(new Object[primaryKeys.size()]);
    BoundStatement bs = ps.bind(boundValues);

    return new BoundStatementWrapper(
        context.getEntityClass(),
        bs,
        boundValues,
        getCQLLevel(consistencyLevel),
        NO_LISTENER,
        NO_SERIAL_CONSISTENCY);
  }