//*-- scan the database by file name and check if the file name exists, otherwise
//*-- delete the entry and keep track of the deletions
DbTools dbt = Constants.getDbt();
dbt.openDB(Constants.EXT_FILES_DB, false, false);
Cursor cursor = null; int count = 0; int errors = 0;
ArrayList<String> delFiles = new ArrayList<String>();
IndexableDoc idoc = new IndexableDoc();
try
{
cursor = dbt.getCurrentDB().openCursor(null, null);
DatabaseEntry key = new DatabaseEntry();
DatabaseEntry data = new DatabaseEntry();
LOOP: while (cursor.getNext(key, data, LockMode.DEFAULT) == OperationStatus.SUCCESS)
{
//*-- check if the file name exists in the filesystem
if (!running) break LOOP;
String filename = new String( key.getData(), "UTF-8");
File file = new File(filename);
if (!file.exists())
{ out.append("ERROR: File " + filename + " does not exist" + Constants.NEWLINE);
idoc = (IndexableDoc) idoc.getBdbBinding().entryToObject(data);
//*-- remove the entry from the database
if (!dbt.delete(filename))
logger.error("Could not delete " + filename + " from the database");
else
{ logger.info("Deleted " + filename + " from the database"); delFiles.add(filename); }
errors++;
} //*-- end of outer if
++count;
} //*-- end of while
//*-- clean up the Lucene index
FSDirectory fsd = FSDirectory.getDirectory(new File(Constants.getINDEXDIR()), false);
IndexReader ir = IndexReader.open(fsd);
for (int i = 0; i < delFiles.size(); i++)
ir.deleteDocuments(new Term("key", (String) delFiles.get(i)) );
ir.close();
}
catch (DatabaseException dbe)
{ logger.error("Could not open cursor to browse File index" + dbe.getMessage() ); }
catch (UnsupportedEncodingException ue)
{ logger.error("Could not decode key" + ue.getMessage() ); }
catch (IOException ie)
{ logger.error("Could not read index directory" + ie.getMessage() ); }
finally
{ try
{ if (cursor != null) cursor.close();
dbt.closeDB();
}
catch (DatabaseException de) { logger.error("Ignore error"); }
}