【问题标题】:ZODB: using both regular FileStorage as well as Zlib compressed storageZODB:同时使用常规 FileStorage 和 Zlib 压缩存储
【发布时间】:2021-06-08 09:31:40
【问题描述】:

我目前正在编写一个 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

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

【问题讨论】:

  • FileStorage 无法打开压缩文件,所以试试 except 并在 except 中用 zlibstorage 打开

标签: python python-3.x database zope zodb


【解决方案1】:
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

【讨论】:

  • 我对此进行了测试,它确实有效。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多