use of com.emc.storageos.db.client.model.DataObject in project coprhd-controller by CoprHD.
the class DbClientImpl method updateTaskStatus.
private Operation updateTaskStatus(Class<? extends DataObject> clazz, URI id, String opId, Operation updateOperation, boolean resetStartTime) {
List<URI> ids = new ArrayList<URI>(Arrays.asList(id));
List<? extends DataObject> objs = queryObjectField(clazz, "status", ids);
if (objs == null || objs.isEmpty()) {
// When "status" map is null (empty) we do not get object when query by the map field name in CF.
// Try to get object by id.
objs = queryObject(clazz, ids);
if (objs == null || objs.isEmpty()) {
_log.error("Cannot find object {} in {}", id, clazz.getSimpleName());
return null;
}
_log.info("Object {} has empty status map", id);
}
DataObject doobj = objs.get(0);
_log.info(String.format("Updating operation %s for object %s with status %s", opId, doobj.getId(), updateOperation.getStatus()));
Operation op = doobj.getOpStatus().updateTaskStatus(opId, updateOperation, resetStartTime);
if (op == null) {
// OpStatusMap does not have entry for a given opId. The entry already expired based on ttl.
// Recreate the entry for this opId from the task object and proceed with update
_log.info("Operation map for object {} does not have entry for operation id {}", doobj.getId(), opId);
Task task = TaskUtils.findTaskForRequestId(this, doobj.getId(), opId);
if (task != null) {
_log.info(String.format("Creating operation %s for object %s from task instance %s", opId, doobj.getId(), task.getId()));
// Create operation instance for the task
Operation operation = TaskUtils.createOperation(task);
doobj.getOpStatus().createTaskStatus(opId, operation);
op = doobj.getOpStatus().updateTaskStatus(opId, updateOperation, false);
if (op == null) {
_log.error(String.format("Failed to update operation %s for object %s ", opId, doobj.getId()));
return null;
}
} else {
_log.warn(String.format("Task for operation %s and object %s does not exist.", opId, doobj.getId()));
return null;
}
}
persistObject(doobj);
return op;
}
use of com.emc.storageos.db.client.model.DataObject in project coprhd-controller by CoprHD.
the class LazyLoader method refreshMappedByField.
/**
* refreshes the mapped by field when the lazy loaded field is replaced by another value
*
* @param obj
* @param lazyLoadedFieldName
*/
public void refreshMappedByField(String lazyLoadedFieldName, DataObject obj) {
DataObjectType doType = TypeMap.getDoType(obj.getClass());
// make sure the lazy loaded field is a valid field
ColumnField lazyLoadedField = doType.getColumnField(lazyLoadedFieldName);
if (lazyLoadedField == null) {
throw new IllegalStateException(String.format("lazy loaded field %s in class %s not found; make sure the argument passed into refreshMappedByField matches the @Name annotation on the getter method", lazyLoadedFieldName, obj.getClass()));
}
// make sure the lazy loaded field has a getter and setter
Method lazyLoadedFieldReadMethod = lazyLoadedField.getPropertyDescriptor().getReadMethod();
if (lazyLoadedFieldReadMethod == null) {
throw new IllegalStateException(String.format("lazy loaded field %s in class %s must have a read method", lazyLoadedFieldName, obj.getClass()));
}
// make sure the lazy loaded field is a supported type
Class lazyLoadedObjType = doType.getColumnField(lazyLoadedFieldName).getPropertyDescriptor().getPropertyType();
if (!Set.class.isAssignableFrom(lazyLoadedObjType) && !List.class.isAssignableFrom(lazyLoadedObjType) && !DataObject.class.isAssignableFrom(lazyLoadedObjType)) {
throw new IllegalStateException(String.format("lazy loaded field %s in class %s is an unsupported type: %s; " + "supported type are DataObject, List and Set", lazyLoadedFieldName, obj.getClass(), lazyLoadedObjType.getName()));
}
// get the mapped by field
ColumnField mappedByField = null;
if (doType != null) {
String mappedByFieldName = doType.getColumnField(lazyLoadedFieldName).getMappedByField();
mappedByField = doType.getColumnField(mappedByFieldName);
if (mappedByField == null) {
// in this case, we can't sync up the the lazy loaded field with the mapped by field
return;
}
}
// make sure the lazy loaded field has a getter and setter
Method mappedByFieldReadMethod = mappedByField.getPropertyDescriptor().getReadMethod();
Method mappedByFieldWriteMethod = mappedByField.getPropertyDescriptor().getWriteMethod();
if (mappedByFieldReadMethod == null || mappedByFieldWriteMethod == null) {
throw new IllegalStateException(String.format("mapped by field %s mapped to lazy loaded field %s in class %s must have both a read method and a write method", mappedByField.getName(), lazyLoadedFieldName, obj.getClass()));
}
// get lazy loaded object
if (Collection.class.isAssignableFrom(lazyLoadedObjType)) {
// make sure the mapped by type is a supported type (StringSet)
Class mappedByObjType = mappedByField.getPropertyDescriptor().getReadMethod().getReturnType();
if (!StringSet.class.isAssignableFrom(mappedByObjType)) {
throw new IllegalStateException(String.format("lazy loaded field %s in class %s has mapped by field %s with an unsupported type: %s;" + " the mappedby field for a collection must be a StringSet", lazyLoadedFieldName, obj.getClass(), mappedByField.getName(), lazyLoadedObjType.getName()));
}
refreshMappedByStringSet(obj, lazyLoadedFieldReadMethod, mappedByFieldReadMethod, mappedByFieldWriteMethod, mappedByObjType);
} else if (DataObject.class.isAssignableFrom(lazyLoadedObjType)) {
Class mappedByObjType = mappedByFieldReadMethod.getReturnType();
if (!URI.class.isAssignableFrom(mappedByObjType)) {
throw new IllegalStateException(String.format("lazy loaded field %s in class %s has mapped by field %s with an unsupported type: %s;" + " the mapped by field for a DataObject must be a URI", lazyLoadedFieldName, obj.getClass(), mappedByField.getName(), mappedByObjType.getName()));
}
refreshMappedByDataObject(obj, lazyLoadedFieldReadMethod, mappedByFieldWriteMethod);
}
}
use of com.emc.storageos.db.client.model.DataObject in project coprhd-controller by CoprHD.
the class LazyLoader method refreshMappedByStringSet.
/**
* @param obj
* @param lazyLoadedFieldReadMethod
* @param mappedByFieldReadMethod
* @param mappedByFieldWriteMethod
* @param mappedByObjType
* @throws IllegalAccessException
* @throws InvocationTargetException
* @throws InstantiationException
*/
private void refreshMappedByStringSet(DataObject obj, Method lazyLoadedFieldReadMethod, Method mappedByFieldReadMethod, Method mappedByFieldWriteMethod, Class mappedByObjType) {
try {
Collection<DataObject> lazyLoadedFieldValue = (Collection) lazyLoadedFieldReadMethod.invoke(obj);
StringSet mappedByFieldValue = (StringSet) mappedByFieldReadMethod.invoke(obj);
// otherwise, set the mapped by stringset to the list of id's in the lazy loaded collection
if (lazyLoadedFieldValue == null || lazyLoadedFieldValue.isEmpty()) {
if (mappedByFieldValue != null) {
mappedByFieldValue.clear();
mappedByFieldWriteMethod.invoke(obj, mappedByFieldValue);
}
} else {
if (mappedByFieldValue == null) {
mappedByFieldValue = (StringSet) mappedByObjType.newInstance();
}
DbClientCallbackEvent cb = mappedByFieldValue.getCallback();
mappedByFieldValue.setCallback(null);
copyCollectionToStringSet(lazyLoadedFieldValue, mappedByFieldValue);
mappedByFieldValue.setCallback(cb);
mappedByFieldWriteMethod.invoke(obj, mappedByFieldValue);
}
} catch (IllegalAccessException | IllegalArgumentException | InstantiationException | InvocationTargetException e) {
// we've done all the checking we can; if we end up here, it's a programming error
log.error(e.getMessage(), e);
}
}
use of com.emc.storageos.db.client.model.DataObject in project coprhd-controller by CoprHD.
the class AggregateDbIndex method addColumn.
@Override
boolean addColumn(String recordKey, CompositeColumnName column, Object value, String className, RowMutator mutator, Integer ttl, DataObject obj) {
IndexColumnName indexEntry = new IndexColumnName(fieldName, recordKey, (UUID) null);
if (groupGlobal) {
ColumnListMutation<IndexColumnName> indexColList = mutator.getIndexColumnList(indexCF, className);
ColumnValue.setColumn(indexColList, indexEntry, value, ttl);
}
for (String field : groupBy) {
ColumnField colField = groupByFields.get(field);
if (colField != null && obj.isInitialized(field)) {
Object groupValue = ColumnField.getFieldValue(colField, obj);
if (groupValue != null) {
ColumnListMutation<IndexColumnName> indexColList = mutator.getIndexColumnList(indexCF, getRowKey(className, groupValue));
ColumnValue.setColumn(indexColList, indexEntry, value, ttl);
}
}
}
return true;
}
use of com.emc.storageos.db.client.model.DataObject in project coprhd-controller by CoprHD.
the class ColumnField method serialize.
/**
* Serializes object field into database updates
*
* @param obj data object to serialize
* @param mutator row mutator to hold insertion queries
* @return boolean
* @throws DatabaseException
*/
public boolean serialize(DataObject obj, RowMutator mutator) {
try {
String id = obj.getId().toString();
if (isLazyLoaded() || _property.getReadMethod() == null) {
return false;
}
Object val = _property.getReadMethod().invoke(obj);
if (val == null) {
return false;
}
boolean changed = false;
switch(_colType) {
case NamedURI:
case Primitive:
{
if (!obj.isChanged(_name)) {
return false;
}
changed = addColumn(id, getColumnName(null, mutator), val, mutator, obj);
break;
}
case TrackingSet:
{
AbstractChangeTrackingSet valueSet = (AbstractChangeTrackingSet) val;
Set<?> addedSet = valueSet.getAddedSet();
if (addedSet != null) {
Iterator<?> it = valueSet.getAddedSet().iterator();
while (it.hasNext()) {
Object itVal = it.next();
String targetVal = valueSet.valToString(itVal);
changed |= addColumn(id, getColumnName(targetVal, mutator), itVal, mutator);
}
}
Set<?> removedVal = valueSet.getRemovedSet();
if (removedVal != null) {
Iterator<?> removedIt = removedVal.iterator();
while (removedIt.hasNext()) {
String targetVal = valueSet.valToString(removedIt.next());
if (_index == null) {
changed |= removeColumn(id, new ColumnWrapper(getColumnName(targetVal, mutator), targetVal), mutator);
} else {
addDeletionMark(id, getColumnName(targetVal, mutator), mutator);
changed = true;
}
}
}
break;
}
case TrackingMap:
{
AbstractChangeTrackingMap valueMap = (AbstractChangeTrackingMap) val;
Set<String> changedSet = valueMap.getChangedKeySet();
if (changedSet != null) {
Iterator<String> it = valueMap.getChangedKeySet().iterator();
while (it.hasNext()) {
String key = it.next();
Object entryVal = valueMap.get(key);
CompositeColumnName colName = getColumnName(key, mutator);
if (clockIndValue != null) {
int ordinal = ((ClockIndependentValue) entryVal).ordinal();
colName = getColumnName(key, String.format("%08d", ordinal), mutator);
}
changed |= addColumn(id, colName, valueMap.valToByte(entryVal), mutator);
}
}
Set<String> removedKey = valueMap.getRemovedKeySet();
if (removedKey != null) {
Iterator<String> removedIt = removedKey.iterator();
while (removedIt.hasNext()) {
String key = removedIt.next();
CompositeColumnName colName = getColumnName(key, mutator);
if (clockIndValue != null) {
Object removedVal = valueMap.getRemovedValue(key);
if (removedVal != null) {
colName = getColumnName(key, String.format("%08d", ((ClockIndependentValue) removedVal).ordinal()), mutator);
}
}
if (_index == null) {
changed |= removeColumn(id, new ColumnWrapper(colName, null), mutator);
} else {
addDeletionMark(id, colName, mutator);
changed = true;
}
}
}
break;
}
case TrackingSetMap:
{
AbstractChangeTrackingSetMap valueMap = (AbstractChangeTrackingSetMap) val;
Set<String> keys = valueMap.keySet();
if (keys != null) {
Iterator<String> it = keys.iterator();
while (it.hasNext()) {
String key = it.next();
AbstractChangeTrackingSet valueSet = valueMap.get(key);
Set<?> addedSet = valueSet.getAddedSet();
if (addedSet != null) {
Iterator<?> itSet = valueSet.getAddedSet().iterator();
while (itSet.hasNext()) {
String value = valueSet.valToString(itSet.next());
changed |= addColumn(id, getColumnName(key, value, mutator), value, mutator);
}
}
Set<?> removedVal = valueSet.getRemovedSet();
if (removedVal != null) {
Iterator<?> removedIt = removedVal.iterator();
while (removedIt.hasNext()) {
String targetVal = valueSet.valToString(removedIt.next());
if (_index == null) {
changed |= removeColumn(id, new ColumnWrapper(getColumnName(key, targetVal, mutator), targetVal), mutator);
} else {
addDeletionMark(id, getColumnName(key, targetVal, mutator), mutator);
changed = true;
}
}
}
}
}
break;
}
case NestedObject:
{
if (!obj.isChanged(_name)) {
break;
}
AbstractSerializableNestedObject nestedObject = (AbstractSerializableNestedObject) val;
changed |= addColumn(id, getColumnName(null, mutator), nestedObject.toBytes(), mutator);
}
}
return changed;
} catch (final InvocationTargetException e) {
throw DatabaseException.fatals.serializationFailedId(obj.getId(), e);
} catch (final IllegalAccessException e) {
throw DatabaseException.fatals.serializationFailedId(obj.getId(), e);
}
}
Aggregations