1 | #!/usr/bin/python
|
---|
2 | import os
|
---|
3 | import simplejson as json
|
---|
4 | import xapian as x
|
---|
5 | import shutil
|
---|
6 | import gc
|
---|
7 |
|
---|
8 | def make_db(path, num_docs=100000):
|
---|
9 | try:
|
---|
10 | shutil.rmtree(path)
|
---|
11 | except OSError, e:
|
---|
12 | if e.errno != 2:
|
---|
13 | raise
|
---|
14 |
|
---|
15 | db = x.WritableDatabase(path, x.DB_CREATE)
|
---|
16 | for i in xrange(1, num_docs):
|
---|
17 | doc = x.Document()
|
---|
18 | doc.set_data(json.dumps({ 'id': i, 'enabled': True }))
|
---|
19 | doc.add_term('XTYPA')
|
---|
20 | db.add_document(doc)
|
---|
21 | return db
|
---|
22 |
|
---|
23 | def run_query(db, num_docs=100000):
|
---|
24 | e = x.Enquire(db)
|
---|
25 | e.set_query(x.Query('XTYPA'))
|
---|
26 | m = e.get_mset(0, num_docs, True, None)
|
---|
27 |
|
---|
28 | # Store the MSetItem's data, which causes a memory leak
|
---|
29 | data = []
|
---|
30 | for i in m:
|
---|
31 | data.append({ 'data': i.document.get_data(), 'id': i.docid, })
|
---|
32 |
|
---|
33 | # Make sure I'm not crazy
|
---|
34 | del num_docs, db, i, e, m, data
|
---|
35 | gc.collect()
|
---|
36 |
|
---|
37 | def main():
|
---|
38 | # print the PID to monitor
|
---|
39 | print 'PID to monitor: {}'.format(os.getpid())
|
---|
40 |
|
---|
41 | db = make_db('ticket637.db')
|
---|
42 | raw_input("database is done, ready?")
|
---|
43 |
|
---|
44 | gc.collect()
|
---|
45 | gc.collect()
|
---|
46 | gc.collect()
|
---|
47 | gc.collect()
|
---|
48 | print "num objects before = ", len(gc.get_objects())
|
---|
49 | run_query(db, 100000)
|
---|
50 | print "num objects after = ", len(gc.get_objects())
|
---|
51 | raw_input('done?')
|
---|
52 |
|
---|
53 | if __name__ == '__main__':
|
---|
54 | main()
|
---|