Skip to content

Commit

Permalink
memfs: support per-instance store (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
skshetry committed Jun 22, 2022
1 parent 7822072 commit 488c15f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 21 deletions.
26 changes: 12 additions & 14 deletions src/dvc_objects/fs/implementations/memory.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
import threading

from funcy import cached_property, wrap_prop

from ..base import FileSystem


class MemoryFileSystem(FileSystem): # pylint:disable=abstract-method
protocol = "memory"
PARAM_CHECKSUM = "md5"

def __eq__(self, other):
# NOTE: all fsspec MemoryFileSystem instances are equivalent and use a
# single global store
return isinstance(other, type(self))
def __init__(self, global_store=True, **kwargs):
from fsspec.implementations.memory import MemoryFileSystem as MemFS

__hash__ = FileSystem.__hash__
super().__init__(**kwargs)
self.fs = fs = MemFS(**self.fs_args)
if not global_store:
fs.store = {}
fs.pseudo_dirs = [""]

@wrap_prop(threading.Lock())
@cached_property
def fs(self):
from fsspec.implementations.memory import MemoryFileSystem as MemFS
def __eq__(self, other):
return (
isinstance(other, type(self)) and self.fs.store is other.fs.store
)

return MemFS(**self.fs_args)
__hash__ = FileSystem.__hash__
8 changes: 1 addition & 7 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,4 @@ def tmp_upath(request):

@pytest.fixture(autouse=True)
def memfs():
fs = MemoryFileSystem()
store = fs.fs.store
assert not store
try:
yield fs
finally:
store.clear()
return MemoryFileSystem(global_store=False)

0 comments on commit 488c15f

Please sign in to comment.