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
6 # http://www.apache.org/licenses/LICENSE-2.0
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 # ====================================================================
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
21 # missing from python interface at the moment
22 DB_LOG_INMEMORY = 0x00020000
25 DbDirectory, IndexWriter, StandardAnalyzer, Document, Field
28 class BerkeleyDbIndexer(object):
33 print "Usage: BerkeleyDbIndexer <index dir> -create"
37 create = len(argv) > 2 and argv[2] == "-create"
39 if not os.path.exists(dbHome):
42 for name in os.listdir(dbHome):
43 if name.startswith('__'):
44 os.remove(os.path.join(dbHome, name))
47 env.set_flags(DB_LOG_INMEMORY, 1);
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)
55 env.open(dbHome, (DB_CREATE | DB_THREAD |
56 DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN), 0)
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)
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)
85 doc.add(Field("contents", "The quick brown fox...",
86 Field.Store.YES, Field.Index.ANALYZED))
87 writer.addDocument(doc)
102 print "Indexing Complete"
104 main = classmethod(main)