PyLucene 3.4.0-1 import
[pylucene.git] / samples / LuceneInAction / lia / tools / BerkeleyDbSearcher.py
1 # ====================================================================
2 #   Licensed under the Apache License, Version 2.0 (the "License");
3 #   you may not use this file except in compliance with the License.
4 #   You may obtain a copy of the License at
5 #
6 #       http://www.apache.org/licenses/LICENSE-2.0
7 #
8 #   Unless required by applicable law or agreed to in writing, software
9 #   distributed under the License is distributed on an "AS IS" BASIS,
10 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 #   See the License for the specific language governing permissions and
12 #   limitations under the License.
13 # ====================================================================
14
15 import os
16
17 from bsddb.db import DBEnv, DB
18 from bsddb.db import \
19      DB_INIT_MPOOL, DB_INIT_LOCK, DB_INIT_TXN, DB_THREAD, DB_BTREE
20
21 # missing from python interface at the moment
22 DB_LOG_INMEMORY = 0x00020000
23
24 from lucene import DbDirectory, IndexSearcher, Term, TermQuery
25
26
27 class BerkeleyDbSearcher(object):
28
29     def main(cls, argv):
30
31         if len(argv) != 2:
32             print "Usage: BerkeleyDbSearcher <index dir>"
33             return
34
35         dbHome = argv[1]
36
37         env = DBEnv()
38         env.set_flags(DB_LOG_INMEMORY, 1);
39         if os.name == 'nt':
40             env.set_cachesize(0, 0x4000000, 1)
41         elif os.name == 'posix':
42             from commands import getstatusoutput
43             if getstatusoutput('uname') == (0, 'Linux'):
44                 env.set_cachesize(0, 0x4000000, 1)
45
46         env.open(dbHome, (DB_THREAD |
47                           DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN), 0)
48
49         index = DB(env)
50         blocks = DB(env)
51         txn = None
52
53         try:
54             txn = env.txn_begin(None)
55             index.open(filename = '__index__', dbtype = DB_BTREE,
56                        flags = DB_THREAD, txn = txn)
57             blocks.open(filename = '__blocks__', dbtype = DB_BTREE,
58                         flags = DB_THREAD, txn = txn)
59         except:
60             if txn is not None:
61                 txn.abort()
62                 txn = None
63             raise
64         else:
65             txn.commit()
66             txn = None
67
68         try:
69             txn = env.txn_begin(None)
70             directory = DbDirectory(txn, index, blocks, 0)
71             searcher = IndexSearcher(directory, True)
72
73             topDocs = searcher.search(TermQuery(Term("contents", "fox")), 50)
74             print topDocs.totalHits, "document(s) found"
75             searcher.close()
76         except:
77             if txn is not None:
78                 txn.abort()
79                 txn = None
80             raise
81         else:
82             txn.abort()
83
84             index.close()
85             blocks.close()
86             env.close()
87
88     main = classmethod(main)