/** * Get all the fieldNames on this entity */ private Set<String> getFieldNames( final Optional<Entity> entity ) { if ( !entity.isPresent() ) { return Collections.emptySet(); } Collection<Field> fields = entity.get().getFields(); Set<String> fieldNames = new HashSet<>( fields.size() ); for ( final Field field : entity.get().getFields() ) { fieldNames.add( field.getName() ); } return fieldNames; } }
/** * Get all unique fields on an entity * @param entity * @return */ public static List<Field> getUniqueFields( Entity entity ) { final Collection<Field> entityFields = entity.getFields(); //preallocate to max possible for more efficient runtime final List<Field> possibleFields = new ArrayList<>( entityFields.size() ); for ( Field field : entity.getFields() ) { if ( field.isUnique() ) { possibleFields.add( field ); } } return possibleFields; } }
@Override public void confirmUniqueValues( ApplicationScope scope, Entity entity, UUID version, String region ) throws UniqueValueException { ready(); try { for (Field field : entity.getFields()) { if (field.isUnique()) { confirmUniqueField( scope, entity, version, field, region ); } } } catch ( UniqueValueException e ) { for (Field field : entity.getFields()) { if (field.isUnique()) { try { cancelUniqueField( scope, entity, version, field, region ); } catch (Throwable ex ) { logger.error( "Error canceling unique field", ex ); } } } throw e; } }
@Override public void reserveUniqueValues( ApplicationScope scope, Entity entity, UUID version, String region ) throws UniqueValueException { ready(); try { for (Field field : entity.getFields()) { if (field.isUnique()) { reserveUniqueField( scope, entity, version, field, region ); } } } catch ( UniqueValueException e ) { for (Field field : entity.getFields()) { if (field.isUnique()) { try { cancelUniqueField( scope, entity, version, field, region ); } catch (Throwable ignored) { logger.error( "Error canceling unique field", ignored ); } } } throw e; } }
public void call( final Throwable t ) { if ( t instanceof CollectionRuntimeException ) { CollectionRuntimeException cre = ( CollectionRuntimeException ) t; final MvccEntity mvccEntity = cre.getEntity(); final ApplicationScope scope = cre.getApplicationScope(); // one batch to handle rollback MutationBatch rollbackMb = null; final BatchStatement uniqueDeleteBatch = new BatchStatement(); final Optional<Entity> entity = mvccEntity.getEntity(); if ( entity.isPresent() ) { for ( final Field field : entity.get().getFields() ) { // if it's unique, add its deletion to the rollback batch if ( field.isUnique() ) { UniqueValue toDelete = new UniqueValueImpl( field, entity.get().getId(), mvccEntity.getVersion() ); uniqueDeleteBatch.add(uniqueValueStrat.deleteCQL(scope, toDelete )); } } // execute the batch statements for deleting unique field entries session.execute(uniqueDeleteBatch); logEntryStrat.delete( scope, entity.get().getId(), mvccEntity.getVersion() ); } } }
final List<Field> uniqueFields = new ArrayList<>( entity.getFields().size() );
final Collection<Field> expectedFields = expected.getFields();
results.addAll( entity.getFields() );
results.addAll( returned.getEntity().get().getFields());
results.addAll( entity.getFields() );
uniqueValueSerializationStrategy.load( context, newEntity.getId().getType(), createReturned.getFields() ); assertFalse( uniqueValues.iterator().hasNext() );