use of com.emc.storageos.db.client.model.AbstractChangeTrackingSetMap 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);
}
}
use of com.emc.storageos.db.client.model.AbstractChangeTrackingSetMap in project coprhd-controller by CoprHD.
the class ColumnValue method setField.
public static void setField(Column<CompositeColumnName> column, PropertyDescriptor pd, Object obj) {
try {
Class type = pd.getPropertyType();
Object objValue = null;
if (AbstractChangeTrackingSetMap.class.isAssignableFrom(type)) {
objValue = pd.getReadMethod().invoke(obj);
if (objValue == null) {
objValue = type.newInstance();
}
AbstractChangeTrackingSetMap<?> trackingMap = (AbstractChangeTrackingSetMap<?>) objValue;
String entryValue = column.getStringValue();
if (entryValue == null || entryValue.isEmpty()) {
trackingMap.removeNoTrack(column.getName().getTwo(), column.getName().getThree());
} else {
trackingMap.putNoTrack(column.getName().getTwo(), entryValue);
}
} else if (AbstractChangeTrackingMap.class.isAssignableFrom(type)) {
objValue = pd.getReadMethod().invoke(obj);
if (objValue == null) {
objValue = type.newInstance();
}
AbstractChangeTrackingMap<?> trackingMap = (AbstractChangeTrackingMap<?>) objValue;
byte[] entryValue = column.getByteArrayValue();
if (entryValue == null || entryValue.length == 0) {
trackingMap.removeNoTrack(column.getName().getTwo());
} else {
trackingMap.putNoTrack(column.getName().getTwo(), column.getByteArrayValue());
}
} else if (AbstractChangeTrackingSet.class.isAssignableFrom(type)) {
objValue = pd.getReadMethod().invoke(obj);
if (objValue == null) {
objValue = type.newInstance();
}
AbstractChangeTrackingSet trackingSet = (AbstractChangeTrackingSet) objValue;
String entryValue = column.getStringValue();
if (entryValue == null || entryValue.isEmpty()) {
trackingSet.removeNoTrack(column.getName().getTwo());
} else {
trackingSet.addNoTrack(entryValue);
}
} else {
objValue = getPrimitiveColumnValue(column, pd);
}
pd.getWriteMethod().invoke(obj, objValue);
} catch (IllegalAccessException e) {
// should never get here
throw DatabaseException.fatals.deserializationFailedProperty(pd.getName(), e);
} catch (InvocationTargetException e) {
throw DatabaseException.fatals.deserializationFailedProperty(pd.getName(), e);
} catch (InstantiationException e) {
throw DatabaseException.fatals.deserializationFailedProperty(pd.getName(), e);
}
}
use of com.emc.storageos.db.client.model.AbstractChangeTrackingSetMap in project coprhd-controller by CoprHD.
the class DBClient method rebuildIndex.
public boolean rebuildIndex(URI id, Class clazz) {
boolean runResult = false;
try {
DataObject queryObject = queryObject(id, clazz);
if (queryObject != null) {
DataObject newObject = queryObject.getClass().newInstance();
newObject.trackChanges();
ConvertUtils.register(new CalendarConverter(null), Calendar.class);
ConvertUtils.register(new DateConverter(null), Date.class);
BeanUtilsBean notNull = new NullAwareBeanUtilsBean();
notNull.copyProperties(newObject, queryObject);
// special change tracking for customized types
BeanInfo bInfo;
try {
bInfo = Introspector.getBeanInfo(clazz);
} catch (IntrospectionException ex) {
log.error("Unexpected exception getting bean info", ex);
throw new RuntimeException("Unexpected exception getting bean info", ex);
}
PropertyDescriptor[] pds = bInfo.getPropertyDescriptors();
for (PropertyDescriptor pd : pds) {
Object val = pd.getReadMethod().invoke(newObject);
if (val instanceof AbstractChangeTrackingSet) {
AbstractChangeTrackingSet valueSet = (AbstractChangeTrackingSet) val;
valueSet.markAllForOverwrite();
} else if (val instanceof AbstractChangeTrackingMap) {
AbstractChangeTrackingMap valueMap = (AbstractChangeTrackingMap) val;
valueMap.markAllForOverwrite();
} else if (val instanceof AbstractChangeTrackingSetMap) {
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);
valueSet.markAllForOverwrite();
}
}
}
}
_dbClient.updateObject(newObject);
logMsg(String.format("Successfully rebuild index for %s in cf %s", id, clazz));
runResult = true;
} else {
logMsg(String.format("Could not find data object record for %s in cf %s, no need to rebuild index. Mark as success too.", id, clazz));
runResult = true;
}
} catch (Exception e) {
logMsg(String.format("Error when rebuilding index for %s in cf %s", id, clazz), true, e);
}
return runResult;
}
Aggregations