* @throws SQLException if a database exception occurs
* @throws CatalogIndexException if a document indexing exception occurs
*/
public int deleteUnreferencedRecords(int maxValuesForIndex)
throws SQLException, CatalogIndexException {
StringSet uuids = new StringSet();
PreparedStatement st = null;
int nRows = 0;
// find unreferenced records
try {
StringBuilder sbSql = new StringBuilder();
sbSql.append("SELECT DOCUUID FROM ").append(getResourceDataTableName());
sbSql.append(" WHERE DOCUUID NOT IN (SELECT DOCUUID FROM ");
sbSql.append(getResourceTableName()).append(")");
logExpression(sbSql.toString());
Connection con = returnConnection().getJdbcConnection();
st = con.prepareStatement(sbSql.toString());
st.setMaxRows(maxValuesForIndex);
ResultSet rs = st.executeQuery();
while (rs.next()) {
uuids.add(rs.getString(1));
}
} finally {
closeStatement(st);
st = null;
}
StringSet fids = new StringSet();
if (cswRemoteRepository.isActive()) {
fids = queryFileIdentifiers(uuids);
}
// delete unreferenced records from the admin table
try {
if (uuids.size() > 0) {
String sMsg = "Deleting "+uuids.size()+" unreferenced documents from table: "+getResourceDataTableName();
LogUtil.getLogger().info(sMsg);
StringBuilder sbSql = new StringBuilder();
sbSql.append("DELETE FROM ").append(getResourceDataTableName());
sbSql.append(" WHERE DOCUUID IN (").append(uuidsToInClause(uuids)).append(")");
logExpression(sbSql.toString());
Connection con = returnConnection().getJdbcConnection();
st = con.prepareStatement(sbSql.toString());
nRows = st.executeUpdate();
}
} finally {
closeStatement(st);
}
// delete unreferenced records from the index
if (uuids.size() > 0) {
CatalogIndexAdapter indexAdapter = getCatalogIndexAdapter();
if (indexAdapter != null) {
String sMsg = "Deleting "+uuids.size()+" unreferenced documents from the catalog index.";
LogUtil.getLogger().info(sMsg);
indexAdapter.deleteDocuments(uuids.toArray(new String[0]));
if (cswRemoteRepository.isActive()) {
if (fids.size() > 0) cswRemoteRepository.onRecordsDeleted(fids);
}
}
}
return nRows;