Source

whoosh / tests / test_codecs.py

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
from __future__ import with_statement
import random
from array import array

import pytest

from whoosh import analysis, fields, formats, index, matching, reading
from whoosh.compat import b, u, permutations, xrange
from whoosh.codec import default_codec
from whoosh.codec import codec
from whoosh.formats import Posting
from whoosh.util.testing import TempDB


def test_top_methods():
    with TempDB("topmeths") as db:
        txn = db.open(write=True, create=True)
        cdc = default_codec()
        fobj = fields.TEXT(sortable=True)

        assert isinstance(cdc.doc_writer(txn), codec.DocWriter)
        assert isinstance(cdc.doc_reader(txn), codec.DocReader)
        assert isinstance(cdc.term_reader(txn), codec.TermReader)
        assert isinstance(cdc.automata(txn, "f", fobj), codec.Automata)
        assert isinstance(cdc.column_writer(txn), codec.ColumnWriter)
        assert isinstance(cdc.column_reader(txn, "f", fobj), codec.ColumnReader)
        txn.cancel()


def test_write_read():
    form = formats.BasicFormat(True, True, True)
    ana = analysis.StandardAnalyzer()
    fobj = fields.FieldType(form, ana, scorable=True, stored=True)

    data = [
        (5, u"alfa bravo charlie"),
        (10, u"bravo charlie delta echo"),
        (12, u"charlie delta echo foxtrot charlie"),
        (30, u"delta echo echo golf"),
    ]

    with TempDB("writeread") as db:
        with db.open(write=True, create=True) as txn:
            cdc = default_codec()
            dw = cdc.doc_writer(txn)

            for docid, value in data:
                dw.start_doc(docid)
                length, gen = form.index(ana, fobj.to_bytes, value)
                dw.add_field("f", fobj, value, length)
                dw.add_field_postings("f", fobj, length, gen)
                dw.finish_doc()
            dw.close()

        terms = [("f", bs) for bs
                 in b"alfa bravo charlie delta echo foxtrot golf".split()]

        with db.open() as txn:
            tr = cdc.term_reader(txn)

            assert ("f", b"echo") in tr
            assert ("f", b"india") not in tr
            assert list(tr.terms_from("f", b"dx")) == b"echo foxtrot golf".split()
            assert list(tr.indexed_field_names()) == ["f"]

            ti = tr.term_info("f", b"echo")
            assert isinstance(ti, reading.TermInfo)
            assert ti.min_id() == 10
            assert ti.max_id() == 30
            assert ti.min_length() == 4
            assert ti.max_length() == 5
            assert ti.max_weight() == 2.0

            dr = cdc.doc_reader(txn)
            with pytest.raises(reading.NoStoredFields):
                dr.stored_fields(35)
            assert dr.is_deleted(2)
            assert not dr.is_deleted(5)
            assert dr.is_deleted(15)
            assert not dr.is_deleted(30)
            assert dr.is_deleted(100000)
            assert list(dr.all_doc_ids()) == [5, 10, 12, 30]
            # assert dr.field_length("f") == 16
            # assert dr.min_field_length("f") == 3
            # assert dr.max_field_length("f") == 5
            assert dr.stored_fields(5) == {"f": u"alfa bravo charlie"}
            assert dr.stored_fields(30) == {"f": u"delta echo echo golf"}
            assert list(dr.all_stored_fields()) == [(docid, {"f": v}) for docid, v in data]

            out = []
            for fieldname, termbytes in terms:
                cm = tr.matcher(fieldname, fobj, termbytes)
                info = []
                while cm.is_active():
                    info.append((cm.id(), cm.length(), cm.weight()))
                    cm.next()
                out.append((termbytes, info))
            assert out == [
                (b"alfa", [(5, 3, 1.0)]),
                (b"bravo", [(5, 3, 1.0), (10, 4, 1.0)]),
                (b"charlie", [(5, 3, 1.0), (10, 4, 1.0), (12, 5, 2.0)]),
                (b"delta", [(10, 4, 1.0), (12, 5, 1.0), (30, 4, 1.0)]),
                (b"echo", [(10, 4, 1.0), (12, 5, 1.0), (30, 4, 2.0)]),
                (b"foxtrot", [(12, 5, 1.0)]),
                (b"golf", [(30, 4, 1.0)]),
            ]


def test_indexed_field_names():
    # Create every 1, 2, and 3 letter permutation of a/b/c
    def names():
        for c1 in "abc":
            n1 = c1
            yield n1
            for c2 in "abc ":
                if c2 != " ":
                    n2 = n1 + c2
                    yield n2
                    for c3 in "abc ":
                        if c3 != " ":
                            n3 = n2 + c3
                            yield n3
    fnames = list(names())

    cdc = default_codec()
    with TempDB("fieldnames") as db:
        with db.open(write=True, create=True) as w:
            dw = cdc.doc_writer(w)

            times = 1000
            assert times > len(fnames)
            fobj = fields.KEYWORD()
            tbytes = b"x"

            for i in xrange(1000):
                fname = fnames[i % len(fnames)]
                dw.start_doc(i)
                dw.add_field(fname, fobj, None, 1)
                posts = [Posting(id=tbytes, weight=1.0)]
                dw.add_field_postings(fname, fobj, 1, posts)
                dw.finish_doc()
            dw.close()

        with db.open() as txn:
            tr = cdc.term_reader(txn)
            names = list(tr.indexed_field_names())
            assert names == fnames


def test_docmap():
    fieldobj = fields.KEYWORD(stored=True)
    base = 0
    docids = []
    times = 10000
    for _ in xrange(times):
        base += random.randint(1, 10)
        docids.append(base)
    docset = set(docids)

    def check(dr):
        output = list(dr.all_doc_ids())
        assert len(output) == len(docids)
        assert output == docids
        assert dr.doc_count() == len(docids)
        for i in xrange(docids[-1] + 1):
            assert dr.is_deleted(i) == (i not in docset)

    cdc = default_codec()
    with TempDB("docmap") as db:
        with db.open(write=True, create=True) as w:
            dw = cdc.doc_writer(w)
            for docid in docids:
                dw.start_doc(docid)
                dw.add_field("a", fieldobj, docid, docid)
                dw.finish_doc()
            dw.close()

        with db.open() as r:
            dr = cdc.doc_reader(r)
            check(dr)

        with db.open(write=True) as w:
            dw = cdc.doc_writer(w)
            deletetimes = times // 2
            for _ in xrange(deletetimes):
                docid = docset.pop()
                dw.delete(docid)
            docids = sorted(docset)
            dw.close()

        with db.open() as r:
            dr = cdc.doc_reader(r)
            check(dr)


def test_remove_field_terms():
    fnames = "abcd"
    fobj = fields.KEYWORD()
    times = 1000
    all_terms = [(fnames[i % len(fnames)], str(i).encode("ascii"))
                 for i in xrange(times)]
    all_terms.sort()

    cdc = default_codec()
    with TempDB("removefield") as db:
        with db.open(write=True, create=True) as w:
            dw = cdc.doc_writer(w)
            dw.start_doc(0)

            for fname in fnames:
                dw.add_field(fname, fobj, None, 1)
                posts = [Posting(id=tbytes, weight=1.0)
                         for fn, tbytes in all_terms
                         if fn == fname]
                dw.add_field_postings(fname, fobj, len(posts), posts)

            dw.finish_doc()
            dw.close()

        with db.open() as r:
            tr = cdc.term_reader(r)
            assert list(tr.terms()) == all_terms

        with db.open(write=True) as w:
            dw = cdc.doc_writer(w)
            dw.remove_field_terms("b")
            dw.close()

        with db.open() as r:
            tr = cdc.term_reader(r)
            result = [term for term in all_terms if term[0] != "b"]
            assert list(tr.terms()) == result


def test_add_matcher():
    # Create some fake terms for a single document
    fnames = "abcd"
    fobj = fields.KEYWORD()
    times = 100
    all_terms = [(fnames[i % len(fnames)], str(i).encode("ascii"))
                 for i in xrange(times)]
    all_terms.sort()

    # Create some fake data for a matcher we'll add
    data = [(1, 2, 3.0), (4, 5, 6.0), (7, 8, 9.0), (10, 11, 1.5)]
    posts = [Posting(id=docid, length=length, weight=weight)
             for docid, length, weight in data]
    lm = matching.ListMatcher(posts)

    cdc = default_codec()
    with TempDB("addmatcher") as db:
        # Write a single document containing the fake terms
        with db.open(write=True, create=True) as w:
            dw = cdc.doc_writer(w)
            dw.start_doc(0)

            for fname in fnames:
                dw.add_field(fname, fobj, None, 6)
                posts = [Posting(id=tbytes, weight=2.5, length=6)
                         for fn, tbytes in all_terms
                         if fn == fname]
                dw.add_field_postings(fname, fobj, 6, posts)

            dw.finish_doc()
            dw.close()

        # Pick one of the fake terms
        fieldname, termbytes = all_terms[len(all_terms) // 3]

        # Add the fake matcher's postings to the chosen term's
        with db.open(write=True) as w:
            dw = cdc.doc_writer(w)
            dw.add_matcher(fieldname, fobj, termbytes, lm)
            dw.close()

        # Open a matcher for the chosen term and check that its postings consist
        # of the first fake document, and then the data from the matcher we
        # added
        with db.open() as r:
            tr = cdc.term_reader(r)
            m = tr.matcher(fieldname, fobj, termbytes)
            result = [(v.id, v.length, v.weight) for v in m.all_values()]
            assert result == [(0, 6, 2.5)] + data


def test_postings():
    import re

    def ana(text, **kwargs):
        t = analysis.Token(positions=True, chars=True)
        for i, match in enumerate(re.finditer("[^ ]+", text)):
            t.text = match.group()
            t.boost = float(i + 1)
            t.pos = i
            t.startchar = match.start()
            t.endchar = match.end()
            t.payload = t.text[0].encode("ascii")
            yield t

    domain = u"alfa bravo charlie".split()
    form = formats.BasicFormat(lengths=True, weights=True, positions=True,
                               characters=True, payloads=True)
    fobj = fields.FieldType(form, ana)
    cdc = default_codec()
    with TempDB("cdcpostings") as db:
        for i, ls in enumerate(permutations(domain)):
            with db.open(write=True, create=True) as w:
                dw = cdc.doc_writer(w)
                value = " ".join(ls)
                length, posts = form.index(ana, fobj.to_bytes, value)
                posts = list(posts)
                dw.start_doc(i)
                dw.add_field("a", fobj, None, length)
                dw.add_field_postings("a", fobj, length, posts)
                dw.finish_doc()
                dw.close()

        result = {}
        with db.open() as r:
            tr = cdc.term_reader(r)
            for fieldname, termbytes in tr.terms():
                m = tr.matcher(fieldname, fobj, termbytes)
                result[termbytes] = list(m.all_values())


def test_doc_id_range():
    cdc = default_codec()
    fieldobj = fields.KEYWORD()
    with TempDB("docidrange") as db:
        with db.open(write=True, create=True) as w:
            dw = cdc.doc_writer(w)
            for docid in (1000, 2000, 3000, 4000, 5000, 6000):
                dw.start_doc(docid)
                dw.add_field("a", fieldobj, None, None)
                dw.finish_doc()
            dw.close()

        with db.open() as r:
            dr = cdc.doc_reader(r)
            assert dr.doc_id_range() == (1000, 6000)
            dr.close()

        with db.open(write=True) as w:
            dw = cdc.doc_writer(w)
            dw.delete(1000)
            dw.delete(2000)
            dw.delete(6000)
            dw.close()

        with db.open() as r:
            dr = cdc.doc_reader(r)
            assert dr.doc_id_range() == (3000, 5000)
            dr.close()


def test_field_length():
    fieldobj = fields.TEXT()
    cdc = default_codec()
    with TempDB("codecfieldlen") as db:
        with db.open(write=True, create=True) as w:
            dw = cdc.doc_writer(w)
            dw.start_doc(0)
            dw.add_field("a", fieldobj, None, 9)
            dw.add_field_postings("a", fieldobj, 9,
                                  [Posting(id=b"word", weight=9, length=9)])
            dw.finish_doc()

            dw.start_doc(1)
            dw.add_field("a", fieldobj, None, 4)
            dw.add_field_postings("a", fieldobj, 4,
                                  [Posting(id=b"word", weight=9, length=4)])
            dw.finish_doc()
            dw.close()

        with db.open() as r:
            dr = cdc.doc_reader(r)
            assert dr.min_field_length("a") == 4
            assert dr.max_field_length("a") == 9


def test_column_readwrite():
    values = b"alfa bravo charlie delta echo foxtrot".split()

    def hink(n):
        return n * 2 + 5

    fieldobj = fields.ID(sortable=True)
    assert fieldobj.column_type
    cdc = default_codec()
    with TempDB("codeccolumn") as db:
        with db.open(write=True, create=True) as w:
            cw = cdc.column_writer(w)
            for i, v in enumerate(values):
                cw.add_value("f", fieldobj, hink(i), v)
            cw.close()

            cr = cdc.column_reader(w, "f", fieldobj)
            for i, v in enumerate(values):
                assert cr[hink(i)] == v

        with db.open() as r:
            cr = cdc.column_reader(r, "f", fieldobj)
            for i, v in enumerate(values):
                assert cr[hink(i)] == v

        delete = (0, 2, 3)
        with db.open(write=True) as w:
            cw = cdc.column_writer(w)
            for i in delete:
                cw.remove_value("f", fieldobj, hink(i))
            cw.close()

        with db.open() as r:
            cr = cdc.column_reader(r, "f", fieldobj)
            for i, v in enumerate(values):
                vv = cr[hink(i)]
                if i in delete:
                    assert vv == fieldobj.column_type.default_value()
                else:
                    assert vv == v