org.apache.lucene.util.BitVector
這個小小的工具類用來保存bit數據,并且提供bit級別的boolean讀寫能力。
這部分能力類似java.util.BitSet
而有趣的地方在于BitVector提供了持久化的能力(保存到文件),
為節省磁盤空間:
if (isSparse()) {
writeDgaps(output); // sparse bit-set more efficiently saved as
// d-gaps.
} else {
writeBits(output);
}
首先判斷數據中是否bit值為1的數據非常少,如果是,就采用Dgaps算法,
這種算法將壓縮數據,結構如下
..
III(IB)+
第1個Int為標記位,值-1表示為Dgaps
第2個Int為數據長度(多少個bit)
第3個Int為數據有多少個bit值為1
而后循環,只保存Byte值非0的串
第1個Int保存和上一個便宜
第2個位為Byte,保存Byte值(也就是8位bit)
呵呵,看來做indexer真是啥都省著用啊
btw,判斷數據中是否bit值為1的數據非常少的算法頁很有趣
private boolean isSparse() {
// note: order of comparisons below set to favor smaller values (no
// binary range search.)
// note: adding 4 because we start with ((int) -1) to indicate d-gaps
// format.
// note: we write the d-gap for the byte number, and the byte (bits[i])
// itself, therefore
// multiplying count by (8+8) or (8+16) or (8+24) etc.:
// - first 8 for writing bits[i] (1 byte vs. 1 bit), and
// - second part for writing the byte-number d-gap as vint.
// note: factor is for read/write of byte-arrays being faster than
// vints.
int factor = 10;
if (bits.length < (1 << 7))
return factor * (4 + (8 + 8) * count()) < size();
if (bits.length < (1 << 14))
return factor * (4 + (8 + 16) * count()) < size();
if (bits.length < (1 << 21))
return factor * (4 + (8 + 24) * count()) < size();
if (bits.length < (1 << 28))
return factor * (4 + (8 + 32) * count()) < size();
return factor * (4 + (8 + 40) * count()) < size();
}
這個小小的工具類用來保存bit數據,并且提供bit級別的boolean讀寫能力。
這部分能力類似java.util.BitSet
而有趣的地方在于BitVector提供了持久化的能力(保存到文件),
為節省磁盤空間:






這種算法將壓縮數據,結構如下
..
III(IB)+
第1個Int為標記位,值-1表示為Dgaps
第2個Int為數據長度(多少個bit)
第3個Int為數據有多少個bit值為1
而后循環,只保存Byte值非0的串
第1個Int保存和上一個便宜
第2個位為Byte,保存Byte值(也就是8位bit)
呵呵,看來做indexer真是啥都省著用啊
btw,判斷數據中是否bit值為1的數據非常少的算法頁很有趣






















