Commit 6637d216 authored by Kirill Smelkov's avatar Kirill Smelkov

lib/zodb: Add zstor_2zurl - way to convert a ZODB storage into URL to access it

Wendelin.core 2 will need to spawn WCFS filesystem server that accesses
the same ZODB database as the program that spawns it. The database
argument passed to WCFS is passed in the form of URL[1,2].  Even though
zodburi provides way to convert an URL into ZODB storage instance, there
is currently no way for reverse operation - to convert ZODB storage
instance into URL to access it(*). So we have to build it by our own.

Provide zstor_2zurl stub that currently works for FileStorage only.
ZEO and NEO support is TODO.

In the future we might want to move this functionality into
zodbtools/py.

[1] https://lab.nexedi.com/nexedi/zodbtools/blob/a2e4dd23/zodbtools/help.py#L27-53
[2] https://lab.nexedi.com/kirr/neo/blob/3d909114/go/zodb/zodbtools/help.go#L25-51

(*) contrary to ZODB/go where this functionality is provided out of the box:
    https://godoc.org/lab.nexedi.com/kirr/neo/go/zodb#IStorage
parent 959ae2d0
...@@ -17,8 +17,9 @@ ...@@ -17,8 +17,9 @@
# #
# See COPYING file for full licensing terms. # See COPYING file for full licensing terms.
# See https://www.nexedi.com/licensing for rationale and options. # See https://www.nexedi.com/licensing for rationale and options.
from wendelin.lib.zodb import LivePersistent, deactivate_btree, dbclose, zconn_at, zmajor from wendelin.lib.zodb import LivePersistent, deactivate_btree, dbclose, zconn_at, zstor_2zurl, zmajor
from wendelin.lib.testing import getTestDB from wendelin.lib.testing import getTestDB
from wendelin.lib import testing
from persistent import Persistent, UPTODATE, GHOST, CHANGED from persistent import Persistent, UPTODATE, GHOST, CHANGED
from ZODB import DB, POSException from ZODB import DB, POSException
from BTrees.IOBTree import IOBTree from BTrees.IOBTree import IOBTree
...@@ -351,6 +352,21 @@ def test_zodb_onresync(): ...@@ -351,6 +352,21 @@ def test_zodb_onresync():
conn.close() conn.close()
# test that zurl does not change from one open to another storage open.
def test_zurlstable():
if not isinstance(testdb, testing.TestDB_FileStorage):
pytest.xfail(reason="zstor_2zurl is TODO for ZEO and NEO")
zurl0 = None
for i in range(10):
zstor = testdb.getZODBStorage()
zurl = zstor_2zurl(zstor)
zstor.close()
if i == 0:
zurl0 = zurl
else:
assert zurl == zurl0
# ---- misc ---- # ---- misc ----
# zsync syncs ZODB storage. # zsync syncs ZODB storage.
......
...@@ -299,3 +299,20 @@ elif zmajor == 4: ...@@ -299,3 +299,20 @@ elif zmajor == 4:
# ZODB3: TODO # ZODB3: TODO
else: else:
pass # raises in onResyncCallback pass # raises in onResyncCallback
# zstor_2zurl converts a ZODB storage to URL to access it.
def zstor_2zurl(zstor):
# There is, sadly, no unified way to do it, as even if storages are created via
# zodburi, after creation its uri is lost. And storages could be created not
# only through URI but e.g. via ZConfig and manually. We want to support all
# those cases...
#
# For this reason extract URL with important for wcfs use-case parameters in
# ad-hoc way.
if isinstance(zstor, FileStorage):
return "file://%s" % (zstor._file_name,)
# TODO ZEO + NEO support
raise NotImplementedError("don't know how to extract zurl from %r" % zstor)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment