if (qname == null)
{key = new WordRef(collectionId, token);}
else {
key = new QNameWordRef(collectionId, qname, token, broker.getBrokerPool().getSymbols());
}
final Lock lock = dbTokens.getLock();
try {
lock.acquire(Lock.READ_LOCK);
final VariableByteInput is = dbTokens.getAsStream(key);
//Does the token already has data in the index ?
if (is == null)
{continue;}
while (is.available() > 0) {
final int storedDocId = is.readInt();
final int storedSection = is.readByte();
final int gidsCount = is.readInt();
//Read (variable) length of node IDs + frequency + offsets
final int length = is.readFixedInt();
final DocumentImpl storedDocument = docs.getDoc(storedDocId);
//Exit if the document is not concerned
if (storedDocument == null) {
is.skipBytes(length);
continue;
}
//Process the nodes
NodeId previous = null;
for (int m = 0; m < gidsCount; m++) {
NodeId nodeId = broker.getBrokerPool().getNodeFactory().createFromStream(previous, is);
previous = nodeId;
final int freq = is.readInt();
NodeProxy storedNode;
switch (storedSection) {
case ATTRIBUTE_SECTION :
storedNode = new NodeProxy(storedDocument, nodeId, Node.ATTRIBUTE_NODE);
break;
case TEXT_SECTION :
storedNode = new NodeProxy(storedDocument, nodeId, Node.TEXT_NODE);
break;
case QNAME_SECTION :
storedNode = new NodeProxy(storedDocument, nodeId,
qname.getNameType() == ElementValue.ATTRIBUTE ?
Node.ATTRIBUTE_NODE : Node.ELEMENT_NODE);
break;
default :
throw new IllegalArgumentException("Invalid section type in '" +
dbTokens.getFile().getName() + "'");
}
// if a context set is specified, we can directly check if the
// matching text node is a descendant of one of the nodes
// in the context set.
if (contextSet != null) {
NodeProxy parent;
switch(storedSection) {
case ATTRIBUTE_SECTION :
if (contextSet instanceof VirtualNodeSet) {
parent = contextSet.parentWithChild(storedNode,
false, true, NodeProxy.UNKNOWN_NODE_LEVEL);
if (parent != null && !parent.getNodeId().equals(storedNode.getNodeId()))
{parent = null;}
} else
{parent = contextSet.get(storedNode);}
break;
case QNAME_SECTION:
case TEXT_SECTION :
parent = contextSet.parentWithChild(storedNode,
false, true, NodeProxy.UNKNOWN_NODE_LEVEL);
break;
default :
throw new IllegalArgumentException("Invalid section type in '" + dbTokens.getFile().getName() + "'");
}
if (parent != null) {
final Match match = new FTMatch(-1, nodeId, token, freq);
readOccurrences(freq, is, match, token.length());
if (axis == NodeSet.ANCESTOR) {
parent.addMatch(match);
final int sizeHint = contextSet.getSizeHint(storedDocument);
result.add(parent, sizeHint);
} else {
storedNode.addMatch(match);
final int sizeHint = contextSet.getSizeHint(storedDocument);
result.add(storedNode, sizeHint);
}
} else {
is.skip(freq);
}
//Otherwise, we add all text nodes without check
} else {
final Match match = new FTMatch(-1, nodeId, token, freq);
readOccurrences(freq, is, match, token.length());
storedNode.addMatch(match);
result.add(storedNode, Constants.NO_SIZE_HINT);
}
context.proceed();
}
}
} 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
} finally {
lock.release(Lock.READ_LOCK);
}
}
return result;
}