alf_prop tables: Improvements to string-related queries and fix duplicate handling

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@15946 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Derek Hulley
2009-08-27 05:24:09 +00:00
parent f16a0f2a74
commit 51a2ef5df3
3 changed files with 115 additions and 28 deletions

View File

@@ -13,6 +13,7 @@
<typeAlias alias="PropertyClass" type="org.alfresco.repo.domain.propval.PropertyClassEntity"/> <typeAlias alias="PropertyClass" type="org.alfresco.repo.domain.propval.PropertyClassEntity"/>
<typeAlias alias="PropertyDateValue" type="org.alfresco.repo.domain.propval.PropertyDateValueEntity"/> <typeAlias alias="PropertyDateValue" type="org.alfresco.repo.domain.propval.PropertyDateValueEntity"/>
<typeAlias alias="PropertyStringValue" type="org.alfresco.repo.domain.propval.PropertyStringValueEntity"/> <typeAlias alias="PropertyStringValue" type="org.alfresco.repo.domain.propval.PropertyStringValueEntity"/>
<typeAlias alias="PropertyStringQuery" type="org.alfresco.repo.domain.propval.PropertyStringQueryEntity"/>
<typeAlias alias="PropertyDoubleValue" type="org.alfresco.repo.domain.propval.PropertyDoubleValueEntity"/> <typeAlias alias="PropertyDoubleValue" type="org.alfresco.repo.domain.propval.PropertyDoubleValueEntity"/>
<typeAlias alias="PropertyValue" type="org.alfresco.repo.domain.propval.PropertyValueEntity"/> <typeAlias alias="PropertyValue" type="org.alfresco.repo.domain.propval.PropertyValueEntity"/>
<typeAlias alias="PropertyLink" type="org.alfresco.repo.domain.propval.PropertyLinkEntity"/> <typeAlias alias="PropertyLink" type="org.alfresco.repo.domain.propval.PropertyLinkEntity"/>
@@ -249,7 +250,7 @@
</select> </select>
<!-- Get the property value by value in alf_prop_string_value --> <!-- Get the property value by value in alf_prop_string_value -->
<select id="select.PropertyValueByStringValue" parameterClass="PropertyValue" resultMap="result.PropertyValue"> <select id="select.PropertyValueByStringValue" parameterClass="PropertyStringQuery" resultMap="result.PropertyValue">
select select
pv.id as id, pv.id as id,
pv.actual_type_id as actual_type_id, pv.actual_type_id as actual_type_id,
@@ -263,7 +264,8 @@
join alf_prop_string_value sv on (sv.id = pv.long_value and pv.persisted_type = #persistedType#) join alf_prop_string_value sv on (sv.id = pv.long_value and pv.persisted_type = #persistedType#)
where where
pv.actual_type_id = #actualTypeId# and pv.actual_type_id = #actualTypeId# and
sv.string_value = #stringValue# sv.string_end_lower = #stringEndLower# and
sv.string_crc = #stringCrc#
</select> </select>
<!-- Get the property value by ID --> <!-- Get the property value by ID -->

View File

@@ -0,0 +1,80 @@
/*
* Copyright (C) 2005-2009 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* As a special exception to the terms and conditions of version 2.0 of
* the GPL, you may redistribute this Program in connection with Free/Libre
* and Open Source Software ("FLOSS") applications as described in Alfresco's
* FLOSS exception. You should have recieved a copy of the text describing
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing"
*/
package org.alfresco.repo.domain.propval;
import org.alfresco.repo.domain.CrcHelper;
import org.alfresco.util.Pair;
/**
* Entity bean for querying against the <b>alf_prop_string_value</b> table.
*
* @author Derek Hulley
* @since 3.2
*/
public class PropertyStringQueryEntity
{
private final Short persistedType;
private final Long actualTypeId;
private final String stringValue;
private final String stringEndLower;
private final Long stringCrc;
public PropertyStringQueryEntity(Short persistedType, Long actualTypeId, String value)
{
this.persistedType = persistedType;
this.actualTypeId = actualTypeId;
stringValue = value;
// Calculate the crc value from the original value
Pair<String, Long> crcPair = CrcHelper.getStringCrcPair(value, 16, false, true);
stringEndLower = crcPair.getFirst();
stringCrc = crcPair.getSecond();
}
public Short getPersistedType()
{
return persistedType;
}
public Long getActualTypeId()
{
return actualTypeId;
}
public String getStringValue()
{
return stringValue;
}
public String getStringEndLower()
{
return stringEndLower;
}
public Long getStringCrc()
{
return stringCrc;
}
}

View File

@@ -34,6 +34,7 @@ import org.alfresco.repo.domain.propval.PropertyDateValueEntity;
import org.alfresco.repo.domain.propval.PropertyDoubleValueEntity; import org.alfresco.repo.domain.propval.PropertyDoubleValueEntity;
import org.alfresco.repo.domain.propval.PropertyIdSearchRow; import org.alfresco.repo.domain.propval.PropertyIdSearchRow;
import org.alfresco.repo.domain.propval.PropertyLinkEntity; import org.alfresco.repo.domain.propval.PropertyLinkEntity;
import org.alfresco.repo.domain.propval.PropertyStringQueryEntity;
import org.alfresco.repo.domain.propval.PropertyStringValueEntity; import org.alfresco.repo.domain.propval.PropertyStringValueEntity;
import org.alfresco.repo.domain.propval.PropertyValueEntity; import org.alfresco.repo.domain.propval.PropertyValueEntity;
import org.alfresco.repo.domain.propval.PropertyValueEntity.PersistedType; import org.alfresco.repo.domain.propval.PropertyValueEntity.PersistedType;
@@ -168,17 +169,26 @@ public class PropertyValueDAOImpl extends AbstractPropertyValueDAOImpl
return value; return value;
} }
@SuppressWarnings("unchecked")
@Override @Override
protected Long findStringValueByValue(String value) protected Long findStringValueByValue(String value)
{ {
PropertyStringValueEntity entity = new PropertyStringValueEntity(); PropertyStringValueEntity entity = new PropertyStringValueEntity();
entity.setValue(value); entity.setValue(value);
Long id = (Long) template.queryForObject( List<Long> rows = (List<Long>) template.queryForList(
SELECT_PROPERTY_STRING_VALUE_BY_VALUE, SELECT_PROPERTY_STRING_VALUE_BY_VALUE,
entity); entity,
0, 1);
// The CRC match prevents incorrect results from coming back. Although there could be // The CRC match prevents incorrect results from coming back. Although there could be
// several matches, we are sure that the matches are case-sensitive. // several matches, we are sure that the matches are case-sensitive.
return id; if (rows.size() > 0)
{
return rows.get(0);
}
else
{
return null;
}
} }
@Override @Override
@@ -215,8 +225,9 @@ public class PropertyValueDAOImpl extends AbstractPropertyValueDAOImpl
entity.setDoubleValue(value); entity.setDoubleValue(value);
List<PropertyDoubleValueEntity> results = (List<PropertyDoubleValueEntity>) template.queryForList( List<PropertyDoubleValueEntity> results = (List<PropertyDoubleValueEntity>) template.queryForList(
SELECT_PROPERTY_DOUBLE_VALUE_BY_VALUE, SELECT_PROPERTY_DOUBLE_VALUE_BY_VALUE,
entity); entity,
// There coult be several matches, so take the first one 0, 1);
// There could be several matches, so just get one
if (results.size() > 0) if (results.size() > 0)
{ {
return results.get(0); return results.get(0);
@@ -277,10 +288,11 @@ public class PropertyValueDAOImpl extends AbstractPropertyValueDAOImpl
// How would it be persisted? // How would it be persisted?
PersistedType persistedType = queryEntity.getPersistedTypeEnum(); PersistedType persistedType = queryEntity.getPersistedTypeEnum();
Short persistedTypeId = queryEntity.getPersistedType();
// Query based on the the persistable value type // Query based on the the persistable value type
String query = null; String query = null;
boolean singleResult = true; // false if multiple query results are possible Object queryObject = queryEntity;
// Handle each persisted type individually // Handle each persisted type individually
@@ -294,8 +306,12 @@ public class PropertyValueDAOImpl extends AbstractPropertyValueDAOImpl
query = SELECT_PROPERTY_VALUE_BY_DOUBLE_VALUE; query = SELECT_PROPERTY_VALUE_BY_DOUBLE_VALUE;
break; break;
case STRING: case STRING:
// It's best to query using the CRC and short end-value
query = SELECT_PROPERTY_VALUE_BY_STRING_VALUE; query = SELECT_PROPERTY_VALUE_BY_STRING_VALUE;
singleResult = false; queryObject = new PropertyStringQueryEntity(
persistedTypeId,
actualTypeId,
queryEntity.getStringValue());
break; break;
case SERIALIZABLE: case SERIALIZABLE:
// No query // No query
@@ -308,25 +324,14 @@ public class PropertyValueDAOImpl extends AbstractPropertyValueDAOImpl
PropertyValueEntity result = null; PropertyValueEntity result = null;
if (query != null) if (query != null)
{ {
if (singleResult) List<PropertyValueEntity> results = (List<PropertyValueEntity>) template.queryForList(
query,
queryObject,
0, 1); // Only want one result
for (PropertyValueEntity row : results)
{ {
result = (PropertyValueEntity) template.queryForObject(query, queryEntity); result = row;
} break;
else
{
Serializable queryValue = queryEntity.getPersistedValue();
List<PropertyValueEntity> results = (List<PropertyValueEntity>) template.queryForList(
query,
queryEntity);
for (PropertyValueEntity row : results)
{
if (queryValue.equals(row.getPersistedValue()))
{
// We have a match
result = row;
break;
}
}
} }
} }