ZODB:同时使用常规 FileStorage 和 Zlib 压缩存储

我目前正在编写一个 Python 桌面应用程序,它执行一些基本的数据分析和数据显示。数据来自我们研究实验室的一些实验,我们使用 FileStorage 将数据存储在 ZODB 数据库中。

打开数据库的代码相当简单,看起来像您所期望的:

self.storage = ZODB.FileStorage.FileStorage(filename)
self.db = ZODB.DB(self.storage)
self.db_connection = self.db.open()
self.db_root = self.db_connection.root

我想开始使用压缩存储来尝试将数据库文件保持在较小的大小 (https://pypi.org/project/zc.zlibstorage/),但我还想保持与以前未使用压缩的数据库文件的向后兼容性存储。

如果我使用带有压缩存储的数据库,我可以简单地更改代码的第一行,所以现在我的代码将如下所示:

self.storage = zc.zlibstorage.ZlibStorage(ZODB.FileStorage.FileStorage(filename))
self.db = ZODB.DB(self.storage)
self.db_connection = self.db.open()
self.db_root = self.db_connection.root

但如果我这样做了,我的程序将如何处理仅使用 FileStorage 而不是 ZLibStorage 的常规数据库?我想同时处理。

有没有办法确定 FileStorage 是否使用压缩,然后根据需要在 ZlibStorage 上分层?像这样:

self.storage = ZODB.FileStorage.FileStorage(filename)
if (self.storage is compressed):                                #pseudocode
    self.storage = zc.zlibstorage.ZlibStorage(self.storage)     #pseudocode
self.db = ZODB.DB(self.storage)
self.db_connection = self.db.open()
self.db_root = self.db_connection.root

还是我完全误解了事情的运作方式?任何帮助,将不胜感激!谢谢!

tkhxq 回答:ZODB:同时使用常规 FileStorage 和 Zlib 压缩存储

zstorage = ZODB.FileStorage.FileStorage('testz.fs') # testz.fs has been created with ZlibStorage
try:
 db = ZODB.DB(zstorage)
except:
 zstorage.close()
 zstorage = zc.zlibstorage.ZlibStorage(ZODB.FileStorage.FileStorage('testz.fs'))
 db = ZODB.DB(zstorage)

不如应有的健壮(使用正确的异常 _pickle.UnpicklingError 改进它)

本文链接:https://www.f2er.com/290128.html

大家都在问