s_logger.debug("Executing on file: " + sDataFile);
}
//Get the connection to use
FileConnection connection = m_adapter.getConnection((FileDataSourceFragment)getFragment(), sDataFile);
try
{
//Check for optimistic lock violation
if (m_nType == UPDATE || m_nType == DELETE)
{
Object lockValue = m_instance.getOldValueDirect(lockAttrib.getOrdinal());
long lCurrentLockValue = connection.getLastModified();
if (s_logger.isDebugEnabled())
{
s_logger.debug("LOCKING " + ( (((Long)lockValue).longValue() < lCurrentLockValue) ? "BAD" : "GOOD") + " instance=" + lockValue + " file=" + lCurrentLockValue);
}
if (((Long)lockValue).longValue() < lCurrentLockValue)
{
throw new OptimisticLockException(m_instance);
}
}
Object dataToWrite = null;
if (m_nType == INSERT || m_nType == UPDATE)
{
Metaclass metaclass = m_instance.getMetaclass();
for (int i = 0, nCount = metaclass.getInstanceAttributeCount(); i < nCount; i++)
{
Attribute attribute = metaclass.getInstanceAttribute(i);
AttributeMapping attributeMapping = m_mapping.getAttributeMapping(attribute);
if (attributeMapping == null)
{
continue;
}
FilePrimitiveMapping mapping = (FilePrimitiveMapping)attributeMapping;
if (mapping.getSysId() == FilePrimitiveMapping.SYSID_DATA)
{
dataToWrite = m_instance.getValueDirect(i);
break;
}
}
if (dataToWrite instanceof String)
{
connection.write((String)dataToWrite);
}
else if (dataToWrite instanceof Binary)
{
connection.write((Binary)dataToWrite);
}
else
{
throw new IllegalArgumentException("Data being written must be Binary or String");
}
}
else if (m_nType == DELETE)
{
connection.delete();
}
else
{
throw new IllegalStateException("Unknown work type");
}
//Update the instance locking attribute
if (m_nType == INSERT || m_nType == UPDATE)
{
/*
* Do not modify instance locking attribute in DELETE case because that
* will immediately set "locking" to zero and replication to fragments
* will fail because they will see this zero value.
*/
long lNewLockValue = connection.getLastModifiedThisTxn();
m_instance.setValueDirect(lockAttrib.getOrdinal(),
Primitive.createLong(lNewLockValue)
);
}
}
catch (IOException ex)
{
ObjUtil.rethrow(ex);
}
finally
{
connection.close();
}
}