in stack/tools/src/main/java/org/apache/usergrid/tools/UniqueValueManager.java [137:262]
public void runTool( CommandLine line ) throws Exception {
startSpring();
logger.info("Staring Tool: UniqueValueManager");
logger.info("Using Cassandra consistency level: {}", System.getProperty("usergrid.read.cl", "CL_LOCAL_QUORUM"));
String operation = line.getOptionValue(OPERATION_ARG) != null ? line.getOptionValue(OPERATION_ARG) : "get";
boolean deleteOp = operation.toLowerCase().equals("delete");
boolean updateOp = operation.toLowerCase().equals("update");
if (deleteOp && !line.hasOption(CONFIRM_DELETE_ARG)) {
throw new RuntimeException("Must add confirmDelete option to use delete.");
}else if( updateOp && !line.hasOption(CONFIRM_UPDATE_ARG) ){
throw new RuntimeException("Must add confirmUpdate option to use update.");
}
String filepath = line.getOptionValue(FILEPATH_ARG);
if (filepath == null || filepath.isEmpty()) {
throw new RuntimeException("File is required -- should contain one row per entity formatted like " +
"'{uuid}|{entityType}|{fieldType}|{fieldValue}'. " +
"Example: 'b9398e88-ef7f-11e5-9e41-0a2cb9e6caa9|user|email|baasadmins@apigee.com'");
}
session = injector.getInstance(Session.class);
mvccEntitySerializationStrategy = injector.getInstance(MvccEntitySerializationStrategy.class);
uniqueValueSerializationStrategy = injector.getInstance(UniqueValueSerializationStrategy.class);
AtomicInteger count = new AtomicInteger(0);
File listFile = new File(filepath);
boolean useSerializationRepair = false;
if(line.hasOption(SERIALIZATION_REPAIR_ARG)){
useSerializationRepair = true;
}
try (BufferedReader br = new BufferedReader(new FileReader(listFile))) {
String fileLine;
while ((fileLine = br.readLine()) != null) {
String[] valuesArray = fileLine.trim().split("\\|");
if (valuesArray.length != 4 && valuesArray.length != 5) {
logger.info("Line: >"+fileLine+"<");
throw new RuntimeException("Invalid file -- should contain one row per entity formatted like " +
"'{uuid}|{entityType}|{fieldType}|{fieldValue}|{newEntityUUID}'. " +
"Example: 'b9398e88-ef7f-11e5-9e41-0a2cb9e6caa9|user|email|whatever@usergrid.com|newEntityUUID'. " +
"Param {newEntityUUID} is optional.");
}
UUID appUuid = UUID.fromString(valuesArray[0]);
String entityType = valuesArray[1];
String fieldType = valuesArray[2];
String fieldValue = valuesArray[3];
UniqueValueSet uniqueValueSet = uniqueValueSerializationStrategy.load(
new ApplicationScopeImpl(new SimpleId(appUuid, "application")),
ConsistencyLevel.valueOf(System.getProperty("usergrid.read.cl", "CL_LOCAL_QUORUM")), entityType,
Collections.singletonList(new StringField(fieldType, fieldValue)), useSerializationRepair);
if( updateOp) {
if(valuesArray.length!=5){
throw new RuntimeException("Missing param {newEntityUUID}");
}
String updateUUID = valuesArray[4];
ApplicationScope applicationScope = new ApplicationScopeImpl(new SimpleId(appUuid, "application"));
com.google.common.base.Optional<MvccEntity> entity =
mvccEntitySerializationStrategy.load(applicationScope, new SimpleId(UUID.fromString(updateUUID), entityType));
if( !entity.isPresent()
|| !entity.get().getEntity().isPresent() ){
throw new RuntimeException("Unable to update unique value index because supplied UUID "+updateUUID+" does not exist");
}
logger.info("Delete unique value: {}", uniqueValueSet.getValue(fieldType));
session.execute(uniqueValueSerializationStrategy.deleteCQL(applicationScope, uniqueValueSet.getValue(fieldType)));
UniqueValue newUniqueValue =
new UniqueValueImpl(new StringField(fieldType, fieldValue), entity.get().getId(), entity.get().getVersion());
logger.info("Writing new unique value: {}", newUniqueValue);
session.execute(uniqueValueSerializationStrategy.writeCQL(applicationScope, newUniqueValue, -1));
logger.info("Re-loading unique value set for field");
}
uniqueValueSet = uniqueValueSerializationStrategy.load(
new ApplicationScopeImpl(new SimpleId(appUuid, "application")),
ConsistencyLevel.valueOf(System.getProperty("usergrid.read.cl", "CL_LOCAL_QUORUM")), entityType,
Collections.singletonList(new StringField(fieldType, fieldValue)), useSerializationRepair);
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("[");
uniqueValueSet.forEach(uniqueValue -> {
String entry = "fieldName=" + uniqueValue.getField().getName() +
", fieldValue=" + uniqueValue.getField().getValue() +
", uuid=" + uniqueValue.getEntityId().getUuid() +
", type=" + uniqueValue.getEntityId().getType() +
", version=" + uniqueValue.getEntityVersion();
stringBuilder.append("{").append(entry).append("},");
});
stringBuilder.deleteCharAt(stringBuilder.length() - 1);
stringBuilder.append("]");
logger.info("Returned unique value set from serialization load = {}", stringBuilder.toString());
if (deleteOp) {
uniqueValueSet.forEach(uniqueValue -> {
logger.info("DELETING UNIQUE VALUE");
try {
BatchStatement batchStatement = uniqueValueSerializationStrategy.
deleteCQL(new ApplicationScopeImpl(new SimpleId(appUuid, "application")), uniqueValue);
session.execute(batchStatement);
}
catch (Exception e) {
logger.error("Exception thrown for UV delete: " + e.getMessage());
}
});
}
}
}
}