Created
January 15, 2013 09:22
-
-
Save s1monw/4537457 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static ConcurrentHashMap<Object, CloseableThreadLocal<TermsEnum>> enums = new ConcurrentHashMap<Object, CloseableThreadLocal<TermsEnum>>(); | |
static CloseableThreadLocal<DocsAndPositionsEnum> docsAndPos = new CloseableThreadLocal<DocsAndPositionsEnum>(); | |
// this works fine for nested docs since they don't have the payload which has the version | |
// so we iterate till we find the one with the payload | |
// LUCENE 4 UPGRADE: We can get rid of the do while loop, since there is only one _uid value (live docs are taken into account) | |
public static DocIdAndVersion loadDocIdAndVersion(AtomicReaderContext context, Term term) { | |
int docId = Lucene.NO_DOC; | |
try { | |
AtomicReader reader = context.reader(); | |
CloseableThreadLocal<TermsEnum> closeableThreadLocal = enums.get(reader.getCoreCacheKey()); | |
if (closeableThreadLocal == null) { | |
closeableThreadLocal = new CloseableThreadLocal<TermsEnum>(); | |
enums.put(reader.getCoreCacheKey(), closeableThreadLocal); | |
} | |
TermsEnum termsEnum = closeableThreadLocal.get(); | |
if (termsEnum == null) { | |
Terms terms = reader.terms(term.field()); | |
termsEnum = terms.iterator(null); | |
closeableThreadLocal.set(termsEnum); | |
} | |
final DocsAndPositionsEnum docsAndPositionsEnum = docsAndPos.get(); | |
final DocsAndPositionsEnum uid = termsEnum == null ? null : termsEnum.docsAndPositions(reader.getLiveDocs(), docsAndPositionsEnum); | |
if (docsAndPositionsEnum == null && uid != null) { | |
docsAndPos.set(uid); | |
} | |
if (uid == null || uid.nextDoc() == DocIdSetIterator.NO_MORE_DOCS) { | |
return null; // no doc | |
} | |
// Note, only master docs uid have version payload, so we can use that info to not | |
// take them into account | |
do { | |
docId = uid.docID(); | |
uid.nextPosition(); | |
if (uid.getPayload() == null) { | |
continue; | |
} | |
if (uid.getPayload().length < 8) { | |
continue; | |
} | |
byte[] payload = new byte[uid.getPayload().length]; | |
System.arraycopy(uid.getPayload().bytes, uid.getPayload().offset, payload, 0, uid.getPayload().length); | |
return new DocIdAndVersion(docId, Numbers.bytesToLong(payload), context); | |
} while (uid.nextDoc() != DocIdSetIterator.NO_MORE_DOCS); | |
return new DocIdAndVersion(docId, -2, context); | |
} catch (Exception e) { | |
return new DocIdAndVersion(docId, -2, context); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment