def test_ondisk(): bs = BitSet([10, 11, 30, 50, 80]) st = RamStorage() f = st.create_file("test") size = bs.to_disk(f) f.close() f = st.open_file("test") b = OnDiskBitSet(f, 0, size) assert list(b) == list(bs) assert b.after(0) == 10 assert b.after(10) == 11 assert b.after(80) is None assert b.after(99) is None assert b.before(0) is None assert b.before(99) == 80 assert b.before(80) == 50 assert b.before(10) is None f.seek(0) b = BitSet.from_disk(f, size) assert list(b) == list(bs)
def __init__(self, dbfile, basepos, length, doccount): self._dbfile = dbfile self._basepos = basepos self._length = length self._doccount = doccount compressed = dbfile.get_byte(basepos + (length - 1)) if compressed: bbytes = zlib.decompress(dbfile.get(basepos, length - 1)) bitset = BitSet.from_bytes(bbytes) else: dbfile.seek(basepos) bitset = OnDiskBitSet(dbfile, basepos, length - 1) self._bitset = bitset
class Writer(ColumnWriter): def __init__(self, dbfile, compressat): self._dbfile = dbfile self._compressat = compressat self._bitset = BitSet() def __repr__(self): return "<Bit.Writer>" def add(self, docnum, value): if value: self._bitset.add(docnum) def finish(self, doccount): dbfile = self._dbfile bits = self._bitset.bits if zlib and len(bits) <= self._compressat: compressed = zlib.compress(array_tobytes(bits), 3) dbfile.write(compressed) dbfile.write_byte(1) else: dbfile.write_array(bits) dbfile.write_byte(0)
def load(self): if isinstance(self._bitset, OnDiskBitSet): bs = self._dbfile.get_array(self._basepos, "B", self._length - 1) self._bitset = BitSet.from_bytes(bs) return self
def __init__(self, dbfile, compressat): self._dbfile = dbfile self._compressat = compressat self._bitset = BitSet()