对于SetDocValue的读取很简单,因为他的写入的格式和之前的SortedDocValue和SortedNumericDocValue有很多重复的地方,所以在读取的时候也是有很多重复的地方。
先看看最终要返回的接口吧,
public abstract class SortedSetDocValues { /** Sole constructor. (For invocation by subclass constructors, typically implicit.) */ protected SortedSetDocValues() {} /*** When returned by {@link #nextOrd()} it means there are no more ordinals for the document. */ public static final long NO_MORE_ORDS = -1; /** 读取当前的doc的下一个byte[]的排序 */ public abstract long nextOrd(); /** 设置要读取的doc的id */ public abstract void setDocument(int docID); /** 查找指定排序的byte[] */ public abstract BytesRef lookupOrd(long ord); /** 返回所有的byte[]的个数 */ public abstract long getValueCount(); /** 查询某个byte[]是否存在,如果不存在返回一个负数,这个方法被覆写了 */ public long lookupTerm(BytesRef key) { long low = 0; long high = getValueCount() - 1; while (low <= high) { long mid = (low + high) >>> 1; final BytesRef term = lookupOrd(mid); int cmp = term.compareTo(key); if (cmp < 0) { low = mid + 1; } else if (cmp > 0) { high = mid - 1; } else { return mid; // key found } } return -(low + 1); // key not found. } /** 返回迭代所有的byte[]的对象 ,这个方法也被覆写了*/ public TermsEnum termsEnum() { return new SortedSetDocValuesTermsEnum(this); } }
上面的功能很简单,基本和之前的SortedDocValue和SortedNumericDocValue一样。再看看具体的操作:
public SortedSetDocValues getSortedSet(FieldInfo field) throws IOException { SortedSetEntry ss = sortedSets.get(field.number); if (ss.format == SORTED_SINGLE_VALUED) { final SortedDocValues values = getSorted(field); return DocValues.singleton(values); } else if (ss.format != SORTED_WITH_ADDRESSES) { throw new AssertionError(); } final long valueCount = binaries.get(field.number).count;//所有的byte[]的个数 // we keep the byte[]s and list of ords on disk, these could be large final LongBinaryDocValues binary = (LongBinaryDocValues) getBinary(field);//获得所有的byte[]。 final LongValues ordinals = getNumeric(ords.get(field.number));//读取第二部分,也就是每个doc的所有的排序的地方, // but the addresses to the ord stream are in RAM, 读取第三部分,即地址部分,用于计算每个doc的排序的地址 final MonotonicBlockPackedReader ordIndex = getOrdIndexInstance(field, ordIndexes.get(field.number)); return new RandomAccessOrds() { long startOffset;//当前的doc的第一个byte[]是总的byte[]的多少个。 long offset;// long endOffset;//当前的doc的下一个doc的第一个byte[]是总的byte[]的多少个。 public long nextOrd() {//读取当前doc的下一个byte的排序 if (offset == endOffset) { return NO_MORE_ORDS; } else { long ord = ordinals.get(offset); offset++; return ord; } } public void setDocument(int docID) {//读取指定的doc, startOffset = offset = ordIndex.get(docID); endOffset = ordIndex.get(docID + 1L); } @Override public BytesRef lookupOrd(long ord) {//根据排序获得byte[] return binary.get(ord); } @Override public long getValueCount() {//所有的byte[]的个数 return valueCount; } @Override public long lookupTerm(BytesRef key) {//返回一个byte[]是否存在,如果存在返回其排序,否则返回一个负数 if (binary instanceof CompressedBinaryDocValues) {//进入 return ((CompressedBinaryDocValues) binary).lookupTerm(key); } else { return super.lookupTerm(key); } } @Override public TermsEnum termsEnum() { if (binary instanceof CompressedBinaryDocValues) {//这个是进入的,因为在存储所有的byte[]的时候,就是前缀压缩的,和之前的SortedDocValue的格式是一样的,所以返回的结果也是一样的 return ((CompressedBinaryDocValues) binary).getTermsEnum(); } else { return super.termsEnum(); } } @Override public long ordAt(int index) { return ordinals.get(startOffset + index); } @Override public int cardinality() { return (int) (endOffset - startOffset); } }; }
如果看懂了SortedDocValue其实这里也很简单。
过年夜,少写点。。
相关推荐
lucene3源码分析
Lucene项目的文档和源码,很好的java学习资料哦
lucene索引查看工具及源码lucene索引查看工具及源码lucene索引查看工具及源码
Lucene.Net-2.9.2 c# 源码,已经用它做了查询网站
lucene全文检索案例源码 lucene全文检索案例源码
Lucene是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎。该资源是Lucene的完整源码,利用源码,可以定制适合自身需要的搜索引擎。
lucene.net2.9.4.2源码版 dll版本是2.9.4.2 对2.9.4版的局部改进版
Lucene3.5全部源码,打包jar文件,可以直接打开查看源码,Lucene开发必备
Lucene源码解读、功能分析,Lucene入门
Lucene学习源码.rar
这是基于lucene搜索引擎的java源码,里面数据库,包括建立索引,增量索引一应俱全,希望对大家有作用。
lucene.net 2.9.1 源码,lucene.net 2.9.1最新dll
lucene-core-4.3.1的源码 可以用的 放心用吧 官网上很多链接都打不开
描述了Lucene中如何使用FST算法构建term的内存索引,使用了很多图,直观的展现了FST图的构建流程,能够对想了解lucene内部实现机制原理的同学有帮助。
Lucene,作为一种全文搜索的辅助工具,为我们进行条件搜索,无论是像Google,Baidu之类的搜索引 擎,还是论坛中的搜索功能,还是其它C/S架构的搜索,都带来了极大的便利和比较高的效率。本文主要是利用Lucene对MS Sql...
Lucene.net 搜索引擎 Lucene.net源码 Lucene.net中文文档; 好的东西需要分享
lucene 华电项目 源码
lucene3.0.3源码和教程
lucene5.0源码包,里面有源码、帮助文档、API、架包
运用在lucene中的中文分词算法源码