public void dropIndex(DocumentImpl document) {
//Return early
if (document == null)
{return;}
final int collectionId = document.getCollection().getId();
final Lock lock = dbTokens.getLock();
for (byte currentSection = 0; currentSection <= QNAME_SECTION; currentSection++) {
//Not very necessary, but anyway...
switch (currentSection) {
case TEXT_SECTION :
case ATTRIBUTE_SECTION :
case QNAME_SECTION :
break;
default :
throw new IllegalArgumentException("Invalid section type in '" +
dbTokens.getFile().getName() + "' (inverted index)");
}
LOG.debug("Removing " + words[currentSection].size() + " tokens");
for (final Iterator i = words[currentSection].entrySet().iterator(); i.hasNext();) {
//Compute a key for the token
final Map.Entry entry = (Map.Entry) i.next();
final Object token = entry.getKey();
Value key;
if (currentSection == QNAME_SECTION) {
final QNameTerm term = (QNameTerm) token;
key = new QNameWordRef(collectionId, term.qname,
term.term, broker.getBrokerPool().getSymbols());
} else {
key = new WordRef(collectionId, token.toString());
}
os.clear();
try {
lock.acquire(Lock.WRITE_LOCK);
boolean changed = false;
os.clear();
final VariableByteInput is = dbTokens.getAsStream(key);
//Does the token already has data in the index ?
if (is == null)
{continue;}
//try {
while (is.available() > 0) {
final int storedDocId = is.readInt();
final byte section = is.readByte();
final int gidsCount = is.readInt();
//Read (variable) length of node IDs + frequency + offsets
final int length = is.readFixedInt();
if (storedDocId != document.getDocId()) {
// data are related to another document:
// copy them to any existing data
os.writeInt(storedDocId);
os.writeByte(section);
os.writeInt(gidsCount);
os.writeFixedInt(length);
is.copyRaw(os, length);
} else {
// data are related to our document:
// skip them
changed = true;
is.skipBytes(length);
}
}
//Store new data, if relevant
if (changed) {
//Well, nothing to store : remove the existing data
if (os.data().size() == 0) {
dbTokens.remove(key);
} else {
if (dbTokens.put(key, os.data()) == BFile.UNKNOWN_ADDRESS) {
LOG.error("Could not put index data for token '" +
token + "' in '" + dbTokens.getFile().getName() + "'");
//TODO : throw an exception ?
}
}
}
} catch (final LockException e) {
LOG.warn("Failed to acquire lock for '" +
dbTokens.getFile().getName() + "'", e);
//TODO : throw exception ? -pb
} catch (final IOException e) {
LOG.error(e.getMessage() + " in '" +
dbTokens.getFile().getName() + "'", e);
//TODO : throw exception ? -pb
} catch (final ReadOnlyException e) {
LOG.error(e.getMessage() + " in '" +
dbTokens.getFile().getName() + "'", e);
//TODO : throw exception ? -pb
} finally {
lock.release(Lock.WRITE_LOCK);
os.clear();
}
}
words[currentSection].clear();
}