Skip to content

Commit a6d96f7

Browse files
author
Martin Durant
committed
tweaks
Note: need to prevent opens on nonexistent files creating empty entries
1 parent 7f557a6 commit a6d96f7

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

fsspec/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ class MMapCache(BaseCache):
370370
def __init__(self, blocksize, fetcher, size, location=None,
371371
blocks=None, **kwargs):
372372
super().__init__(blocksize, fetcher, size)
373-
self.blocks = set() if blocks is None else set(blocks)
373+
self.blocks = set() if blocks is None else blocks
374374
self.location = location
375375
self.cache = self._makefile()
376376

fsspec/implementations/cached.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class CachingFileSystem(FS):
1414
def __init__(self, *args, **kwargs):
1515
if cache_storage == "TMP":
1616
storage = tempfile.mkdtemp()
17+
else:
18+
storage = cache_storage
1719
os.makedirs(storage, exist_ok=True)
1820
self.storage = storage
1921
self.load_cache()
@@ -24,11 +26,18 @@ def load_cache(self):
2426
if os.path.exists(fn):
2527
with open(fn) as f:
2628
self.cache = ujson.load(f)
29+
for c in self.cache.values():
30+
if isinstance(c['blocks'], list):
31+
c['blocks'] = set(c['blocks'])
2732
else:
2833
self.cache = {}
2934

3035
def save_cache(self):
3136
fn = os.path.join(self.storage, 'cache.json')
37+
cache = {k: v.copy() for k, v in self.cache.items()}
38+
for c in cache.values():
39+
if isinstance(c['blocks'], set):
40+
c['blocks'] = list(c['blocks'])
3241
with open(fn, 'w') as f:
3342
ujson.dump(self.cache, f)
3443

@@ -40,7 +49,7 @@ def _open(self, path, mode='rb', **kwargs):
4049
hash, blocks = detail['fn'], detail['blocks']
4150
fn = os.path.join(self.storage, hash)
4251
if blocks is True:
43-
return open(fn)
52+
return open(fn, 'rb')
4453
else:
4554
blocks = set(blocks)
4655
else:
@@ -56,8 +65,10 @@ def _open(self, path, mode='rb', **kwargs):
5665
return f
5766

5867
def close_and_update(self, f, close):
59-
if len(self.cache[f.path]['blocks']) * f.blocksize >= f.size:
60-
self.cache[f.path]['blocks'] = True
68+
c = self.cache[f.path]
69+
if (c['blocks'] is not True
70+
and len(['blocks']) * f.blocksize >= f.size):
71+
c['blocks'] = True
6172
self.save_cache()
6273
close()
6374

0 commit comments

Comments
 (0)