PyLucene 3.4.0-1 import
[pylucene.git] / samples / LuceneInAction / lia / tools / BerkeleyDbIndexer.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_CREATE, DB_BTREE
20
21 # missing from python interface at the moment
22 DB_LOG_INMEMORY = 0x00020000
23
24 from lucene import \
25      DbDirectory, IndexWriter, StandardAnalyzer, Document, Field
26
27
28 class BerkeleyDbIndexer(object):
29
30     def main(cls, argv):
31
32         if len(argv) < 2:
33             print "Usage: BerkeleyDbIndexer <index dir> -create"
34             return
35
36         dbHome = argv[1]
37         create = len(argv) > 2 and argv[2] == "-create"
38
39         if not os.path.exists(dbHome):
40             os.makedirs(dbHome)
41         elif create:
42             for name in os.listdir(dbHome):
43                 if name.startswith('__'):
44                     os.remove(os.path.join(dbHome, name))
45
46         env = DBEnv()
47         env.set_flags(DB_LOG_INMEMORY, 1);
48         if os.name == 'nt':
49             env.set_cachesize(0, 0x4000000, 1)
50         elif os.name == 'posix':
51             from commands import getstatusoutput
52             if getstatusoutput('uname') == (0, 'Linux'):
53                 env.set_cachesize(0, 0x4000000, 1)
54
55         env.open(dbHome, (DB_CREATE | DB_THREAD |
56                           DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN), 0)
57
58         index = DB(env)
59         blocks = DB(env)
60         txn = None
61         
62         try:
63             txn = env.txn_begin(None)
64             index.open(filename = '__index__', dbtype = DB_BTREE,
65                        flags = DB_CREATE | DB_THREAD, txn = txn)
66             blocks.open(filename = '__blocks__', dbtype = DB_BTREE,
67                         flags = DB_CREATE | DB_THREAD, txn = txn)
68         except:
69             if txn is not None:
70                 txn.abort()
71                 txn = None
72             raise
73         else:
74             txn.commit()
75             txn = None
76
77         try:
78             txn = env.txn_begin(None)
79             directory = DbDirectory(txn, index, blocks, 0)
80             writer = IndexWriter(directory, StandardAnalyzer(), create,
81                                  IndexWriter.MaxFieldLength.UNLIMITED)
82             writer.setUseCompoundFile(False)
83
84             doc = Document()
85             doc.add(Field("contents", "The quick brown fox...",
86                           Field.Store.YES, Field.Index.ANALYZED))
87             writer.addDocument(doc)
88
89             writer.optimize()
90             writer.close()
91         except:
92             if txn is not None:
93                 txn.abort()
94                 txn = None
95             raise
96         else:
97             txn.commit()
98             index.close()
99             blocks.close()
100             env.close()
101
102         print "Indexing Complete"
103
104     main = classmethod(main)