我以为我明白这一点,但我开始怀疑!
如果您考虑repoze.catalog 文档示例:
from repoze.catalog.catalog import FileStorageCatalogFactory
from repoze.catalog.catalog import ConnectionManager
from repoze.catalog.indexes.field import CatalogFieldIndex
from repoze.catalog.indexes.text import CatalogTextIndex
factory = FileStorageCatalogFactory('catalog.db', 'mycatalog')
_initialized = False
def initialize_catalog():
global _initialized
if not _initialized:
# create a catalog
manager = ConnectionManager()
catalog = factory(manager)
# set up indexes
catalog['flavors'] = CatalogFieldIndex('flavor')
catalog['texts'] = CatalogTextIndex('text')
# commit the indexes
manager.commit()
manager.close()
_initialized = True
class Content(object):
def __init__(self, flavor, text):
self.flavor = flavor
self.text = text
if __name__ == '__main__':
initialize_catalog()
manager = ConnectionManager()
catalog = factory(manager)
content = {
1:Content('peach', 'i am so very very peachy'),
2:Content('pistachio', 'i am nutty'),
}
for docid, doc in content.items():
catalog.index_doc(docid, doc)
manager.commit()
这向您展示了如何为 Content 类的两个实例生成目录条目,但实际保存对象的正确机制是什么?
我一开始有一个完全独立的 ZODB 数据库,我在其中存储了在 docid 上键入的对象,该 docid 用于在 repoze.catalog 下对它们进行编目,但是当涉及事务时,这不太令人满意,因为在添加对象时我必须发出提交用于存储对象的目录和 ZODB 数据库。
我曾假设我能够访问 repoze.catalog 结构中的 ZODB 目录并使用它来存储实际对象,但我很难找到如何做到这一点。